001: /*
002: * This program is free software; you can redistribute it and/or
003: * modify it under the terms of the GNU General Public License
004: * as published by the Free Software Foundation; either version 2
005: * of the License, or (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011:
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package net.sf.jftp.gui.tasks;
017:
018: import net.sf.jftp.config.*;
019: import net.sf.jftp.gui.base.FtpHost;
020: import net.sf.jftp.gui.framework.*;
021: import net.sf.jftp.system.StringUtils;
022: import net.sf.jftp.util.*;
023:
024: import java.awt.*;
025: import java.awt.event.*;
026:
027: import java.io.File;
028:
029: import javax.swing.*;
030: import javax.swing.event.ListSelectionEvent;
031: import javax.swing.event.ListSelectionListener;
032:
033: public class HostList extends JDialog {
034: private String promptHost = " Host : ";
035: private String promptUser = " User : ";
036: private String promptPass = " Password : ";
037: private String promptName = " Name : ";
038: private String promptPort = " Port : ";
039: private String promptButtonCancel = "Cancel";
040: private String promptButtonOk = " Ok ";
041: private String promptButtonSave = " Apply ";
042: private String promptButtonNew = " New ";
043: private String promptButtonDelete = "Delete";
044: private String promptDialogTitle = " J-FTP Host Selection ";
045:
046: // has an ok, cancel button, and a save
047: // needs to load itself too.
048: private JPanel jpHostInfo;
049: private JTextField jtfHost;
050: private JTextField jtfUser;
051: private JPasswordField jtfPass;
052: private JTextField jtfName;
053: private JTextField jtfPort;
054: private JLabel jlHost;
055: private JLabel jlUser;
056: private JLabel jlPass;
057: private JLabel jlName;
058: private JLabel jlPort;
059: private JSplitPane jsplitpane;
060: private JScrollPane jscrollpane;
061: private JPanel jpbuttons;
062: private JButton jbsave;
063: private JButton jbcancel;
064: private JButton jbok;
065: private JButton jbnew;
066: private JButton jbdelete;
067: private JList hostList;
068: private DefaultListModel hostListModel;
069:
070: /**
071: * The currently selected FtpHost instance
072: */
073: private FtpHost selectedHostInfo = null;
074:
075: /**
076: * Constructs an instance of the HostList with the
077: * given parent and initializes the UI for the host list.
078: * calling getFtpHost() will show the dialog and wait until
079: * the user clicks ok() or cancel().
080: * @param parent The parent JDialog
081: */
082: public HostList(JDialog parent) {
083: super (parent);
084: setTitle(promptDialogTitle);
085: init();
086: setSize(600, 300);
087: }
088:
089: /**
090: * Adds listeners to any components that need them
091: */
092: protected void initListeners() {
093: hostList.addListSelectionListener(new ListSelectionListener() {
094: public void valueChanged(ListSelectionEvent lse) {
095: onSelectHost();
096: }
097: });
098: jbsave.addActionListener(new ActionListener() {
099: public void actionPerformed(ActionEvent ae) {
100: onSave();
101: }
102: });
103: jbok.addActionListener(new ActionListener() {
104: public void actionPerformed(ActionEvent ae) {
105: onOk();
106: }
107: });
108: jbcancel.addActionListener(new ActionListener() {
109: public void actionPerformed(ActionEvent ae) {
110: onCancel();
111: }
112: });
113: jbnew.addActionListener(new ActionListener() {
114: public void actionPerformed(ActionEvent ae) {
115: onNew();
116: }
117: });
118: jbdelete.addActionListener(new ActionListener() {
119: public void actionPerformed(ActionEvent ae) {
120: onDelete();
121: }
122: });
123: }
124:
125: /**
126: * This method makes the dialog popup
127: * and the user must select ok or cancel
128: * upon clicking ok, the selected FtpHost will be returned
129: * upon cancel, a null will be returned.
130: */
131: public FtpHost getFtpHost() {
132: selectedHostInfo = null;
133: setVisible(true);
134:
135: return selectedHostInfo;
136: }
137:
138: /**
139: * overall initialization routine called from the ctor
140: */
141: protected void init() {
142: this .initPrompts();
143: this .initHostInfoPanel();
144: this .initButtonPanel();
145: this .initHostListFrame();
146: this .loadHostList();
147: initListeners();
148:
149: if (hostListModel.size() > 0) {
150: hostList.setSelectedIndex(0);
151: } else {
152: updateHostInfoPanel();
153: }
154:
155: selectedHostInfo = getSelected();
156: setModal(true);
157: }
158:
159: /**
160: * This is where your internationalization can
161: * take hold, you can change the values of the prompt
162: * strings to whatever
163: */
164: protected void initPrompts() {
165: // do nothing
166: }
167:
168: /**
169: * initialize the button panel
170: */
171: protected void initButtonPanel() {
172: jpbuttons = new JPanel();
173: jpbuttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
174: jbcancel = new JButton(promptButtonCancel);
175: jbok = new JButton(promptButtonOk);
176: jbsave = new JButton(promptButtonSave);
177: jbnew = new JButton(promptButtonNew);
178: jbdelete = new JButton(promptButtonDelete);
179: jpbuttons.add(jbsave);
180: jpbuttons.add(jbok);
181: jpbuttons.add(jbcancel);
182: }
183:
184: /**
185: * Build the host info panel
186: */
187: protected void initHostInfoPanel() {
188: jtfHost = new JTextField(20);
189: jtfUser = new JTextField(20);
190: jtfPass = new JPasswordField(20);
191: jtfName = new JTextField(20);
192: jtfPort = new JTextField(20);
193: jlHost = new JLabel(promptHost);
194: jlUser = new JLabel(promptUser);
195: jlPass = new JLabel(promptPass);
196: jlName = new JLabel(promptName);
197: jlPort = new JLabel(promptPort);
198:
199: jpHostInfo = new JPanel();
200:
201: GridBagLayout gbl = new GridBagLayout();
202: jpHostInfo.setLayout(gbl);
203:
204: GridBagConstraints gbc = new GridBagConstraints();
205: gbc.gridx = 0;
206: gbc.gridy = 0;
207: gbc.weightx = 0.0;
208: gbc.weighty = 0.0;
209: gbc.anchor = gbc.NORTHWEST;
210: gbc.fill = gbc.HORIZONTAL;
211:
212: gbl.setConstraints(jlName, gbc);
213:
214: gbc.gridy = 1;
215: gbl.setConstraints(jlHost, gbc);
216:
217: gbc.gridy = 2;
218: gbl.setConstraints(jlUser, gbc);
219:
220: gbc.gridy = 3;
221: gbl.setConstraints(jlPass, gbc);
222:
223: gbc.gridy = 4;
224: gbl.setConstraints(jlPort, gbc);
225:
226: gbc.gridy = 0;
227: gbc.gridx = 1;
228: gbc.weightx = 1.0;
229: gbl.setConstraints(jtfName, gbc);
230:
231: gbc.gridy = 1;
232: gbl.setConstraints(jtfHost, gbc);
233:
234: gbc.gridy = 2;
235: gbl.setConstraints(jtfUser, gbc);
236:
237: gbc.gridy = 3;
238: gbl.setConstraints(jtfPass, gbc);
239:
240: gbc.gridy = 4;
241: gbl.setConstraints(jtfPort, gbc);
242:
243: jpHostInfo.add(jlName);
244: jpHostInfo.add(jlHost);
245: jpHostInfo.add(jlUser);
246: jpHostInfo.add(jlPass);
247: jpHostInfo.add(jlPort);
248: jpHostInfo.add(jtfName);
249: jpHostInfo.add(jtfHost);
250: jpHostInfo.add(jtfUser);
251: jpHostInfo.add(jtfPass);
252: jpHostInfo.add(jtfPort);
253: }
254:
255: /**
256: * Initializes the overall dialog/frame
257: */
258: protected void initHostListFrame() {
259: hostListModel = new DefaultListModel();
260: hostList = new JList(hostListModel);
261: jscrollpane = new JScrollPane(hostList);
262:
263: JPanel jptempleft = new JPanel(new BorderLayout());
264: jptempleft.add(jscrollpane, BorderLayout.CENTER);
265:
266: JPanel jptempbutt = new JPanel(new FlowLayout());
267: jptempbutt.add(jbnew);
268: jptempbutt.add(jbdelete);
269: jptempleft.add(jptempbutt, BorderLayout.SOUTH);
270:
271: JPanel jptemp = new JPanel(new BorderLayout());
272: jptemp.add(jpbuttons, BorderLayout.SOUTH);
273: jptemp.add(jpHostInfo, BorderLayout.CENTER);
274:
275: jsplitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
276: jptempleft, jptemp);
277: getContentPane().add(jsplitpane);
278: }
279:
280: /**
281: * Loads the host list from the hard drive
282: */
283: protected void loadHostList() {
284: //Log.out("x");
285: // current host number
286: int i = 0;
287:
288: while (i >= 0) {
289: String filename = Settings.login.concat(String.valueOf(i));
290: String[] host_info = LoadSet.loadSet(filename);
291:
292: if ((host_info == null) || (host_info.length == 1)) {
293: // no file was loaded, break out.
294: i = -1;
295:
296: continue;
297: }
298:
299: FtpHost ftpHost = new FtpHost();
300:
301: try {
302: ftpHost.hostname = host_info[0];
303: ftpHost.username = host_info[1];
304: ftpHost.password = host_info[2];
305: ftpHost.name = host_info[3];
306: ftpHost.port = host_info[4];
307: } catch (ArrayIndexOutOfBoundsException aioobe) {
308: // do nothing, this can happen
309: }
310:
311: hostListModel.addElement(ftpHost);
312: i++;
313: }
314: }
315:
316: public void onSelectHost() {
317: // update the old one, then show the new one
318: updateHostInfoObject();
319: selectedHostInfo = getSelected();
320: updateHostInfoPanel();
321: }
322:
323: /**
324: * Delete button handler
325: */
326: public void onDelete() {
327: Object selected = hostList.getSelectedValue();
328: hostListModel.removeElement(selected);
329: selectedHostInfo = null;
330:
331: if (hostListModel.size() > 0) {
332: hostList.setSelectedIndex(0);
333: } else {
334: updateHostInfoPanel();
335: }
336:
337: onSave();
338: hostList.repaint();
339: }
340:
341: /**
342: * Save button handler
343: */
344: public void onSave() {
345: updateHostInfoObject();
346:
347: // remove all previously saved hosts
348: int i = 0;
349:
350: while (true) {
351: File f = new File(Settings.login.concat(String.valueOf(i)));
352:
353: if (f.exists()) {
354: f.delete();
355: i++;
356: } else {
357: break;
358: }
359: }
360:
361: int len = hostListModel.size();
362:
363: for (i = 0; i < len; i++) {
364: FtpHost ftphost = (FtpHost) hostListModel.elementAt(i);
365: String htmp = StringUtils.cut(ftphost.hostname, " ");
366: String utmp = StringUtils.cut(ftphost.username, " ");
367: String ptmp = StringUtils.cut(ftphost.password, " ");
368: String ntmp = StringUtils.cut(ftphost.name, " ");
369: String ttmp = StringUtils.cut(ftphost.port, " ");
370: SaveSet s = new SaveSet(Settings.login.concat(String
371: .valueOf(i)), htmp, utmp, ptmp, ntmp, ttmp);
372: }
373:
374: hostList.repaint();
375: }
376:
377: /**
378: * OK Button handler
379: */
380: public void onOk() {
381: selectedHostInfo = getSelected();
382: onSave();
383: dispose();
384: }
385:
386: /**
387: * Cancel button handler
388: */
389: public void onCancel() {
390: selectedHostInfo = null;
391: dispose();
392: }
393:
394: /**
395: * Create a default one and stuff itin the list
396: */
397: public void onNew() {
398: FtpHost ftpHost = new FtpHost();
399: ftpHost.name = "undefined";
400: ftpHost.username = "undefined";
401: ftpHost.hostname = "undefined";
402: ftpHost.password = "undefined";
403: ftpHost.port = "21";
404: hostListModel.addElement(ftpHost);
405: hostList.setSelectedValue(ftpHost, true);
406: selectedHostInfo = ftpHost;
407: }
408:
409: /**
410: * Returns the selected FtpHost from the hostList
411: */
412: private FtpHost getSelected() {
413: int sel = hostList.getSelectedIndex();
414:
415: if ((sel < 0) || (sel > (hostListModel.size() - 1))) {
416: return null;
417: } else {
418: return (FtpHost) hostListModel.elementAt(hostList
419: .getSelectedIndex());
420: }
421: }
422:
423: /**
424: * Updates the screen to reflect the values from the currently
425: * selected FtpHost object. If none is selected, then
426: * it clears the panel
427: */
428: private void updateHostInfoPanel() {
429: if (selectedHostInfo == null) {
430: jtfName.setText("");
431: jtfUser.setText("");
432: jtfPass.setText("");
433: jtfHost.setText("");
434: jtfPort.setText("");
435: jtfName.setEnabled(false);
436: jtfUser.setEnabled(false);
437: jtfHost.setEnabled(false);
438: jtfPass.setEnabled(false);
439: jtfPort.setEnabled(false);
440: } else {
441: jtfName.setEnabled(true);
442: jtfUser.setEnabled(true);
443: jtfHost.setEnabled(true);
444: jtfPass.setEnabled(true);
445: jtfPort.setEnabled(true);
446: jtfName.setText(selectedHostInfo.name);
447: jtfUser.setText(selectedHostInfo.username);
448: jtfPass.setText(selectedHostInfo.password);
449: jtfHost.setText(selectedHostInfo.hostname);
450: jtfPort.setText(selectedHostInfo.port);
451: }
452: }
453:
454: /**
455: * Updates the currently selected FtpHost object called
456: * "selectedHostInfo" from the contents of the screen
457: */
458: private void updateHostInfoObject() {
459: if (selectedHostInfo == null) {
460: return;
461: }
462:
463: selectedHostInfo.hostname = jtfHost.getText();
464: selectedHostInfo.name = jtfName.getText();
465: selectedHostInfo.username = jtfUser.getText();
466: selectedHostInfo.password = new String(jtfPass.getPassword());
467: selectedHostInfo.port = jtfPort.getText();
468: }
469: }
|