KickJava   Java API By Example, From Geeks To Geeks.

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


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.nodes.Node;
22
23 import java.awt.Dialog JavaDoc;
24 import java.awt.datatransfer.Transferable JavaDoc;
25 import java.awt.dnd.*;
26
27 import java.io.IOException JavaDoc;
28
29 import java.util.TooManyListenersException JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32
33 import javax.swing.JComponent JavaDoc;
34 import javax.swing.SwingUtilities JavaDoc;
35 import org.openide.util.Exceptions;
36
37
38 /** Support for the drag operations in explorer.
39 *
40 * @author Jiri Rechtacek
41 */

42 abstract class ExplorerDragSupport implements DragSourceListener, DragGestureListener {
43     // Attributes
44

45     /** True when we are active, false otherwise */
46     boolean active = false;
47
48     /** Recognizes default gesture */
49     DragGestureRecognizer defaultGesture;
50
51     /** The component which we are supporting (our client) */
52     protected JComponent JavaDoc comp;
53     ExplorerDnDManager exDnD = ExplorerDnDManager.getDefault();
54
55     abstract int getAllowedDropActions();
56
57     /** Initiating the drag */
58     public void dragGestureRecognized(DragGestureEvent dge) {
59         // 1. get seleced dragged nodes
60
Node[] nodes = obtainNodes(dge);
61
62         // check nodes
63
if ((nodes == null) || (nodes.length == 0)) {
64             return;
65         }
66
67         // 2. detect highest common action
68
int possibleNodeAction = getAllowedDragActions();
69
70         for (int i = 0; i < nodes.length; i++) {
71             if ((possibleNodeAction & DnDConstants.ACTION_MOVE) != 0) {
72                 if (!nodes[i].canCut()) {
73                     possibleNodeAction = DnDConstants.ACTION_COPY | DnDConstants.ACTION_REFERENCE;
74                 }
75             }
76
77             if ((possibleNodeAction & DnDConstants.ACTION_COPY) != 0) {
78                 if (!nodes[i].canCopy()) {
79                     possibleNodeAction = DnDConstants.ACTION_NONE;
80                 }
81             }
82         }
83
84         exDnD = ExplorerDnDManager.getDefault();
85         exDnD.setNodeAllowedActions(possibleNodeAction);
86
87         int dragAction = dge.getDragAction();
88
89         boolean dragStatus = canDrag(dragAction, possibleNodeAction);
90
91         // 3. get transferable and start the drag
92
try {
93             // for MOVE
94
Transferable JavaDoc transferable;
95
96             if ((possibleNodeAction & DnDConstants.ACTION_MOVE) != 0) {
97                 // for MOVE
98
transferable = DragDropUtilities.getNodeTransferable(nodes, DnDConstants.ACTION_MOVE);
99                 exDnD.setDraggedTransferable(transferable, true);
100
101                 // for COPY too
102
transferable = DragDropUtilities.getNodeTransferable(nodes, DnDConstants.ACTION_COPY);
103                 exDnD.setDraggedTransferable(transferable, false);
104             } else if ((possibleNodeAction & DnDConstants.ACTION_COPY) != 0) {
105                 // for COPY
106
transferable = DragDropUtilities.getNodeTransferable(nodes, DnDConstants.ACTION_COPY);
107                 exDnD.setDraggedTransferable(transferable, false);
108             } else {
109                 // transferable for NONE
110
transferable = Node.EMPTY.drag();
111                 exDnD.setDraggedTransferable(transferable, false);
112             }
113
114             exDnD.setDraggedNodes(nodes);
115
116             Dialog JavaDoc d = (Dialog JavaDoc) SwingUtilities.getAncestorOfClass(Dialog JavaDoc.class, comp);
117
118             if ((d != null) && d.isModal()) {
119                 exDnD.setDnDActive(false);
120
121                 return;
122             } else {
123                 exDnD.setDnDActive(true);
124                 dge.startDrag(null, transferable, this);
125             }
126         } catch (InvalidDnDOperationException exc) {
127             // cannot start the drag, notify as informational
128
Logger.getLogger(ExplorerDragSupport.class.getName()).log(Level.WARNING, null, exc);
129             exDnD.setDnDActive(false);
130         } catch (IOException JavaDoc exc) {
131             // cannot start the drag, notify user
132
Exceptions.printStackTrace(exc);
133             exDnD.setDnDActive(false);
134         }
135     }
136
137     protected int getAllowedDragActions() {
138         return DnDConstants.ACTION_NONE;
139     }
140
141     private boolean canDrag(int targetAction, int possibleAction) {
142         return (possibleAction & targetAction) != 0;
143     }
144
145     public void dragEnter(DragSourceDragEvent dsde) {
146         doDragOver(dsde);
147     }
148
149     public void dragOver(DragSourceDragEvent dsde) {
150         doDragOver(dsde);
151     }
152
153     public void dropActionChanged(DragSourceDragEvent dsde) {
154     }
155
156     public void dragExit(DragSourceEvent dse) {
157     }
158
159     private void doDragOver(DragSourceDragEvent dsde) {
160     }
161
162     public void dragDropEnd(DragSourceDropEvent dsde) {
163         // not transferable for MOVE nor COPY
164
exDnD.setDraggedTransferable(null, true);
165         exDnD.setDraggedTransferable(null, false);
166
167         // no nodes are dragged
168
exDnD.setDraggedNodes(null);
169
170         // no drop candidate
171
NodeRenderer.dragExit();
172
173         // no more active
174
exDnD.setDnDActive(false);
175     }
176
177     /** Activates or deactivates Drag support on asociated JTree
178     * component
179     * @param active true if the support should be active, false
180     * otherwise
181     */

182     public void activate(boolean active) {
183         if (this.active == active) {
184             return;
185         }
186
187         this.active = active;
188
189         DragGestureRecognizer dgr = getDefaultGestureRecognizer();
190
191         if (active) {
192             dgr.setSourceActions(getAllowedDragActions());
193
194             try {
195                 dgr.removeDragGestureListener(this);
196                 dgr.addDragGestureListener(this);
197             } catch (TooManyListenersException JavaDoc exc) {
198                 throw new IllegalStateException JavaDoc("Too many listeners for drag gesture."); // NOI18N
199
}
200         } else {
201             dgr.removeDragGestureListener(this);
202         }
203     }
204
205     /** Safe getter for default gesture<br>
206     * (creates the gesture when called for the first time)
207     */

208     DragGestureRecognizer getDefaultGestureRecognizer() {
209         if (defaultGesture == null) {
210             DragSource ds = DragSource.getDefaultDragSource();
211             defaultGesture = ds.createDefaultDragGestureRecognizer(comp, getAllowedDragActions(), this);
212         }
213
214         return defaultGesture;
215     }
216
217     /** Utility method. Returns either selected nodes in tree
218     * (if cursor hotspot is above some selected node) or the node
219     * the cursor points to.
220     * @return Node array or null if position of the cursor points
221     * to no node.
222     */

223     abstract Node[] obtainNodes(DragGestureEvent dge);
224 }
225  /* end class ExplorerDragSupport */
226
Popular Tags