KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > gui > hostchooser > SftpHostChooser


1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package net.sf.jftp.gui.hostchooser;
17
18 import java.awt.Cursor JavaDoc;
19 import java.awt.GridLayout JavaDoc;
20 import java.awt.Insets JavaDoc;
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.awt.event.ComponentEvent JavaDoc;
24 import java.awt.event.ComponentListener JavaDoc;
25 import java.awt.event.WindowEvent JavaDoc;
26 import java.awt.event.WindowListener JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 import javax.swing.JCheckBox JavaDoc;
31 import javax.swing.JComboBox JavaDoc;
32 import javax.swing.JDialog JavaDoc;
33 import javax.swing.JFileChooser JavaDoc;
34 import javax.swing.JLabel JavaDoc;
35 import javax.swing.event.ChangeEvent JavaDoc;
36 import javax.swing.event.ChangeListener JavaDoc;
37
38 import net.sf.jftp.JFtp;
39 import net.sf.jftp.config.LoadSet;
40 import net.sf.jftp.config.SaveSet;
41 import net.sf.jftp.config.Settings;
42 import net.sf.jftp.gui.framework.HButton;
43 import net.sf.jftp.gui.framework.HFrame;
44 import net.sf.jftp.gui.framework.HPanel;
45 import net.sf.jftp.gui.framework.HPasswordField;
46 import net.sf.jftp.gui.framework.HTextField;
47 import net.sf.jftp.net.Sftp2Connection;
48 import net.sf.jftp.net.SftpConnection;
49 import net.sf.jftp.net.StartConnection;
50 import net.sf.jftp.system.logging.Log;
51 import net.sf.jftp.tools.SshShell;
52
53
54 public class SftpHostChooser extends HFrame implements ActionListener JavaDoc,
55                                                        WindowListener JavaDoc, ChangeListener JavaDoc
56 {
57     public HTextField host = new HTextField("Host:", "localhost");
58     public HTextField user = new HTextField("Username:", "guest");
59     public HTextField port = new HTextField("Port:", "22");
60     public HPasswordField pass = new HPasswordField("Password/Phrase:",
61                                                     "nopasswd");
62     public JComboBox JavaDoc enc = new JComboBox JavaDoc();
63     public JComboBox JavaDoc cs = new JComboBox JavaDoc();
64     public JComboBox JavaDoc keys = new JComboBox JavaDoc();
65     public JLabel JavaDoc encL = new JLabel JavaDoc("Pref. Encryption");
66     public JLabel JavaDoc csL = new JLabel JavaDoc("Pref. Message Auth.");
67     public JLabel JavaDoc keysL = new JLabel JavaDoc("Pref. Public Key");
68     public JLabel JavaDoc keyfileL = new JLabel JavaDoc("(No File)");
69     private HPanel okP = new HPanel();
70     private HPanel keyP = new HPanel();
71     private HButton ok = new HButton("Connect");
72     private HButton keyfile = new HButton("Choose Key File");
73     private ComponentListener JavaDoc listener = null;
74     private boolean useLocal = false;
75     private boolean shell = false;
76     private String JavaDoc keyfileName = null;
77     private JCheckBox JavaDoc useJSch = new JCheckBox JavaDoc("Use JSch instead of j2ssh");
78     
79     public SftpHostChooser(ComponentListener JavaDoc l, boolean local)
80     {
81         listener = l;
82         useLocal = local;
83         init();
84     }
85
86     public SftpHostChooser(ComponentListener JavaDoc l)
87     {
88         listener = l;
89         init();
90     }
91
92     public SftpHostChooser()
93     {
94         init();
95     }
96
97     public SftpHostChooser(boolean shell)
98     {
99         this.shell = shell;
100         init();
101     }
102
103     public void init()
104     {
105         //setSize(500, 200);
106
setLocation(100, 150);
107         setTitle("Sftp Connection...");
108         setBackground(okP.getBackground());
109         getContentPane().setLayout(new GridLayout JavaDoc(7, 2, 5, 3));
110
111         //***MY CHANGES
112
try
113         {
114             File JavaDoc f = new File JavaDoc(Settings.appHomeDir);
115             f.mkdir();
116
117             File JavaDoc f1 = new File JavaDoc(Settings.login);
118             f1.createNewFile();
119
120             File JavaDoc f2 = new File JavaDoc(Settings.login_def_sftp);
121             f2.createNewFile();
122         }
123         catch(IOException JavaDoc ex)
124         {
125             ex.printStackTrace();
126         }
127
128         String JavaDoc[] login = LoadSet.loadSet(Settings.login_def_sftp);
129
130         if((login[0] != null) && (login.length > 1))
131         {
132             host.setText(login[0]);
133             user.setText(login[1]);
134         }
135
136         /*
137         else {
138                 host.setText("localhost");
139                 user.setText("guest");
140
141         }
142         */

143         if(Settings.getStorePasswords())
144         {
145             if((login != null) && (login.length > 2) && (login[2] != null))
146             {
147                 pass.setText(login[2]);
148             }
149         }
150         else
151         {
152             pass.setText("");
153         }
154
155         enc.addItem("3des-cbc");
156         enc.addItem("blowfish-cbc");
157
158         cs.addItem("hmac-sha1");
159         cs.addItem("hmac-sha1-96");
160         cs.addItem("hmac-md5");
161         cs.addItem("hmac-md5-96");
162
163         keys.addItem("ssh-rsa");
164         keys.addItem("ssh-dss");
165
166         //***end of my changes (for this section)
167
getContentPane().add(host);
168         getContentPane().add(port);
169         getContentPane().add(user);
170         getContentPane().add(pass);
171         getContentPane().add(encL);
172         getContentPane().add(enc);
173         getContentPane().add(csL);
174         getContentPane().add(cs);
175         getContentPane().add(keysL);
176         getContentPane().add(keys);
177         getContentPane().add(keyP);
178         getContentPane().add(new JLabel JavaDoc("Keyfiles are usually located ~/.ssh/ on UNIX"));
179         getContentPane().add(useJSch);
180         getContentPane().add(okP);
181
182         keyP.add(keyfileL);
183         keyP.add(keyfile);
184         okP.add(new JLabel JavaDoc(" "));
185         okP.add(ok);
186         ok.addActionListener(this);
187         keyfile.addActionListener(this);
188
189         setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
190
191         pass.text.addActionListener(this);
192         
193         useJSch.addChangeListener(this);
194
195         pack();
196         setModal(false);
197         setVisible(false);
198         addWindowListener(this);
199     }
200
201     public void stateChanged(ChangeEvent JavaDoc e) {
202         if(useJSch.isSelected()) {
203             enc.setEnabled(false);
204             cs.setEnabled(false);
205             keys.setEnabled(false);
206         }
207         else {
208             keyfile.setEnabled(true);
209             enc.setEnabled(true);
210             cs.setEnabled(true);
211             keys.setEnabled(true);
212         }
213     }
214     
215     public void update()
216     {
217         fixLocation();
218         setVisible(true);
219         toFront();
220         host.requestFocus();
221     }
222
223     public void setShell(boolean shell)
224     {
225         this.shell = shell;
226     }
227
228     public boolean getShell()
229     {
230         return shell;
231     }
232
233     public void actionPerformed(ActionEvent JavaDoc e)
234     {
235         if((e.getSource() == ok) || (e.getSource() == pass.text))
236         {
237             // Switch windows
238
//this.setVisible(false);
239
//this.setModal(false);
240
//JFtp.mainFrame.setVisible(true);
241
//JFtp.mainFrame.toFront();
242
setCursor(new Cursor JavaDoc(Cursor.WAIT_CURSOR));
243
244             SftpConnection con = null;
245
246             String JavaDoc htmp = host.getText().trim();
247             String JavaDoc utmp = user.getText().trim();
248             String JavaDoc ptmp = pass.getText();
249
250             //***port number: to be initialized in a future version?
251
int potmp = 22;
252
253             try
254             {
255                 potmp = Integer.parseInt(port.getText());
256             }
257             catch(Exception JavaDoc ex)
258             {
259                 Log.debug("Error: Not a number!");
260             }
261
262             String JavaDoc potmpString = new String JavaDoc("" + potmp);
263
264             com.sshtools.j2ssh.configuration.SshConnectionProperties properties = new com.sshtools.j2ssh.configuration.SshConnectionProperties();
265             properties.setHost(htmp);
266             //Log.debug(htmp+":"+properties.getHost());
267
properties.setPort(potmp);
268             properties.setPrefSCEncryption((String JavaDoc) enc.getSelectedItem());
269             properties.setPrefCSMac((String JavaDoc) cs.getSelectedItem());
270             properties.setPrefPublicKey((String JavaDoc) keys.getSelectedItem());
271
272             if(shell)
273             {
274                 SshShell s = new SshShell(properties, utmp, ptmp, potmp);
275                 setCursor(new Cursor JavaDoc(Cursor.DEFAULT_CURSOR));
276                 this.dispose();
277                 s.toFront();
278
279                 return;
280             }
281             else
282             {
283                 try
284                 {
285                     boolean status;
286
287                     SaveSet s = new SaveSet(Settings.login_def_sftp, htmp,
288                             utmp, ptmp, potmpString, "null", "null");
289
290                     if(!useJSch.isSelected()) {
291                         StartConnection.setSshProperties(properties);
292                         StartConnection.setSshKeyfile(keyfileName);
293                         status = StartConnection.startCon("SFTP", htmp, utmp, ptmp,
294                                 potmp, "", useLocal);
295                     }
296                     else {
297                         Sftp2Connection con2 = new Sftp2Connection(htmp, ""+potmp, keyfileName);
298
299                         if(con2.login(utmp, ptmp))
300                         {
301                             if(useLocal)
302                             {
303                                 JFtp.statusP.jftp.addLocalConnection(htmp, con2);
304                             }
305                             else
306                             {
307                                 JFtp.statusP.jftp.addConnection(htmp, con2);
308                             }
309
310                             if(con2.chdir(con2.getPWD()) || con2.chdir("/"))
311                             {
312                                 ;
313                             }
314                         }
315                     }
316                 }
317                 catch(Exception JavaDoc ex)
318                 {
319                     ex.printStackTrace();
320                     Log.debug("Could not create SftpConnection, does this distribution come with j2ssh?");
321                 }
322             }
323
324             setCursor(new Cursor JavaDoc(Cursor.DEFAULT_CURSOR));
325             this.dispose();
326             JFtp.mainFrame.setVisible(true);
327             JFtp.mainFrame.toFront();
328
329             if(listener != null)
330             {
331                 listener.componentResized(new ComponentEvent JavaDoc(this, 0));
332             }
333         }
334         else if(e.getSource() == keyfile)
335         {
336             JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
337             int returnVal = chooser.showOpenDialog(this);
338
339             if(returnVal == JFileChooser.APPROVE_OPTION)
340             {
341                 keyfileName = chooser.getSelectedFile().getPath();
342
343                 if(keyfileName != null)
344                 {
345                     keyfileL.setText("(File present)");
346                 }
347             }
348             else {
349                 keyfileName = null;
350                 
351                 if(keyfileName != null)
352                 {
353                     keyfileL.setText("(No File)");
354                 }
355             }
356         }
357     }
358
359     public void windowClosing(WindowEvent JavaDoc e)
360     {
361         //System.exit(0);
362
this.dispose();
363     }
364
365     public void windowClosed(WindowEvent JavaDoc e)
366     {
367     }
368
369     public void windowActivated(WindowEvent JavaDoc e)
370     {
371     }
372
373     public void windowDeactivated(WindowEvent JavaDoc e)
374     {
375     }
376
377     public void windowIconified(WindowEvent JavaDoc e)
378     {
379     }
380
381     public void windowDeiconified(WindowEvent JavaDoc e)
382     {
383     }
384
385     public void windowOpened(WindowEvent JavaDoc e)
386     {
387     }
388
389     public Insets JavaDoc getInsets()
390     {
391         Insets JavaDoc std = super.getInsets();
392
393         return new Insets JavaDoc(std.top + 10, std.left + 10, std.bottom + 10,
394                           std.right + 10);
395     }
396
397     public void pause(int time)
398     {
399         try
400         {
401             Thread.sleep(time);
402         }
403         catch(Exception JavaDoc ex)
404         {
405         }
406     }
407 }
408
Popular Tags