View Javadoc

1   /*
2    * jcurl java curling software framework https://JCurl.mro.name
3    * Copyright (C) 2005-2009 M. Rohrmoser
4    * 
5    * This program is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License as published by the
7    * Free Software Foundation; either version 2 of the License, or (at your
8    * option) any later version.
9    * 
10   * This program is distributed in the hope that it will be useful, but
11   * WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13   * Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License along
16   * with this program; if not, write to the Free Software Foundation, Inc.,
17   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18   */
19  package org.jcurl.demo.jtree;
20  
21  import java.awt.Point;
22  import java.awt.dnd.DnDConstants;
23  
24  import javax.swing.tree.DefaultMutableTreeNode;
25  import javax.swing.tree.DefaultTreeModel;
26  import javax.swing.tree.TreePath;
27  
28  /**
29   * http://forum.java.sun.com/thread.jspa?threadID=296255&start=0
30   * 
31   * @author <a href="http://forum.java.sun.com/profile.jspa?userID=82795">Deudeu</a>
32   * @version $Id: DefaultTreeTransferHandler.java 776 2008-03-16 10:17:28Z
33   *          mrohrmoser $
34   */
35  public class DefaultTreeTransferHandler extends AbstractTreeTransferHandler {
36  
37  	public DefaultTreeTransferHandler(final DNDTree tree, final int action) {
38  		super(tree, action, true);
39  	}
40  
41  	@Override
42  	public boolean canPerformAction(final DNDTree target,
43  			final DefaultMutableTreeNode draggedNode, final int action,
44  			final Point location) {
45  		final TreePath pathTarget = target.getPathForLocation(location.x,
46  				location.y);
47  		if (pathTarget == null) {
48  			target.setSelectionPath(null);
49  			return false;
50  		}
51  		target.setSelectionPath(pathTarget);
52  		if (action == DnDConstants.ACTION_COPY)
53  			return true;
54  		else if (action == DnDConstants.ACTION_MOVE) {
55  			final DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) pathTarget
56  					.getLastPathComponent();
57  			if (draggedNode.isRoot() || parentNode == draggedNode.getParent()
58  					|| draggedNode.isNodeDescendant(parentNode))
59  				return false;
60  			else
61  				return true;
62  		} else
63  			return false;
64  	}
65  
66  	@Override
67  	public boolean executeDrop(final DNDTree target,
68  			final DefaultMutableTreeNode draggedNode,
69  			final DefaultMutableTreeNode newParentNode, final int action) {
70  		if (action == DnDConstants.ACTION_COPY) {
71  			final DefaultMutableTreeNode newNode = DNDTree
72  					.makeDeepCopy(draggedNode);
73  			((DefaultTreeModel) target.getModel()).insertNodeInto(newNode,
74  					newParentNode, newParentNode.getChildCount());
75  			final TreePath treePath = new TreePath(newNode.getPath());
76  			target.scrollPathToVisible(treePath);
77  			target.setSelectionPath(treePath);
78  			return true;
79  		}
80  		if (action == DnDConstants.ACTION_MOVE) {
81  			draggedNode.removeFromParent();
82  			((DefaultTreeModel) target.getModel()).insertNodeInto(draggedNode,
83  					newParentNode, newParentNode.getChildCount());
84  			final TreePath treePath = new TreePath(draggedNode.getPath());
85  			target.scrollPathToVisible(treePath);
86  			target.setSelectionPath(treePath);
87  			return true;
88  		}
89  		return false;
90  	}
91  }