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.hostchooser;
017:
018: import java.awt.BorderLayout;
019: import java.awt.Cursor;
020: import java.awt.FlowLayout;
021: import java.awt.Font;
022: import java.awt.GridLayout;
023: import java.awt.Insets;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.awt.event.ComponentEvent;
027: import java.awt.event.ComponentListener;
028: import java.awt.event.WindowEvent;
029: import java.awt.event.WindowListener;
030: import java.io.IOException;
031:
032: import javax.swing.JCheckBox;
033: import javax.swing.JDialog;
034: import javax.swing.JLabel;
035: import javax.swing.JPanel;
036: import javax.swing.JTextArea;
037:
038: import net.sf.jftp.JFtp;
039: import net.sf.jftp.config.LoadSet;
040: import net.sf.jftp.config.SaveSet;
041: import net.sf.jftp.config.Settings;
042: import net.sf.jftp.gui.base.FtpHost;
043: import net.sf.jftp.gui.framework.HButton;
044: import net.sf.jftp.gui.framework.HFrame;
045: import net.sf.jftp.gui.framework.HPanel;
046: import net.sf.jftp.gui.framework.HPasswordField;
047: import net.sf.jftp.gui.framework.HTextField;
048: import net.sf.jftp.gui.tasks.HostList;
049: import net.sf.jftp.net.FilesystemConnection;
050: import net.sf.jftp.net.FtpConnection;
051: import net.sf.jftp.net.FtpURLConnection;
052: import net.sf.jftp.net.StartConnection;
053: import net.sf.jftp.system.StringUtils;
054: import net.sf.jftp.system.logging.Log;
055:
056: public class HostChooser extends HFrame implements ActionListener,
057: WindowListener {
058: public HTextField host = new HTextField("Hostname:",
059: "localhost ");
060: public HTextField user = new HTextField("Username:",
061: "anonymous ");
062:
063: //public static HTextField pass = new HTextField("Password:","none@nowhere.no");
064: public HPasswordField pass = new HPasswordField("Password:",
065: "none@nowhere.no");
066: public HTextField port = new HTextField("Port: ", "21");
067: public HTextField cwd = new HTextField("Remote: ",
068: Settings.defaultDir);
069: public HTextField lcwd = new HTextField("Local: ",
070: Settings.defaultWorkDir);
071: public HTextField dl = new HTextField("Max. connections: ", "3");
072: public HTextField crlf = new HTextField(
073: "Override server newline: ", "<default>");
074: private JCheckBox anonBox = new JCheckBox("Use anonymous login",
075: false);
076: private JCheckBox listBox = new JCheckBox(
077: "LIST compatibility mode", false);
078: private JCheckBox dirBox = new JCheckBox("Use default directories",
079: Settings.getUseDefaultDir());
080: private JCheckBox modeBox = new JCheckBox(
081: "Use active Ftp (no need to)", false);
082: private JCheckBox threadBox = new JCheckBox("Multiple connections",
083: false);
084: private HPanel okP = new HPanel();
085: private HButton ok = new HButton("Connect");
086: private HButton backMode = new HButton("Yes");
087: private HButton frontMode = new HButton("No");
088: private HFrame h = new HFrame();
089: private HPanel listP = new HPanel();
090: private HButton list = new HButton("Choose from or edit list...");
091: private ComponentListener listener = null;
092: private int mode = 0;
093: private boolean useLocal = false;
094: private boolean ext = Settings.showNewlineOption;
095:
096: public HostChooser(ComponentListener l, boolean local) {
097: listener = l;
098: useLocal = local;
099: //TODO ext -> newlineoption
100: //this.ext = ext;
101: init();
102: }
103:
104: public HostChooser(ComponentListener l) {
105: listener = l;
106: init();
107: }
108:
109: public HostChooser() {
110: init();
111: }
112:
113: public void init() {
114: //setSize(600, 340);
115: // setLocation(50, 150);
116: setTitle("Ftp Connection...");
117: setBackground(okP.getBackground());
118: getContentPane().setLayout(
119: new GridLayout((ext ? 8 : 7), 2, 5, 3));
120:
121: anonBox.setSelected(false);
122: user.setEnabled(true);
123: pass.text.setEnabled(true);
124:
125: LoadSet l = new LoadSet();
126: String[] login = l.loadSet(Settings.login_def);
127:
128: if ((login != null) && (login[0] != null)) {
129: host.setText(login[0]);
130: user.setText(login[1]);
131:
132: if (login[3] != null) {
133: port.setText(login[3]);
134: }
135:
136: if (login[4] != null) {
137: cwd.setText(login[4]);
138: }
139:
140: if (login[5] != null) {
141: lcwd.setText(login[5]);
142: }
143: }
144:
145: if (Settings.getStorePasswords()) {
146: if (login != null) {
147: pass.setText(login[2]);
148: }
149: } else {
150: pass.setText("");
151: }
152:
153: getContentPane().add(host);
154: getContentPane().add(port);
155: getContentPane().add(anonBox);
156: getContentPane().add(listBox);
157: getContentPane().add(user);
158: getContentPane().add(pass);
159: getContentPane().add(dirBox);
160: getContentPane().add(modeBox);
161: getContentPane().add(lcwd);
162: getContentPane().add(cwd);
163: getContentPane().add(threadBox);
164: getContentPane().add(dl);
165:
166: if (ext) {
167: getContentPane().add(crlf);
168:
169: JPanel x1 = new JPanel();
170: x1.setLayout(new BorderLayout(2, 2));
171: JLabel l1 = new JLabel("Unix: LF, Mac/MVS: CR, Win: CRLF");
172: l1.setFont(new Font("Dialog", Font.PLAIN, 10));
173: JLabel l2 = new JLabel(
174: "Don't change this unless you transfer text only");
175: l2.setFont(new Font("Dialog", Font.PLAIN, 10));
176: x1.add("North", l1);
177: x1.add("South", l2);
178: getContentPane().add(x1);
179: }
180:
181: modeBox.setSelected(!Settings.getFtpPasvMode());
182: threadBox.setSelected(Settings.getEnableMultiThreading());
183: dirBox.setSelected(Settings.getUseDefaultDir());
184: anonBox.addActionListener(this );
185: threadBox.addActionListener(this );
186:
187: getContentPane().add(okP);
188: okP.add(ok);
189: ok.addActionListener(this );
190:
191: getContentPane().add(listP);
192: listP.add(list);
193: list.addActionListener(this );
194:
195: dirBox.addActionListener(this );
196: setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
197:
198: lcwd.setEnabled(!dirBox.isSelected());
199: cwd.setEnabled(!dirBox.isSelected());
200: pass.text.addActionListener(this );
201:
202: pack();
203: setModal(false);
204: setVisible(false);
205: invalidate();
206: validate();
207: addWindowListener(this );
208: prepareBackgroundMessage();
209: }
210:
211: public void update() {
212: fixLocation();
213: setVisible(true);
214: toFront();
215: host.requestFocus();
216: }
217:
218: public void update(String url) {
219: try {
220: FtpURLConnection uc = new FtpURLConnection(
221: new java.net.URL(url));
222: FtpConnection con = uc.getFtpConnection();
223:
224: JFtp.statusP.jftp.addConnection(url, con);
225:
226: //con.addConnectionListener((ConnectionListener) JFtp.localDir);
227: //con.addConnectionListener((ConnectionListener) JFtp.remoteDir);
228: //JFtp.remoteDir.setCon(con);
229: uc.connect();
230:
231: int response = uc.getLoginResponse();
232:
233: if (response != FtpConnection.LOGIN_OK) {
234: setTitle("Wrong password!");
235: host.setText(uc.getHost());
236: port.setText(Integer.toString(uc.getPort()));
237: user.setText(uc.getUser());
238: pass.setText(uc.getPass());
239: setVisible(true);
240: toFront();
241: host.requestFocus();
242: } else {
243: this .dispose();
244:
245: if (listener != null) {
246: listener.componentResized(new ComponentEvent(this ,
247: 0));
248: }
249:
250: JFtp.mainFrame.setVisible(true);
251: JFtp.mainFrame.toFront();
252: }
253: }
254:
255: //catch(MalformedURLException ex)
256: //{
257: // Log.debug("Malformed URL!");
258: // ex.printStackTrace();
259: //}
260: catch (IOException ex) {
261: Log.debug("Error!");
262: ex.printStackTrace();
263: }
264: }
265:
266: public void actionPerformed(ActionEvent e) {
267: if ((e.getSource() == ok) || (e.getSource() == pass.text)) {
268: // Switch windows
269: //this.setVisible(false);
270: setCursor(new Cursor(Cursor.WAIT_CURSOR));
271:
272: //JFtp.mainFrame.setVisible(true);
273: //JFtp.mainFrame.toFront();
274: FtpConnection con = null;
275: JFtp.setHost(host.getText());
276:
277: String htmp = StringUtils.cut(host.getText(), " ");
278: String utmp = StringUtils.cut(user.getText(), " ");
279: String ptmp = StringUtils.cut(pass.getText(), " ");
280: String potmp = StringUtils.cut(port.getText(), " ");
281:
282: Settings.setProperty("jftp.ftpPasvMode", !modeBox
283: .isSelected());
284: Settings.setProperty("jftp.enableMultiThreading", threadBox
285: .isSelected());
286: Settings.setProperty("jftp.useDefaultDir", dirBox
287: .isSelected());
288:
289: if (listBox.isSelected()) {
290: FtpConnection.LIST = "LIST";
291: } else {
292: FtpConnection.LIST = "LIST -laL";
293: }
294:
295: /* All the information of the current server are stored in JFtp.HostInfo */
296: JFtp.hostinfo.hostname = htmp;
297: JFtp.hostinfo.username = utmp;
298: JFtp.hostinfo.password = ptmp;
299: JFtp.hostinfo.port = potmp;
300: JFtp.hostinfo.type = "ftp";
301:
302: boolean pasv = Settings.getFtpPasvMode();
303: boolean threads = Settings.getEnableMultiThreading();
304:
305: /*
306: if(!pasv && threads) // only passive ftp threading works
307: {
308: //Settings.setProperty("jftp.enableMultiThreading", false);
309: JDialog j = new JDialog();
310: j.setTitle("Warning");
311: j.setLocation(150, 150);
312: j.setSize(450, 100);
313: j.getContentPane().add(new JLabel("Multithreading in active mode is EXPERIMENTAL"));
314: j.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
315: j.setModal(true);
316: j.setVisible(true);
317: }*/
318:
319: int x = Integer.parseInt(dl.getText().trim());
320: Settings.maxConnections = x;
321:
322: Settings.save();
323:
324: //*** My modifications are below here
325: String dtmp;
326: String ltmp;
327:
328: if (dirBox.isSelected()) {
329: dtmp = Settings.defaultDir;
330: ltmp = Settings.defaultWorkDir;
331: } else {
332: dtmp = cwd.getText();
333: ltmp = lcwd.getText();
334: }
335:
336: //*** More of my stuff
337: SaveSet s = new SaveSet(Settings.login_def, htmp, utmp,
338: ptmp, potmp, dtmp, ltmp);
339:
340: if (JFtp.localDir instanceof FilesystemConnection) {
341: if (!JFtp.localDir.setPath(ltmp)) {
342: if (!JFtp.localDir.setPath(System
343: .getProperty("user.home"))) {
344: JFtp.localDir.setPath("/");
345: }
346: }
347: }
348:
349: int response = StartConnection.startFtpCon(htmp, utmp,
350: ptmp, Integer.parseInt(potmp), dtmp, useLocal, crlf
351: .getText().trim());
352:
353: /*
354:
355: con = new FtpConnection(htmp,
356: Integer.parseInt(potmp), dtmp);
357:
358:
359: //con.addConnectionListener((ConnectionListener) JFtp.localDir);
360: //con.addConnectionListener((ConnectionListener) JFtp.remoteDir);
361: //JFtp.remoteDir.setCon(con);
362:
363: if (useLocal)
364: JFtp.statusP.jftp.addLocalConnection(
365: htmp, con);
366: else
367: JFtp.statusP.jftp.addConnection(htmp, con);
368:
369: int response = con.login(utmp, ptmp);
370:
371: */
372: //*** START OF STUFF TO PUT INTO SEPARATE METHOD
373: /*
374: if (response!=FtpConnection.LOGIN_OK)
375: tryFtpAgain(response, htmp, ptmp, utmp, potmp,
376: dtmp, useLocal);
377:
378: */
379: //*** END OF STUFF TO PUT INTO SEPARATE METHOD
380: //else
381: //{
382: setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
383: this .dispose();
384:
385: //this.setVisible(false);
386: JFtp.mainFrame.setVisible(true);
387: JFtp.mainFrame.toFront();
388:
389: if (listener != null) {
390: listener.componentResized(new ComponentEvent(this , 0));
391: }
392:
393: //}
394: } else if (e.getSource() == list) {
395: HostList hl = new HostList(this );
396: FtpHost selectedHost = hl.getFtpHost();
397:
398: if (selectedHost == null) {
399: return;
400: }
401:
402: host.setText(selectedHost.hostname);
403: pass.setText(selectedHost.password);
404: user.setText(selectedHost.username);
405: port.setText(selectedHost.port);
406:
407: Settings.setProperty("jftp.useDefaultDir", true);
408: dirBox.setSelected(Settings.getUseDefaultDir());
409: lcwd.setEnabled(!dirBox.isSelected());
410: cwd.setEnabled(!dirBox.isSelected());
411: } else if (e.getSource() == dirBox) {
412: if (!dirBox.isSelected()) {
413: lcwd.setEnabled(true);
414: cwd.setEnabled(true);
415: } else {
416: lcwd.setEnabled(false);
417: cwd.setEnabled(false);
418: }
419: } else if (e.getSource() == anonBox) {
420: if (!anonBox.isSelected()) {
421: user.setEnabled(true);
422: pass.text.setEnabled(true);
423: } else {
424: user.setText("anonymous");
425: pass.setText("no@no.no");
426: user.setEnabled(false);
427: pass.text.setEnabled(false);
428: }
429: } else if (e.getSource() == threadBox) {
430: if (threadBox.isSelected()) {
431: dl.setEnabled(true);
432: } else {
433: dl.setEnabled(false);
434: }
435: } else if (e.getSource() == backMode) {
436: mode = 1;
437: h.setVisible(false);
438: ;
439: } else if (e.getSource() == frontMode) {
440: mode = 2;
441: h.setVisible(false);
442: }
443: }
444:
445: /*
446: private void setFilesystemConnection()
447: {
448: FilesystemConnection c = new FilesystemConnection();
449: c.addConnectionListener(((ConnectionListener)JFtp.remoteDir));
450: if(!c.chdir("/")) c.chdir("C:\\");
451: JFtp.remoteDir.setCon(c);
452: }
453: */
454: private void prepareBackgroundMessage() {
455: HPanel p = new HPanel();
456: p.add(backMode);
457: p.add(frontMode);
458: p.setLayout(new FlowLayout(FlowLayout.CENTER));
459:
460: backMode.addActionListener(this );
461: frontMode.addActionListener(this );
462:
463: h.getContentPane().setLayout(new BorderLayout(10, 10));
464: h.setTitle("Connection failed!");
465: h.setLocation(150, 200);
466:
467: JTextArea text = new JTextArea();
468: h.getContentPane().add("Center", text);
469: h.getContentPane().add("South", p);
470: text
471: .setText(" ---------------- Output -----------------\n\n"
472: + "The server is busy at the moment.\n\n"
473: + "Do you want JFtp to go to disappear and try to login\n"
474: + "continuously?\n\n"
475: + "(It will show up again when it has initiated a connection)\n\n");
476: JFtp.log.setText("");
477: text.setEditable(false);
478: h.pack();
479: }
480:
481: public void windowClosing(WindowEvent e) {
482: //System.exit(0);
483: this .dispose();
484: }
485:
486: public void windowClosed(WindowEvent e) {
487: }
488:
489: public void windowActivated(WindowEvent e) {
490: }
491:
492: public void windowDeactivated(WindowEvent e) {
493: }
494:
495: public void windowIconified(WindowEvent e) {
496: }
497:
498: public void windowDeiconified(WindowEvent e) {
499: }
500:
501: public void windowOpened(WindowEvent e) {
502: }
503:
504: public Insets getInsets() {
505: Insets std = super .getInsets();
506:
507: return new Insets(std.top + 10, std.left + 10, std.bottom + 10,
508: std.right + 10);
509: }
510:
511: public void pause(int time) {
512: try {
513: Thread.sleep(time);
514: } catch (Exception ex) {
515: }
516: }
517:
518: private void tryFtpAgain(int response, String htmp, String ptmp,
519: String utmp, String potmp, String dtmp, boolean useLocal) {
520: //*** FOR TESTING PURPOSES
521: //System.out.println(htmp + " " + ptmp + " " + utmp);
522: //System.out.println(potmp + " " + dtmp);
523: //***
524: if ((response == FtpConnection.OFFLINE) && Settings.reconnect) {
525: //FtpConnection con;
526: setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
527:
528: h.setVisible(true);
529:
530: while (mode == 0) {
531: pause(10);
532: }
533:
534: JFtp.mainFrame.setVisible(false);
535:
536: while ((response == FtpConnection.OFFLINE) && (mode == 1)) {
537: System.out.print("Server is full, next attempt in ");
538:
539: int r = 5;
540:
541: for (int i = 0; i < r; r--) {
542: System.out.print("" + r + "-");
543:
544: try {
545: Thread.sleep(1000);
546: } catch (Exception ex) {
547: }
548: }
549:
550: System.out.println("0...");
551:
552: /* con = new
553: FtpConnection(
554: htmp,
555: Integer.parseInt(potmp),
556: dtmp);
557:
558: */
559: //response = con.login(utmp, ptmp);
560: response = StartConnection.startFtpCon(htmp, utmp,
561: ptmp, Integer.parseInt(potmp), dtmp, useLocal);
562: }
563:
564: if (mode == 1) {
565: JFtp.mainFrame.setVisible(true);
566: } else {
567: // Switch windows
568: JFtp.mainFrame.setVisible(false);
569: this .setVisible(true);
570: this .toFront();
571:
572: return;
573: }
574: } else if ((response != FtpConnection.LOGIN_OK)
575: || ((response == FtpConnection.OFFLINE) && (!Settings.reconnect))) {
576: setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
577:
578: //setFilesystemConnection();
579: if (useLocal) {
580: JFtp.statusP.jftp.closeCurrentLocalTab();
581: } else {
582: JFtp.statusP.jftp.closeCurrentTab();
583: }
584:
585: //this.setVisible(true);
586: //this.toFront();
587: return;
588: }
589: }
590:
591: //*** tryFtpAgain
592: }
|