[Mercurial] IDEA-55930. Ask authentication for Clone and Identify commands. Simplifie...
authorKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Thu, 1 Jul 2010 12:20:56 +0000 (16:20 +0400)
committerKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Thu, 1 Jul 2010 12:20:56 +0000 (16:20 +0400)
platform/platform-api/src/com/intellij/util/net/AuthenticationPanel.java
plugins/hg4idea/resources/org/zmlx/hg4idea/HgVcsMessages.properties
plugins/hg4idea/src/org/zmlx/hg4idea/command/HgCloneCommand.java
plugins/hg4idea/src/org/zmlx/hg4idea/command/HgCommandAuthenticator.java
plugins/hg4idea/src/org/zmlx/hg4idea/command/HgIdentifyCommand.java
plugins/hg4idea/src/org/zmlx/hg4idea/ui/HgUsernamePasswordDialog.form [deleted file]
plugins/hg4idea/src/org/zmlx/hg4idea/ui/HgUsernamePasswordDialog.java

index 2e9b6f641fd6291e6b38e55b9463fa368e767f81..b6b94a4d5df98b65fde3c21176fccd98af69906d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2000-2009 JetBrains s.r.o.
+ * Copyright 2000-2010 JetBrains s.r.o.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  */
 package com.intellij.util.net;
 
+import org.jetbrains.annotations.Nullable;
+
 import javax.swing.*;
 
 /**
- * Created by IntelliJ IDEA.
- * User: stathik
- * Date: Sep 12, 2003
- * Time: 8:40:40 PM
- * To change this template use Options | File Templates.
+ * Panel for authentication - contains text fields for login and password, and a checkbox to remember
+ * password in the Password Safe.
+ * 
+ * @author stathik
+ * @author Kirill Likhodedov
  */
 public class AuthenticationPanel extends JPanel {
   private JPanel myMainPanel;
@@ -31,7 +33,13 @@ public class AuthenticationPanel extends JPanel {
   private JPasswordField myPasswordTextField;
   private JCheckBox rememberPasswordCheckBox;
 
-  public AuthenticationPanel(String description, String login, String password, boolean rememberPassword) {
+  /**
+   * @param description       Description text above the text fields.
+   * @param login             Initial login value.
+   * @param password          Initial password value.
+   * @param rememberPassword  Default value for the 'remember password' checkbox.
+   */
+  public AuthenticationPanel(@Nullable String description, @Nullable String login, @Nullable String password, boolean rememberPassword) {
     add(myMainPanel);
     myDescriptionLabel.setText(description);
     myLoginTextField.setText(login);
@@ -50,4 +58,13 @@ public class AuthenticationPanel extends JPanel {
   public boolean isRememberPassword () {
     return rememberPasswordCheckBox.isSelected();
   }
+
+  /**
+   * @return the component which should be focused when the dialog appears on the screen. May be used in dialogs.
+   * @see com.intellij.openapi.ui.DialogWrapper#getPreferredFocusedComponent()
+   */
+  public JComponent getPreferredFocusedComponent() {
+    return getLogin().isEmpty() ? myLoginTextField : myPasswordTextField;
+  }
+
 }
index 6b893c39e4f5ae656eeb1c2289bc9cdba074416f..696c098227e4ccc000b67a1647b0eac45d3b5923 100644 (file)
@@ -100,4 +100,6 @@ hg4idea.status.currentSituation.description=<html>Current mercurial branch and p
 hg4idea.warning.no-default-update-path=Skipped \"{0}\". No default update path.
 hg4idea.merge.please-commit=Merged heads, please commit repository \"{0}\"
 hg4idea.error.invalidExecutable=\"{0}\" is not a valid mercurial executable
-hg4idea.integrate.other.head=Other head: {0}
\ No newline at end of file
+hg4idea.integrate.other.head=Other head: {0}
+
+hgidea.dialog.login.password.required=Login and password required
\ No newline at end of file
index 36c42f5a32cda5de957e07e637dadd02ebb0a49b..1384ddc2f5282de0b52ecbdb8565fbdeda5beb5a 100644 (file)
@@ -4,6 +4,7 @@ import com.intellij.openapi.project.Project;
 import org.jetbrains.annotations.Nullable;
 
 import java.nio.charset.Charset;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
@@ -13,6 +14,7 @@ public class HgCloneCommand {
 
   private String repositoryURL;
   private String directory;
+  private final HgCommandAuthenticator authenticator = new HgCommandAuthenticator();
 
   public HgCloneCommand(Project project) {
     this.project = project;
@@ -28,9 +30,9 @@ public class HgCloneCommand {
 
   @Nullable
   public HgCommandResult execute() {
-    List<String> arguments = new LinkedList<String>();
+    final List<String> arguments = new ArrayList<String>(2);
     arguments.add(repositoryURL);
     arguments.add(directory);
-    return HgCommandService.getInstance(project).execute(null, Collections.<String>emptyList(), "clone", arguments, Charset.defaultCharset());
+    return authenticator.executeCommandAndAuthenticateIfNecessary(project, null, repositoryURL, "clone", arguments);
   }
 }
index 1307bf9e2b54bc4316412d284f410eaaf7230321..019d02cf257284e3d1b471a6a9679f094872b380 100644 (file)
@@ -17,6 +17,7 @@ import com.intellij.openapi.application.ModalityState;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.vcsUtil.VcsUtil;
+import org.jetbrains.annotations.Nullable;
 import org.zmlx.hg4idea.ui.HgUsernamePasswordDialog;
 
 import java.net.URISyntaxException;
@@ -27,6 +28,7 @@ import java.util.List;
  */
 class HgCommandAuthenticator {
 
+  @Nullable
   protected HgCommandResult executeCommandAndAuthenticateIfNecessary(Project project, VirtualFile localRepository, String remoteRepository, String command, List<String> arguments) {
     HgCommandService service = HgCommandService.getInstance(project);
     HgCommandResult result = service.execute(localRepository, command, arguments);
@@ -70,8 +72,7 @@ class HgCommandAuthenticator {
     }
 
     public void run() {
-      HgUsernamePasswordDialog dialog = new HgUsernamePasswordDialog(project);
-      dialog.setUsername(hgUrl.getUsername());
+      final HgUsernamePasswordDialog dialog = new HgUsernamePasswordDialog(project, hgUrl.getUsername());
       dialog.show();
 
       if (dialog.isOK()) {
index 8ff3a68e2a42bc894c8e9251509339f28cb28409..dbc3d899cf79653c2fc7b577b69555a39d3b5c7e 100644 (file)
@@ -4,13 +4,15 @@ import com.intellij.openapi.project.Project;
 import org.jetbrains.annotations.Nullable;
 
 import java.nio.charset.Charset;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
 
 public class HgIdentifyCommand {
-  private final Project project;
 
+  private final Project project;
+  private final HgCommandAuthenticator authenticator = new HgCommandAuthenticator();
   private String source;
 
   public HgIdentifyCommand(Project project) {
@@ -27,10 +29,8 @@ public class HgIdentifyCommand {
 
   @Nullable
   public HgCommandResult execute() {
-    List<String> arguments = new LinkedList<String>();
+    final List<String> arguments = new LinkedList<String>();
     arguments.add(source);
-
-    HgCommandService hgCommandService = HgCommandService.getInstance(project);
-    return hgCommandService.execute(null, Collections.<String>emptyList(), "identify", arguments, Charset.defaultCharset());
+    return authenticator.executeCommandAndAuthenticateIfNecessary(project, null, source, "identify", arguments);
   }
 }
diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/ui/HgUsernamePasswordDialog.form b/plugins/hg4idea/src/org/zmlx/hg4idea/ui/HgUsernamePasswordDialog.form
deleted file mode 100644 (file)
index 3760042..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.zmlx.hg4idea.ui.HgUsernamePasswordDialog">
-  <grid id="27dc6" binding="basePanel" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
-    <margin top="0" left="0" bottom="0" right="0"/>
-    <constraints>
-      <xy x="20" y="20" width="500" height="400"/>
-    </constraints>
-    <properties/>
-    <border type="none"/>
-    <children>
-      <component id="24f37" class="javax.swing.JLabel">
-        <constraints>
-          <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
-        </constraints>
-        <properties>
-          <labelFor value="f28fb"/>
-          <text value="&amp;Username"/>
-        </properties>
-      </component>
-      <component id="f28fb" class="javax.swing.JTextField" binding="usernameTxt">
-        <constraints>
-          <grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
-            <preferred-size width="150" height="-1"/>
-          </grid>
-        </constraints>
-        <properties/>
-      </component>
-      <component id="b8a4d" class="javax.swing.JLabel">
-        <constraints>
-          <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
-        </constraints>
-        <properties>
-          <labelFor value="78233"/>
-          <text value="&amp;Password"/>
-        </properties>
-      </component>
-      <vspacer id="c29cf">
-        <constraints>
-          <grid row="2" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
-        </constraints>
-      </vspacer>
-      <component id="78233" class="javax.swing.JPasswordField" binding="passwordTxt">
-        <constraints>
-          <grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
-            <preferred-size width="150" height="-1"/>
-          </grid>
-        </constraints>
-        <properties>
-          <text value=""/>
-        </properties>
-      </component>
-    </children>
-  </grid>
-</form>
index c11c028ed606425681987fd985467eefb262be53..f7bee725727de60f699197d1a91be1083cacf77c 100644 (file)
@@ -14,34 +14,36 @@ package org.zmlx.hg4idea.ui;
 
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.DialogWrapper;
+import com.intellij.util.net.AuthenticationPanel;
+import org.zmlx.hg4idea.HgVcsMessages;
 
 import javax.swing.*;
 
 public class HgUsernamePasswordDialog extends DialogWrapper {
-  private JPanel basePanel;
-  private JTextField usernameTxt;
-  private JPasswordField passwordTxt;
+  private AuthenticationPanel authPanel;
 
-  public HgUsernamePasswordDialog(Project project) {
+  public HgUsernamePasswordDialog(Project project, String login) {
     super(project, false);
-    setTitle("Username and password required");
+    setTitle(HgVcsMessages.message("hgidea.dialog.login.password.required"));
+    authPanel = new AuthenticationPanel(null, login, "", false);
     init();
   }
 
-  public void setUsername(String username) {
-    usernameTxt.setText(username);
+  protected JComponent createCenterPanel() {
+    return authPanel;
   }
 
-  public String getUsername() {
-    return usernameTxt.getText();
+  @Override
+  public JComponent getPreferredFocusedComponent() {
+    return authPanel.getPreferredFocusedComponent();
   }
 
-  public char[] getPassword() {
-    return passwordTxt.getPassword();
+  public String getUsername() {
+    return authPanel.getLogin();
   }
 
-  protected JComponent createCenterPanel() {
-    return basePanel;
+  public char[] getPassword() {
+    return authPanel.getPassword().toCharArray();
   }
 
-}
+}
\ No newline at end of file