KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > view > TreeViewDragSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.explorer.view;
20
21 import org.openide.explorer.ExplorerManager;
22 import org.openide.nodes.Node;
23
24 import java.awt.Component JavaDoc;
25 import java.awt.dnd.*;
26
27 import javax.swing.JTree JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
29 import javax.swing.tree.*;
30
31
32 /** Support for the drag operations in the TreeView.
33 *
34 * @author Dafe Simonek, Jiri Rechtacek
35 */

36 final class TreeViewDragSupport extends ExplorerDragSupport {
37     // Attributes
38
// Associations
39

40     /** The view that manages viewing the data in a tree. */
41     protected TreeView view;
42
43     /** The tree which we are supporting (our client) */
44     private JTree JavaDoc tree;
45
46     /** Cell renderer - PENDING - do we need it? */
47
48     //protected DnDTreeViewCellRenderer cellRenderer;
49
// Operations
50

51     /** Creates new TreeViewDragSupport, initializes gesture */
52     public TreeViewDragSupport(TreeView view, JTree JavaDoc tree) {
53         this.view = view;
54         this.comp = tree;
55         this.tree = tree;
56     }
57
58     public int getAllowedDragActions() {
59         return view.getAllowedDragActions();
60     }
61
62     int getAllowedDropActions() {
63         return view.getAllowedDropActions();
64     }
65
66     public void dragGestureRecognized(DragGestureEvent dge) {
67         super.dragGestureRecognized(dge);
68
69         // notify tree cell editor that DnD operationm is active
70
if (exDnD.isDnDActive()) {
71             TreeCellEditor tce = ((JTree JavaDoc) tree).getCellEditor();
72
73             if (tce instanceof TreeViewCellEditor) {
74                 ((TreeViewCellEditor) tce).setDnDActive(true);
75             }
76         }
77     }
78
79     public void dragDropEnd(DragSourceDropEvent dsde) {
80         // get the droped nodes
81
Node[] dropedNodes = exDnD.getDraggedNodes();
82         super.dragDropEnd(dsde);
83
84         // if any original glass pane was stored (the DnD was broken e.g. by Esc)
85
if (DropGlassPane.isOriginalPaneStored()) {
86             // give back the orig glass pane
87
DropGlassPane.putBackOriginal();
88
89             // DnD is not active
90
exDnD.setDnDActive(false);
91         }
92
93         // select the droped nodes
94
try {
95             if (dropedNodes != null) {
96                 ExplorerManager.Provider panel = (ExplorerManager.Provider) SwingUtilities.getAncestorOfClass(
97                         ExplorerManager.Provider.class, view
98                     );
99
100                 if (panel != null) {
101                     panel.getExplorerManager().setSelectedNodes(dropedNodes);
102                 }
103             }
104         } catch (Exception JavaDoc e) {
105             // don't care
106
}
107
108         // notify tree cell editor that DnD operationm is active
109
// no more
110
TreeCellEditor tce = tree.getCellEditor();
111
112         if (tce instanceof TreeViewCellEditor) {
113             ((TreeViewCellEditor) tce).setDnDActive(false);
114         }
115     }
116
117     /** Utility method. Returns either selected nodes in tree
118     * (if cursor hotspot is above some selected node) or the node
119     * the cursor points to.
120     * @return Node array or null if position of the cursor points
121     * to no node.
122     */

123     Node[] obtainNodes(DragGestureEvent dge) {
124         TreePath[] tps = tree.getSelectionPaths();
125
126         if (tps == null) {
127             return null;
128         }
129
130         Node[] result = new Node[tps.length];
131
132         int cnt = 0;
133
134         for (int i = 0; i < tps.length; i++) {
135             if (tree.getPathBounds(tps[i]).contains(dge.getDragOrigin())) {
136                 cnt++;
137             }
138
139             result[i] = DragDropUtilities.secureFindNode(tps[i].getLastPathComponent());
140         }
141
142         // #41954:
143
// if the drag source is not at all in path location, do not return
144
// any nodes
145
return (cnt == 0) ? null : result;
146     }
147 }
148  /* end class TreeViewDragSupport */
149
Popular Tags