shortcutdisk.java

来自「JavaExplorer是一个独立于平台的浏览器」· Java 代码 · 共 407 行

JAVA
407
字号
/**  * File and FTP Explorer  * Copyright 2002  * BOESCH Vincent  *  * 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 javaexplorer.gui.disk;import java.awt.*;import java.awt.event.*;import javaexplorer.Launcher;import javaexplorer.gui.dnd.*;import javaexplorer.gui.renderer.*;import javaexplorer.gui.treenode.shortcut.*;import javaexplorer.model.XFile;import javaexplorer.util.shortcut.*;import javax.swing.*;import javax.swing.border.*;import javax.swing.event.*;import javax.swing.tree.*;/** *@author     BOESCH Vincent *@created    21 janvier 2002 *@version    3.3 */public class ShortcutDisk extends JPanel implements Disk, ActionListener,    TreeSelectionListener {    private Launcher _launcher = null;    private Shortcut _scCut = null;    private ShortcutTreeNode _stnRoot = null;    private ShortcutTreeNode _stnSelected = null;    BorderLayout borderLayout1 = new BorderLayout();    BorderLayout borderLayout2 = new BorderLayout();    GridLayout gridLayout2 = new GridLayout();    JLabel jLabel1 = new JLabel();    JLabel jLabel2 = new JLabel();    JPanel jPanel1 = new JPanel();    JPanel jPanel2 = new JPanel();    JPanel jPanel3 = new JPanel();    JScrollPane jScrollPane1 = new JScrollPane();    XTree jTree1 = new XTree();    JButton jbtCreate = new JButton();    JButton jbtCut = new JButton();    JButton jbtDelete = new JButton();    JButton jbtModify = new JButton();    JButton jbtPaste = new JButton();    JTextField jtfFile = new JTextField();    JTextField jtfTitle = new JTextField();    TitledBorder titledBorder1;    /**     *  Constructeur objet ShortcutDisk     *     *@param  launcher  Description of the     *      Parameter     */    public ShortcutDisk(Launcher launcher) {        try {            _launcher = launcher;            jbInit();        } catch (Exception e) {            javaexplorer.util.Log.addError(e);        }    }    /**     *@param  e  Description of the Parameter     */    public void actionPerformed(ActionEvent e) {        Object obj = e.getSource();        if (obj == jbtDelete) {            deleteShortcut();            return;        }        if (obj == jbtModify) {            modifyShortcut();            return;        }        if (obj == jbtCreate) {            createDir();            return;        }        if (obj == jbtCut) {            cutShortcut();            return;        }        if (obj == jbtPaste) {            pasteShortcut();            return;        }    }    /**     */    public void createDir() {        if (_stnSelected == null) {            return;        }        String title = jtfTitle.getText();        if ((title == null) || (title.length() == 0)) {            return;        }        Shortcut sc = _stnSelected.getShortcut();        ShortcutContainer parent = null;        if (sc instanceof ShortcutContainer) {            parent = (ShortcutContainer) sc;        } else {            ShortcutTreeNode tnParent = (ShortcutTreeNode) _stnSelected.getParent();            if (tnParent == null) {                tnParent = _stnRoot;            }            parent = (ShortcutContainer) tnParent.getShortcut();        }        ShortcutContainer scc = new ShortcutContainer();        scc.setTitle(title);        parent.addShortcut(scc);        jTree1.setModel(new DefaultTreeModel(_stnRoot));        jTree1.setSelectionRow(0);    }    /**     */    public void cutShortcut() {        if (_stnSelected == null) {            return;        }        _scCut = _stnSelected.getShortcut();        deleteShortcut();    }    /**     */    public void deleteShortcut() {        if ((_stnSelected == null) || (_stnSelected == _stnRoot)) {            return;        }        ShortcutTreeNode parent = (ShortcutTreeNode) _stnSelected.getParent();        ShortcutContainer scc = null;        if (parent == null) {            parent = _stnRoot;        }        scc = (ShortcutContainer) (parent.getShortcut());        scc.removeShortcut(_stnSelected.getShortcut().getTitle());        jTree1.setModel(new DefaultTreeModel(_stnRoot));        jTree1.setSelectionRow(0);    }    /**     *  Gets the container attribute of the     *  ShortcutDisk object     *     *@return    The container value     */    public ShortcutContainer getContainer() {        return (ShortcutContainer) _stnRoot.getShortcut();    }    /**     *  Gets the root attribute of the ShortcutDisk     *  object     *     *@return    The root value     */    public Object getRoot() {        return _stnRoot;    }    /**     *@param  e  Description of the Parameter     */    void jTree1_keyPressed(KeyEvent e) {        int code = e.getKeyCode();        if (e.isControlDown()) {            switch (code) {            case KeyEvent.VK_X:                cutShortcut();                break;            case KeyEvent.VK_V:                pasteShortcut();                break;            case KeyEvent.VK_N:                createDir();                break;            case KeyEvent.VK_M:                modifyShortcut();                break;            }        } else {            if (code == KeyEvent.VK_DELETE) {                deleteShortcut();            }        }    }    /**     *@throws  Exception  Description of the     *      Exception     */    private void jbInit() throws Exception {        titledBorder1 = new TitledBorder("");        this.setLayout(borderLayout1);        jPanel1.setLayout(borderLayout2);        jScrollPane1.setBorder(BorderFactory.createLineBorder(Color.black));        this.setBorder(titledBorder1);        jPanel2.setLayout(gridLayout2);        gridLayout2.setRows(2);        gridLayout2.setColumns(2);        jLabel2.setText("link");        jLabel1.setText("label");        jbtDelete.setText("Delete");        jbtModify.setText("Modify");        jbtCreate.setText("Create Dir");        jtfFile.setToolTipText("");        jtfFile.setEditable(false);        jTree1.addTreeSelectionListener(this);        jbtCut.setText("Cut");        jbtPaste.setText("Paste");        jTree1.addKeyListener(new java.awt.event.KeyAdapter() {                public void keyPressed(KeyEvent e) {                    jTree1_keyPressed(e);                }            });        this.add(jScrollPane1, BorderLayout.CENTER);        this.add(jPanel1, BorderLayout.SOUTH);        jPanel1.add(jPanel2, BorderLayout.CENTER);        jPanel2.add(jLabel1, null);        jPanel2.add(jtfTitle, null);        jPanel2.add(jLabel2, null);        jPanel2.add(jtfFile, null);        jPanel1.add(jPanel3, BorderLayout.SOUTH);        jPanel3.add(jbtDelete, null);        jPanel3.add(jbtModify, null);        jPanel3.add(jbtCut, null);        jPanel3.add(jbtPaste, null);        jPanel3.add(jbtCreate, null);        jScrollPane1.getViewport().add(jTree1, null);        jTree1.setRootVisible(true);        jTree1.setCellRenderer(new ShortcutTreeCellRenderer());        jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);        jbtDelete.addActionListener(this);        jbtModify.addActionListener(this);        jbtCreate.addActionListener(this);        jbtCut.addActionListener(this);        jbtPaste.addActionListener(this);    }    /**     */    public void modifyShortcut() {        if (_stnSelected == null) {            return;        }        Shortcut sc = _stnSelected.getShortcut();        String title = jtfTitle.getText();        if ((title == null) || (title.length() == 0)) {            return;        }        sc.setTitle(title);        jTree1.setModel(new DefaultTreeModel(_stnRoot));        jTree1.setSelectionRow(0);    }    /**     */    public void pasteShortcut() {        if (_scCut == null) {            return;        }        if (_stnSelected == null) {            return;        }        ShortcutContainer parent = null;        Shortcut sc = _stnSelected.getShortcut();        if (sc instanceof ShortcutContainer) {            parent = (ShortcutContainer) sc;        } else {            ShortcutTreeNode tnParent = (ShortcutTreeNode) _stnSelected.getParent();            if (tnParent == null) {                tnParent = _stnRoot;            }            parent = (ShortcutContainer) tnParent.getShortcut();        }        parent.addShortcut(_scCut);        _scCut = null;        jTree1.setModel(new DefaultTreeModel(_stnRoot));        jTree1.setSelectionRow(0);    }    /**     *@param  f  Description of the Parameter     */    public void refreshView(XFile f) {        //Sans objet    }    /**     *  Sets the container attribute of the     *  ShortcutDisk object     *     *@param  sc  The new container value     */    public void setContainer(ShortcutContainer sc) {        _stnRoot = new ShortcutTreeNode(sc);        jTree1.setModel(new DefaultTreeModel(_stnRoot));        jTree1.setSelectionRow(0);    }    /**     *  Sets the launcher attribute of the     *  ShortcutDisk object     *     *@param  launcher  The new launcher value     */    public void setLauncher(Launcher launcher) {        _launcher = launcher;    }    /**     */    public void updateVisual() {        Shortcut sc = _stnSelected.getShortcut();        jtfTitle.setText(sc.getTitle());        if (sc instanceof ShortcutContainer) {            jtfFile.setText("");        } else {            jtfFile.setText(sc.getXFile().toString());        }    }    /**     *@param  e  Description of the Parameter     */    public void valueChanged(TreeSelectionEvent e) {        TreePath tp = jTree1.getSelectionPath();        if (tp != null) {            Object obj = tp.getLastPathComponent();            if (obj != null) {                _stnSelected = (ShortcutTreeNode) obj;                updateVisual();            }        }    }}

⌨️ 快捷键说明

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