/plugins/FTP/tags/ftp_0_7_5/ftp/LoginDialog.java

#
Java | 334 lines | 257 code | 38 blank | 39 comment | 63 complexity | 543354cff5b6493f4fb53f690dd2a7bc MD5 | raw file

✨ Summary
  1. /*
  2. * LoginDialog.java - FTP login dialog
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package ftp;
  23. import javax.swing.*;
  24. import javax.swing.border.*;
  25. import javax.swing.event.*;
  26. import java.awt.*;
  27. import java.awt.event.*;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import org.gjt.sp.jedit.gui.*;
  31. import org.gjt.sp.jedit.*;
  32. import org.gjt.sp.util.Log;
  33. import com.sshtools.j2ssh.transport.publickey.*;
  34. import com.sshtools.common.authentication.PassphraseDialog;
  35. public class LoginDialog extends EnhancedDialog implements ActionListener
  36. {
  37. //{{{ LoginDialog constructor
  38. public LoginDialog(Component comp, boolean _secure, String host,
  39. String user, String password)
  40. {
  41. super(JOptionPane.getFrameForComponent(comp),
  42. jEdit.getProperty(_secure ?
  43. "login.title-sftp" : "login.title-ftp"),
  44. true);
  45. this.secure = _secure;
  46. JPanel content = new JPanel(new VariableGridLayout(
  47. VariableGridLayout.FIXED_NUM_COLUMNS,1,6,6));
  48. content.setBorder(new EmptyBorder(12,12,12,12));
  49. setContentPane(content);
  50. content.add(createFieldPanel(secure,host,user,password));
  51. if(!secure)
  52. {
  53. passive = new JCheckBox(jEdit.getProperty("login.passive"),
  54. jEdit.getBooleanProperty("vfs.ftp.passive"));
  55. content.add(passive);
  56. }
  57. Box buttons = new Box(BoxLayout.X_AXIS);
  58. buttons.add(Box.createGlue());
  59. ok = new JButton(jEdit.getProperty("common.ok"));
  60. ok.addActionListener(this);
  61. getRootPane().setDefaultButton(ok);
  62. buttons.add(ok);
  63. buttons.add(Box.createHorizontalStrut(6));
  64. cancel = new JButton(jEdit.getProperty("common.cancel"));
  65. cancel.addActionListener(this);
  66. buttons.add(cancel);
  67. buttons.add(Box.createGlue());
  68. content.add(buttons);
  69. JTextField focus;
  70. if(host == null)
  71. focus = hostField;
  72. else if(user == null)
  73. focus = userField;
  74. else
  75. focus = passwordField;
  76. GUIUtilities.requestFocus(this,focus);
  77. pack();
  78. setLocationRelativeTo(comp);
  79. show();
  80. } //}}}
  81. //{{{ ok() method
  82. public void ok()
  83. {
  84. hostField.addCurrentToHistory();
  85. userField.addCurrentToHistory();
  86. if (privateKeyField!=null) {
  87. privateKeyField.addCurrentToHistory();
  88. }
  89. if(passive != null)
  90. jEdit.setBooleanProperty("vfs.ftp.passive",passive.isSelected());
  91. if(hostField.hasFocus() && userField.getText().length() == 0)
  92. userField.requestFocus();
  93. else if(userField.hasFocus() && passwordField.getPassword().length == 0)
  94. passwordField.requestFocus();
  95. else if (privateKeyField == null && passwordField.getPassword().length == 0)
  96. return;
  97. else if (passwordField.getPassword().length == 0 && privateKeyField != null && privateKeyField.getText().length() == 0)
  98. return;
  99. else
  100. {
  101. host = hostField.getText();
  102. user = userField.getText();
  103. if (privateKeyField!=null && privateKeyField.getText().length() > 0) {
  104. try{
  105. SshPrivateKeyFile file = SshPrivateKeyFile.parse(new File(privateKeyField.getText()));
  106. if (file.isPassphraseProtected()) {
  107. Log.log(Log.DEBUG, this, "Key File is password protected.");
  108. PassphraseDialog ppd = new PassphraseDialog(this);
  109. ppd.setMessage(jEdit.getProperty("login.privatekeypassword"));
  110. ppd.show();
  111. if (ppd.isCancelled())
  112. return;
  113. privateKey = file.toPrivateKey(new String(ppd.getPassphrase()));
  114. } else {
  115. privateKey = file.toPrivateKey(null);
  116. }
  117. privateKeyFilename = privateKeyField.getText();
  118. } catch (InvalidSshKeyException iske) {
  119. GUIUtilities.error(this,"vfs.sftp.invalid-privatekey",new Object[] {iske.getMessage()});
  120. return;
  121. } catch (IOException ioe) {
  122. GUIUtilities.error(this,"vfs.sftp.invalid-privatekey",new Object[] {ioe.getMessage()});
  123. return;
  124. }
  125. }
  126. if(host.length() == 0 || user.length() == 0)
  127. {
  128. getToolkit().beep();
  129. return;
  130. }
  131. password = new String(passwordField.getPassword());
  132. isOK = true;
  133. dispose();
  134. }
  135. } //}}}
  136. //{{{ cancel() method
  137. public void cancel()
  138. {
  139. dispose();
  140. } //}}}
  141. //{{{ isOK() method
  142. public boolean isOK()
  143. {
  144. return isOK;
  145. } //}}}
  146. //{{{ getHost() method
  147. public String getHost()
  148. {
  149. return host;
  150. } //}}}
  151. //{{{ getUser() method
  152. public String getUser()
  153. {
  154. return user;
  155. } //}}}
  156. //{{{ getPassword() method
  157. public String getPassword()
  158. {
  159. return password;
  160. } //}}}
  161. //{{{ getPrivateKey() method
  162. public SshPrivateKey getPrivateKey()
  163. {
  164. return privateKey;
  165. } //}}}
  166. //{{{ getPrivateKeyFilename() method
  167. public String getPrivateKeyFilename()
  168. {
  169. return privateKeyFilename;
  170. } //}}}
  171. //{{{ actionPerformed() method
  172. public void actionPerformed(ActionEvent evt)
  173. {
  174. Object source = evt.getSource();
  175. if(source == ok)
  176. ok();
  177. else if(source == cancel)
  178. cancel();
  179. else if(source == hostField)
  180. userField.requestFocus();
  181. else if(source == userField)
  182. passwordField.requestFocus();
  183. else if(source == passwordField)
  184. ok();
  185. else if(source == privateKeyField)
  186. ok();
  187. } //}}}
  188. //{{{ Private members
  189. private HistoryTextField hostField;
  190. private HistoryTextField userField;
  191. private JPasswordField passwordField;
  192. private HistoryTextField privateKeyField;
  193. private JButton privateKeySelect;
  194. private JCheckBox passive;
  195. private String host;
  196. private String user;
  197. private String password;
  198. private String privateKeyFilename;
  199. private SshPrivateKey privateKey;
  200. private boolean isOK;
  201. private boolean secure;
  202. private JButton ok;
  203. private JButton cancel;
  204. //{{{ createFieldPanel() method
  205. private JPanel createFieldPanel(boolean secure, String host, String user,
  206. String password)
  207. {
  208. JPanel panel = new JPanel(new VariableGridLayout(
  209. VariableGridLayout.FIXED_NUM_COLUMNS,2,6,6));
  210. JLabel label = new JLabel(jEdit.getProperty("login.host"),
  211. SwingConstants.RIGHT);
  212. panel.add(label);
  213. hostField = new HistoryTextField(secure ? "sftp.host" : "ftp.host");
  214. hostField.setText(host);
  215. hostField.setColumns(20);
  216. if(host != null)
  217. hostField.setEnabled(false);
  218. if (secure)
  219. hostField.getDocument().addDocumentListener(new FieldCompletionListener());
  220. hostField.addActionListener(this);
  221. panel.add(hostField);
  222. label = new JLabel(jEdit.getProperty("login.user"),
  223. SwingConstants.RIGHT);
  224. panel.add(label);
  225. userField = new HistoryTextField("ftp.user");
  226. userField.setText(user);
  227. userField.setColumns(20);
  228. if (secure)
  229. userField.getDocument().addDocumentListener(new FieldCompletionListener());
  230. userField.addActionListener(this);
  231. panel.add(userField);
  232. label = new JLabel(jEdit.getProperty("login.password"),
  233. SwingConstants.RIGHT);
  234. panel.add(label);
  235. passwordField = new JPasswordField(password,20);
  236. passwordField.addActionListener(this);
  237. panel.add(passwordField);
  238. if (secure)
  239. {
  240. Box privateKeyBox = Box.createHorizontalBox();
  241. privateKeyField = new HistoryTextField("sftp.privateKey");
  242. privateKeyField.addActionListener(this);
  243. label = new JLabel(jEdit.getProperty("login.privateKey"),
  244. SwingConstants.RIGHT);
  245. panel.add(label);
  246. privateKeyBox.add(privateKeyField);
  247. privateKeySelect = new JButton("...");
  248. privateKeySelect.setMargin(new Insets(0,0,0,0));
  249. privateKeySelect.addActionListener(new PrivateKeySelectActionListener(this));
  250. privateKeyBox.add(privateKeySelect);
  251. panel.add(privateKeyBox);
  252. checkKey();
  253. }
  254. return panel;
  255. } //}}}
  256. //{{{ checkKey() method
  257. public void checkKey()
  258. {
  259. String host = hostField.getText();
  260. String user = userField.getText();
  261. if(host.indexOf(":") == -1)
  262. host = host + ":" + FtpVFS.getDefaultPort(secure);
  263. privateKeyField.setText(jEdit.getProperty("ftp.keys."+host+"."+user));
  264. }
  265. //}}}
  266. //}}}
  267. //{{{ class PrivateKeySelectActionListener
  268. class PrivateKeySelectActionListener implements ActionListener
  269. {
  270. private Component parent;
  271. public PrivateKeySelectActionListener(Component c) {
  272. super();
  273. parent = c;
  274. }
  275. public void actionPerformed(ActionEvent e) {
  276. JFileChooser chooser = new JFileChooser();
  277. chooser.setDialogTitle(jEdit.getProperty("login.selectprivatekey"));
  278. chooser.setFileHidingEnabled(false);
  279. int returnVal = chooser.showOpenDialog(parent);
  280. if(returnVal == JFileChooser.APPROVE_OPTION) {
  281. try{
  282. privateKeyField.setText(chooser.getSelectedFile().getCanonicalPath());
  283. } catch(java.io.IOException err) {
  284. // Might be nice to pop this up
  285. }
  286. }
  287. }
  288. } //}}}
  289. //{{{ class FieldCompletionListener
  290. class FieldCompletionListener implements DocumentListener
  291. {
  292. public void changedUpdate(DocumentEvent e) { checkKey(); }
  293. public void insertUpdate(DocumentEvent e) { checkKey(); }
  294. public void removeUpdate(DocumentEvent e) { checkKey(); }
  295. } //}}}
  296. }