0001: /*
0002: * This program is free software; you can redistribute it and/or
0003: * modify it under the terms of the GNU General Public License
0004: * as published by the Free Software Foundation; either version 2
0005: * of the License, or (at your option) any later version.
0006: *
0007: * This program is distributed in the hope that it will be useful,
0008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0010: * GNU General Public License for more details.
0011:
0012: * You should have received a copy of the GNU General Public License
0013: * along with this program; if not, write to the Free Software
0014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
0015: */
0016: package net.sf.jftp;
0017:
0018: import java.awt.BorderLayout;
0019: import java.awt.Component;
0020: import java.awt.Dimension;
0021: import java.awt.FlowLayout;
0022: import java.awt.Image;
0023: import java.awt.Point;
0024: import java.awt.Toolkit;
0025: import java.awt.datatransfer.DataFlavor;
0026: import java.awt.datatransfer.Transferable;
0027: import java.awt.dnd.DnDConstants;
0028: import java.awt.dnd.DropTarget;
0029: import java.awt.dnd.DropTargetDragEvent;
0030: import java.awt.dnd.DropTargetDropEvent;
0031: import java.awt.dnd.DropTargetEvent;
0032: import java.awt.dnd.DropTargetListener;
0033: import java.awt.event.ActionListener;
0034: import java.awt.event.ComponentEvent;
0035: import java.awt.event.ComponentListener;
0036: import java.awt.event.WindowEvent;
0037: import java.awt.event.WindowListener;
0038: import java.io.BufferedInputStream;
0039: import java.io.BufferedOutputStream;
0040: import java.io.DataInputStream;
0041: import java.io.File;
0042: import java.io.FileOutputStream;
0043: import java.io.PrintWriter;
0044: import java.io.Reader;
0045: import java.io.StringWriter;
0046: import java.net.URL;
0047: import java.util.Hashtable;
0048:
0049: import javax.swing.JDesktopPane;
0050: import javax.swing.JFrame;
0051: import javax.swing.JInternalFrame;
0052: import javax.swing.JLabel;
0053: import javax.swing.JPanel;
0054: import javax.swing.JScrollBar;
0055: import javax.swing.JScrollPane;
0056: import javax.swing.JSplitPane;
0057: import javax.swing.JTabbedPane;
0058: import javax.swing.JTextArea;
0059: import javax.swing.JToolBar;
0060: import javax.swing.SwingUtilities;
0061: import javax.swing.UIManager;
0062: import javax.swing.event.ChangeEvent;
0063: import javax.swing.event.ChangeListener;
0064: import javax.swing.event.InternalFrameEvent;
0065: import javax.swing.event.InternalFrameListener;
0066: import javax.swing.plaf.basic.BasicInternalFrameTitlePane.RestoreAction;
0067:
0068: import net.sf.jftp.config.Settings;
0069: import net.sf.jftp.gui.base.AppMenuBar;
0070: import net.sf.jftp.gui.base.DownloadList;
0071: import net.sf.jftp.gui.base.DownloadQueue;
0072: import net.sf.jftp.gui.base.LocalDir;
0073: import net.sf.jftp.gui.base.LogFlusher;
0074: import net.sf.jftp.gui.base.RemoteDir;
0075: import net.sf.jftp.gui.base.StatusPanel;
0076: import net.sf.jftp.gui.base.dir.Dir;
0077: import net.sf.jftp.gui.base.dir.DirEntry;
0078: import net.sf.jftp.gui.framework.FileTransferable;
0079: import net.sf.jftp.gui.framework.GUIDefaults;
0080: import net.sf.jftp.gui.framework.HDesktopBackground;
0081: import net.sf.jftp.gui.framework.HImage;
0082: import net.sf.jftp.gui.hostchooser.HostChooser;
0083: import net.sf.jftp.gui.tasks.HostInfo;
0084: import net.sf.jftp.net.BasicConnection;
0085: import net.sf.jftp.net.ConnectionHandler;
0086: import net.sf.jftp.net.ConnectionListener;
0087: import net.sf.jftp.net.FilesystemConnection;
0088: import net.sf.jftp.net.FtpConnection;
0089: import net.sf.jftp.system.LocalIO;
0090: import net.sf.jftp.system.UpdateDaemon;
0091: import net.sf.jftp.system.logging.Log;
0092: import net.sf.jftp.system.logging.Logger;
0093: import net.sf.jftp.tools.RSSFeeder;
0094:
0095: public class JFtp extends JPanel implements WindowListener,
0096: ComponentListener, Logger, ChangeListener,
0097: InternalFrameListener {
0098: public static boolean mainUsed = false;
0099: public static StatusPanel statusP;
0100: public static JLabel statusL = new JLabel(
0101: "Welcome to JFtp... ");
0102: public static JFrame mainFrame;
0103: public static Dir localDir;
0104: public static Dir remoteDir;
0105: public static DownloadList dList = new DownloadList();
0106: public static DownloadQueue dQueue = new DownloadQueue();
0107: public static boolean uiBlocked = false;
0108: public static HostInfo hostinfo = new HostInfo();
0109:
0110: //public static BasicConnection controlConnection = null;
0111: private static ConnectionHandler defaultConnectionHandler = new ConnectionHandler();
0112: public static JDesktopPane desktop = new JDesktopPane();
0113: private static JScrollPane logSp;
0114: public static JTextArea log;
0115:
0116: //***appMenuBar: this must now be a public object that JFtp refers to
0117: public static AppMenuBar menuBar = null;
0118: public static final int CAPACITY = 9; //number of connections remembered
0119: public static final int CONNECTION_DATA_LENGTH = 10;
0120: public static DropTarget dropTarget;
0121: public static DropTargetListener dtListener;
0122: public static int acceptableActions = DnDConstants.ACTION_COPY;
0123: private LogFlusher flusher;
0124: private boolean initSize = true;
0125: public JTabbedPane remoteConnectionPanel = new JTabbedPane();
0126: public JTabbedPane localConnectionPanel = new JTabbedPane();
0127: private String oldText = "";
0128: private HDesktopBackground background;
0129:
0130: /** JSplitPane that holds the directory panes and the log/dl JSplitPane */
0131: private JSplitPane workP = null;
0132:
0133: /** JSplitPane that holds the log download parts */
0134: private JSplitPane logP = null;
0135: private JInternalFrame j1;
0136: private JInternalFrame j2;
0137: private JInternalFrame j3;
0138: private JInternalFrame j4;
0139: private JInternalFrame j5;
0140: private Hashtable internalFrames = new Hashtable();
0141: public HostChooser hc;
0142: private String buffer = "";
0143: private long oldtime = 0;
0144: private UpdateDaemon daemon;
0145: public RSSFeeder feeder;
0146:
0147: //***
0148: public JFtp() {
0149: Log.setLogger(this );
0150:
0151: // we have jesktop-environment
0152: if (statusP != null) {
0153: statusP.remove(statusP.close);
0154: }
0155:
0156: init();
0157: }
0158:
0159: public JFtp(boolean mainUsed) {
0160: Log.setLogger(this );
0161: this .mainUsed = mainUsed;
0162: init();
0163: }
0164:
0165: public void init() {
0166: dtListener = new DTListener();
0167: dropTarget = new DropTarget(this , this .acceptableActions,
0168: dtListener, true);
0169:
0170: setLayout(new BorderLayout());
0171:
0172: setBackground(GUIDefaults.mainBack);
0173: setForeground(GUIDefaults.front);
0174:
0175: statusP = new StatusPanel(this );
0176: add("North", statusP);
0177:
0178: localDir = (Dir) new LocalDir(Settings.defaultWorkDir);
0179: localDir.setDownloadList(dList);
0180:
0181: remoteDir = (Dir) new RemoteDir();
0182: remoteDir.setDownloadList(dList);
0183: desktop.setDropTarget(this .dropTarget);
0184:
0185: Dimension d = Settings.getWindowSize();
0186: setPreferredSize(d);
0187: setSize(d);
0188:
0189: int width = (int) d.getWidth();
0190: int height = (int) d.getHeight();
0191:
0192: dList.setMinimumSize(new Dimension((int) (width / 2.5),
0193: (int) (height * 0.20)));
0194: dList.setPreferredSize(new Dimension((int) (width / 2.5),
0195: (int) (height * 0.25)));
0196: dList.setSize(new Dimension((int) (width / 2.5),
0197: (int) (height * 0.25)));
0198:
0199: desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
0200: addBackgroundImage();
0201:
0202: j1 = new JInternalFrame("Local filesystem", true, false, true,
0203: true);
0204: j1.setMinimumSize(new Dimension(300, 300));
0205: j1.setLocation(5, 5);
0206: localConnectionPanel.addTab("file://", null,
0207: (Component) localDir, "Filesystem");
0208: localConnectionPanel.setSelectedIndex(0);
0209: localConnectionPanel.addChangeListener(this );
0210: j1.getContentPane().add(localConnectionPanel);
0211: localDir.fresh();
0212: desktop.add(j1);
0213: j1.setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
0214: j1.addInternalFrameListener(this );
0215: j1.pack();
0216: j1.setSize(new Dimension(400, j1.getSize().height));
0217: j1.show();
0218:
0219: j2 = new JInternalFrame("Remote connections", true, false,
0220: true, true);
0221: j2.setLocation(430, 5);
0222: remoteConnectionPanel.addTab("file://", null,
0223: (Component) remoteDir, "Filesystem");
0224: remoteConnectionPanel.setSelectedIndex(0);
0225: remoteConnectionPanel.addChangeListener(this );
0226: j2.getContentPane().add(remoteConnectionPanel);
0227: desktop.add(j2);
0228: j2.setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
0229: j2.addInternalFrameListener(this );
0230: j2.pack();
0231: j2.setSize(new Dimension(415, j1.getSize().height));
0232: j2.show();
0233:
0234: log = new JTextArea();
0235: log.setBackground(GUIDefaults.light);
0236: log.setForeground(GUIDefaults.front);
0237: log.setEditable(false);
0238: logSp = new JScrollPane(log);
0239: logSp.setSize(new Dimension(438, 148));
0240:
0241: j5 = new JInternalFrame("Queue System", true, false, true, true);
0242: j5.setLocation(5, 400);
0243: j5.getContentPane().add(dQueue, BorderLayout.CENTER);
0244: desktop.add(j5);
0245: j5.pack();
0246: j5.setSize(new Dimension(440, 150));
0247: j5.show();
0248:
0249: j3 = new JInternalFrame("Log", true, false, true, true);
0250:
0251: int x = (int) (desktop.getSize().width / 2);
0252: j3.setLocation(5, 425);
0253: j3.getContentPane().add(logSp, BorderLayout.CENTER);
0254: desktop.add(j3);
0255: j3.pack();
0256: j3.setSize(new Dimension(440, 150));
0257: j3.show();
0258:
0259: j4 = new JInternalFrame("Download Manager", true, false, true,
0260: true);
0261: j4.setLocation(450, 425);
0262: j4.getContentPane().add(dList, BorderLayout.CENTER);
0263: desktop.add(j4);
0264: j4.pack();
0265: j4.setSize(new Dimension(380, 150));
0266: j4.show();
0267:
0268: j1.toFront();
0269: j2.toFront();
0270:
0271: add("Center", desktop);
0272:
0273: JToolBar j = new JToolBar();
0274: j.setFloatable(false);
0275: j.add(statusP.status, FlowLayout.LEFT);
0276:
0277: if (Settings.getEnableRSS()) {
0278: feeder = new RSSFeeder();
0279: j.add(feeder);
0280: }
0281:
0282: add("South", j);
0283:
0284: addComponentListener(this );
0285: componentResized(new ComponentEvent(log, 0));
0286:
0287: restoreInternalPositions();
0288: validate();
0289: setVisible(true);
0290:
0291: if (!mainUsed) {
0292: SwingUtilities.invokeLater(new Runnable() {
0293: public void run() {
0294: chooseHost();
0295: }
0296: });
0297: } else {
0298: chooseHost();
0299: }
0300:
0301: flusher = new LogFlusher();
0302: daemon = new UpdateDaemon(this );
0303:
0304: }
0305:
0306: protected void chooseHost() {
0307: hc = new HostChooser(this );
0308:
0309: if (!mainUsed) {
0310: hc.update();
0311: }
0312: }
0313:
0314: public static String getHost() {
0315: return statusP.getHost();
0316: }
0317:
0318: public static void setHost(String which) {
0319: statusP.setHost(which);
0320: }
0321:
0322: public static void localUpdate() {
0323: localDir.fresh();
0324: }
0325:
0326: public static void remoteUpdate() {
0327: remoteDir.fresh();
0328: }
0329:
0330: private void saveInternalPositions() {
0331: saveInternalPosition(j1, "local");
0332: saveInternalPosition(j2, "remote");
0333: saveInternalPosition(j5, "queue");
0334: saveInternalPosition(j3, "log");
0335: saveInternalPosition(j4, "manager");
0336: }
0337:
0338: private void restoreInternalPositions() {
0339: if (Settings.getProperty("jftp.iframes.resize").equals("false")) {
0340:
0341: } else {
0342: restoreInternalPosition(j1, "local");
0343: restoreInternalPosition(j2, "remote");
0344: restoreInternalPosition(j5, "queue");
0345: restoreInternalPosition(j3, "log");
0346: restoreInternalPosition(j4, "manager");
0347: }
0348: }
0349:
0350: private void restoreInternalPosition(JInternalFrame f, String desc) {
0351: String x = Settings.getProperty("jftp.iframes." + desc + ".x");
0352: String y = Settings.getProperty("jftp.iframes." + desc + ".y");
0353: String w = Settings.getProperty("jftp.iframes." + desc
0354: + ".width");
0355: String h = Settings.getProperty("jftp.iframes." + desc
0356: + ".height");
0357:
0358: if (x.indexOf(".") >= 0)
0359: x = x.substring(0, x.indexOf("."));
0360: if (y.indexOf(".") >= 0)
0361: y = y.substring(0, y.indexOf("."));
0362:
0363: try {
0364: f.setLocation(Integer.parseInt(x), Integer.parseInt(y));
0365: f.setSize(Integer.parseInt(w), Integer.parseInt(h));
0366: } catch (Exception ex) {
0367: Log.out("Can not set internal fram position for: " + desc);
0368: ex.printStackTrace();
0369: }
0370: }
0371:
0372: private void saveInternalPosition(JInternalFrame f, String desc) {
0373: Point p = f.getLocation();
0374:
0375: Settings.setProperty("jftp.iframes." + desc + ".x", ""
0376: + p.getX());
0377: Settings.setProperty("jftp.iframes." + desc + ".y", ""
0378: + p.getY());
0379: Settings.setProperty("jftp.iframes." + desc + ".width", f
0380: .getWidth());
0381: Settings.setProperty("jftp.iframes." + desc + ".height", f
0382: .getHeight());
0383: }
0384:
0385: public void windowClosing(WindowEvent e) {
0386: saveInternalPositions();
0387:
0388: Settings.setProperty("jftp.window.width", this .getWidth());
0389: Settings.setProperty("jftp.window.height", this .getHeight());
0390:
0391: if (!mainUsed) {
0392: Settings.setProperty("jftp.window.x", (int) this
0393: .getLocationOnScreen().getX());
0394: Settings.setProperty("jftp.window.y", (int) this
0395: .getLocationOnScreen().getY());
0396: } else {
0397: Settings.setProperty("jftp.window.x", (int) mainFrame
0398: .getLocationOnScreen().getX());
0399: Settings.setProperty("jftp.window.y", (int) mainFrame
0400: .getLocationOnScreen().getY());
0401: }
0402:
0403: Settings.save();
0404: safeDisconnect();
0405:
0406: if (Settings.isStandalone) {
0407: System.exit(0);
0408: } else {
0409: mainFrame.dispose();
0410: }
0411: }
0412:
0413: public static void safeDisconnect() {
0414: BasicConnection con = remoteDir.getCon();
0415:
0416: if ((con != null) && con.isConnected()) {
0417: try {
0418: con.disconnect();
0419: } catch (Exception ex) {
0420: }
0421: }
0422:
0423: FilesystemConnection c = new FilesystemConnection();
0424: c.addConnectionListener((ConnectionListener) remoteDir);
0425: remoteDir.setCon(c);
0426:
0427: if (!c.chdir("/")) {
0428: c.chdir("C:\\");
0429: }
0430: }
0431:
0432: public void windowClosed(WindowEvent e) {
0433: }
0434:
0435: public void windowActivated(WindowEvent e) {
0436: }
0437:
0438: public void windowDeactivated(WindowEvent e) {
0439: }
0440:
0441: public void windowIconified(WindowEvent e) {
0442: }
0443:
0444: public void windowDeiconified(WindowEvent e) {
0445: }
0446:
0447: public void windowOpened(WindowEvent e) {
0448: }
0449:
0450: public void componentHidden(ComponentEvent e) {
0451: }
0452:
0453: public void componentMoved(ComponentEvent e) {
0454: }
0455:
0456: public void componentShown(ComponentEvent e) {
0457: }
0458:
0459: public void componentResized(ComponentEvent e) {
0460: localDir.actionPerformed(this , "local");
0461: remoteDir.actionPerformed(this , "remote");
0462: desktop.remove(background);
0463: addBackgroundImage();
0464: validate();
0465: }
0466:
0467: public void addBackgroundImage() {
0468: try {
0469: background = new HDesktopBackground(Settings.background,
0470: null);
0471: background.setBounds(0, 0, getSize().width,
0472: getSize().height);
0473: desktop.add(background, new Integer(Integer.MIN_VALUE));
0474: } catch (Exception ex) {
0475: Log.out(Settings.background
0476: + " missing, no background image used");
0477: }
0478: }
0479:
0480: private static void compile() {
0481: /*
0482: try
0483: {
0484: Compiler.enable();
0485: System.out.println(Compiler.compileClass(Class.forName("java.util.Hashtable")));
0486: System.out.println(Compiler.compileClasses("net.sf.jftp"));
0487: System.out.println(Compiler.compileClasses("net.sf.jftp.JFtp"));
0488: System.out.println(Compiler.compileClasses("JFtp"));
0489: }
0490: catch(ClassNotFoundException ex)
0491: {
0492: ex.printStackTrace();
0493: }
0494: */
0495: }
0496:
0497: private static void setSocksProxyOptions(String proxy, String port) {
0498: if (proxy.equals("") || port.equals("")) {
0499: return;
0500: }
0501:
0502: java.util.Properties sysprops = System.getProperties();
0503:
0504: // Remove previous values
0505: sysprops.remove("socksProxyHost");
0506: sysprops.remove("socksProxyPort");
0507:
0508: // Set your values
0509: sysprops.put("socksProxyHost", proxy);
0510: sysprops.put("socksProxyPort", port);
0511:
0512: Log.out("socks proxy: " + sysprops.get("socksProxyHost") + ":"
0513: + sysprops.get("socksProxyPort"));
0514: }
0515:
0516: public static void main(String[] argv) {
0517: try {
0518: long start = System.currentTimeMillis();
0519:
0520: Log.out("starting up jftp...");
0521: compile();
0522: System.setProperty("sshtools.logfile", Settings.appHomeDir
0523: + "log4.txt");
0524:
0525: if (Settings.autoUpdate) {
0526: checkForUpgrade();
0527: }
0528:
0529: Settings.enableResuming = true;
0530: Settings.enableUploadResuming = true;
0531: Settings.noUploadResumingQuestion = false;
0532:
0533: setSocksProxyOptions(Settings.getSocksProxyHost(), Settings
0534: .getSocksProxyPort());
0535:
0536: JFtp jftp = new JFtp(true);
0537: UIManager.getLookAndFeelDefaults().put("ClassLoader",
0538: jftp.getClass().getClassLoader());
0539:
0540: //boolean lookSet = false;
0541: String tmp = Settings.getLookAndFeel();
0542:
0543: //UIManager.installLookAndFeel("Metouia", "net.sourceforge.mlf.metouia.MetouiaLookAndFeel");
0544: if (tmp != null) {
0545: jftp.setLookAndFeel(Settings.getLookAndFeel());
0546: } else {
0547: jftp
0548: .setLookAndFeel("net.sourceforge.mlf.metouia.MetouiaLookAndFeel");
0549: }
0550:
0551: //else
0552: //{
0553: // jftp.setLookAndFeel("com.incors.plaf.kunststoff.KunststoffLookAndFeel");
0554: // lookSet = true;
0555: //}
0556: //if(!lookSet) UIManager.installLookAndFeel("Kunststoff", "com.incors.plaf.kunststoff.KunststoffLookAndFeel");
0557:
0558: /*
0559: * Don't try to add the Kunststoff look and feel if it has
0560: * already been added.
0561: */
0562: if ((Settings.getLookAndFeel() == null)
0563: || !Settings
0564: .getLookAndFeel()
0565: .equals(
0566: "com.incors.plaf.kunststoff.KunststoffLookAndFeel")) {
0567: /*
0568: * Somehow even though UIManager.installLookAndFeel throws a
0569: * ClassNotFoundException, it ends up getting added to the
0570: * list of installed look and feels anyway. What we do here is
0571: * make Java throw a ClassNotFoundException before then by
0572: * checking whether or not the class exists, and only if the
0573: * class exists does it then get added to the list of
0574: * available look and feel's.
0575: */
0576: try {
0577: Class
0578: .forName("com.incors.plaf.kunststoff.KunststoffLookAndFeel");
0579: UIManager
0580: .installLookAndFeel("Kunststoff",
0581: "com.incors.plaf.kunststoff.KunststoffLookAndFeel");
0582: } catch (ClassNotFoundException cnfe) {
0583: }
0584: }
0585:
0586: /*
0587: * Don't try to add the Metouia look and feel if it has
0588: * already been added.
0589: */
0590: if ((Settings.getLookAndFeel() == null)
0591: || !Settings
0592: .getLookAndFeel()
0593: .equals(
0594: "net.sourceforge.mlf.metouia.MetouiaLookAndFeel")) {
0595: /*
0596: * Somehow even though UIManager.installLookAndFeel throws a
0597: * ClassNotFoundException, it ends up getting added to the
0598: * list of installed look and feels anyway. What we do here is
0599: * make Java throw a ClassNotFoundException before then by
0600: * checking whether or not the class exists, and only if the
0601: * class exists does it then get added to the list of
0602: * available look and feel's.
0603: */
0604: try {
0605: Class
0606: .forName("net.sourceforge.mlf.metouia.MetouiaLookAndFeel");
0607: UIManager
0608: .installLookAndFeel("Metouia",
0609: "net.sourceforge.mlf.metouia.MetouiaLookAndFeel");
0610: } catch (ClassNotFoundException cnfe) {
0611: }
0612: }
0613:
0614: /*
0615: try
0616: {
0617: Class.forName("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
0618: UIManager.installLookAndFeel("PlasticXP",
0619: "com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
0620:
0621: Class.forName("com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
0622: UIManager.installLookAndFeel("Plastic3D",
0623: "com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
0624:
0625: Class.forName("com.jgoodies.looks.plastic.PlasticLookAndFeel");
0626: UIManager.installLookAndFeel("Plastic",
0627: "com.jgoodies.looks.plastic.PlasticLookAndFeel");
0628:
0629: Class.forName("com.jgoodies.looks.windows.WindowsLookAndFeel");
0630: UIManager.installLookAndFeel("Windows",
0631: "com.jgoodies.looks.windows.WindowsLookAndFeel");
0632: }
0633: catch(ClassNotFoundException cnfe)
0634: {
0635: ex.printStackTrace();
0636: }
0637: */
0638: mainFrame = new JFrame();
0639: mainFrame.setLocation(Settings.getWindowLocation());
0640:
0641: mainFrame.setTitle(Settings.title + " - Version "
0642: + getVersion());
0643:
0644: mainFrame.setResizable(Settings.resize);
0645: mainFrame.addWindowListener(jftp);
0646:
0647: Image icon = HImage.getImage(jftp, Settings.iconImage);
0648: mainFrame.setIconImage(icon);
0649: mainFrame.setFont(GUIDefaults.font);
0650:
0651: //*** MY CHANGES
0652: //mainFrame.setJMenuBar(new AppMenuBar(jftp));
0653: menuBar = new AppMenuBar(jftp);
0654: mainFrame.setJMenuBar(menuBar);
0655:
0656: //***
0657: mainFrame.getContentPane().setLayout(new BorderLayout());
0658: mainFrame.getContentPane().add("Center", jftp);
0659: SwingUtilities.updateComponentTreeUI(mainFrame);
0660: mainFrame.pack();
0661: mainFrame.validate();
0662: mainFrame.setVisible(true);
0663:
0664: if (argv.length > 0) {
0665: jftp.hc.update(argv[0]);
0666: } else {
0667: //jftp.hc.update();
0668: }
0669:
0670: Log.out("jftp is up and running.");
0671:
0672: long end = System.currentTimeMillis();
0673:
0674: Log.out("startup time: " + (end - start) + "ms.");
0675: } catch (Error ex) {
0676: ex.printStackTrace();
0677: }
0678: }
0679:
0680: private void log(String msg) {
0681: if (msg.startsWith("200") || msg.startsWith("227")) {
0682: if ((msg.indexOf("NOOP") > 0) || (msg.indexOf("Type") > 0)
0683: || (msg.indexOf("MODE") > 0)
0684: || (msg.indexOf("Passive") > 0)) {
0685: if (Settings.hideStatus) {
0686: return;
0687: }
0688: }
0689: } else if (log == null) {
0690: return;
0691: }
0692:
0693: if (!msg.equals("")) {
0694: buffer = buffer + " " + msg;
0695: }
0696:
0697: log.append(buffer);
0698: buffer = "";
0699:
0700: long time = System.currentTimeMillis();
0701:
0702: if (((time - oldtime) < Settings.uiRefresh)) {
0703: UpdateDaemon.updateLog();
0704:
0705: return;
0706: }
0707:
0708: oldtime = time;
0709:
0710: JScrollBar bar = logSp.getVerticalScrollBar();
0711: bar.setValue(bar.getMaximum());
0712:
0713: //logSp.paintImmediately(0, 0, logSp.getSize().width,
0714: // logSp.getSize().height);
0715: /*
0716: SwingUtilities.invokeLater(new Runnable()
0717: {
0718: public void run()
0719: {
0720: repaint();
0721: //paintImmediately(0, 0, getSize().width, getSize().height);
0722: }
0723: });
0724: */
0725: repaint();
0726: revalidate();
0727: }
0728:
0729: private void logRaw(String msg) {
0730: log.append(" " + msg);
0731: Log.out("NOTE: logRaw called");
0732: paintImmediately(0, 0, getSize().width, getSize().height);
0733:
0734: //logSp.paintImmediately(0,0,logSp.getSize().width,logSp.getSize().height);
0735: }
0736:
0737: public void clearLog() {
0738: log.setText("");
0739: logSp.paintImmediately(0, 0, logSp.getSize().width, logSp
0740: .getSize().height);
0741: }
0742:
0743: private void log(String msg, Throwable throwable) {
0744: PrintWriter p = new PrintWriter(new StringWriter());
0745: throwable.printStackTrace(p);
0746: log(msg);
0747: log(p.toString());
0748: }
0749:
0750: public void debug(String msg) {
0751: log(msg + "\n");
0752: }
0753:
0754: public void debugRaw(String msg) {
0755: logRaw(msg);
0756: }
0757:
0758: public void debug(String msg, Throwable throwable) {
0759: log(msg, throwable);
0760: }
0761:
0762: public void warn(String msg) {
0763: log(msg);
0764: }
0765:
0766: public void warn(String msg, Throwable throwable) {
0767: log(msg, throwable);
0768: }
0769:
0770: public void error(String msg) {
0771: log(msg);
0772: }
0773:
0774: public void error(String msg, Throwable throwable) {
0775: log(msg, throwable);
0776: }
0777:
0778: public void info(String msg) {
0779: log(msg);
0780: }
0781:
0782: public void info(String msg, Throwable throwable) {
0783: log(msg, throwable);
0784: }
0785:
0786: public void fatal(String msg) {
0787: log(msg);
0788: }
0789:
0790: public void fatal(String msg, Throwable throwable) {
0791: log(msg, throwable);
0792: }
0793:
0794: public void debugSize(int size, boolean recv, boolean last,
0795: String file) {
0796: }
0797:
0798: public static String getVersion() {
0799: try {
0800: URL u = ClassLoader.getSystemResource(Settings.readme);
0801:
0802: if (u == null) {
0803: u = HImage.class.getResource("/" + Settings.readme);
0804: }
0805:
0806: DataInputStream i = new DataInputStream(u.openStream());
0807: String tmp = i.readLine();
0808: tmp = tmp.substring(tmp.lastIndexOf(">") + 1);
0809: tmp = tmp.substring(0, tmp.indexOf("<"));
0810:
0811: return tmp;
0812: } catch (Exception ex) {
0813: }
0814:
0815: return "";
0816: }
0817:
0818: private static void checkForUpgrade() {
0819: try {
0820: Log.out("Checking for updates...");
0821:
0822: URL u = ClassLoader.getSystemResource(Settings.readme);
0823:
0824: if (u == null) {
0825: u = HImage.class.getResource("/" + Settings.readme);
0826: }
0827:
0828: DataInputStream i = new DataInputStream(u.openStream());
0829: String tmp = i.readLine();
0830: tmp = tmp.substring(tmp.lastIndexOf(">"));
0831: tmp = tmp.substring(tmp.indexOf(".") + 1);
0832: tmp = tmp.substring(0, tmp.indexOf("<"));
0833:
0834: int x = Integer.parseInt(tmp) + 1;
0835:
0836: String nextVersion = "jftp-1.";
0837:
0838: if (x < 10) {
0839: nextVersion = nextVersion + "0";
0840: }
0841:
0842: nextVersion = nextVersion + x + ".tar.gz";
0843:
0844: //System.out.println(nextVersion);
0845: File dl = new File(nextVersion);
0846:
0847: if (!dl.exists() || (dl.length() <= 0)) {
0848: URL url = new URL(
0849: "http://osdn.dl.sourceforge.net/sourceforge/j-ftp/"
0850: + nextVersion);
0851: BufferedOutputStream f = new BufferedOutputStream(
0852: new FileOutputStream(dl));
0853: BufferedInputStream in = new BufferedInputStream(url
0854: .openStream());
0855: byte[] buf = new byte[4096];
0856: int stat = 1;
0857:
0858: Log.out("\ndownloading update: " + dl.getAbsolutePath()
0859: + "\n\n");
0860:
0861: while (stat > 0) {
0862: //while(in.available() <= 0) LocalIO.pause(10);
0863: stat = in.read(buf);
0864:
0865: if (stat == -1) {
0866: break;
0867: }
0868:
0869: f.write(buf, 0, stat);
0870: System.out.print(".");
0871: }
0872:
0873: f.flush();
0874: f.close();
0875: in.close();
0876: }
0877:
0878: Log
0879: .out("\n\n\na newer version was found!\nplease install the File "
0880: + dl.getAbsolutePath()
0881: + " or even better visit the homepage to download the latest version...\n"
0882: + "you can turn this feature off if you don't like it (view readme for details)\n\nStarting anyway...\n\n");
0883:
0884: //LocalIO.pause(5000);
0885: //System.exit(0);
0886: } catch (Exception ex) {
0887: // FileNotFoundException, everything is ok
0888: //ex.printStackTrace();
0889: }
0890:
0891: Log.out("finished check...");
0892: }
0893:
0894: public static ConnectionHandler getConnectionHandler() {
0895: BasicConnection con = remoteDir.getCon();
0896:
0897: if ((con != null) && con instanceof FtpConnection) {
0898: return ((FtpConnection) con).getConnectionHandler();
0899: } else {
0900: return defaultConnectionHandler;
0901: }
0902: }
0903:
0904: public void fireUpdate() {
0905: // if(!(JFtp.remoteDir.getCon() instanceof FilesystemConnection))
0906: LocalIO.pause(200);
0907: repaint();
0908:
0909: /*
0910: else
0911: {
0912: LocalIO.pause(200);
0913: paintImmediately(0,0,getSize().width,getSize().height);
0914: }
0915: */
0916: }
0917:
0918: public void ensureLogging() {
0919: if (buffer.equals("")) {
0920: JScrollBar bar;
0921:
0922: if ((logSp == null)
0923: || ((bar = logSp.getVerticalScrollBar()) == null)
0924: || bar == null
0925: || (bar.getValue() == bar.getMaximum())
0926: || bar.getValueIsAdjusting()) {
0927: return;
0928: } else {
0929: bar.setValue(bar.getMaximum());
0930: logSp.repaint();
0931:
0932: /*
0933: logSp.paintImmediately(0, 0, logSp.getSize().width,
0934: logSp.getSize().height);
0935: */
0936: /*
0937: SwingUtilities.invokeLater(new Runnable()
0938: {
0939: public void run()
0940: {
0941: repaint();
0942: //paintImmediately(0, 0, getSize().width,
0943: // getSize().height);
0944: }
0945: });
0946: */
0947: repaint();
0948: revalidate();
0949: }
0950:
0951: return;
0952: }
0953:
0954: Log.out("flushing log buffer...");
0955: oldtime = 0;
0956: log("");
0957: }
0958:
0959: public void setLookAndFeel(String name) {
0960: if (name == null) {
0961: return;
0962: }
0963:
0964: try {
0965: UIManager.setLookAndFeel(name);
0966:
0967: if (mainFrame != null) {
0968: //SwingUtilities.updateComponentTreeUI(mainFrame);
0969: //invalidate();
0970: //validate();
0971: //repaint();
0972: SwingUtilities.invokeLater(new Runnable() {
0973: public void run() {
0974: SwingUtilities.updateComponentTreeUI(mainFrame);
0975: SwingUtilities
0976: .updateComponentTreeUI(JFtp.statusP);
0977: }
0978: });
0979: }
0980: } catch (Exception ex) {
0981: Log.debug("Error setting look and feel: " + ex);
0982: }
0983: }
0984:
0985: public void addConnection(String name, BasicConnection con) {
0986: con.addConnectionListener((ConnectionListener) localDir);
0987:
0988: Dir tmp = (Dir) new RemoteDir();
0989: tmp.setDownloadList(dList);
0990: con.addConnectionListener((ConnectionListener) tmp);
0991: tmp.setCon(con);
0992:
0993: int x = remoteConnectionPanel.getSelectedIndex();
0994: remoteConnectionPanel.addTab(parse(name), null,
0995: (Component) tmp, "Switch to: " + parse(name));
0996: remoteConnectionPanel.setSelectedIndex(x + 1);
0997: j2.setClosable(true);
0998: }
0999:
1000: public void addLocalConnection(String name, BasicConnection con) {
1001: con.addConnectionListener((ConnectionListener) remoteDir);
1002:
1003: Dir tmp = (Dir) new LocalDir();
1004: tmp.setDownloadList(dList);
1005: con.addConnectionListener((ConnectionListener) tmp);
1006: tmp.setCon(con);
1007:
1008: int x = localConnectionPanel.getSelectedIndex();
1009: localConnectionPanel.addTab(parse(name), null, (Component) tmp,
1010: "Switch to: " + parse(name));
1011: localConnectionPanel.setSelectedIndex(x + 1);
1012: j1.setClosable(true);
1013: }
1014:
1015: private String parse(String what) {
1016: if (what.indexOf("@") >= 0) {
1017: return what.substring(what.lastIndexOf("@") + 1);
1018: } else {
1019: return what;
1020: }
1021: }
1022:
1023: public void stateChanged(ChangeEvent e) {
1024: remoteDir = (Dir) remoteConnectionPanel.getSelectedComponent();
1025: localDir = (Dir) localConnectionPanel.getSelectedComponent();
1026: remoteDir.getCon().setLocalPath(localDir.getPath());
1027:
1028: //localDir.getCon().setLocalPath(remoteDir.getPath());
1029: }
1030:
1031: public void closeCurrentTab() {
1032: int x = remoteConnectionPanel.getSelectedIndex();
1033:
1034: if (x > 0) {
1035: safeDisconnect();
1036: remoteConnectionPanel.remove(x);
1037: remoteConnectionPanel.setSelectedIndex(x - 1);
1038: }
1039:
1040: if (remoteConnectionPanel.getTabCount() < 2) {
1041: j2.setClosable(false);
1042: }
1043: }
1044:
1045: public void closeCurrentLocalTab() {
1046: int x = localConnectionPanel.getSelectedIndex();
1047:
1048: if (x > 0) {
1049: BasicConnection con = localDir.getCon();
1050:
1051: if ((con != null) && con.isConnected()) {
1052: try {
1053: con.disconnect();
1054: } catch (Exception ex) {
1055: }
1056: }
1057:
1058: localConnectionPanel.remove(x);
1059: localConnectionPanel.setSelectedIndex(x - 1);
1060: }
1061:
1062: if (localConnectionPanel.getTabCount() < 2) {
1063: j1.setClosable(false);
1064: }
1065: }
1066:
1067: public void addToDesktop(String title, Component c, int w, int h) {
1068: JInternalFrame jt = new JInternalFrame(title, false, true,
1069: false, true);
1070:
1071: if (w < 500) {
1072: jt.setLocation(200, 100);
1073: } else {
1074: jt.setLocation(80, 100);
1075: }
1076:
1077: jt.getContentPane().add(c);
1078: desktop.add(jt);
1079:
1080: internalFrames.put("" + c.hashCode(), jt);
1081:
1082: jt.pack();
1083: jt.setSize(new Dimension(w, h));
1084: jt.show();
1085: }
1086:
1087: public void removeFromDesktop(int component) {
1088: JInternalFrame f = (JInternalFrame) internalFrames.get(""
1089: + component);
1090:
1091: if (f != null) {
1092: f.dispose();
1093:
1094: //internalFrames.remove(component);
1095: } else {
1096: Log.debug("ERROR: " + component
1097: + " not found in Hashtable!");
1098: }
1099: }
1100:
1101: public void setClosable(int component, boolean ok) {
1102: JInternalFrame f = (JInternalFrame) internalFrames.get(""
1103: + component);
1104:
1105: if (f != null) {
1106: f.setClosable(ok);
1107: } else {
1108: Log.debug("ERROR: " + component
1109: + " not found in Hashtable!");
1110: }
1111: }
1112:
1113: public void setLocation(int component, int x, int y) {
1114: JInternalFrame f = (JInternalFrame) internalFrames.get(""
1115: + component);
1116:
1117: if (f != null) {
1118: f.setLocation(x, y);
1119: } else {
1120: Log.debug("ERROR: " + component
1121: + " not found in Hashtable!");
1122: }
1123: }
1124:
1125: //*** REFRESH MENU BAR
1126: public static void updateMenuBar() {
1127: //mainFrame.setJMenuBar(new AppMenuBar(this));
1128: menuBar.resetFileItems();
1129: }
1130:
1131: public void internalFrameClosing(InternalFrameEvent e) {
1132: if (e.getSource() == j1) {
1133: closeCurrentLocalTab();
1134: } else if (e.getSource() == j2) {
1135: closeCurrentTab();
1136: }
1137: }
1138:
1139: public void internalFrameActivated(InternalFrameEvent e) {
1140: }
1141:
1142: public void internalFrameClosed(InternalFrameEvent e) {
1143: }
1144:
1145: public void internalFrameDeactivated(InternalFrameEvent e) {
1146: }
1147:
1148: public void internalFrameDeiconified(InternalFrameEvent e) {
1149: }
1150:
1151: public void internalFrameIconified(InternalFrameEvent e) {
1152: }
1153:
1154: public void internalFrameOpened(InternalFrameEvent e) {
1155: }
1156:
1157: public void drop() {
1158: try {
1159: handleDrop(null, Toolkit.getDefaultToolkit()
1160: .getSystemClipboard().getContents(this ));
1161: } catch (Exception ex) {
1162: ex.printStackTrace();
1163: }
1164: }
1165:
1166: public void handleDrop(DropTargetDropEvent e, Transferable t)
1167: throws Exception {
1168: System.out.println("Starting dropAttempt");
1169:
1170: DataFlavor chosen = DataFlavor.javaFileListFlavor;
1171: DataFlavor second = FileTransferable.plainTextFlavor;
1172: DataFlavor flavor = null;
1173: Object data = null;
1174:
1175: if (e != null) {
1176: flavor = e.getCurrentDataFlavors()[0];
1177: e.acceptDrop(acceptableActions);
1178:
1179: Class c = flavor.getDefaultRepresentationClass();
1180: }
1181:
1182: if (flavor == null) {
1183: flavor = second;
1184: }
1185:
1186: String name = "";
1187:
1188: if (t.isDataFlavorSupported(chosen)) {
1189: System.out.println("Using List DnD style");
1190:
1191: java.util.List myList = (java.util.List) t
1192: .getTransferData(chosen);
1193:
1194: File[] f = (File[]) myList.toArray();
1195:
1196: for (int i = 0; i < f.length; i++) {
1197: name = f[i].getAbsolutePath();
1198: System.out.println("DnD file: " + name);
1199: }
1200:
1201: draggedTransfer(f, name);
1202: } else if (t.isDataFlavorSupported(second)) {
1203: System.out.println("Using text/plain DnD style");
1204:
1205: data = t.getTransferData(flavor);
1206:
1207: String str = "";
1208: int i = 0;
1209:
1210: if (data instanceof Reader) {
1211: int c;
1212:
1213: while ((c = ((Reader) data).read()) != -1) {
1214: if (((i == 1) && (c == 0))) {
1215: //System.out.println("Applying charset bugfix");
1216: i = -1;
1217: } else {
1218: str = str + new Character((char) c).toString();
1219: }
1220:
1221: i++;
1222: }
1223: } else {
1224: str = "" + data;
1225: }
1226:
1227: System.out.println("Object data: \"" + str + "\"");
1228:
1229: if (str.startsWith("<")) {
1230: Log.debug("Mozilla DnD detected (preparsing)");
1231: str = str.substring(str.indexOf("\"") + 1);
1232: str = str.substring(0, str.indexOf("\""));
1233: Log.debug("Parsed data: " + str);
1234: }
1235:
1236: //str = "[c:\\windows\\test.txt]";
1237: if (str.indexOf("[") >= 0) {
1238: Log.debug("Windows DnD detected");
1239: name = str.substring(str.indexOf("[") + 1);
1240: name = name.substring(0, name.lastIndexOf("]")); // last was str
1241: } else if (new String(str).startsWith("file://")) {
1242: name = str.substring(7);
1243: Log.debug("File URL DnD detected: " + name);
1244: }
1245:
1246: if (!new File(name).exists()) {
1247: System.out.println("No file string in clipboard: "
1248: + name);
1249:
1250: return;
1251: }
1252:
1253: System.out.println("DnD file: " + name);
1254:
1255: File[] f1 = new File[1];
1256: f1[0] = new File(name);
1257:
1258: draggedTransfer(f1, name);
1259: }
1260: }
1261:
1262: private void draggedTransfer(File[] f, String name) {
1263: if ((f.length == 1) && f[0].isFile()) {
1264: String path = "";
1265:
1266: if (name.indexOf("/") >= 0) {
1267: path = name.substring(0, name.lastIndexOf("/") + 1);
1268: name = name.substring(name.lastIndexOf("/") + 1);
1269: }
1270:
1271: Log.debug("DnD: " + path + " -> " + name);
1272:
1273: //TODO: parse "\\"
1274: if (!path.trim().equals("")) {
1275: ((LocalDir) localDir).chdir(path);
1276: }
1277:
1278: ((LocalDir) localDir).startTransfer(new DirEntry(name,
1279: ((ActionListener) localDir)));
1280: } else {
1281: Log
1282: .debug("Dragging multiple files or dirs is not yet supported.");
1283: }
1284: }
1285:
1286: class DTListener implements DropTargetListener {
1287: public void dragEnter(DropTargetDragEvent e) {
1288: e.acceptDrag(JFtp.acceptableActions);
1289: }
1290:
1291: public void dragOver(DropTargetDragEvent e) {
1292: e.acceptDrag(JFtp.acceptableActions);
1293: }
1294:
1295: public void dropActionChanged(DropTargetDragEvent e) {
1296: e.acceptDrag(JFtp.acceptableActions);
1297: }
1298:
1299: public void dragExit(DropTargetEvent e) {
1300: }
1301:
1302: public void drop(DropTargetDropEvent e) {
1303: try {
1304: handleDrop(e, e.getTransferable());
1305:
1306: e.dropComplete(true);
1307: UpdateDaemon.updateRemoteDir();
1308: } catch (Throwable t) {
1309: t.printStackTrace();
1310: e.dropComplete(false);
1311:
1312: return;
1313: }
1314: }
1315: }
1316: }
|