remotedir.java

来自「java ftp 操作代码,程序可以直接运行」· Java 代码 · 共 1,646 行 · 第 1/4 页

JAVA
1,646
字号
/* * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */package net.sf.jftp.gui.base;import java.awt.BorderLayout;import java.awt.Cursor;import java.awt.FlowLayout;import java.awt.Font;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.PrintStream;import java.lang.reflect.Array;import java.util.Date;import javax.swing.DefaultListModel;import javax.swing.ImageIcon;import javax.swing.JComboBox;import javax.swing.JEditorPane;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JToolBar;import javax.swing.SwingUtilities;import javax.swing.event.ListSelectionListener;import net.sf.jftp.JFtp;import net.sf.jftp.config.SaveSet;import net.sf.jftp.config.Settings;import net.sf.jftp.gui.base.dir.DirCanvas;import net.sf.jftp.gui.base.dir.DirCellRenderer;import net.sf.jftp.gui.base.dir.DirComponent;import net.sf.jftp.gui.base.dir.DirEntry;import net.sf.jftp.gui.base.dir.DirLister;import net.sf.jftp.gui.base.dir.DirPanel;import net.sf.jftp.gui.base.dir.TableUtils;import net.sf.jftp.gui.framework.HFrame;import net.sf.jftp.gui.framework.HImage;import net.sf.jftp.gui.framework.HImageButton;import net.sf.jftp.gui.tasks.Creator;import net.sf.jftp.gui.tasks.Displayer;import net.sf.jftp.gui.tasks.PathChanger;import net.sf.jftp.gui.tasks.RemoteCommand;import net.sf.jftp.net.BasicConnection;import net.sf.jftp.net.ConnectionListener;import net.sf.jftp.net.FilesystemConnection;import net.sf.jftp.net.FtpConnection;import net.sf.jftp.net.SftpConnection;import net.sf.jftp.net.SmbConnection;import net.sf.jftp.system.LocalIO;import net.sf.jftp.system.StringUtils;import net.sf.jftp.system.UpdateDaemon;import net.sf.jftp.system.logging.Log;import net.sf.jftp.tools.Shell;public class RemoteDir extends DirComponent implements ListSelectionListener,                                                   ActionListener,                                                   ConnectionListener,                                                   KeyListener{    //HImageButton openButton;    static final String deleteString = "rm";    static final String mkdirString = "mkdir";    static final String refreshString = "fresh";    static final String cdString = "cd";    static final String cmdString = "cmd";    static final String downloadString = "<-";    static final String uploadString = "->";    static final String queueString = "que";    static final String cdUpString = "cdUp";    static final String rnString = "rn";    HImageButton deleteButton;    HImageButton mkdirButton;    HImageButton cmdButton;    HImageButton refreshButton;    HImageButton cdButton;    HImageButton uploadButton;    HImageButton downloadButton;    HImageButton queueButton;    HImageButton cdUpButton;    HImageButton rnButton;    //static final String openString = "open";    private DirCanvas label = new DirCanvas(this);    private boolean pathChanged = true;    private boolean firstGui = true;    private int pos = 0;    private JPanel p = new JPanel();    private JToolBar buttonPanel = new JToolBar()    {        public Insets getInsets()        {            return new Insets(0, 0, 0, 0);        }    };    //private JPanel buttonPanel = new JPanel();    private JToolBar currDirPanel = new JToolBar()    {        public Insets getInsets()        {            return new Insets(0, 0, 0, 0);        }    };    private DefaultListModel jlm;    private JScrollPane jsp = new JScrollPane(jl);    private int tmpindex = -1;    private HImageButton list = new HImageButton(Settings.listImage, "list",                                                 "Show remote listing...", this);    private HImageButton transferType = new HImageButton(Settings.typeImage,                                                         "type",                                                         "Toggle transfer type...",                                                         this);    private JPopupMenu popupMenu = new JPopupMenu();    private JMenuItem props = new JMenuItem("Properties");    private DirEntry currentPopup = null;    private String sortMode = null;    String[] sortTypes = new String[] { "Normal", "Reverse", "Size", "Size/Re" };    private JComboBox sorter = new JComboBox(sortTypes);    private boolean dateEnabled = false;    /**    * RemoteDir constructor.    */    public RemoteDir()    {        type = "remote";        con = new FilesystemConnection();        con.addConnectionListener(this);        if(!con.chdir("/"))        {            con.chdir("C:\\");        }        setDate();    }    /**    * RemoteDir constructor.    */    public RemoteDir(String path)    {        type = "remote";        this.path = path;        con = new FilesystemConnection();        con.addConnectionListener(this);        con.chdir(path);        setDate();    }    /**    * Creates the gui and adds the MouseListener etc.    */    public void gui_init()    {        setLayout(new BorderLayout());        currDirPanel.setFloatable(false);        buttonPanel.setFloatable(false);        FlowLayout f = new FlowLayout(FlowLayout.LEFT);        f.setHgap(1);        f.setVgap(2);        buttonPanel.setLayout(f);        buttonPanel.setMargin(new Insets(0, 0, 0, 0));        props.addActionListener(this);        popupMenu.add(props);        rnButton = new HImageButton(Settings.textFileImage, rnString,                                    "Rename selected file or directory", this);        rnButton.setToolTipText("Rename selected");        list.setToolTipText("Show remote listing...");        transferType.setToolTipText("Toggle transfer type...");        deleteButton = new HImageButton(Settings.deleteImage, deleteString,                                        "Delete  selected", this);        deleteButton.setToolTipText("Delete selected");        mkdirButton = new HImageButton(Settings.mkdirImage, mkdirString,                                       "Create a new directory", this);        mkdirButton.setToolTipText("Create directory");        refreshButton = new HImageButton(Settings.refreshImage, refreshString,                                         "Refresh current directory", this);        refreshButton.setToolTipText("Refresh directory");		refreshButton.setRolloverIcon(new ImageIcon(HImage.getImage(this, Settings.refreshImage2)));		refreshButton.setRolloverEnabled(true);        cdButton = new HImageButton(Settings.cdImage, cdString,                                    "Change directory", this);        cdButton.setToolTipText("Change directory");        cmdButton = new HImageButton(Settings.cmdImage, cmdString,                                     "Execute remote command", this);        cmdButton.setToolTipText("Execute remote command");        downloadButton = new HImageButton(Settings.downloadImage,                                          downloadString, "Download selected",                                          this);        downloadButton.setToolTipText("Download selected");        queueButton = new HImageButton(Settings.downloadImage, queueString,                                       "Queue selected", this);        queueButton.setToolTipText("Queue selected");        cdUpButton = new HImageButton(Settings.cdUpImage, cdUpString,                                      "Go to Parent Directory", this);        cdUpButton.setToolTipText("Go to Parent Directory");        //openButton = new HImageButton(Settings.openImage,openString,"Connect to server",this);        //openButton.setToolTipText("Connect");        setLabel();        label.setSize(getSize().width - 10, 24);        currDirPanel.add(label);        currDirPanel.setSize(getSize().width - 10, 32);        label.setSize(getSize().width - 20, 24);        p.setLayout(new BorderLayout());        p.add("North", currDirPanel);        buttonPanel.add(downloadButton);        //buttonPanel.add(new JLabel(" "));        buttonPanel.add(queueButton);        buttonPanel.add(new JLabel("  "));        //buttonPanel.add(openButton);        //buttonPanel.add(new JLabel("   "));        buttonPanel.add(refreshButton);        buttonPanel.add(new JLabel("  "));        buttonPanel.add(rnButton);        buttonPanel.add(mkdirButton);        buttonPanel.add(cdButton);        buttonPanel.add(deleteButton);        buttonPanel.add(cdUpButton);        buttonPanel.add(new JLabel("  "));        buttonPanel.add(cmdButton);        buttonPanel.add(list);        buttonPanel.add(transferType);        //buttonPanel.add(new JLabel(" "));        buttonPanel.add(sorter);        buttonPanel.setVisible(true);        buttonPanel.setSize(getSize().width - 10, 32);        p.add("West", buttonPanel);        add("North", p);        sorter.addActionListener(this);        //setDirList(true);        jlm = new DefaultListModel();        jl = new JList(jlm);        jl.setCellRenderer(new DirCellRenderer());        jl.setVisibleRowCount(Settings.visibleFileRows);        jl.setDropTarget(JFtp.statusP.jftp.dropTarget);        // add this becaus we need to fetch only doubleclicks        MouseListener mouseListener = new MouseAdapter()        {            public void mousePressed(MouseEvent e)            {                if(JFtp.uiBlocked)                {                    return;                }                if(e.isPopupTrigger() || SwingUtilities.isRightMouseButton(e))                {                    int index = jl.getSelectedIndex() - 1;                    if(index < -1)                    {                        return;                    }                    String tgt = (String) jl.getSelectedValue().toString();                    if(index < 0)                    {                    }                    else if((dirEntry == null) || (dirEntry.length < index) ||                                (dirEntry[index] == null))                    {                        return;                    }                    else                    {                        currentPopup = dirEntry[index];                        popupMenu.show(e.getComponent(), e.getX(), e.getY());                    }                }            }            public void mouseClicked(MouseEvent e)            {                if(JFtp.uiBlocked)                {                    return;                }                	        	if(Settings.getUseJTableLayout()) {	        		TableUtils.copyTableSelectionsToJList(jl, table);	        	}                //System.out.println("DirEntryListener::");                if(e.getClickCount() == 2)                {                    //System.out.println("2xList selection: "+jl.getSelectedValue().toString());                    int index = jl.getSelectedIndex() - 1;                    // mousewheel bugfix                    if(index < -1)                    {                        return;                    }                    String tgt = (String) jl.getSelectedValue().toString();                    if(index < 0)                    {                        if(JFtp.mainFrame != null)                        {                            JFtp.mainFrame.setCursor(Cursor.WAIT_CURSOR);                        }                        con.chdir(path + tgt);                        if(JFtp.mainFrame != null)                        {                            JFtp.mainFrame.setCursor(Cursor.DEFAULT_CURSOR);                        }                    }                    else if((dirEntry == null) || (dirEntry.length < index) ||                                (dirEntry[index] == null))                    {                        return;                    }                    else if(dirEntry[index].isDirectory())                    {                        con.chdir(path + tgt);                    }                    else if(dirEntry[index].isLink())                    {                        if(!con.chdir(path + tgt))                        {                            showContentWindow(path +                                              dirEntry[index].toString(),                                              dirEntry[index]);                            //blockedTransfer(index);                        }                    }                    else                    {                        showContentWindow(path + dirEntry[index].toString(),                                          dirEntry[index]);                        //blockedTransfer(index);                    }                }            }        };                if(Settings.newTableGui) {        	jsp = new JScrollPane(table);        	table.getSelectionModel().addListSelectionListener(this);        	table.addMouseListener(mouseListener);        }        else {        	jsp = new JScrollPane(jl);            jl.addListSelectionListener(this);            jl.addKeyListener(this);            jl.addMouseListener(mouseListener);            jl.requestFocus();        }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?