/plugins/FTP/tags/release-0-9-4/ftp/LoginDialog.java

# · Java · 373 lines · 261 code · 49 blank · 63 comment · 67 complexity · ddfa77d0f9c021900853ffb2504159c3 MD5 · raw file

  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 org.gjt.sp.jedit.gui.*;
  29. import org.gjt.sp.jedit.*;
  30. @SuppressWarnings("serial")
  31. public class LoginDialog extends EnhancedDialog implements ActionListener
  32. {
  33. //{{{ LoginDialog constructor
  34. public LoginDialog(Component comp, boolean _secure, String host, String user, String password)
  35. {
  36. super(JOptionPane.getFrameForComponent(comp),
  37. jEdit.getProperty(_secure ? "login.title-sftp" : "login.title-ftp"), true);
  38. this.secure = _secure;
  39. JPanel content = new JPanel(new VariableGridLayout(
  40. VariableGridLayout.FIXED_NUM_COLUMNS,1,6,6));
  41. content.setBorder(new EmptyBorder(12,12,12,12));
  42. setContentPane(content);
  43. content.add(createFieldPanel(secure,host,user,password));
  44. useProxy = new JCheckBox(
  45. jEdit.getProperty(secure ? "login.useProxy" : "login.useProxyHttp"),
  46. jEdit.getBooleanProperty("vfs.ftp.useProxy", false)
  47. );
  48. if(!secure) {
  49. passive = new JCheckBox(jEdit.getProperty("login.passive"),
  50. jEdit.getBooleanProperty("vfs.ftp.passive"));
  51. passive.addItemListener(new ItemListener() {
  52. public void itemStateChanged(ItemEvent e) {
  53. useProxy.setEnabled(passive.isSelected());
  54. }
  55. });
  56. useProxy.setEnabled(passive.isSelected());
  57. content.add(passive);
  58. }
  59. content.add(useProxy);
  60. storePassword = new JCheckBox(jEdit.getProperty("login.storePassword"),
  61. jEdit.getBooleanProperty("vfs.ftp.storePassword"));
  62. content.add(storePassword);
  63. Box buttons = new Box(BoxLayout.X_AXIS);
  64. buttons.add(Box.createGlue());
  65. ok = new JButton(jEdit.getProperty("common.ok"));
  66. ok.addActionListener(this);
  67. getRootPane().setDefaultButton(ok);
  68. buttons.add(ok);
  69. buttons.add(Box.createHorizontalStrut(6));
  70. cancel = new JButton(jEdit.getProperty("common.cancel"));
  71. cancel.addActionListener(this);
  72. buttons.add(cancel);
  73. buttons.add(Box.createGlue());
  74. content.add(buttons);
  75. JTextField focus;
  76. if(host == null)
  77. focus = hostField;
  78. else if(user == null)
  79. focus = userField;
  80. else
  81. focus = passwordField;
  82. GUIUtilities.requestFocus(this,focus);
  83. pack();
  84. setLocationRelativeTo(comp);
  85. setModal(true);
  86. setVisible(true);
  87. } //}}}
  88. //{{{ ok() method
  89. public void ok()
  90. {
  91. hostField.addCurrentToHistory();
  92. userField.addCurrentToHistory();
  93. if (privateKeyField!=null) {
  94. privateKeyField.addCurrentToHistory();
  95. }
  96. if(passive != null)
  97. jEdit.setBooleanProperty("vfs.ftp.passive",passive.isSelected());
  98. if(storePassword != null)
  99. jEdit.setBooleanProperty("vfs.ftp.storePassword",storePassword.isSelected());
  100. if (useProxy != null)
  101. jEdit.setBooleanProperty("vfs.ftp.useProxy",useProxy.isSelected());
  102. if(hostField.hasFocus() && userField.getText().length() == 0)
  103. userField.requestFocus();
  104. else if(userField.hasFocus() && passwordField.getPassword().length == 0)
  105. passwordField.requestFocus();
  106. else if (privateKeyField == null && passwordField.getPassword().length == 0)
  107. return;
  108. else if (passwordField.getPassword().length == 0 && privateKeyField != null && privateKeyField.getText().length() == 0)
  109. return;
  110. else
  111. {
  112. host = hostField.getText();
  113. user = userField.getText();
  114. if (privateKeyField!=null && privateKeyField.getText().length() > 0) {
  115. //try{
  116. //SshPrivateKeyFile file = SshPrivateKeyFile.parse(new File(privateKeyField.getText()));
  117. //if (file.isPassphraseProtected()) {
  118. // Log.log(Log.DEBUG, this, "Key File is password protected.");
  119. // PassphraseDialog ppd = new PassphraseDialog(jEdit.getActiveView());
  120. // Point p = this.getLocation();
  121. // Dimension s = this.getSize();
  122. // Dimension ppds = ppd.getSize();
  123. // ppd.setLocation((int)(p.x + s.width/2 - ppds.width/2), (int)(p.y + s.height/2 - ppds.height/2));
  124. // ppd.setMessage(jEdit.getProperty("login.privatekeypassword"));
  125. // ppd.setVisible(true);
  126. // if (ppd.isCancelled())
  127. // return;
  128. // privateKey = file.toPrivateKey(new String(ppd.getPassphrase()));
  129. //} else {
  130. // privateKey = file.toPrivateKey(null);
  131. //}
  132. privateKeyFilename = privateKeyField.getText();
  133. privateKey = privateKeyField.getText();
  134. //} catch (InvalidSshKeyException iske) {
  135. // GUIUtilities.error(this,"vfs.sftp.invalid-privatekey",new Object[] {iske.getMessage()});
  136. // return;
  137. //} catch (IOException ioe) {
  138. // GUIUtilities.error(this,"vfs.sftp.invalid-privatekey",new Object[] {ioe.getMessage()});
  139. // return;
  140. //}
  141. }
  142. if(host.length() == 0 || user.length() == 0)
  143. {
  144. getToolkit().beep();
  145. return;
  146. }
  147. password = new String(passwordField.getPassword());
  148. isOK = true;
  149. dispose();
  150. }
  151. } //}}}
  152. //{{{ cancel() method
  153. public void cancel()
  154. {
  155. dispose();
  156. } //}}}
  157. //{{{ isOK() method
  158. public boolean isOK()
  159. {
  160. return isOK;
  161. } //}}}
  162. //{{{ getHost() method
  163. public String getHost()
  164. {
  165. return host;
  166. } //}}}
  167. //{{{ getUser() method
  168. public String getUser()
  169. {
  170. return user;
  171. } //}}}
  172. //{{{ getPassword() method
  173. public String getPassword()
  174. {
  175. return password;
  176. } //}}}
  177. //{{{ getPrivateKey() method
  178. public String getPrivateKey()
  179. {
  180. return privateKey;
  181. } //}}}
  182. //{{{ getPrivateKeyFilename() method
  183. public String getPrivateKeyFilename()
  184. {
  185. return privateKeyFilename;
  186. } //}}}
  187. //{{{ actionPerformed() method
  188. public void actionPerformed(ActionEvent evt)
  189. {
  190. Object source = evt.getSource();
  191. if(source == ok)
  192. ok();
  193. else if(source == cancel)
  194. cancel();
  195. else if(source == hostField)
  196. userField.requestFocus();
  197. else if(source == userField)
  198. passwordField.requestFocus();
  199. else if(source == passwordField)
  200. ok();
  201. else if(source == privateKeyField)
  202. ok();
  203. } //}}}
  204. //{{{ Private members
  205. private HistoryTextField hostField;
  206. private HistoryTextField userField;
  207. private JPasswordField passwordField;
  208. private HistoryTextField privateKeyField;
  209. private JButton privateKeySelect;
  210. private JCheckBox passive;
  211. private JCheckBox storePassword;
  212. private JCheckBox useProxy;
  213. private String host;
  214. private String user;
  215. private String password;
  216. private String privateKeyFilename;
  217. private String privateKey;
  218. private boolean isOK;
  219. private boolean secure;
  220. private JButton ok;
  221. private JButton cancel;
  222. //{{{ createFieldPanel() method
  223. private JPanel createFieldPanel(boolean secure, String host, String user,
  224. String password)
  225. {
  226. JPanel panel = new JPanel(new VariableGridLayout(
  227. VariableGridLayout.FIXED_NUM_COLUMNS,2,6,6));
  228. JLabel label = new JLabel(jEdit.getProperty("login.host"),
  229. SwingConstants.RIGHT);
  230. panel.add(label);
  231. hostField = new HistoryTextField(secure ? "sftp.host" : "ftp.host");
  232. hostField.setText(host);
  233. hostField.setColumns(20);
  234. if(host != null)
  235. hostField.setEnabled(false);
  236. hostField.getDocument().addDocumentListener(new FieldCompletionListener());
  237. hostField.addActionListener(this);
  238. panel.add(hostField);
  239. label = new JLabel(jEdit.getProperty("login.user"),
  240. SwingConstants.RIGHT);
  241. panel.add(label);
  242. userField = new HistoryTextField("ftp.user");
  243. userField.setText(user);
  244. userField.setColumns(20);
  245. userField.getDocument().addDocumentListener(new FieldCompletionListener());
  246. userField.addActionListener(this);
  247. panel.add(userField);
  248. label = new JLabel(jEdit.getProperty("login.password"),
  249. SwingConstants.RIGHT);
  250. panel.add(label);
  251. passwordField = new JPasswordField(password,20);
  252. passwordField.addActionListener(this);
  253. panel.add(passwordField);
  254. if (secure)
  255. {
  256. Box privateKeyBox = Box.createHorizontalBox();
  257. privateKeyField = new HistoryTextField("sftp.privateKey");
  258. privateKeyField.addActionListener(this);
  259. label = new JLabel(jEdit.getProperty("login.privateKey"),
  260. SwingConstants.RIGHT);
  261. panel.add(label);
  262. privateKeyBox.add(privateKeyField);
  263. privateKeySelect = new JButton("...");
  264. privateKeySelect.setMargin(new Insets(0,0,0,0));
  265. privateKeySelect.addActionListener(new PrivateKeySelectActionListener(this));
  266. privateKeyBox.add(privateKeySelect);
  267. panel.add(privateKeyBox);
  268. }
  269. checkKey();
  270. return panel;
  271. } //}}}
  272. //{{{ checkKey() method
  273. public void checkKey()
  274. {
  275. String host = hostField.getText();
  276. String user = userField.getText();
  277. if(host.indexOf(":") == -1)
  278. host = host + ":" + FtpVFS.getDefaultPort(secure);
  279. String key = jEdit.getProperty("ftp.keys."+host+"."+user);
  280. String pass = ConnectionManager.getPassword(host+"."+user);
  281. if (secure)
  282. {
  283. if (key != null)
  284. privateKeyField.setText(key);
  285. else
  286. privateKeyField.setText("");
  287. }
  288. if (pass != null)
  289. passwordField.setText(pass);
  290. else
  291. passwordField.setText("");
  292. }
  293. //}}}
  294. //}}}
  295. //{{{ class PrivateKeySelectActionListener
  296. class PrivateKeySelectActionListener implements ActionListener
  297. {
  298. private Component parent;
  299. public PrivateKeySelectActionListener(Component c) {
  300. super();
  301. parent = c;
  302. }
  303. public void actionPerformed(ActionEvent e) {
  304. JFileChooser chooser = new JFileChooser();
  305. chooser.setDialogTitle(jEdit.getProperty("login.selectprivatekey"));
  306. chooser.setFileHidingEnabled(false);
  307. int returnVal = chooser.showOpenDialog(parent);
  308. if(returnVal == JFileChooser.APPROVE_OPTION) {
  309. try{
  310. privateKeyField.setText(chooser.getSelectedFile().getCanonicalPath());
  311. } catch(java.io.IOException err) {
  312. // Might be nice to pop this up
  313. }
  314. }
  315. }
  316. } //}}}
  317. //{{{ class FieldCompletionListener
  318. class FieldCompletionListener implements DocumentListener
  319. {
  320. public void changedUpdate(DocumentEvent e) { checkKey(); }
  321. public void insertUpdate(DocumentEvent e) { checkKey(); }
  322. public void removeUpdate(DocumentEvent e) { checkKey(); }
  323. } //}}}
  324. }