dndtree.java

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

JAVA
217
字号
/**  * 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.dnd;import java.awt.*;import java.awt.datatransfer.*;import java.awt.dnd.*;import java.awt.event.*;import java.io.IOException;import javaexplorer.Launcher;import javaexplorer.gui.treenode.xfile.*;import javaexplorer.model.*;import javax.swing.tree.*;/** *@author     BOESCH Vincent *@created    21 janvier 2002 *@version    3.3 */public class DNDTree extends XTree implements DropTargetListener,    DragSourceListener, DragGestureListener {    private Launcher _launcher = null;    /**     *  enables this component to be a dropTarget     */    DropTarget dropTarget = null;    /**     *  constructor - initializes the DropTarget     *  and DragSource.     *     *@param  launcher  Description of the     *      Parameter     */    public DNDTree(Launcher launcher) {        super();        _launcher = launcher;        dropTarget = new DropTarget(this, this);        DragGestureRecognizer dgr = _launcher.getDragSource()                                             .createDragGestureRecognizer(MouseRightDragGestureRecognizer.class,                this, DnDConstants.ACTION_COPY_OR_MOVE, this);        if (dgr == null) {            _launcher.getDragSource().createDefaultDragGestureRecognizer(this,                DnDConstants.ACTION_COPY_OR_MOVE, this);        }        this.addMouseListener(new MouseAdapter() {            });    }    /**     *  this message goes to DragSourceListener,     *  informing it that the dragging has     *  ended     *     *@param  event  Description of the Parameter     */    public void dragDropEnd(DragSourceDropEvent event) {    }    /**     *  is invoked when you are dragging over     *  the DropSite     *     *@param  event  Description of the Parameter     */    public void dragEnter(DropTargetDragEvent event) {        event.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);    }    /**     *  this message goes to DragSourceListener,     *  informing it that the dragging has     *  entered the DropSite     *     *@param  event  Description of the Parameter     */    public void dragEnter(DragSourceDragEvent event) {    }    /**     *  is invoked when you are exit the DropSite     *  without dropping     *     *@param  event  Description of the Parameter     */    public void dragExit(DropTargetEvent event) {    }    /**     *  this message goes to DragSourceListener,     *  informing it that the dragging has     *  exited the DropSite     *     *@param  event  Description of the Parameter     */    public void dragExit(DragSourceEvent event) {    }    /**     *  a drag gesture has been initiated     *     *@param  event  Description of the Parameter     */    public void dragGestureRecognized(DragGestureEvent event) {        TreePath[] tb_tp = getSelectionPaths();        if (tb_tp == null) {            return;        }        XFile[] tbxf = new XFile[tb_tp.length];        XFileTreeNode jftn = null;        for (int i = 0; i < tb_tp.length; i++) {            jftn = (XFileTreeNode) tb_tp[i].getLastPathComponent();            if (jftn != null) {                tbxf[i] = jftn.getXFile();            }        }        XFTransferable xft = new XFTransferable(tbxf);        // as the name suggests, starts the dragging        _launcher.getDragSource().startDrag(event, DragSource.DefaultMoveDrop,            xft, this);    }    /**     *  is invoked when a drag operation is     *  going on     *     *@param  event  Description of the Parameter     */    public void dragOver(DropTargetDragEvent event) {    }    /**     *  this message goes to DragSourceListener,     *  informing it that the dragging is currently     *  ocurring over the DropSite     *     *@param  event  Description of the Parameter     */    public void dragOver(DragSourceDragEvent event) {    }    /**     *  a drop has occurred     *     *@param  event  Description of the Parameter     */    public void drop(DropTargetDropEvent event) {        XFile[] tbxf = null;        XFile f = null;        Point p = null;        try {            p = event.getLocation();            XFileTreeNode jftn = null;            if (getRowCount() == 0) {                jftn = (XFileTreeNode) getModel().getRoot();            } else {                TreePath tp = getClosestPathForLocation(p.x, p.y);                jftn = (XFileTreeNode) tp.getLastPathComponent();            }            if (jftn != null) {                f = jftn.getXFile();            }            if (f == null) {                return;            }            if (!f.isDirectory()) {                f = f.getXParent();            }            Transferable transferable = event.getTransferable();            // we accept only Strings            if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {                event.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);                tbxf = (XFile[]) transferable.getTransferData(DataFlavor.stringFlavor);                f.setLauncher(_launcher);                //Fin du drop pour mise 

⌨️ 快捷键说明

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