Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/gwt/src/org/rstudio/studio/client/common/vcs/SshKeyChooser.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
.viewPublicKeyLink { | ||
font-size: 11px; | ||
} | ||
|
||
.sshButtonPanel { | ||
margin-top: -3px; | ||
margin-left: 0px; | ||
} |
232 changes: 232 additions & 0 deletions
232
src/gwt/src/org/rstudio/studio/client/common/vcs/SshKeyChooser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
/* | ||
* SshKeyChooser.java | ||
* | ||
* Copyright (C) 2009-11 by RStudio, Inc. | ||
* | ||
* This program is licensed to you under the terms of version 3 of the | ||
* GNU Affero General Public License. This program is distributed WITHOUT | ||
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, | ||
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the | ||
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. | ||
* | ||
*/ | ||
package org.rstudio.studio.client.common.vcs; | ||
|
||
import org.rstudio.core.client.BrowseCap; | ||
import org.rstudio.core.client.files.FileSystemItem; | ||
import org.rstudio.core.client.widget.FileChooserTextBox; | ||
import org.rstudio.core.client.widget.HyperlinkLabel; | ||
import org.rstudio.core.client.widget.NullProgressIndicator; | ||
import org.rstudio.core.client.widget.OperationWithInput; | ||
import org.rstudio.core.client.widget.ProgressIndicator; | ||
import org.rstudio.core.client.widget.SmallButton; | ||
import org.rstudio.core.client.widget.TextBoxWithButton; | ||
import org.rstudio.studio.client.application.Desktop; | ||
import org.rstudio.studio.client.server.ServerError; | ||
import org.rstudio.studio.client.server.ServerRequestCallback; | ||
|
||
import com.google.gwt.core.client.GWT; | ||
import com.google.gwt.event.dom.client.ClickEvent; | ||
import com.google.gwt.event.dom.client.ClickHandler; | ||
import com.google.gwt.event.logical.shared.ValueChangeHandler; | ||
import com.google.gwt.event.shared.HandlerRegistration; | ||
import com.google.gwt.resources.client.ClientBundle; | ||
import com.google.gwt.resources.client.CssResource; | ||
import com.google.gwt.user.client.Command; | ||
import com.google.gwt.user.client.ui.Composite; | ||
import com.google.gwt.user.client.ui.FlowPanel; | ||
import com.google.gwt.user.client.ui.HasHorizontalAlignment; | ||
import com.google.gwt.user.client.ui.HorizontalPanel; | ||
import com.google.gwt.user.client.ui.Label; | ||
|
||
|
||
// TODO: Project setup -- auth config (shared with New Proj from VC) | ||
|
||
|
||
|
||
public class SshKeyChooser extends Composite | ||
{ | ||
public static boolean isSupportedForCurrentPlatform() | ||
{ | ||
return !Desktop.isDesktop() || BrowseCap.isWindows(); | ||
} | ||
|
||
public SshKeyChooser(GitServerOperations server, | ||
String defaultSshKeyDir, | ||
String textWidth) | ||
|
||
{ | ||
server_ = server; | ||
defaultSshKeyDir_ = defaultSshKeyDir; | ||
progressIndicator_ = new NullProgressIndicator(); | ||
|
||
FlowPanel panel = new FlowPanel(); | ||
|
||
// caption panel | ||
HorizontalPanel captionPanel = new HorizontalPanel(); | ||
captionPanel.setWidth(textWidth); | ||
Label sshKeyPathLabel = new Label("SSH key path:"); | ||
captionPanel.add(sshKeyPathLabel); | ||
captionPanel.setCellHorizontalAlignment( | ||
sshKeyPathLabel, | ||
HasHorizontalAlignment.ALIGN_LEFT); | ||
|
||
HorizontalPanel linkPanel = new HorizontalPanel(); | ||
publicKeyLink_ = new HyperlinkLabel("View public key"); | ||
publicKeyLink_.addStyleName(RES.styles().viewPublicKeyLink()); | ||
publicKeyLink_.addClickHandler(new ClickHandler() { | ||
@Override | ||
public void onClick(ClickEvent event) | ||
{ | ||
viewPublicKey(); | ||
} | ||
}); | ||
linkPanel.add(publicKeyLink_); | ||
captionPanel.add(publicKeyLink_); | ||
captionPanel.setCellHorizontalAlignment( | ||
publicKeyLink_, | ||
HasHorizontalAlignment.ALIGN_RIGHT); | ||
panel.add(captionPanel); | ||
|
||
|
||
// chooser | ||
sshKeyPathChooser_ = new FileChooserTextBox( | ||
null, | ||
"(None)", | ||
null, | ||
new Command() { | ||
@Override | ||
public void execute() | ||
{ | ||
publicKeyLink_.setVisible(true); | ||
} | ||
}); | ||
sshKeyPathChooser_.setTextWidth(textWidth); | ||
setSshKey(""); | ||
panel.add(sshKeyPathChooser_); | ||
|
||
|
||
// ssh key path action buttons | ||
HorizontalPanel sshButtonPanel = new HorizontalPanel(); | ||
sshButtonPanel.addStyleName(RES.styles().sshButtonPanel()); | ||
createKeyButton_ = new SmallButton(); | ||
createKeyButton_.setText("Create New Key..."); | ||
createKeyButton_.addClickHandler(new ClickHandler() { | ||
@Override | ||
public void onClick(ClickEvent event) | ||
{ | ||
new CreateKeyDialog(defaultSshKeyDir_, | ||
server_, | ||
new OperationWithInput<String>() { | ||
@Override | ||
public void execute(String keyPath) | ||
{ | ||
setSshKey(keyPath); | ||
} | ||
}).showModal(); | ||
} | ||
}); | ||
sshButtonPanel.add(createKeyButton_); | ||
panel.add(sshButtonPanel); | ||
|
||
// default visibility of create key based on desktop vs. server | ||
setAllowKeyCreation(!Desktop.isDesktop()); | ||
|
||
initWidget(panel); | ||
} | ||
|
||
public void setProgressIndicator(ProgressIndicator progressIndicator) | ||
{ | ||
progressIndicator_ = progressIndicator; | ||
} | ||
|
||
// use a special adornment when the displayed key matches an | ||
// arbitrary default value | ||
public void setDefaultSskKey(String keyPath) | ||
{ | ||
sshKeyPathChooser_.setUseDefaultValue(keyPath); | ||
} | ||
|
||
public void setSshKey(String keyPath) | ||
{ | ||
sshKeyPathChooser_.setText(keyPath); | ||
publicKeyLink_.setVisible(getSshKey().length() > 0); | ||
} | ||
|
||
public String getSshKey() | ||
{ | ||
return sshKeyPathChooser_.getText().trim(); | ||
} | ||
|
||
public void setAllowKeyCreation(boolean allowKeyCreation) | ||
{ | ||
createKeyButton_.setVisible(allowKeyCreation); | ||
} | ||
|
||
public HandlerRegistration addValueChangeHandler( | ||
ValueChangeHandler<String> handler) | ||
{ | ||
return sshKeyPathChooser_.addValueChangeHandler(handler); | ||
} | ||
|
||
|
||
private void viewPublicKey() | ||
{ | ||
progressIndicator_.onProgress("Reading public key..."); | ||
|
||
// compute path to public key | ||
FileSystemItem privKey = | ||
FileSystemItem.createFile(sshKeyPathChooser_.getText()); | ||
FileSystemItem keyDir = privKey.getParentPath(); | ||
final String keyPath = keyDir.completePath(privKey.getStem() + ".pub"); | ||
|
||
server_.gitSshPublicKey(keyPath, | ||
new ServerRequestCallback<String> () { | ||
|
||
@Override | ||
public void onResponseReceived(String publicKeyContents) | ||
{ | ||
progressIndicator_.onCompleted(); | ||
|
||
new ShowPublicKeyDialog("Public Key", | ||
publicKeyContents).showModal(); | ||
} | ||
|
||
@Override | ||
public void onError(ServerError error) | ||
{ | ||
String msg = "Error attempting to read key '" + keyPath + "' (" + | ||
error.getUserMessage() + ")"; | ||
progressIndicator_.onError(msg); | ||
} | ||
}); | ||
} | ||
|
||
|
||
static interface Styles extends CssResource | ||
{ | ||
String viewPublicKeyLink(); | ||
String sshButtonPanel(); | ||
} | ||
|
||
static interface Resources extends ClientBundle | ||
{ | ||
@Source("SshKeyChooser.css") | ||
Styles styles(); | ||
} | ||
|
||
static Resources RES = (Resources)GWT.create(Resources.class) ; | ||
public static void ensureStylesInjected() | ||
{ | ||
RES.styles().ensureInjected(); | ||
} | ||
|
||
private String defaultSshKeyDir_; | ||
|
||
private HyperlinkLabel publicKeyLink_; | ||
private TextBoxWithButton sshKeyPathChooser_; | ||
private SmallButton createKeyButton_; | ||
|
||
private final GitServerOperations server_; | ||
private ProgressIndicator progressIndicator_; | ||
} |