KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > gui > tasks > HostList


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.tasks;
17
18 import net.sf.jftp.config.*;
19 import net.sf.jftp.gui.base.FtpHost;
20 import net.sf.jftp.gui.framework.*;
21 import net.sf.jftp.system.StringUtils;
22 import net.sf.jftp.util.*;
23
24 import java.awt.*;
25 import java.awt.event.*;
26
27 import java.io.File JavaDoc;
28
29 import javax.swing.*;
30 import javax.swing.event.ListSelectionEvent JavaDoc;
31 import javax.swing.event.ListSelectionListener JavaDoc;
32
33
34 public class HostList extends JDialog
35 {
36     private String JavaDoc promptHost = " Host : ";
37     private String JavaDoc promptUser = " User : ";
38     private String JavaDoc promptPass = " Password : ";
39     private String JavaDoc promptName = " Name : ";
40     private String JavaDoc promptPort = " Port : ";
41     private String JavaDoc promptButtonCancel = "Cancel";
42     private String JavaDoc promptButtonOk = " Ok ";
43     private String JavaDoc promptButtonSave = " Apply ";
44     private String JavaDoc promptButtonNew = " New ";
45     private String JavaDoc promptButtonDelete = "Delete";
46     private String JavaDoc promptDialogTitle = " J-FTP Host Selection ";
47
48     // has an ok, cancel button, and a save
49
// needs to load itself too.
50
private JPanel jpHostInfo;
51     private JTextField jtfHost;
52     private JTextField jtfUser;
53     private JPasswordField jtfPass;
54     private JTextField jtfName;
55     private JTextField jtfPort;
56     private JLabel jlHost;
57     private JLabel jlUser;
58     private JLabel jlPass;
59     private JLabel jlName;
60     private JLabel jlPort;
61     private JSplitPane jsplitpane;
62     private JScrollPane jscrollpane;
63     private JPanel jpbuttons;
64     private JButton jbsave;
65     private JButton jbcancel;
66     private JButton jbok;
67     private JButton jbnew;
68     private JButton jbdelete;
69     private JList hostList;
70     private DefaultListModel hostListModel;
71
72     /**
73      * The currently selected FtpHost instance
74      */

75     private FtpHost selectedHostInfo = null;
76
77     /**
78      * Constructs an instance of the HostList with the
79      * given parent and initializes the UI for the host list.
80      * calling getFtpHost() will show the dialog and wait until
81      * the user clicks ok() or cancel().
82      * @param parent The parent JDialog
83      */

84     public HostList(JDialog parent)
85     {
86         super(parent);
87         setTitle(promptDialogTitle);
88         init();
89         setSize(600, 300);
90     }
91
92     /**
93      * Adds listeners to any components that need them
94      */

95     protected void initListeners()
96     {
97         hostList.addListSelectionListener(new ListSelectionListener JavaDoc()
98             {
99                 public void valueChanged(ListSelectionEvent JavaDoc lse)
100                 {
101                     onSelectHost();
102                 }
103             });
104         jbsave.addActionListener(new ActionListener()
105             {
106                 public void actionPerformed(ActionEvent ae)
107                 {
108                     onSave();
109                 }
110             });
111         jbok.addActionListener(new ActionListener()
112             {
113                 public void actionPerformed(ActionEvent ae)
114                 {
115                     onOk();
116                 }
117             });
118         jbcancel.addActionListener(new ActionListener()
119             {
120                 public void actionPerformed(ActionEvent ae)
121                 {
122                     onCancel();
123                 }
124             });
125         jbnew.addActionListener(new ActionListener()
126             {
127                 public void actionPerformed(ActionEvent ae)
128                 {
129                     onNew();
130                 }
131             });
132         jbdelete.addActionListener(new ActionListener()
133             {
134                 public void actionPerformed(ActionEvent ae)
135                 {
136                     onDelete();
137                 }
138             });
139     }
140
141     /**
142      * This method makes the dialog popup
143      * and the user must select ok or cancel
144      * upon clicking ok, the selected FtpHost will be returned
145      * upon cancel, a null will be returned.
146      */

147     public FtpHost getFtpHost()
148     {
149         selectedHostInfo = null;
150         setVisible(true);
151
152         return selectedHostInfo;
153     }
154
155     /**
156      * overall initialization routine called from the ctor
157      */

158     protected void init()
159     {
160         this.initPrompts();
161         this.initHostInfoPanel();
162         this.initButtonPanel();
163         this.initHostListFrame();
164         this.loadHostList();
165         initListeners();
166
167         if(hostListModel.size() > 0)
168         {
169             hostList.setSelectedIndex(0);
170         }
171         else
172         {
173             updateHostInfoPanel();
174         }
175
176         selectedHostInfo = getSelected();
177         setModal(true);
178     }
179
180     /**
181      * This is where your internationalization can
182      * take hold, you can change the values of the prompt
183      * strings to whatever
184      */

185     protected void initPrompts()
186     {
187         // do nothing
188
}
189
190     /**
191      * initialize the button panel
192      */

193     protected void initButtonPanel()
194     {
195         jpbuttons = new JPanel();
196         jpbuttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
197         jbcancel = new JButton(promptButtonCancel);
198         jbok = new JButton(promptButtonOk);
199         jbsave = new JButton(promptButtonSave);
200         jbnew = new JButton(promptButtonNew);
201         jbdelete = new JButton(promptButtonDelete);
202         jpbuttons.add(jbsave);
203         jpbuttons.add(jbok);
204         jpbuttons.add(jbcancel);
205     }
206
207     /**
208      * Build the host info panel
209      */

210     protected void initHostInfoPanel()
211     {
212         jtfHost = new JTextField(20);
213         jtfUser = new JTextField(20);
214         jtfPass = new JPasswordField(20);
215         jtfName = new JTextField(20);
216         jtfPort = new JTextField(20);
217         jlHost = new JLabel(promptHost);
218         jlUser = new JLabel(promptUser);
219         jlPass = new JLabel(promptPass);
220         jlName = new JLabel(promptName);
221         jlPort = new JLabel(promptPort);
222
223         jpHostInfo = new JPanel();
224
225         GridBagLayout gbl = new GridBagLayout();
226         jpHostInfo.setLayout(gbl);
227
228         GridBagConstraints gbc = new GridBagConstraints();
229         gbc.gridx = 0;
230         gbc.gridy = 0;
231         gbc.weightx = 0.0;
232         gbc.weighty = 0.0;
233         gbc.anchor = gbc.NORTHWEST;
234         gbc.fill = gbc.HORIZONTAL;
235
236         gbl.setConstraints(jlName, gbc);
237
238         gbc.gridy = 1;
239         gbl.setConstraints(jlHost, gbc);
240
241         gbc.gridy = 2;
242         gbl.setConstraints(jlUser, gbc);
243
244         gbc.gridy = 3;
245         gbl.setConstraints(jlPass, gbc);
246
247         gbc.gridy = 4;
248         gbl.setConstraints(jlPort, gbc);
249
250         gbc.gridy = 0;
251         gbc.gridx = 1;
252         gbc.weightx = 1.0;
253         gbl.setConstraints(jtfName, gbc);
254
255         gbc.gridy = 1;
256         gbl.setConstraints(jtfHost, gbc);
257
258         gbc.gridy = 2;
259         gbl.setConstraints(jtfUser, gbc);
260
261         gbc.gridy = 3;
262         gbl.setConstraints(jtfPass, gbc);
263
264         gbc.gridy = 4;
265         gbl.setConstraints(jtfPort, gbc);
266
267         jpHostInfo.add(jlName);
268         jpHostInfo.add(jlHost);
269         jpHostInfo.add(jlUser);
270         jpHostInfo.add(jlPass);
271         jpHostInfo.add(jlPort);
272         jpHostInfo.add(jtfName);
273         jpHostInfo.add(jtfHost);
274         jpHostInfo.add(jtfUser);
275         jpHostInfo.add(jtfPass);
276         jpHostInfo.add(jtfPort);
277     }
278
279     /**
280      * Initializes the overall dialog/frame
281      */

282     protected void initHostListFrame()
283     {
284         hostListModel = new DefaultListModel();
285         hostList = new JList(hostListModel);
286         jscrollpane = new JScrollPane(hostList);
287
288         JPanel jptempleft = new JPanel(new BorderLayout());
289         jptempleft.add(jscrollpane, BorderLayout.CENTER);
290
291         JPanel jptempbutt = new JPanel(new FlowLayout());
292         jptempbutt.add(jbnew);
293         jptempbutt.add(jbdelete);
294         jptempleft.add(jptempbutt, BorderLayout.SOUTH);
295
296         JPanel jptemp = new JPanel(new BorderLayout());
297         jptemp.add(jpbuttons, BorderLayout.SOUTH);
298         jptemp.add(jpHostInfo, BorderLayout.CENTER);
299
300         jsplitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jptempleft,
301                                     jptemp);
302         getContentPane().add(jsplitpane);
303     }
304
305     /**
306      * Loads the host list from the hard drive
307      */

308     protected void loadHostList()
309     {
310         //Log.out("x");
311
// current host number
312
int i = 0;
313
314         while(i >= 0)
315         {
316             String JavaDoc filename = Settings.login.concat(String.valueOf(i));
317             String JavaDoc[] host_info = LoadSet.loadSet(filename);
318
319             if((host_info == null) || (host_info.length == 1))
320             {
321                 // no file was loaded, break out.
322
i = -1;
323
324                 continue;
325             }
326
327             FtpHost ftpHost = new FtpHost();
328
329             try
330             {
331                 ftpHost.hostname = host_info[0];
332                 ftpHost.username = host_info[1];
333                 ftpHost.password = host_info[2];
334                 ftpHost.name = host_info[3];
335                 ftpHost.port = host_info[4];
336             }
337             catch(ArrayIndexOutOfBoundsException JavaDoc aioobe)
338             {
339                 // do nothing, this can happen
340
}
341
342             hostListModel.addElement(ftpHost);
343             i++;
344         }
345     }
346
347     public void onSelectHost()
348     {
349         // update the old one, then show the new one
350
updateHostInfoObject();
351         selectedHostInfo = getSelected();
352         updateHostInfoPanel();
353     }
354
355     /**
356      * Delete button handler
357      */

358     public void onDelete()
359     {
360         Object JavaDoc selected = hostList.getSelectedValue();
361         hostListModel.removeElement(selected);
362         selectedHostInfo = null;
363
364         if(hostListModel.size() > 0)
365         {
366             hostList.setSelectedIndex(0);
367         }
368         else
369         {
370             updateHostInfoPanel();
371         }
372
373         onSave();
374         hostList.repaint();
375     }
376
377     /**
378      * Save button handler
379      */

380     public void onSave()
381     {
382         updateHostInfoObject();
383
384         // remove all previously saved hosts
385
int i = 0;
386
387         while(true)
388         {
389             File JavaDoc f = new File JavaDoc(Settings.login.concat(String.valueOf(i)));
390
391             if(f.exists())
392             {
393                 f.delete();
394                 i++;
395             }
396             else
397             {
398                 break;
399             }
400         }
401
402         int len = hostListModel.size();
403
404         for(i = 0; i < len; i++)
405         {
406             FtpHost ftphost = (FtpHost) hostListModel.elementAt(i);
407             String JavaDoc htmp = StringUtils.cut(ftphost.hostname, " ");
408             String JavaDoc utmp = StringUtils.cut(ftphost.username, " ");
409             String JavaDoc ptmp = StringUtils.cut(ftphost.password, " ");
410             String JavaDoc ntmp = StringUtils.cut(ftphost.name, " ");
411             String JavaDoc ttmp = StringUtils.cut(ftphost.port, " ");
412             SaveSet s = new SaveSet(Settings.login.concat(String.valueOf(i)),
413                                     htmp, utmp, ptmp, ntmp, ttmp);
414         }
415
416         hostList.repaint();
417     }
418
419     /**
420       * OK Button handler
421       */

422     public void onOk()
423     {
424         selectedHostInfo = getSelected();
425         onSave();
426         dispose();
427     }
428
429     /**
430      * Cancel button handler
431      */

432     public void onCancel()
433     {
434         selectedHostInfo = null;
435         dispose();
436     }
437
438     /**
439      * Create a default one and stuff itin the list
440      */

441     public void onNew()
442     {
443         FtpHost ftpHost = new FtpHost();
444         ftpHost.name = "undefined";
445         ftpHost.username = "undefined";
446         ftpHost.hostname = "undefined";
447         ftpHost.password = "undefined";
448         ftpHost.port = "21";
449         hostListModel.addElement(ftpHost);
450         hostList.setSelectedValue(ftpHost, true);
451         selectedHostInfo = ftpHost;
452     }
453
454     /**
455      * Returns the selected FtpHost from the hostList
456      */

457     private FtpHost getSelected()
458     {
459         int sel = hostList.getSelectedIndex();
460
461         if((sel < 0) || (sel > (hostListModel.size() - 1)))
462         {
463             return null;
464         }
465         else
466         {
467             return (FtpHost) hostListModel.elementAt(hostList.getSelectedIndex());
468         }
469     }
470
471     /**
472      * Updates the screen to reflect the values from the currently
473      * selected FtpHost object. If none is selected, then
474      * it clears the panel
475      */

476     private void updateHostInfoPanel()
477     {
478         if(selectedHostInfo == null)
479         {
480             jtfName.setText("");
481             jtfUser.setText("");
482             jtfPass.setText("");
483             jtfHost.setText("");
484             jtfPort.setText("");
485             jtfName.setEnabled(false);
486             jtfUser.setEnabled(false);
487             jtfHost.setEnabled(false);
488             jtfPass.setEnabled(false);
489             jtfPort.setEnabled(false);
490         }
491         else
492         {
493             jtfName.setEnabled(true);
494             jtfUser.setEnabled(true);
495             jtfHost.setEnabled(true);
496             jtfPass.setEnabled(true);
497             jtfPort.setEnabled(true);
498             jtfName.setText(selectedHostInfo.name);
499             jtfUser.setText(selectedHostInfo.username);
500             jtfPass.setText(selectedHostInfo.password);
501             jtfHost.setText(selectedHostInfo.hostname);
502             jtfPort.setText(selectedHostInfo.port);
503         }
504     }
505
506     /**
507      * Updates the currently selected FtpHost object called
508      * "selectedHostInfo" from the contents of the screen
509      */

510     private void updateHostInfoObject()
511     {
512         if(selectedHostInfo == null)
513         {
514             return;
515         }
516
517         selectedHostInfo.hostname = jtfHost.getText();
518         selectedHostInfo.name = jtfName.getText();
519         selectedHostInfo.username = jtfUser.getText();
520         selectedHostInfo.password = new String JavaDoc(jtfPass.getPassword());
521         selectedHostInfo.port = jtfPort.getText();
522     }
523 }
524
Popular Tags