1 16 package net.sf.jftp; 17 18 import java.awt.BorderLayout ; 19 import java.awt.Component ; 20 import java.awt.Dimension ; 21 import java.awt.FlowLayout ; 22 import java.awt.Image ; 23 import java.awt.Point ; 24 import java.awt.Toolkit ; 25 import java.awt.datatransfer.DataFlavor ; 26 import java.awt.datatransfer.Transferable ; 27 import java.awt.dnd.DnDConstants ; 28 import java.awt.dnd.DropTarget ; 29 import java.awt.dnd.DropTargetDragEvent ; 30 import java.awt.dnd.DropTargetDropEvent ; 31 import java.awt.dnd.DropTargetEvent ; 32 import java.awt.dnd.DropTargetListener ; 33 import java.awt.event.ActionListener ; 34 import java.awt.event.ComponentEvent ; 35 import java.awt.event.ComponentListener ; 36 import java.awt.event.WindowEvent ; 37 import java.awt.event.WindowListener ; 38 import java.io.BufferedInputStream ; 39 import java.io.BufferedOutputStream ; 40 import java.io.DataInputStream ; 41 import java.io.File ; 42 import java.io.FileOutputStream ; 43 import java.io.PrintWriter ; 44 import java.io.Reader ; 45 import java.io.StringWriter ; 46 import java.net.URL ; 47 import java.util.Hashtable ; 48 49 import javax.swing.JDesktopPane ; 50 import javax.swing.JFrame ; 51 import javax.swing.JInternalFrame ; 52 import javax.swing.JLabel ; 53 import javax.swing.JPanel ; 54 import javax.swing.JScrollBar ; 55 import javax.swing.JScrollPane ; 56 import javax.swing.JSplitPane ; 57 import javax.swing.JTabbedPane ; 58 import javax.swing.JTextArea ; 59 import javax.swing.JToolBar ; 60 import javax.swing.SwingUtilities ; 61 import javax.swing.UIManager ; 62 import javax.swing.event.ChangeEvent ; 63 import javax.swing.event.ChangeListener ; 64 import javax.swing.event.InternalFrameEvent ; 65 import javax.swing.event.InternalFrameListener ; 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 implements WindowListener , ComponentListener , 97 Logger, ChangeListener , 98 InternalFrameListener 99 { 100 public static boolean mainUsed = false; 101 public static StatusPanel statusP; 102 public static JLabel statusL = new JLabel ("Welcome to JFtp... "); 103 public static JFrame 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 private static ConnectionHandler defaultConnectionHandler = new ConnectionHandler(); 113 public static JDesktopPane desktop = new JDesktopPane (); 114 private static JScrollPane logSp; 115 public static JTextArea log; 116 117 public static AppMenuBar menuBar = null; 119 public static final int CAPACITY = 9; public static final int CONNECTION_DATA_LENGTH = 10; 121 public static DropTarget dropTarget; 122 public static DropTargetListener dtListener; 123 public static int acceptableActions = DnDConstants.ACTION_COPY; 124 private LogFlusher flusher; 125 private boolean initSize = true; 126 public JTabbedPane remoteConnectionPanel = new JTabbedPane (); 127 public JTabbedPane localConnectionPanel = new JTabbedPane (); 128 private String oldText = ""; 129 private HDesktopBackground background; 130 131 132 private JSplitPane workP = null; 133 134 135 private JSplitPane logP = null; 136 private JInternalFrame j1; 137 private JInternalFrame j2; 138 private JInternalFrame j3; 139 private JInternalFrame j4; 140 private JInternalFrame j5; 141 private Hashtable internalFrames = new Hashtable (); 142 public HostChooser hc; 143 private String buffer = ""; 144 private long oldtime = 0; 145 private UpdateDaemon daemon; 146 public RSSFeeder feeder; 147 148 public JFtp() 150 { 151 Log.setLogger(this); 152 153 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 (this, this.acceptableActions, dtListener, 173 true); 174 175 setLayout(new BorderLayout ()); 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 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 ((int) (width / 2.5), 198 (int) (height * 0.20))); 199 dList.setPreferredSize(new Dimension ((int) (width / 2.5), 200 (int) (height * 0.25))); 201 dList.setSize(new Dimension ((int) (width / 2.5), (int) (height * 0.25))); 202 203 desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); 204 addBackgroundImage(); 205 206 j1 = new JInternalFrame ("Local filesystem", true, false, true, true); 207 j1.setMinimumSize(new Dimension (300, 300)); 208 j1.setLocation(5, 5); 209 localConnectionPanel.addTab("file://", null, (Component ) 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 (400, j1.getSize().height)); 220 j1.show(); 221 222 j2 = new JInternalFrame ("Remote connections", true, false, true, true); 223 j2.setLocation(430, 5); 224 remoteConnectionPanel.addTab("file://", null, (Component ) 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 (415, j1.getSize().height)); 234 j2.show(); 235 236 log = new JTextArea (); 237 log.setBackground(GUIDefaults.light); 238 log.setForeground(GUIDefaults.front); 239 log.setEditable(false); 240 logSp = new JScrollPane (log); 241 logSp.setSize(new Dimension (438, 148)); 242 243 j5 = new JInternalFrame ("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 (440, 150)); 249 j5.show(); 250 251 j3 = new JInternalFrame ("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 (440, 150)); 259 j3.show(); 260 261 j4 = new JInternalFrame ("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 (380, 150)); 267 j4.show(); 268 269 j1.toFront(); 270 j2.toFront(); 271 272 add("Center", desktop); 273 274 JToolBar j = new JToolBar (); 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 (log, 0)); 288 289 restoreInternalPositions(); 290 validate(); 291 setVisible(true); 292 293 if(!mainUsed) 294 { 295 SwingUtilities.invokeLater(new Runnable () 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 getHost() 324 { 325 return statusP.getHost(); 326 } 327 328 public static void setHost(String 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 f, String desc) { 367 String x = Settings.getProperty("jftp.iframes."+desc+".x"); 368 String y = Settings.getProperty("jftp.iframes."+desc+".y"); 369 String w = Settings.getProperty("jftp.iframes."+desc+".width"); 370 String 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 ex) { 380 Log.out("Can not set internal fram position for: "+desc); 381 ex.printStackTrace(); 382 } 383 } 384 385 private void saveInternalPosition(JInternalFrame f, String desc) { 386 Point 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 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 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 e) 455 { 456 } 457 458 public void windowActivated(WindowEvent e) 459 { 460 } 461 462 public void windowDeactivated(WindowEvent e) 463 { 464 } 465 466 public void windowIconified(WindowEvent e) 467 { 468 } 469 470 public void windowDeiconified(WindowEvent e) 471 { 472 } 473 474 public void windowOpened(WindowEvent e) 475 { 476 } 477 478 public void componentHidden(ComponentEvent e) 479 { 480 } 481 482 public void componentMoved(ComponentEvent e) 483 { 484 } 485 486 public void componentShown(ComponentEvent e) 487 { 488 } 489 490 public void componentResized(ComponentEvent 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 (Integer.MIN_VALUE)); 506 } 507 catch(Exception ex) 508 { 509 Log.out(Settings.background + " missing, no background image used"); 510 } 511 } 512 513 private static void compile() 514 { 515 529 } 530 531 private static void setSocksProxyOptions(String proxy, String port) 532 { 533 if(proxy.equals("") || port.equals("")) 534 { 535 return; 536 } 537 538 java.util.Properties sysprops = System.getProperties(); 539 540 sysprops.remove("socksProxyHost"); 542 sysprops.remove("socksProxyPort"); 543 544 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 [] 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 String tmp = Settings.getLookAndFeel(); 580 581 if(tmp != null) 583 { 584 jftp.setLookAndFeel(Settings.getLookAndFeel()); 585 } 586 else 587 { 588 jftp.setLookAndFeel("net.sourceforge.mlf.metouia.MetouiaLookAndFeel"); 589 } 590 591 598 602 if((Settings.getLookAndFeel() == null) || 603 !Settings.getLookAndFeel().equals("com.incors.plaf.kunststoff.KunststoffLookAndFeel")) 604 { 605 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 cnfe) 621 { 622 } 623 } 624 625 629 if((Settings.getLookAndFeel() == null) || 630 !Settings.getLookAndFeel().equals("net.sourceforge.mlf.metouia.MetouiaLookAndFeel")) 631 { 632 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 cnfe) 648 { 649 } 650 } 651 652 676 mainFrame = new JFrame (); 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 icon = HImage.getImage(jftp, Settings.iconImage); 685 mainFrame.setIconImage(icon); 686 mainFrame.setFont(GUIDefaults.font); 687 688 menuBar = new AppMenuBar(jftp); 691 mainFrame.setJMenuBar(menuBar); 692 693 mainFrame.getContentPane().setLayout(new BorderLayout ()); 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 } 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 ex) { 717 ex.printStackTrace(); 718 } 719 } 720 721 private void log(String 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 bar = logSp.getVerticalScrollBar(); 759 bar.setValue(bar.getMaximum()); 760 761 773 repaint(); 774 revalidate(); 775 } 776 777 private void logRaw(String msg) 778 { 779 log.append(" " + msg); 780 Log.out("NOTE: logRaw called"); 781 paintImmediately(0, 0, getSize().width, getSize().height); 782 783 } 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 msg, Throwable throwable) 794 { 795 PrintWriter p = new PrintWriter (new StringWriter ()); 796 throwable.printStackTrace(p); 797 log(msg); 798 log(p.toString()); 799 } 800 801 public void debug(String msg) 802 { 803 log(msg + "\n"); 804 } 805 806 public void debugRaw(String msg) 807 { 808 logRaw(msg); 809 } 810 811 public void debug(String msg, Throwable throwable) 812 { 813 log(msg, throwable); 814 } 815 816 public void warn(String msg) 817 { 818 log(msg); 819 } 820 821 public void warn(String msg, Throwable throwable) 822 { 823 log(msg, throwable); 824 } 825 826 public void error(String msg) 827 { 828 log(msg); 829 } 830 831 public void error(String msg, Throwable throwable) 832 { 833 log(msg, throwable); 834 } 835 836 public void info(String msg) 837 { 838 log(msg); 839 } 840 841 public void info(String msg, Throwable throwable) 842 { 843 log(msg, throwable); 844 } 845 846 public void fatal(String msg) 847 { 848 log(msg); 849 } 850 851 public void fatal(String msg, Throwable throwable) 852 { 853 log(msg, throwable); 854 } 855 856 public void debugSize(int size, boolean recv, boolean last, String file) 857 { 858 } 859 860 public static String getVersion() 861 { 862 try 863 { 864 URL u = ClassLoader.getSystemResource(Settings.readme); 865 866 if(u == null) 867 { 868 u = HImage.class.getResource("/" + Settings.readme); 869 } 870 871 DataInputStream i = new DataInputStream (u.openStream()); 872 String 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 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 u = ClassLoader.getSystemResource(Settings.readme); 892 893 if(u == null) 894 { 895 u = HImage.class.getResource("/" + Settings.readme); 896 } 897 898 DataInputStream i = new DataInputStream (u.openStream()); 899 String 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 nextVersion = "jftp-1."; 907 908 if(x < 10) 909 { 910 nextVersion = nextVersion + "0"; 911 } 912 913 nextVersion = nextVersion + x + ".tar.gz"; 914 915 File dl = new File (nextVersion); 917 918 if(!dl.exists() || (dl.length() <= 0)) 919 { 920 URL url = new URL ("http://osdn.dl.sourceforge.net/sourceforge/j-ftp/" + 921 nextVersion); 922 BufferedOutputStream f = new BufferedOutputStream (new FileOutputStream (dl)); 923 BufferedInputStream in = new BufferedInputStream (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 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 } 957 catch(Exception ex) 958 { 959 } 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 LocalIO.pause(200); 984 repaint(); 985 986 993 } 994 995 public void ensureLogging() 996 { 997 if(buffer.equals("")) 998 { 999 JScrollBar 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 1017 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 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.invokeLater(new Runnable () 1058 { 1059 public void run() 1060 { 1061 SwingUtilities.updateComponentTreeUI(mainFrame); 1062 SwingUtilities.updateComponentTreeUI(JFtp.statusP); 1063 } 1064 }); 1065 } 1066 } 1067 catch(Exception ex) 1068 { 1069 Log.debug("Error setting look and feel: " + ex); 1070 } 1071 } 1072 1073 public void addConnection(String 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 ) tmp, 1084 "Switch to: " + parse(name)); 1085 remoteConnectionPanel.setSelectedIndex(x + 1); 1086 j2.setClosable(true); 1087 } 1088 1089 public void addLocalConnection(String 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 ) tmp, 1100 "Switch to: " + parse(name)); 1101 localConnectionPanel.setSelectedIndex(x + 1); 1102 j1.setClosable(true); 1103 } 1104 1105 private String parse(String 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 e) 1118 { 1119 remoteDir = (Dir) remoteConnectionPanel.getSelectedComponent(); 1120 localDir = (Dir) localConnectionPanel.getSelectedComponent(); 1121 remoteDir.getCon().setLocalPath(localDir.getPath()); 1122 1123 } 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 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 title, Component c, int w, int h) 1173 { 1174 JInternalFrame jt = new JInternalFrame (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 (w, h)); 1192 jt.show(); 1193 } 1194 1195 public void removeFromDesktop(int component) 1196 { 1197 JInternalFrame f = (JInternalFrame ) internalFrames.get("" + component); 1198 1199 if(f != null) 1200 { 1201 f.dispose(); 1202 1203 } 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 f = (JInternalFrame ) 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 f = (JInternalFrame ) 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 public static void updateMenuBar() 1241 { 1242 menuBar.resetFileItems(); 1244 } 1245 1246 public void internalFrameClosing(InternalFrameEvent 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 e) 1259 { 1260 } 1261 1262 public void internalFrameClosed(InternalFrameEvent e) 1263 { 1264 } 1265 1266 public void internalFrameDeactivated(InternalFrameEvent e) 1267 { 1268 } 1269 1270 public void internalFrameDeiconified(InternalFrameEvent e) 1271 { 1272 } 1273 1274 public void internalFrameIconified(InternalFrameEvent e) 1275 { 1276 } 1277 1278 public void internalFrameOpened(InternalFrameEvent 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 ex) 1291 { 1292 ex.printStackTrace(); 1293 } 1294 } 1295 1296 public void handleDrop(DropTargetDropEvent e, Transferable t) 1297 throws Exception 1298 { 1299 System.out.println("Starting dropAttempt"); 1300 1301 DataFlavor chosen = DataFlavor.javaFileListFlavor; 1302 DataFlavor second = FileTransferable.plainTextFlavor; 1303 DataFlavor flavor = null; 1304 Object data = null; 1305 1306 if(e != null) 1307 { 1308 flavor = e.getCurrentDataFlavors()[0]; 1309 e.acceptDrop(acceptableActions); 1310 1311 Class c = flavor.getDefaultRepresentationClass(); 1312 } 1313 1314 if(flavor == null) 1315 { 1316 flavor = second; 1317 } 1318 1319 String name = ""; 1320 1321 if(t.isDataFlavorSupported(chosen)) 1322 { 1323 System.out.println("Using List DnD style"); 1324 1325 java.util.List myList = (java.util.List ) t.getTransferData(chosen); 1326 1327 File [] f = (File []) 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 str = ""; 1344 int i = 0; 1345 1346 if(data instanceof Reader ) 1347 { 1348 int c; 1349 1350 while((c = ((Reader ) data).read()) != -1) 1351 { 1352 if(((i == 1) && (c == 0))) 1353 { 1354 i = -1; 1356 } 1357 else 1358 { 1359 str = str + new Character ((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 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("]")); } 1387 else if(new String (str).startsWith("file://")) 1388 { 1389 name = str.substring(7); 1390 Log.debug("File URL DnD detected: " + name); 1391 } 1392 1393 if(!new File (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 [] f1 = new File [1]; 1403 f1[0] = new File (name); 1404 1405 draggedTransfer(f1, name); 1406 } 1407 } 1408 1409 private void draggedTransfer(File [] f, String name) 1410 { 1411 if((f.length == 1) && f[0].isFile()) 1412 { 1413 String 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 if(!path.trim().equals("")) 1425 { 1426 ((LocalDir) localDir).chdir(path); 1427 } 1428 1429 ((LocalDir) localDir).startTransfer(new DirEntry(name, 1430 ((ActionListener ) 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 1439 { 1440 public void dragEnter(DropTargetDragEvent e) 1441 { 1442 e.acceptDrag(JFtp.acceptableActions); 1443 } 1444 1445 public void dragOver(DropTargetDragEvent e) 1446 { 1447 e.acceptDrag(JFtp.acceptableActions); 1448 } 1449 1450 public void dropActionChanged(DropTargetDragEvent e) 1451 { 1452 e.acceptDrag(JFtp.acceptableActions); 1453 } 1454 1455 public void dragExit(DropTargetEvent e) 1456 { 1457 } 1458 1459 public void drop(DropTargetDropEvent e) 1460 { 1461 try 1462 { 1463 handleDrop(e, e.getTransferable()); 1464 1465 e.dropComplete(true); 1466 UpdateDaemon.updateRemoteDir(); 1467 } 1468 catch(Throwable t) 1469 { 1470 t.printStackTrace(); 1471 e.dropComplete(false); 1472 1473 return; 1474 } 1475 } 1476 } 1477} 1478 | Popular Tags |