/bundles/plugins-trunk/FTP/ftp/LoginDialog.java

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