KickJava   Java API By Example, From Geeks To Geeks.

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


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.BorderLayout JavaDoc;
19 import java.awt.Cursor JavaDoc;
20 import java.awt.FlowLayout JavaDoc;
21 import java.awt.Font JavaDoc;
22 import java.awt.GridLayout JavaDoc;
23 import java.awt.Insets JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.awt.event.ComponentEvent JavaDoc;
27 import java.awt.event.ComponentListener JavaDoc;
28 import java.awt.event.WindowEvent JavaDoc;
29 import java.awt.event.WindowListener JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 import javax.swing.JCheckBox JavaDoc;
33 import javax.swing.JDialog JavaDoc;
34 import javax.swing.JLabel JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36 import javax.swing.JTextArea 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.base.FtpHost;
43 import net.sf.jftp.gui.framework.HButton;
44 import net.sf.jftp.gui.framework.HFrame;
45 import net.sf.jftp.gui.framework.HPanel;
46 import net.sf.jftp.gui.framework.HPasswordField;
47 import net.sf.jftp.gui.framework.HTextField;
48 import net.sf.jftp.gui.tasks.HostList;
49 import net.sf.jftp.net.FilesystemConnection;
50 import net.sf.jftp.net.FtpConnection;
51 import net.sf.jftp.net.FtpURLConnection;
52 import net.sf.jftp.net.StartConnection;
53 import net.sf.jftp.system.StringUtils;
54 import net.sf.jftp.system.logging.Log;
55
56
57 public class HostChooser extends HFrame implements ActionListener JavaDoc,
58                                                    WindowListener JavaDoc
59 {
60     public HTextField host = new HTextField("Hostname:", "localhost ");
61     public HTextField user = new HTextField("Username:", "anonymous ");
62
63     //public static HTextField pass = new HTextField("Password:","none@nowhere.no");
64
public HPasswordField pass = new HPasswordField("Password:",
65                                                     "none@nowhere.no");
66     public HTextField port = new HTextField("Port: ", "21");
67     public HTextField cwd = new HTextField("Remote: ", Settings.defaultDir);
68     public HTextField lcwd = new HTextField("Local: ", Settings.defaultWorkDir);
69     public HTextField dl = new HTextField("Max. connections: ", "3");
70     public HTextField crlf = new HTextField("Override server newline: ", "<default>");
71     private JCheckBox JavaDoc anonBox = new JCheckBox JavaDoc("Use anonymous login", false);
72     private JCheckBox JavaDoc listBox = new JCheckBox JavaDoc("LIST compatibility mode", false);
73     private JCheckBox JavaDoc dirBox = new JCheckBox JavaDoc("Use default directories",
74                                              Settings.getUseDefaultDir());
75     private JCheckBox JavaDoc modeBox = new JCheckBox JavaDoc("Use active Ftp (no need to)",
76                                               false);
77     private JCheckBox JavaDoc threadBox = new JCheckBox JavaDoc("Multiple connections", false);
78     private HPanel okP = new HPanel();
79     private HButton ok = new HButton("Connect");
80     private HButton backMode = new HButton("Yes");
81     private HButton frontMode = new HButton("No");
82     private HFrame h = new HFrame();
83     private HPanel listP = new HPanel();
84     private HButton list = new HButton("Choose from or edit list...");
85     private ComponentListener JavaDoc listener = null;
86     private int mode = 0;
87     private boolean useLocal = false;
88     private boolean ext = Settings.showNewlineOption;
89
90     public HostChooser(ComponentListener JavaDoc l, boolean local)
91     {
92         listener = l;
93         useLocal = local;
94         //TODO ext -> newlineoption
95
//this.ext = ext;
96
init();
97     }
98
99     public HostChooser(ComponentListener JavaDoc l)
100     {
101         listener = l;
102         init();
103     }
104
105     public HostChooser()
106     {
107         init();
108     }
109
110     public void init()
111     {
112         //setSize(600, 340);
113
// setLocation(50, 150);
114
setTitle("Ftp Connection...");
115         setBackground(okP.getBackground());
116         getContentPane().setLayout(new GridLayout JavaDoc((ext ? 8 : 7), 2, 5, 3));
117
118         anonBox.setSelected(false);
119         user.setEnabled(true);
120         pass.text.setEnabled(true);
121
122         LoadSet l = new LoadSet();
123         String JavaDoc[] login = l.loadSet(Settings.login_def);
124
125         if((login != null) && (login[0] != null))
126         {
127             host.setText(login[0]);
128             user.setText(login[1]);
129
130             if(login[3] != null)
131             {
132                 port.setText(login[3]);
133             }
134
135             if(login[4] != null)
136             {
137                 cwd.setText(login[4]);
138             }
139
140             if(login[5] != null)
141             {
142                 lcwd.setText(login[5]);
143             }
144         }
145
146         if(Settings.getStorePasswords())
147         {
148             if(login != null)
149             {
150                 pass.setText(login[2]);
151             }
152         }
153         else
154         {
155             pass.setText("");
156         }
157
158         getContentPane().add(host);
159         getContentPane().add(port);
160         getContentPane().add(anonBox);
161         getContentPane().add(listBox);
162         getContentPane().add(user);
163         getContentPane().add(pass);
164         getContentPane().add(dirBox);
165         getContentPane().add(modeBox);
166         getContentPane().add(lcwd);
167         getContentPane().add(cwd);
168         getContentPane().add(threadBox);
169         getContentPane().add(dl);
170
171 if(ext) {
172         getContentPane().add(crlf);
173
174     JPanel JavaDoc x1 = new JPanel JavaDoc();
175     x1.setLayout(new BorderLayout JavaDoc(2,2));
176     JLabel JavaDoc l1 = new JLabel JavaDoc("Unix: LF, Mac/MVS: CR, Win: CRLF");
177     l1.setFont(new Font JavaDoc("Dialog", Font.PLAIN, 10));
178     JLabel JavaDoc l2 = new JLabel JavaDoc("Don't change this unless you transfer text only");
179     l2.setFont(new Font JavaDoc("Dialog", Font.PLAIN, 10));
180     x1.add("North", l1);
181     x1.add("South", l2);
182         getContentPane().add(x1);
183 }
184
185         modeBox.setSelected(!Settings.getFtpPasvMode());
186         threadBox.setSelected(Settings.getEnableMultiThreading());
187         dirBox.setSelected(Settings.getUseDefaultDir());
188         anonBox.addActionListener(this);
189         threadBox.addActionListener(this);
190
191         getContentPane().add(okP);
192         okP.add(ok);
193         ok.addActionListener(this);
194
195         getContentPane().add(listP);
196         listP.add(list);
197         list.addActionListener(this);
198
199         dirBox.addActionListener(this);
200         setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
201
202         lcwd.setEnabled(!dirBox.isSelected());
203         cwd.setEnabled(!dirBox.isSelected());
204         pass.text.addActionListener(this);
205
206         pack();
207         setModal(false);
208         setVisible(false);
209         invalidate();
210         validate();
211         addWindowListener(this);
212         prepareBackgroundMessage();
213     }
214
215     public void update()
216     {
217     fixLocation();
218         setVisible(true);
219         toFront();
220         host.requestFocus();
221     }
222
223     public void update(String JavaDoc url)
224     {
225         try
226         {
227             FtpURLConnection uc = new FtpURLConnection(new java.net.URL JavaDoc(url));
228             FtpConnection con = uc.getFtpConnection();
229
230             JFtp.statusP.jftp.addConnection(url, con);
231
232             //con.addConnectionListener((ConnectionListener) JFtp.localDir);
233
//con.addConnectionListener((ConnectionListener) JFtp.remoteDir);
234
//JFtp.remoteDir.setCon(con);
235
uc.connect();
236
237             int response = uc.getLoginResponse();
238
239             if(response != FtpConnection.LOGIN_OK)
240             {
241                 setTitle("Wrong password!");
242                 host.setText(uc.getHost());
243                 port.setText(Integer.toString(uc.getPort()));
244                 user.setText(uc.getUser());
245                 pass.setText(uc.getPass());
246                 setVisible(true);
247                 toFront();
248                 host.requestFocus();
249             }
250             else
251             {
252                 this.dispose();
253
254                 if(listener != null)
255                 {
256                     listener.componentResized(new ComponentEvent JavaDoc(this, 0));
257                 }
258
259                 JFtp.mainFrame.setVisible(true);
260                 JFtp.mainFrame.toFront();
261             }
262         }
263
264         //catch(MalformedURLException ex)
265
//{
266
// Log.debug("Malformed URL!");
267
// ex.printStackTrace();
268
//}
269
catch(IOException JavaDoc ex)
270         {
271             Log.debug("Error!");
272             ex.printStackTrace();
273         }
274     }
275
276     public void actionPerformed(ActionEvent JavaDoc e)
277     {
278         if((e.getSource() == ok) || (e.getSource() == pass.text))
279         {
280             // Switch windows
281
//this.setVisible(false);
282
setCursor(new Cursor JavaDoc(Cursor.WAIT_CURSOR));
283
284             //JFtp.mainFrame.setVisible(true);
285
//JFtp.mainFrame.toFront();
286
FtpConnection con = null;
287             JFtp.setHost(host.getText());
288
289             String JavaDoc htmp = StringUtils.cut(host.getText(), " ");
290             String JavaDoc utmp = StringUtils.cut(user.getText(), " ");
291             String JavaDoc ptmp = StringUtils.cut(pass.getText(), " ");
292             String JavaDoc potmp = StringUtils.cut(port.getText(), " ");
293
294             Settings.setProperty("jftp.ftpPasvMode", !modeBox.isSelected());
295             Settings.setProperty("jftp.enableMultiThreading",
296                                  threadBox.isSelected());
297             Settings.setProperty("jftp.useDefaultDir", dirBox.isSelected());
298
299             if(listBox.isSelected())
300             {
301                 FtpConnection.LIST = "LIST";
302             }
303             else
304             {
305                 FtpConnection.LIST = "LIST -laL";
306             }
307
308             /* All the information of the current server are stored in JFtp.HostInfo */
309             JFtp.hostinfo.hostname = htmp;
310             JFtp.hostinfo.username = utmp;
311             JFtp.hostinfo.password = ptmp;
312             JFtp.hostinfo.port = potmp;
313             JFtp.hostinfo.type = "ftp";
314
315             boolean pasv = Settings.getFtpPasvMode();
316             boolean threads = Settings.getEnableMultiThreading();
317
318             /*
319             if(!pasv && threads) // only passive ftp threading works
320             {
321                 //Settings.setProperty("jftp.enableMultiThreading", false);
322                 JDialog j = new JDialog();
323                 j.setTitle("Warning");
324                 j.setLocation(150, 150);
325                 j.setSize(450, 100);
326                 j.getContentPane().add(new JLabel("Multithreading in active mode is EXPERIMENTAL"));
327                 j.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
328                 j.setModal(true);
329                 j.setVisible(true);
330             }*/

331
332             int x = Integer.parseInt(dl.getText().trim());
333             Settings.maxConnections = x;
334
335             Settings.save();
336
337             //*** My modifications are below here
338
String JavaDoc dtmp;
339             String JavaDoc ltmp;
340
341             if(dirBox.isSelected())
342             {
343                 dtmp = Settings.defaultDir;
344                 ltmp = Settings.defaultWorkDir;
345             }
346             else
347             {
348                 dtmp = cwd.getText();
349                 ltmp = lcwd.getText();
350             }
351
352             //*** More of my stuff
353
SaveSet s = new SaveSet(Settings.login_def, htmp, utmp, ptmp,
354                                     potmp, dtmp, ltmp);
355
356             if(JFtp.localDir instanceof FilesystemConnection)
357             {
358                 if(!JFtp.localDir.setPath(ltmp))
359                 {
360                     if(!JFtp.localDir.setPath(System.getProperty("user.home")))
361                     {
362                         JFtp.localDir.setPath("/");
363                     }
364                 }
365             }
366
367             int response = StartConnection.startFtpCon(htmp, utmp, ptmp,
368                                                        Integer.parseInt(potmp),
369                                                        dtmp, useLocal, crlf.getText().trim());
370
371             /*
372
373             con = new FtpConnection(htmp,
374                                     Integer.parseInt(potmp), dtmp);
375
376
377             //con.addConnectionListener((ConnectionListener) JFtp.localDir);
378             //con.addConnectionListener((ConnectionListener) JFtp.remoteDir);
379             //JFtp.remoteDir.setCon(con);
380
381             if (useLocal)
382                    JFtp.statusP.jftp.addLocalConnection(
383                            htmp, con);
384             else
385                    JFtp.statusP.jftp.addConnection(htmp, con);
386
387             int response = con.login(utmp, ptmp);
388
389             */

390             //*** START OF STUFF TO PUT INTO SEPARATE METHOD
391
/*
392             if (response!=FtpConnection.LOGIN_OK)
393                     tryFtpAgain(response, htmp, ptmp, utmp, potmp,
394                         dtmp, useLocal);
395
396             */

397             //*** END OF STUFF TO PUT INTO SEPARATE METHOD
398
//else
399
//{
400
setCursor(new Cursor JavaDoc(Cursor.DEFAULT_CURSOR));
401             this.dispose();
402
403             //this.setVisible(false);
404
JFtp.mainFrame.setVisible(true);
405             JFtp.mainFrame.toFront();
406
407             if(listener != null)
408             {
409                 listener.componentResized(new ComponentEvent JavaDoc(this, 0));
410             }
411
412             //}
413
}
414         else if(e.getSource() == list)
415         {
416             HostList hl = new HostList(this);
417             FtpHost selectedHost = hl.getFtpHost();
418
419             if(selectedHost == null)
420             {
421                 return;
422             }
423
424             host.setText(selectedHost.hostname);
425             pass.setText(selectedHost.password);
426             user.setText(selectedHost.username);
427             port.setText(selectedHost.port);
428
429             Settings.setProperty("jftp.useDefaultDir", true);
430             dirBox.setSelected(Settings.getUseDefaultDir());
431             lcwd.setEnabled(!dirBox.isSelected());
432             cwd.setEnabled(!dirBox.isSelected());
433         }
434         else if(e.getSource() == dirBox)
435         {
436             if(!dirBox.isSelected())
437             {
438                 lcwd.setEnabled(true);
439                 cwd.setEnabled(true);
440             }
441             else
442             {
443                 lcwd.setEnabled(false);
444                 cwd.setEnabled(false);
445             }
446         }
447         else if(e.getSource() == anonBox)
448         {
449             if(!anonBox.isSelected())
450             {
451                 user.setEnabled(true);
452                 pass.text.setEnabled(true);
453             }
454             else
455             {
456                 user.setText("anonymous");
457                 pass.setText("no@no.no");
458                 user.setEnabled(false);
459                 pass.text.setEnabled(false);
460             }
461         }
462         else if(e.getSource() == threadBox)
463         {
464             if(threadBox.isSelected())
465             {
466                 dl.setEnabled(true);
467             }
468             else
469             {
470                 dl.setEnabled(false);
471             }
472         }
473         else if(e.getSource() == backMode)
474         {
475             mode = 1;
476             h.setVisible(false);
477             ;
478         }
479         else if(e.getSource() == frontMode)
480         {
481             mode = 2;
482             h.setVisible(false);
483         }
484     }
485
486     /*
487     private void setFilesystemConnection()
488     {
489     FilesystemConnection c = new FilesystemConnection();
490     c.addConnectionListener(((ConnectionListener)JFtp.remoteDir));
491     if(!c.chdir("/")) c.chdir("C:\\");
492     JFtp.remoteDir.setCon(c);
493     }
494     */

495     private void prepareBackgroundMessage()
496     {
497         HPanel p = new HPanel();
498         p.add(backMode);
499         p.add(frontMode);
500         p.setLayout(new FlowLayout JavaDoc(FlowLayout.CENTER));
501
502         backMode.addActionListener(this);
503         frontMode.addActionListener(this);
504
505         h.getContentPane().setLayout(new BorderLayout JavaDoc(10, 10));
506         h.setTitle("Connection failed!");
507         h.setLocation(150, 200);
508
509         JTextArea JavaDoc text = new JTextArea JavaDoc();
510         h.getContentPane().add("Center", text);
511         h.getContentPane().add("South", p);
512         text.setText(" ---------------- Output -----------------\n\n" +
513                      "The server is busy at the moment.\n\n" +
514                      "Do you want JFtp to go to disappear and try to login\n" +
515                      "continuously?\n\n" +
516                      "(It will show up again when it has initiated a connection)\n\n");
517         JFtp.log.setText("");
518         text.setEditable(false);
519         h.pack();
520     }
521
522     public void windowClosing(WindowEvent JavaDoc e)
523     {
524         //System.exit(0);
525
this.dispose();
526     }
527
528     public void windowClosed(WindowEvent JavaDoc e)
529     {
530     }
531
532     public void windowActivated(WindowEvent JavaDoc e)
533     {
534     }
535
536     public void windowDeactivated(WindowEvent JavaDoc e)
537     {
538     }
539
540     public void windowIconified(WindowEvent JavaDoc e)
541     {
542     }
543
544     public void windowDeiconified(WindowEvent JavaDoc e)
545     {
546     }
547
548     public void windowOpened(WindowEvent JavaDoc e)
549     {
550     }
551
552     public Insets JavaDoc getInsets()
553     {
554         Insets JavaDoc std = super.getInsets();
555
556         return new Insets JavaDoc(std.top + 10, std.left + 10, std.bottom + 10,
557                           std.right + 10);
558     }
559
560     public void pause(int time)
561     {
562         try
563         {
564             Thread.sleep(time);
565         }
566         catch(Exception JavaDoc ex)
567         {
568         }
569     }
570
571     private void tryFtpAgain(int response, String JavaDoc htmp, String JavaDoc ptmp,
572                              String JavaDoc utmp, String JavaDoc potmp, String JavaDoc dtmp,
573                              boolean useLocal)
574     {
575         //*** FOR TESTING PURPOSES
576
//System.out.println(htmp + " " + ptmp + " " + utmp);
577
//System.out.println(potmp + " " + dtmp);
578
//***
579
if((response == FtpConnection.OFFLINE) && Settings.reconnect)
580         {
581             //FtpConnection con;
582
setCursor(new Cursor JavaDoc(Cursor.DEFAULT_CURSOR));
583
584             h.setVisible(true);
585
586             while(mode == 0)
587             {
588                 pause(10);
589             }
590
591             JFtp.mainFrame.setVisible(false);
592
593             while((response == FtpConnection.OFFLINE) && (mode == 1))
594             {
595                 System.out.print("Server is full, next attempt in ");
596
597                 int r = 5;
598
599                 for(int i = 0; i < r; r--)
600                 {
601                     System.out.print("" + r + "-");
602
603                     try
604                     {
605                         Thread.sleep(1000);
606                     }
607                     catch(Exception JavaDoc ex)
608                     {
609                     }
610                 }
611
612                 System.out.println("0...");
613
614                 /* con = new
615                         FtpConnection(
616                                 htmp,
617                                 Integer.parseInt(potmp),
618                                 dtmp);
619
620                 */

621                 //response = con.login(utmp, ptmp);
622
response = StartConnection.startFtpCon(htmp, utmp, ptmp,
623                                                        Integer.parseInt(potmp),
624                                                        dtmp, useLocal);
625             }
626
627             if(mode == 1)
628             {
629                 JFtp.mainFrame.setVisible(true);
630             }
631             else
632             {
633                 // Switch windows
634
JFtp.mainFrame.setVisible(false);
635                 this.setVisible(true);
636                 this.toFront();
637
638                 return;
639             }
640         }
641         else if((response != FtpConnection.LOGIN_OK) ||
642                     ((response == FtpConnection.OFFLINE) &&
643                     (!Settings.reconnect)))
644         {
645             setCursor(new Cursor JavaDoc(Cursor.DEFAULT_CURSOR));
646
647             //setFilesystemConnection();
648
if(useLocal)
649             {
650                 JFtp.statusP.jftp.closeCurrentLocalTab();
651             }
652             else
653             {
654                 JFtp.statusP.jftp.closeCurrentTab();
655             }
656
657             //this.setVisible(true);
658
//this.toFront();
659
return;
660         }
661     }
662
663     //*** tryFtpAgain
664
}
665
Popular Tags