KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > JFtp


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;
17
18 import java.awt.BorderLayout JavaDoc;
19 import java.awt.Component JavaDoc;
20 import java.awt.Dimension JavaDoc;
21 import java.awt.FlowLayout JavaDoc;
22 import java.awt.Image JavaDoc;
23 import java.awt.Point JavaDoc;
24 import java.awt.Toolkit JavaDoc;
25 import java.awt.datatransfer.DataFlavor JavaDoc;
26 import java.awt.datatransfer.Transferable JavaDoc;
27 import java.awt.dnd.DnDConstants JavaDoc;
28 import java.awt.dnd.DropTarget JavaDoc;
29 import java.awt.dnd.DropTargetDragEvent JavaDoc;
30 import java.awt.dnd.DropTargetDropEvent JavaDoc;
31 import java.awt.dnd.DropTargetEvent JavaDoc;
32 import java.awt.dnd.DropTargetListener JavaDoc;
33 import java.awt.event.ActionListener JavaDoc;
34 import java.awt.event.ComponentEvent JavaDoc;
35 import java.awt.event.ComponentListener JavaDoc;
36 import java.awt.event.WindowEvent JavaDoc;
37 import java.awt.event.WindowListener JavaDoc;
38 import java.io.BufferedInputStream JavaDoc;
39 import java.io.BufferedOutputStream JavaDoc;
40 import java.io.DataInputStream JavaDoc;
41 import java.io.File JavaDoc;
42 import java.io.FileOutputStream JavaDoc;
43 import java.io.PrintWriter JavaDoc;
44 import java.io.Reader JavaDoc;
45 import java.io.StringWriter JavaDoc;
46 import java.net.URL JavaDoc;
47 import java.util.Hashtable JavaDoc;
48
49 import javax.swing.JDesktopPane JavaDoc;
50 import javax.swing.JFrame JavaDoc;
51 import javax.swing.JInternalFrame JavaDoc;
52 import javax.swing.JLabel JavaDoc;
53 import javax.swing.JPanel JavaDoc;
54 import javax.swing.JScrollBar JavaDoc;
55 import javax.swing.JScrollPane JavaDoc;
56 import javax.swing.JSplitPane JavaDoc;
57 import javax.swing.JTabbedPane JavaDoc;
58 import javax.swing.JTextArea JavaDoc;
59 import javax.swing.JToolBar JavaDoc;
60 import javax.swing.SwingUtilities JavaDoc;
61 import javax.swing.UIManager JavaDoc;
62 import javax.swing.event.ChangeEvent JavaDoc;
63 import javax.swing.event.ChangeListener JavaDoc;
64 import javax.swing.event.InternalFrameEvent JavaDoc;
65 import javax.swing.event.InternalFrameListener JavaDoc;
66 import javax.swing.plaf.basic.BasicInternalFrameTitlePane.RestoreAction;
67
68 import net.sf.jftp.config.Settings;
69 import net.sf.jftp.gui.base.AppMenuBar;
70 import net.sf.jftp.gui.base.DownloadList;
71 import net.sf.jftp.gui.base.DownloadQueue;
72 import net.sf.jftp.gui.base.LocalDir;
73 import net.sf.jftp.gui.base.LogFlusher;
74 import net.sf.jftp.gui.base.RemoteDir;
75 import net.sf.jftp.gui.base.StatusPanel;
76 import net.sf.jftp.gui.base.dir.Dir;
77 import net.sf.jftp.gui.base.dir.DirEntry;
78 import net.sf.jftp.gui.framework.FileTransferable;
79 import net.sf.jftp.gui.framework.GUIDefaults;
80 import net.sf.jftp.gui.framework.HDesktopBackground;
81 import net.sf.jftp.gui.framework.HImage;
82 import net.sf.jftp.gui.hostchooser.HostChooser;
83 import net.sf.jftp.gui.tasks.HostInfo;
84 import net.sf.jftp.net.BasicConnection;
85 import net.sf.jftp.net.ConnectionHandler;
86 import net.sf.jftp.net.ConnectionListener;
87 import net.sf.jftp.net.FilesystemConnection;
88 import net.sf.jftp.net.FtpConnection;
89 import net.sf.jftp.system.LocalIO;
90 import net.sf.jftp.system.UpdateDaemon;
91 import net.sf.jftp.system.logging.Log;
92 import net.sf.jftp.system.logging.Logger;
93 import net.sf.jftp.tools.RSSFeeder;
94
95
96 public class JFtp extends JPanel JavaDoc implements WindowListener JavaDoc, ComponentListener JavaDoc,
97                                             Logger, ChangeListener JavaDoc,
98                                             InternalFrameListener JavaDoc
99 {
100     public static boolean mainUsed = false;
101     public static StatusPanel statusP;
102     public static JLabel JavaDoc statusL = new JLabel JavaDoc("Welcome to JFtp... ");
103     public static JFrame JavaDoc mainFrame;
104     public static Dir localDir;
105     public static Dir remoteDir;
106     public static DownloadList dList = new DownloadList();
107     public static DownloadQueue dQueue = new DownloadQueue();
108     public static boolean uiBlocked = false;
109     public static HostInfo hostinfo = new HostInfo();
110
111     //public static BasicConnection controlConnection = null;
112
private static ConnectionHandler defaultConnectionHandler = new ConnectionHandler();
113     public static JDesktopPane JavaDoc desktop = new JDesktopPane JavaDoc();
114     private static JScrollPane JavaDoc logSp;
115     public static JTextArea JavaDoc log;
116
117     //***appMenuBar: this must now be a public object that JFtp refers to
118
public static AppMenuBar menuBar = null;
119     public static final int CAPACITY = 9; //number of connections remembered
120
public static final int CONNECTION_DATA_LENGTH = 10;
121     public static DropTarget JavaDoc dropTarget;
122     public static DropTargetListener JavaDoc dtListener;
123     public static int acceptableActions = DnDConstants.ACTION_COPY;
124     private LogFlusher flusher;
125     private boolean initSize = true;
126     public JTabbedPane JavaDoc remoteConnectionPanel = new JTabbedPane JavaDoc();
127     public JTabbedPane JavaDoc localConnectionPanel = new JTabbedPane JavaDoc();
128     private String JavaDoc oldText = "";
129     private HDesktopBackground background;
130
131     /** JSplitPane that holds the directory panes and the log/dl JSplitPane */
132     private JSplitPane JavaDoc workP = null;
133
134     /** JSplitPane that holds the log download parts */
135     private JSplitPane JavaDoc logP = null;
136     private JInternalFrame JavaDoc j1;
137     private JInternalFrame JavaDoc j2;
138     private JInternalFrame JavaDoc j3;
139     private JInternalFrame JavaDoc j4;
140     private JInternalFrame JavaDoc j5;
141     private Hashtable JavaDoc internalFrames = new Hashtable JavaDoc();
142     public HostChooser hc;
143     private String JavaDoc buffer = "";
144     private long oldtime = 0;
145     private UpdateDaemon daemon;
146     public RSSFeeder feeder;
147
148     //***
149
public JFtp()
150     {
151         Log.setLogger(this);
152
153         // we have jesktop-environment
154
if(statusP != null)
155         {
156             statusP.remove(statusP.close);
157         }
158
159         init();
160     }
161
162     public JFtp(boolean mainUsed)
163     {
164         Log.setLogger(this);
165         this.mainUsed = mainUsed;
166         init();
167     }
168
169     public void init()
170     {
171         dtListener = new DTListener();
172         dropTarget = new DropTarget JavaDoc(this, this.acceptableActions, dtListener,
173                                     true);
174
175         setLayout(new BorderLayout JavaDoc());
176
177         setBackground(GUIDefaults.mainBack);
178         setForeground(GUIDefaults.front);
179
180         statusP = new StatusPanel(this);
181         add("North", statusP);
182
183         localDir = (Dir) new LocalDir(Settings.defaultWorkDir);
184         localDir.setDownloadList(dList);
185
186         remoteDir = (Dir) new RemoteDir();
187         remoteDir.setDownloadList(dList);
188         desktop.setDropTarget(this.dropTarget);
189
190         Dimension JavaDoc d = Settings.getWindowSize();
191         setPreferredSize(d);
192         setSize(d);
193
194         int width = (int) d.getWidth();
195         int height = (int) d.getHeight();
196
197         dList.setMinimumSize(new Dimension JavaDoc((int) (width / 2.5),
198                                            (int) (height * 0.20)));
199         dList.setPreferredSize(new Dimension JavaDoc((int) (width / 2.5),
200                                              (int) (height * 0.25)));
201         dList.setSize(new Dimension JavaDoc((int) (width / 2.5), (int) (height * 0.25)));
202
203         desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
204         addBackgroundImage();
205
206         j1 = new JInternalFrame JavaDoc("Local filesystem", true, false, true, true);
207         j1.setMinimumSize(new Dimension JavaDoc(300, 300));
208         j1.setLocation(5, 5);
209         localConnectionPanel.addTab("file://", null, (Component JavaDoc) localDir,
210                                     "Filesystem");
211         localConnectionPanel.setSelectedIndex(0);
212         localConnectionPanel.addChangeListener(this);
213         j1.getContentPane().add(localConnectionPanel);
214         localDir.fresh();
215         desktop.add(j1);
216         j1.setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
217         j1.addInternalFrameListener(this);
218         j1.pack();
219         j1.setSize(new Dimension JavaDoc(400, j1.getSize().height));
220         j1.show();
221
222         j2 = new JInternalFrame JavaDoc("Remote connections", true, false, true, true);
223         j2.setLocation(430, 5);
224         remoteConnectionPanel.addTab("file://", null, (Component JavaDoc) remoteDir,
225                                      "Filesystem");
226         remoteConnectionPanel.setSelectedIndex(0);
227         remoteConnectionPanel.addChangeListener(this);
228         j2.getContentPane().add(remoteConnectionPanel);
229         desktop.add(j2);
230         j2.setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
231         j2.addInternalFrameListener(this);
232         j2.pack();
233         j2.setSize(new Dimension JavaDoc(415, j1.getSize().height));
234         j2.show();
235
236         log = new JTextArea JavaDoc();
237         log.setBackground(GUIDefaults.light);
238         log.setForeground(GUIDefaults.front);
239         log.setEditable(false);
240         logSp = new JScrollPane JavaDoc(log);
241         logSp.setSize(new Dimension JavaDoc(438, 148));
242
243         j5 = new JInternalFrame JavaDoc("Queue System", true, false, true, true);
244         j5.setLocation(5, 400);
245         j5.getContentPane().add(dQueue, BorderLayout.CENTER);
246         desktop.add(j5);
247         j5.pack();
248         j5.setSize(new Dimension JavaDoc(440, 150));
249         j5.show();
250
251         j3 = new JInternalFrame JavaDoc("Log", true, false, true, true);
252
253         int x = (int) (desktop.getSize().width / 2);
254         j3.setLocation(5, 425);
255         j3.getContentPane().add(logSp, BorderLayout.CENTER);
256         desktop.add(j3);
257         j3.pack();
258         j3.setSize(new Dimension JavaDoc(440, 150));
259         j3.show();
260
261         j4 = new JInternalFrame JavaDoc("Download Manager", true, false, true, true);
262         j4.setLocation(450, 425);
263         j4.getContentPane().add(dList, BorderLayout.CENTER);
264         desktop.add(j4);
265         j4.pack();
266         j4.setSize(new Dimension JavaDoc(380, 150));
267         j4.show();
268
269         j1.toFront();
270         j2.toFront();
271
272         add("Center", desktop);
273
274         JToolBar JavaDoc j = new JToolBar JavaDoc();
275         j.setFloatable(false);
276         j.add(statusP.status, FlowLayout.LEFT);
277
278         if(Settings.getEnableRSS())
279         {
280             feeder = new RSSFeeder();
281             j.add(feeder);
282         }
283
284         add("South", j);
285
286         addComponentListener(this);
287         componentResized(new ComponentEvent JavaDoc(log, 0));
288
289         restoreInternalPositions();
290         validate();
291         setVisible(true);
292
293         if(!mainUsed)
294         {
295             SwingUtilities.invokeLater(new Runnable JavaDoc()
296                 {
297                     public void run()
298                     {
299                         chooseHost();
300                     }
301                 });
302         }
303         else
304         {
305             chooseHost();
306         }
307
308         flusher = new LogFlusher();
309         daemon = new UpdateDaemon(this);
310         
311     }
312
313     protected void chooseHost()
314     {
315         hc = new HostChooser(this);
316
317         if(!mainUsed)
318         {
319             hc.update();
320         }
321     }
322
323     public static String JavaDoc getHost()
324     {
325         return statusP.getHost();
326     }
327
328     public static void setHost(String JavaDoc which)
329     {
330         statusP.setHost(which);
331     }
332
333     public static void localUpdate()
334     {
335         localDir.fresh();
336     }
337
338     public static void remoteUpdate()
339     {
340         remoteDir.fresh();
341     }
342     
343     
344
345     private void saveInternalPositions() {
346         saveInternalPosition(j1, "local");
347         saveInternalPosition(j2, "remote");
348         saveInternalPosition(j5, "queue");
349         saveInternalPosition(j3, "log");
350         saveInternalPosition(j4, "manager");
351     }
352     
353     private void restoreInternalPositions() {
354         if(Settings.getProperty("jftp.iframes.resize").equals("false")) {
355             
356         }
357         else {
358             restoreInternalPosition(j1, "local");
359             restoreInternalPosition(j2, "remote");
360             restoreInternalPosition(j5, "queue");
361             restoreInternalPosition(j3, "log");
362             restoreInternalPosition(j4, "manager");
363         }
364     }
365     
366     private void restoreInternalPosition(JInternalFrame JavaDoc f, String JavaDoc desc) {
367         String JavaDoc x = Settings.getProperty("jftp.iframes."+desc+".x");
368         String JavaDoc y = Settings.getProperty("jftp.iframes."+desc+".y");
369         String JavaDoc w = Settings.getProperty("jftp.iframes."+desc+".width");
370         String JavaDoc h = Settings.getProperty("jftp.iframes."+desc+".height");
371         
372         if(x.indexOf(".") >= 0) x = x.substring(0, x.indexOf("."));
373         if(y.indexOf(".") >= 0) y = y.substring(0, y.indexOf("."));
374         
375         try {
376             f.setLocation(Integer.parseInt(x), Integer.parseInt(y));
377             f.setSize(Integer.parseInt(w), Integer.parseInt(h));
378         }
379         catch(Exception JavaDoc ex) {
380             Log.out("Can not set internal fram position for: "+desc);
381             ex.printStackTrace();
382         }
383     }
384     
385     private void saveInternalPosition(JInternalFrame JavaDoc f, String JavaDoc desc) {
386         Point JavaDoc p = f.getLocation();
387    
388         Settings.setProperty("jftp.iframes."+desc+".x", ""+p.getX());
389         Settings.setProperty("jftp.iframes."+desc+".y", ""+p.getY());
390         Settings.setProperty("jftp.iframes."+desc+".width", f.getWidth());
391         Settings.setProperty("jftp.iframes."+desc+".height", f.getHeight());
392     }
393     
394     public void windowClosing(WindowEvent JavaDoc e)
395     {
396         saveInternalPositions();
397         
398         Settings.setProperty("jftp.window.width", this.getWidth());
399         Settings.setProperty("jftp.window.height", this.getHeight());
400
401         if(!mainUsed)
402         {
403             Settings.setProperty("jftp.window.x",
404                                  (int) this.getLocationOnScreen().getX());
405             Settings.setProperty("jftp.window.y",
406                                  (int) this.getLocationOnScreen().getY());
407         }
408         else
409         {
410             Settings.setProperty("jftp.window.x",
411                                  (int) mainFrame.getLocationOnScreen().getX());
412             Settings.setProperty("jftp.window.y",
413                                  (int) mainFrame.getLocationOnScreen().getY());
414         }
415
416         Settings.save();
417         safeDisconnect();
418
419         if(Settings.isStandalone)
420         {
421             System.exit(0);
422         }
423         else
424         {
425             mainFrame.dispose();
426         }
427     }
428
429     public static void safeDisconnect()
430     {
431         BasicConnection con = remoteDir.getCon();
432
433         if((con != null) && con.isConnected())
434         {
435             try
436             {
437                 con.disconnect();
438             }
439             catch(Exception JavaDoc ex)
440             {
441             }
442         }
443
444         FilesystemConnection c = new FilesystemConnection();
445         c.addConnectionListener((ConnectionListener) remoteDir);
446         remoteDir.setCon(c);
447
448         if(!c.chdir("/"))
449         {
450             c.chdir("C:\\");
451         }
452     }
453
454     public void windowClosed(WindowEvent JavaDoc e)
455     {
456     }
457
458     public void windowActivated(WindowEvent JavaDoc e)
459     {
460     }
461
462     public void windowDeactivated(WindowEvent JavaDoc e)
463     {
464     }
465
466     public void windowIconified(WindowEvent JavaDoc e)
467     {
468     }
469
470     public void windowDeiconified(WindowEvent JavaDoc e)
471     {
472     }
473
474     public void windowOpened(WindowEvent JavaDoc e)
475     {
476     }
477
478     public void componentHidden(ComponentEvent JavaDoc e)
479     {
480     }
481
482     public void componentMoved(ComponentEvent JavaDoc e)
483     {
484     }
485
486     public void componentShown(ComponentEvent JavaDoc e)
487     {
488     }
489
490     public void componentResized(ComponentEvent JavaDoc e)
491     {
492         localDir.actionPerformed(this, "local");
493         remoteDir.actionPerformed(this, "remote");
494         desktop.remove(background);
495         addBackgroundImage();
496         validate();
497     }
498
499     public void addBackgroundImage()
500     {
501         try
502         {
503             background = new HDesktopBackground(Settings.background, null);
504             background.setBounds(0, 0, getSize().width, getSize().height);
505             desktop.add(background, new Integer JavaDoc(Integer.MIN_VALUE));
506         }
507         catch(Exception JavaDoc ex)
508         {
509             Log.out(Settings.background + " missing, no background image used");
510         }
511     }
512
513     private static void compile()
514     {
515         /*
516         try
517         {
518                Compiler.enable();
519            System.out.println(Compiler.compileClass(Class.forName("java.util.Hashtable")));
520            System.out.println(Compiler.compileClasses("net.sf.jftp"));
521            System.out.println(Compiler.compileClasses("net.sf.jftp.JFtp"));
522            System.out.println(Compiler.compileClasses("JFtp"));
523          }
524          catch(ClassNotFoundException ex)
525          {
526                  ex.printStackTrace();
527          }
528          */

529     }
530
531     private static void setSocksProxyOptions(String JavaDoc proxy, String JavaDoc port)
532     {
533         if(proxy.equals("") || port.equals(""))
534         {
535             return;
536         }
537
538         java.util.Properties JavaDoc sysprops = System.getProperties();
539
540         // Remove previous values
541
sysprops.remove("socksProxyHost");
542         sysprops.remove("socksProxyPort");
543
544         // Set your values
545
sysprops.put("socksProxyHost", proxy);
546         sysprops.put("socksProxyPort", port);
547
548         Log.out("socks proxy: " + sysprops.get("socksProxyHost") + ":" +
549                 sysprops.get("socksProxyPort"));
550     }
551
552     public static void main(String JavaDoc[] argv)
553     {
554         try {
555             long start = System.currentTimeMillis();
556
557             Log.out("starting up jftp...");
558             compile();
559             System.setProperty("sshtools.logfile", Settings.appHomeDir +
560             "log4.txt");
561
562             if(Settings.autoUpdate)
563             {
564                 checkForUpgrade();
565             }
566
567             Settings.enableResuming = true;
568             Settings.enableUploadResuming = true;
569             Settings.noUploadResumingQuestion = false;
570
571             setSocksProxyOptions(Settings.getSocksProxyHost(),
572                     Settings.getSocksProxyPort());
573
574             JFtp jftp = new JFtp(true);
575             UIManager.getLookAndFeelDefaults().put("ClassLoader",
576                     jftp.getClass().getClassLoader());
577
578             //boolean lookSet = false;
579
String JavaDoc tmp = Settings.getLookAndFeel();
580
581             //UIManager.installLookAndFeel("Metouia", "net.sourceforge.mlf.metouia.MetouiaLookAndFeel");
582
if(tmp != null)
583             {
584                 jftp.setLookAndFeel(Settings.getLookAndFeel());
585             }
586             else
587             {
588                 jftp.setLookAndFeel("net.sourceforge.mlf.metouia.MetouiaLookAndFeel");
589             }
590
591             //else
592
//{
593
// jftp.setLookAndFeel("com.incors.plaf.kunststoff.KunststoffLookAndFeel");
594
// lookSet = true;
595
//}
596
//if(!lookSet) UIManager.installLookAndFeel("Kunststoff", "com.incors.plaf.kunststoff.KunststoffLookAndFeel");
597

598             /*
599              * Don't try to add the Kunststoff look and feel if it has
600              * already been added.
601              */

602             if((Settings.getLookAndFeel() == null) ||
603                     !Settings.getLookAndFeel().equals("com.incors.plaf.kunststoff.KunststoffLookAndFeel"))
604             {
605                 /*
606                  * Somehow even though UIManager.installLookAndFeel throws a
607                  * ClassNotFoundException, it ends up getting added to the
608                  * list of installed look and feels anyway. What we do here is
609                  * make Java throw a ClassNotFoundException before then by
610                  * checking whether or not the class exists, and only if the
611                  * class exists does it then get added to the list of
612                  * available look and feel's.
613                  */

614                 try
615                 {
616                     Class.forName("com.incors.plaf.kunststoff.KunststoffLookAndFeel");
617                     UIManager.installLookAndFeel("Kunststoff",
618                     "com.incors.plaf.kunststoff.KunststoffLookAndFeel");
619                 }
620                 catch(ClassNotFoundException JavaDoc cnfe)
621                 {
622                 }
623             }
624
625             /*
626              * Don't try to add the Metouia look and feel if it has
627              * already been added.
628              */

629             if((Settings.getLookAndFeel() == null) ||
630                     !Settings.getLookAndFeel().equals("net.sourceforge.mlf.metouia.MetouiaLookAndFeel"))
631             {
632                 /*
633                  * Somehow even though UIManager.installLookAndFeel throws a
634                  * ClassNotFoundException, it ends up getting added to the
635                  * list of installed look and feels anyway. What we do here is
636                  * make Java throw a ClassNotFoundException before then by
637                  * checking whether or not the class exists, and only if the
638                  * class exists does it then get added to the list of
639                  * available look and feel's.
640                  */

641                 try
642                 {
643                     Class.forName("net.sourceforge.mlf.metouia.MetouiaLookAndFeel");
644                     UIManager.installLookAndFeel("Metouia",
645                     "net.sourceforge.mlf.metouia.MetouiaLookAndFeel");
646                 }
647                 catch(ClassNotFoundException JavaDoc cnfe)
648                 {
649                 }
650             }
651
652             /*
653                 try
654                 {
655                     Class.forName("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
656                     UIManager.installLookAndFeel("PlasticXP",
657                                                  "com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
658
659                     Class.forName("com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
660                     UIManager.installLookAndFeel("Plastic3D",
661                                                  "com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
662
663                     Class.forName("com.jgoodies.looks.plastic.PlasticLookAndFeel");
664                     UIManager.installLookAndFeel("Plastic",
665                                                  "com.jgoodies.looks.plastic.PlasticLookAndFeel");
666
667                     Class.forName("com.jgoodies.looks.windows.WindowsLookAndFeel");
668                     UIManager.installLookAndFeel("Windows",
669                                                  "com.jgoodies.looks.windows.WindowsLookAndFeel");
670                 }
671                 catch(ClassNotFoundException cnfe)
672                 {
673                     ex.printStackTrace();
674                 }
675              */

676             mainFrame = new JFrame JavaDoc();
677             mainFrame.setLocation(Settings.getWindowLocation());
678
679             mainFrame.setTitle(Settings.title + " - Version " + getVersion());
680
681             mainFrame.setResizable(Settings.resize);
682             mainFrame.addWindowListener(jftp);
683
684             Image JavaDoc icon = HImage.getImage(jftp, Settings.iconImage);
685             mainFrame.setIconImage(icon);
686             mainFrame.setFont(GUIDefaults.font);
687
688             //*** MY CHANGES
689
//mainFrame.setJMenuBar(new AppMenuBar(jftp));
690
menuBar = new AppMenuBar(jftp);
691             mainFrame.setJMenuBar(menuBar);
692
693             //***
694
mainFrame.getContentPane().setLayout(new BorderLayout JavaDoc());
695             mainFrame.getContentPane().add("Center", jftp);
696             SwingUtilities.updateComponentTreeUI(mainFrame);
697             mainFrame.pack();
698             mainFrame.validate();
699             mainFrame.setVisible(true);
700
701             if(argv.length > 0)
702             {
703                 jftp.hc.update(argv[0]);
704             }
705             else
706             {
707                 //jftp.hc.update();
708
}
709
710             Log.out("jftp is up and running.");
711
712             long end = System.currentTimeMillis();
713
714             Log.out("startup time: " + (end - start) + "ms.");
715         }
716         catch(Error JavaDoc ex) {
717             ex.printStackTrace();
718         }
719     }
720
721     private void log(String JavaDoc msg)
722     {
723         if(msg.startsWith("200") || msg.startsWith("227"))
724         {
725             if((msg.indexOf("NOOP") > 0) || (msg.indexOf("Type") > 0) ||
726                    (msg.indexOf("MODE") > 0) || (msg.indexOf("Passive") > 0))
727             {
728                 if(Settings.hideStatus)
729                 {
730                     return;
731                 }
732             }
733         }
734         else if(log == null)
735         {
736             return;
737         }
738
739         if(!msg.equals(""))
740         {
741             buffer = buffer + " " + msg;
742         }
743
744         log.append(buffer);
745         buffer = "";
746
747         long time = System.currentTimeMillis();
748
749         if(((time - oldtime) < Settings.uiRefresh))
750         {
751             UpdateDaemon.updateLog();
752
753             return;
754         }
755
756         oldtime = time;
757
758         JScrollBar JavaDoc bar = logSp.getVerticalScrollBar();
759         bar.setValue(bar.getMaximum());
760
761         //logSp.paintImmediately(0, 0, logSp.getSize().width,
762
// logSp.getSize().height);
763
/*
764         SwingUtilities.invokeLater(new Runnable()
765             {
766                 public void run()
767                 {
768                     repaint();
769                     //paintImmediately(0, 0, getSize().width, getSize().height);
770                 }
771             });
772         */

773         repaint();
774         revalidate();
775     }
776
777     private void logRaw(String JavaDoc msg)
778     {
779         log.append(" " + msg);
780         Log.out("NOTE: logRaw called");
781         paintImmediately(0, 0, getSize().width, getSize().height);
782
783         //logSp.paintImmediately(0,0,logSp.getSize().width,logSp.getSize().height);
784
}
785
786     public void clearLog()
787     {
788         log.setText("");
789         logSp.paintImmediately(0, 0, logSp.getSize().width,
790                                logSp.getSize().height);
791     }
792
793     private void log(String JavaDoc msg, Throwable JavaDoc throwable)
794     {
795         PrintWriter JavaDoc p = new PrintWriter JavaDoc(new StringWriter JavaDoc());
796         throwable.printStackTrace(p);
797         log(msg);
798         log(p.toString());
799     }
800
801     public void debug(String JavaDoc msg)
802     {
803         log(msg + "\n");
804     }
805
806     public void debugRaw(String JavaDoc msg)
807     {
808         logRaw(msg);
809     }
810
811     public void debug(String JavaDoc msg, Throwable JavaDoc throwable)
812     {
813         log(msg, throwable);
814     }
815
816     public void warn(String JavaDoc msg)
817     {
818         log(msg);
819     }
820
821     public void warn(String JavaDoc msg, Throwable JavaDoc throwable)
822     {
823         log(msg, throwable);
824     }
825
826     public void error(String JavaDoc msg)
827     {
828         log(msg);
829     }
830
831     public void error(String JavaDoc msg, Throwable JavaDoc throwable)
832     {
833         log(msg, throwable);
834     }
835
836     public void info(String JavaDoc msg)
837     {
838         log(msg);
839     }
840
841     public void info(String JavaDoc msg, Throwable JavaDoc throwable)
842     {
843         log(msg, throwable);
844     }
845
846     public void fatal(String JavaDoc msg)
847     {
848         log(msg);
849     }
850
851     public void fatal(String JavaDoc msg, Throwable JavaDoc throwable)
852     {
853         log(msg, throwable);
854     }
855
856     public void debugSize(int size, boolean recv, boolean last, String JavaDoc file)
857     {
858     }
859
860     public static String JavaDoc getVersion()
861     {
862         try
863         {
864             URL JavaDoc u = ClassLoader.getSystemResource(Settings.readme);
865
866             if(u == null)
867             {
868                 u = HImage.class.getResource("/" + Settings.readme);
869             }
870
871             DataInputStream JavaDoc i = new DataInputStream JavaDoc(u.openStream());
872             String JavaDoc tmp = i.readLine();
873             tmp = tmp.substring(tmp.lastIndexOf(">") + 1);
874             tmp = tmp.substring(0, tmp.indexOf("<"));
875
876             return tmp;
877         }
878         catch(Exception JavaDoc ex)
879         {
880         }
881
882         return "";
883     }
884
885     private static void checkForUpgrade()
886     {
887         try
888         {
889             Log.out("Checking for updates...");
890
891             URL JavaDoc u = ClassLoader.getSystemResource(Settings.readme);
892
893             if(u == null)
894             {
895                 u = HImage.class.getResource("/" + Settings.readme);
896             }
897
898             DataInputStream JavaDoc i = new DataInputStream JavaDoc(u.openStream());
899             String JavaDoc tmp = i.readLine();
900             tmp = tmp.substring(tmp.lastIndexOf(">"));
901             tmp = tmp.substring(tmp.indexOf(".") + 1);
902             tmp = tmp.substring(0, tmp.indexOf("<"));
903
904             int x = Integer.parseInt(tmp) + 1;
905
906             String JavaDoc nextVersion = "jftp-1.";
907
908             if(x < 10)
909             {
910                 nextVersion = nextVersion + "0";
911             }
912
913             nextVersion = nextVersion + x + ".tar.gz";
914
915             //System.out.println(nextVersion);
916
File JavaDoc dl = new File JavaDoc(nextVersion);
917
918             if(!dl.exists() || (dl.length() <= 0))
919             {
920                 URL JavaDoc url = new URL JavaDoc("http://osdn.dl.sourceforge.net/sourceforge/j-ftp/" +
921                                   nextVersion);
922                 BufferedOutputStream JavaDoc f = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(dl));
923                 BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(url.openStream());
924                 byte[] buf = new byte[4096];
925                 int stat = 1;
926
927                 Log.out("\ndownloading update: " + dl.getAbsolutePath() +
928                         "\n\n");
929
930                 while(stat > 0)
931                 {
932                     //while(in.available() <= 0) LocalIO.pause(10);
933
stat = in.read(buf);
934
935                     if(stat == -1)
936                     {
937                         break;
938                     }
939
940                     f.write(buf, 0, stat);
941                     System.out.print(".");
942                 }
943
944                 f.flush();
945                 f.close();
946                 in.close();
947             }
948
949             Log.out("\n\n\na newer version was found!\nplease install the File " +
950                     dl.getAbsolutePath() +
951                     " or even better visit the homepage to download the latest version...\n" +
952                     "you can turn this feature off if you don't like it (view readme for details)\n\nStarting anyway...\n\n");
953
954             //LocalIO.pause(5000);
955
//System.exit(0);
956
}
957         catch(Exception JavaDoc ex)
958         {
959             // FileNotFoundException, everything is ok
960
//ex.printStackTrace();
961
}
962
963         Log.out("finished check...");
964     }
965
966     public static ConnectionHandler getConnectionHandler()
967     {
968         BasicConnection con = remoteDir.getCon();
969
970         if((con != null) && con instanceof FtpConnection)
971         {
972             return ((FtpConnection) con).getConnectionHandler();
973         }
974         else
975         {
976             return defaultConnectionHandler;
977         }
978     }
979
980     public void fireUpdate()
981     {
982         // if(!(JFtp.remoteDir.getCon() instanceof FilesystemConnection))
983
LocalIO.pause(200);
984         repaint();
985
986         /*
987          else
988          {
989                  LocalIO.pause(200);
990                  paintImmediately(0,0,getSize().width,getSize().height);
991          }
992          */

993     }
994
995     public void ensureLogging()
996     {
997         if(buffer.equals(""))
998         {
999             JScrollBar JavaDoc bar;
1000
1001            if((logSp == null) ||
1002                   ((bar = logSp.getVerticalScrollBar()) == null) || bar == null ||
1003                   (bar.getValue() == bar.getMaximum()) ||
1004                   bar.getValueIsAdjusting())
1005            {
1006                return;
1007            }
1008            else
1009            {
1010                bar.setValue(bar.getMaximum());
1011                logSp.repaint();
1012
1013                /*
1014                logSp.paintImmediately(0, 0, logSp.getSize().width,
1015                               logSp.getSize().height);
1016                */

1017                /*
1018                SwingUtilities.invokeLater(new Runnable()
1019                    {
1020                        public void run()
1021                        {
1022                            repaint();
1023                            //paintImmediately(0, 0, getSize().width,
1024                            // getSize().height);
1025                        }
1026                    });
1027                 */

1028                repaint();
1029                revalidate();
1030            }
1031
1032            return;
1033        }
1034
1035        Log.out("flushing log buffer...");
1036        oldtime = 0;
1037        log("");
1038    }
1039
1040    public void setLookAndFeel(String JavaDoc name)
1041    {
1042        if(name == null)
1043        {
1044            return;
1045        }
1046
1047        try
1048        {
1049            UIManager.setLookAndFeel(name);
1050
1051            if(mainFrame != null)
1052            {
1053                //SwingUtilities.updateComponentTreeUI(mainFrame);
1054
//invalidate();
1055
//validate();
1056
//repaint();
1057
SwingUtilities.invokeLater(new Runnable JavaDoc()
1058                    {
1059                        public void run()
1060                        {
1061                            SwingUtilities.updateComponentTreeUI(mainFrame);
1062                            SwingUtilities.updateComponentTreeUI(JFtp.statusP);
1063                        }
1064                    });
1065            }
1066        }
1067        catch(Exception JavaDoc ex)
1068        {
1069            Log.debug("Error setting look and feel: " + ex);
1070        }
1071    }
1072
1073    public void addConnection(String JavaDoc name, BasicConnection con)
1074    {
1075        con.addConnectionListener((ConnectionListener) localDir);
1076
1077        Dir tmp = (Dir) new RemoteDir();
1078        tmp.setDownloadList(dList);
1079        con.addConnectionListener((ConnectionListener) tmp);
1080        tmp.setCon(con);
1081
1082        int x = remoteConnectionPanel.getSelectedIndex();
1083        remoteConnectionPanel.addTab(parse(name), null, (Component JavaDoc) tmp,
1084                                     "Switch to: " + parse(name));
1085        remoteConnectionPanel.setSelectedIndex(x + 1);
1086        j2.setClosable(true);
1087    }
1088
1089    public void addLocalConnection(String JavaDoc name, BasicConnection con)
1090    {
1091        con.addConnectionListener((ConnectionListener) remoteDir);
1092
1093        Dir tmp = (Dir) new LocalDir();
1094        tmp.setDownloadList(dList);
1095        con.addConnectionListener((ConnectionListener) tmp);
1096        tmp.setCon(con);
1097
1098        int x = localConnectionPanel.getSelectedIndex();
1099        localConnectionPanel.addTab(parse(name), null, (Component JavaDoc) tmp,
1100                                    "Switch to: " + parse(name));
1101        localConnectionPanel.setSelectedIndex(x + 1);
1102        j1.setClosable(true);
1103    }
1104
1105    private String JavaDoc parse(String JavaDoc what)
1106    {
1107        if(what.indexOf("@") >= 0)
1108        {
1109            return what.substring(what.lastIndexOf("@") + 1);
1110        }
1111        else
1112        {
1113            return what;
1114        }
1115    }
1116
1117    public void stateChanged(ChangeEvent JavaDoc e)
1118    {
1119        remoteDir = (Dir) remoteConnectionPanel.getSelectedComponent();
1120        localDir = (Dir) localConnectionPanel.getSelectedComponent();
1121        remoteDir.getCon().setLocalPath(localDir.getPath());
1122
1123        //localDir.getCon().setLocalPath(remoteDir.getPath());
1124
}
1125
1126    public void closeCurrentTab()
1127    {
1128        int x = remoteConnectionPanel.getSelectedIndex();
1129
1130        if(x > 0)
1131        {
1132            safeDisconnect();
1133            remoteConnectionPanel.remove(x);
1134            remoteConnectionPanel.setSelectedIndex(x - 1);
1135        }
1136
1137        if(remoteConnectionPanel.getTabCount() < 2)
1138        {
1139            j2.setClosable(false);
1140        }
1141    }
1142
1143    public void closeCurrentLocalTab()
1144    {
1145        int x = localConnectionPanel.getSelectedIndex();
1146
1147        if(x > 0)
1148        {
1149            BasicConnection con = localDir.getCon();
1150
1151            if((con != null) && con.isConnected())
1152            {
1153                try
1154                {
1155                    con.disconnect();
1156                }
1157                catch(Exception JavaDoc ex)
1158                {
1159                }
1160            }
1161
1162            localConnectionPanel.remove(x);
1163            localConnectionPanel.setSelectedIndex(x - 1);
1164        }
1165
1166        if(localConnectionPanel.getTabCount() < 2)
1167        {
1168            j1.setClosable(false);
1169        }
1170    }
1171
1172    public void addToDesktop(String JavaDoc title, Component JavaDoc c, int w, int h)
1173    {
1174        JInternalFrame JavaDoc jt = new JInternalFrame JavaDoc(title, false, true, false, true);
1175
1176        if(w < 500)
1177        {
1178            jt.setLocation(200, 100);
1179        }
1180        else
1181        {
1182            jt.setLocation(80, 100);
1183        }
1184
1185        jt.getContentPane().add(c);
1186        desktop.add(jt);
1187
1188        internalFrames.put("" + c.hashCode(), jt);
1189
1190        jt.pack();
1191        jt.setSize(new Dimension JavaDoc(w, h));
1192        jt.show();
1193    }
1194
1195    public void removeFromDesktop(int component)
1196    {
1197        JInternalFrame JavaDoc f = (JInternalFrame JavaDoc) internalFrames.get("" + component);
1198
1199        if(f != null)
1200        {
1201            f.dispose();
1202
1203            //internalFrames.remove(component);
1204
}
1205        else
1206        {
1207            Log.debug("ERROR: " + component + " not found in Hashtable!");
1208        }
1209    }
1210
1211    public void setClosable(int component, boolean ok)
1212    {
1213        JInternalFrame JavaDoc f = (JInternalFrame JavaDoc) internalFrames.get("" + component);
1214
1215        if(f != null)
1216        {
1217            f.setClosable(ok);
1218        }
1219        else
1220        {
1221            Log.debug("ERROR: " + component + " not found in Hashtable!");
1222        }
1223    }
1224
1225    public void setLocation(int component, int x, int y)
1226    {
1227        JInternalFrame JavaDoc f = (JInternalFrame JavaDoc) internalFrames.get("" + component);
1228
1229        if(f != null)
1230        {
1231            f.setLocation(x, y);
1232        }
1233        else
1234        {
1235            Log.debug("ERROR: " + component + " not found in Hashtable!");
1236        }
1237    }
1238
1239    //*** REFRESH MENU BAR
1240
public static void updateMenuBar()
1241    {
1242        //mainFrame.setJMenuBar(new AppMenuBar(this));
1243
menuBar.resetFileItems();
1244    }
1245
1246    public void internalFrameClosing(InternalFrameEvent JavaDoc e)
1247    {
1248        if(e.getSource() == j1)
1249        {
1250            closeCurrentLocalTab();
1251        }
1252        else if(e.getSource() == j2)
1253        {
1254            closeCurrentTab();
1255        }
1256    }
1257
1258    public void internalFrameActivated(InternalFrameEvent JavaDoc e)
1259    {
1260    }
1261
1262    public void internalFrameClosed(InternalFrameEvent JavaDoc e)
1263    {
1264    }
1265
1266    public void internalFrameDeactivated(InternalFrameEvent JavaDoc e)
1267    {
1268    }
1269
1270    public void internalFrameDeiconified(InternalFrameEvent JavaDoc e)
1271    {
1272    }
1273
1274    public void internalFrameIconified(InternalFrameEvent JavaDoc e)
1275    {
1276    }
1277
1278    public void internalFrameOpened(InternalFrameEvent JavaDoc e)
1279    {
1280    }
1281
1282    public void drop()
1283    {
1284        try
1285        {
1286            handleDrop(null,
1287                       Toolkit.getDefaultToolkit().getSystemClipboard()
1288                              .getContents(this));
1289        }
1290        catch(Exception JavaDoc ex)
1291        {
1292            ex.printStackTrace();
1293        }
1294    }
1295
1296    public void handleDrop(DropTargetDropEvent JavaDoc e, Transferable JavaDoc t)
1297                    throws Exception JavaDoc
1298    {
1299        System.out.println("Starting dropAttempt");
1300
1301        DataFlavor JavaDoc chosen = DataFlavor.javaFileListFlavor;
1302        DataFlavor JavaDoc second = FileTransferable.plainTextFlavor;
1303        DataFlavor JavaDoc flavor = null;
1304        Object JavaDoc data = null;
1305
1306        if(e != null)
1307        {
1308            flavor = e.getCurrentDataFlavors()[0];
1309            e.acceptDrop(acceptableActions);
1310
1311            Class JavaDoc c = flavor.getDefaultRepresentationClass();
1312        }
1313
1314        if(flavor == null)
1315        {
1316            flavor = second;
1317        }
1318
1319        String JavaDoc name = "";
1320
1321        if(t.isDataFlavorSupported(chosen))
1322        {
1323            System.out.println("Using List DnD style");
1324
1325            java.util.List JavaDoc myList = (java.util.List JavaDoc) t.getTransferData(chosen);
1326
1327            File JavaDoc[] f = (File JavaDoc[]) myList.toArray();
1328
1329            for(int i = 0; i < f.length; i++)
1330            {
1331                name = f[i].getAbsolutePath();
1332                System.out.println("DnD file: " + name);
1333            }
1334
1335            draggedTransfer(f, name);
1336        }
1337        else if(t.isDataFlavorSupported(second))
1338        {
1339            System.out.println("Using text/plain DnD style");
1340
1341            data = t.getTransferData(flavor);
1342
1343            String JavaDoc str = "";
1344            int i = 0;
1345
1346            if(data instanceof Reader JavaDoc)
1347            {
1348                int c;
1349
1350                while((c = ((Reader JavaDoc) data).read()) != -1)
1351                {
1352                    if(((i == 1) && (c == 0)))
1353                    {
1354                        //System.out.println("Applying charset bugfix");
1355
i = -1;
1356                    }
1357                    else
1358                    {
1359                        str = str + new Character JavaDoc((char) c).toString();
1360                    }
1361
1362                    i++;
1363                }
1364            }
1365            else
1366            {
1367                str = "" + data;
1368            }
1369
1370            System.out.println("Object data: \"" + str + "\"");
1371
1372            if(str.startsWith("<"))
1373            {
1374                Log.debug("Mozilla DnD detected (preparsing)");
1375                str = str.substring(str.indexOf("\"") + 1);
1376                str = str.substring(0, str.indexOf("\""));
1377                Log.debug("Parsed data: " + str);
1378            }
1379
1380            //str = "[c:\\windows\\test.txt]";
1381
if(str.indexOf("[") >= 0)
1382            {
1383                Log.debug("Windows DnD detected");
1384                name = str.substring(str.indexOf("[") + 1);
1385                name = name.substring(0, name.lastIndexOf("]")); // last was str
1386
}
1387            else if(new String JavaDoc(str).startsWith("file://"))
1388            {
1389                name = str.substring(7);
1390                Log.debug("File URL DnD detected: " + name);
1391            }
1392
1393            if(!new File JavaDoc(name).exists())
1394            {
1395                System.out.println("No file string in clipboard: " + name);
1396
1397                return;
1398            }
1399
1400            System.out.println("DnD file: " + name);
1401
1402            File JavaDoc[] f1 = new File JavaDoc[1];
1403            f1[0] = new File JavaDoc(name);
1404
1405            draggedTransfer(f1, name);
1406        }
1407    }
1408
1409    private void draggedTransfer(File JavaDoc[] f, String JavaDoc name)
1410    {
1411        if((f.length == 1) && f[0].isFile())
1412        {
1413            String JavaDoc path = "";
1414
1415            if(name.indexOf("/") >= 0)
1416            {
1417                path = name.substring(0, name.lastIndexOf("/") + 1);
1418                name = name.substring(name.lastIndexOf("/") + 1);
1419            }
1420
1421            Log.debug("DnD: " + path + " -> " + name);
1422
1423            //TODO: parse "\\"
1424
if(!path.trim().equals(""))
1425            {
1426                ((LocalDir) localDir).chdir(path);
1427            }
1428
1429            ((LocalDir) localDir).startTransfer(new DirEntry(name,
1430                                                             ((ActionListener JavaDoc) localDir)));
1431        }
1432        else
1433        {
1434            Log.debug("Dragging multiple files or dirs is not yet supported.");
1435        }
1436    }
1437
1438    class DTListener implements DropTargetListener JavaDoc
1439    {
1440        public void dragEnter(DropTargetDragEvent JavaDoc e)
1441        {
1442            e.acceptDrag(JFtp.acceptableActions);
1443        }
1444
1445        public void dragOver(DropTargetDragEvent JavaDoc e)
1446        {
1447            e.acceptDrag(JFtp.acceptableActions);
1448        }
1449
1450        public void dropActionChanged(DropTargetDragEvent JavaDoc e)
1451        {
1452            e.acceptDrag(JFtp.acceptableActions);
1453        }
1454
1455        public void dragExit(DropTargetEvent JavaDoc e)
1456        {
1457        }
1458
1459        public void drop(DropTargetDropEvent JavaDoc e)
1460        {
1461            try
1462            {
1463                handleDrop(e, e.getTransferable());
1464
1465                e.dropComplete(true);
1466                UpdateDaemon.updateRemoteDir();
1467            }
1468            catch(Throwable JavaDoc t)
1469            {
1470                t.printStackTrace();
1471                e.dropComplete(false);
1472
1473                return;
1474            }
1475        }
1476    }
1477}
1478
Popular Tags