Debian Codegraph @

laf-widget/src/main/java/org/pushingpixels/lafwidget/tree/dnd/TreeDragAndDropWidget.java

raw
1 /*
2 * Copyright (c) 2005-2010 Laf-Widget Kirill Grouchnikov. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * o Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * o Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * o Neither the name of Laf-Widget Kirill Grouchnikov nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package org.pushingpixels.lafwidget.tree.dnd;
31
32 import java.awt.*;
33 import java.awt.datatransfer.*;
34 import java.awt.dnd.*;
35 import java.awt.image.BufferedImage;
36 import java.beans.PropertyChangeEvent;
37 import java.beans.PropertyChangeListener;
38 import java.lang.reflect.Method;
39 import java.util.Enumeration;
40 import java.util.EventListener;
41
42 import javax.swing.JTree;
43 import javax.swing.event.EventListenerList;
44 import javax.swing.tree.*;
45
46 import org.pushingpixels.lafwidget.*;
47
48 /**
49 * TreeWrapper is used to handle drag and drop and popup menus in any JTree.
50 *
51 * @author Antonio Vieiro ([email protected]), $Author: kirillcool $
52 */
53 public class TreeDragAndDropWidget extends LafWidgetAdapter<JTree> {
54 /**
55 * This to avoid excesive creation of objects in invocation.
56 */
57 private static Class[] EMPTY_CLASS_ARRAY = new Class[0];
58
59 /**
60 * This to avoid excessive reflection to find the "getTransferable" method.
61 */
62 private static Method getTransferableMethod = null;
63
64 /**
65 * This to avoid excesive creation of objects in invocation.
66 */
67 private static Object[] EMPTY_OBJECT_ARRAY = new Object[0];
68
69 protected DnDCellRendererProxy rendererProxy;
70
71 protected DragSource dragSource;
72
73 protected DropTarget dropTarget;
74
75 protected MutableTreeNode dropNode;
76
77 protected EventListenerList listeners;
78
79 protected PropertyChangeListener propertyChangeListener;
80
81 protected PropertyChangeListener cellRendererChangeListener;
82
83 protected TreeDropTargetListener dropListener;
84
85 protected TreeDragGestureListener gestureListener;
86
87 protected DragGestureRecognizer dragGestureRecognizer;
88
89 /*
90 * (non-Javadoc)
91 *
92 * @see org.pushingpixels.lafwidget.LafWidget#requiresCustomLafSupport()
93 */
94 @Override
95 public boolean requiresCustomLafSupport() {
96 return false;
97 }
98
99 /*
100 * (non-Javadoc)
101 *
102 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#installListeners()
103 */
104 @Override
105 public void installListeners() {
106 this.listeners = new EventListenerList();
107
108 this.propertyChangeListener = new PropertyChangeListener() {
109 @Override
110 public void propertyChange(PropertyChangeEvent evt) {
111 if (LafWidget.TREE_AUTO_DND_SUPPORT.equals(evt
112 .getPropertyName())) {
113 Object oldValue = evt.getOldValue();
114 Object newValue = evt.getNewValue();
115 boolean hadDnd = false;
116 if (oldValue instanceof Boolean)
117 hadDnd = (Boolean) oldValue;
118 boolean hasDnd = false;
119 if (newValue instanceof Boolean)
120 hasDnd = (Boolean) newValue;
121
122 if (!hadDnd && hasDnd
123 && TreeDragAndDropWidget.this.jcomp.isEnabled()) {
124 TreeDragAndDropWidget.this.installDnDSupport();
125 }
126 if (hadDnd && !hasDnd) {
127 TreeDragAndDropWidget.this.uninstallDnDSupport();
128 }
129 }
130
131 if ("enabled".equals(evt.getPropertyName())) {
132 boolean wasEnabled = (Boolean) evt.getOldValue();
133 boolean isEnabled = (Boolean) evt.getNewValue();
134 if (!wasEnabled && isEnabled) {
135 if (LafWidgetUtilities
136 .hasAutomaticDnDSupport(TreeDragAndDropWidget.this.jcomp))
137 TreeDragAndDropWidget.this.installDnDSupport();
138 }
139 if (wasEnabled && !isEnabled)
140 TreeDragAndDropWidget.this.uninstallDnDSupport();
141 }
142 }
143 };
144 this.jcomp.addPropertyChangeListener(this.propertyChangeListener);
145
146 if (this.jcomp.isEnabled()
147 && LafWidgetUtilities.hasAutomaticDnDSupport(this.jcomp))
148 this.installDnDSupport();
149 }
150
151 private void installDnDSupport() {
152 if (this.cellRendererChangeListener != null) {
153 // already installed.
154 return;
155 }
156
157 this.cellRendererChangeListener = new PropertyChangeListener() {
158 @Override
159 public void propertyChange(PropertyChangeEvent evt) {
160 String name = evt.getPropertyName();
161
162 if (name.equals(JTree.CELL_RENDERER_PROPERTY)) {
163 TreeCellRenderer renderer = TreeDragAndDropWidget.this.jcomp
164 .getCellRenderer();
165 if (!(renderer instanceof DnDCellRendererProxy)) {
166 TreeDragAndDropWidget.this.rendererProxy = new DnDCellRendererProxy(
167 renderer);
168 TreeDragAndDropWidget.this.jcomp
169 .setCellRenderer(TreeDragAndDropWidget.this.rendererProxy);
170 TreeDragAndDropWidget.this.jcomp.repaint();
171 } else
172 TreeDragAndDropWidget.this.rendererProxy = (DnDCellRendererProxy) renderer;
173 }
174 }
175 };
176
177 this.jcomp.addPropertyChangeListener(this.cellRendererChangeListener);
178 if (this.jcomp.getCellRenderer() != null) {
179 this.jcomp.setCellRenderer(new DnDCellRendererProxy(this.jcomp
180 .getCellRenderer()));
181 }
182 this.dragSource = new DragSource();
183 this.gestureListener = new TreeDragGestureListener();
184 this.dragGestureRecognizer = this.dragSource
185 .createDefaultDragGestureRecognizer(this.jcomp,
186 DnDConstants.ACTION_COPY_OR_MOVE, this.gestureListener);
187 this.dropListener = new TreeDropTargetListener();
188 this.dropTarget = new AutoScrollingTreeDropTarget(this.jcomp,
189 this.dropListener);
190 }
191
192 /*
193 * (non-Javadoc)
194 *
195 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#uninstallListeners()
196 */
197 @Override
198 public void uninstallListeners() {
199 this.jcomp.removePropertyChangeListener(this.propertyChangeListener);
200 this.propertyChangeListener = null;
201
202 this.uninstallDnDSupport();
203 }
204
205 private void uninstallDnDSupport() {
206 if (this.cellRendererChangeListener != null) {
207 this.jcomp
208 .removePropertyChangeListener(this.cellRendererChangeListener);
209 this.cellRendererChangeListener = null;
210 }
211
212 TreeCellRenderer tcl = this.jcomp.getCellRenderer();
213 if (tcl instanceof DnDCellRendererProxy) {
214 TreeCellRenderer origRenderer = ((DnDCellRendererProxy) tcl)
215 .getOriginalTreeCellRenderer();
216 this.jcomp.setCellRenderer(origRenderer);
217 }
218
219 if (this.dropListener != null) {
220 this.dropTarget.removeDropTargetListener(this.dropListener);
221 this.dropListener = null;
222 this.jcomp.setDropTarget(null);
223 }
224
225 if (this.dragGestureRecognizer != null) {
226 this.dragGestureRecognizer
227 .removeDragGestureListener(this.gestureListener);
228 this.gestureListener = null;
229 this.dragGestureRecognizer = null;
230 }
231 }
232
233 /**
234 * Internal class that implements DragGestureListener.
235 */
236 class TreeDragGestureListener implements DragGestureListener {
237 @Override
238 public void dragGestureRecognized(DragGestureEvent dge) {
239 // If tree is disabled then discard drag from it
240 if (!TreeDragAndDropWidget.this.jcomp.isEnabled())
241 return;
242
243 // Select the node that the user is trying to drag, if any.
244 TreePath draggedPath = TreeDragAndDropWidget.this.jcomp
245 .getClosestPathForLocation(dge.getDragOrigin().x, dge
246 .getDragOrigin().y);
247 if (draggedPath == null)
248 return;
249 TreeNode node = (TreeNode) draggedPath.getLastPathComponent();
250 if ((node instanceof MutableTreeNode) && (node.getParent() != null)
251 && (node.getParent() instanceof MutableTreeNode))
252 ;
253 else
254 return;
255
256 // Prepare the node for transfer
257 TransferableTreeNode transferableNode = new TransferableTreeNode(
258 TreeDragAndDropWidget.this.jcomp, (MutableTreeNode) node,
259 TreeDragAndDropWidget.this.jcomp.isExpanded(draggedPath));
260 TreeDragAndDropWidget.this.rendererProxy.setDraggedNode(node);
261
262 // Initialize the drag. If isDragImageSupported then build a
263 // transparent image
264 BufferedImage image = null;
265 Point imageOffset = null;
266 // Create an image with the dragged node.
267 TreeCellRenderer renderer = TreeDragAndDropWidget.this.rendererProxy
268 .getOriginalTreeCellRenderer();
269 Rectangle dragBounds = TreeDragAndDropWidget.this.jcomp
270 .getPathBounds(draggedPath);
271 imageOffset = new Point(dge.getDragOrigin().x - dragBounds.x, dge
272 .getDragOrigin().y
273 - dragBounds.y);
274 Component component = renderer.getTreeCellRendererComponent(
275 TreeDragAndDropWidget.this.jcomp, node, false,
276 TreeDragAndDropWidget.this.jcomp.isExpanded(draggedPath),
277 node.isLeaf(), 0, false);
278 component.setSize(dragBounds.width, dragBounds.height);
279 image = new BufferedImage(dragBounds.width, dragBounds.height,
280 BufferedImage.TYPE_INT_ARGB);
281 Graphics2D g2d = image.createGraphics();
282 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC,
283 0.75f));
284 component.paint(g2d);
285 g2d.dispose();
286
287 // Initiate the drag
288 if (DragSource.isDragImageSupported()) {
289 TreeDragAndDropWidget.this.dragSource.startDrag(dge, null,
290 image, imageOffset, transferableNode,
291 new TreeDragSourceListener());
292 } else {
293 TreeDragAndDropWidget.this.dragSource.startDrag(dge, null,
294 transferableNode, new TreeDragSourceListener());
295 }
296 }
297 }
298
299 /**
300 * Internal class that implements DragSourceListener.
301 */
302 class TreeDragSourceListener implements DragSourceListener {
303 @Override
304 public void dragExit(DragSourceEvent dse) {
305 // dropNode = null;
306 // rendererProxy.setDropNode( null );
307 // tree.repaint();
308 }
309
310 @Override
311 public void dropActionChanged(DragSourceDragEvent dsde) {
312 }
313
314 @Override
315 public void dragOver(DragSourceDragEvent dsde) {
316 }
317
318 @Override
319 public void dragEnter(DragSourceDragEvent dsde) {
320 }
321
322 @Override
323 public void dragDropEnd(DragSourceDropEvent dsde) {
324 TreeDragAndDropWidget.this.resetDragAndDrop();
325 }
326 }
327
328 class TreeDropTargetListener implements DropTargetListener {
329 @Override
330 public void drop(DropTargetDropEvent dtde) {
331 TreePath dropPath = TreeDragAndDropWidget.this.jcomp
332 .getClosestPathForLocation(dtde.getLocation().x, dtde
333 .getLocation().y);
334
335 if (!TreeDragAndDropWidget.this.jcomp.isEnabled()
336 || (dropPath == null)) {
337 dtde.rejectDrop();
338 dtde.dropComplete(false);
339 TreeDragAndDropWidget.this.resetDragAndDrop();
340 return;
341 }
342
343 TreeDragAndDropWidget.this.dropNode = (MutableTreeNode) dropPath
344 .getLastPathComponent();
345
346 // Handle dropping java JVM local objects (tree nodes)
347 try {
348 dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
349 // Are we dropping a JVM local object?
350 TransferableTreeNode ttn = (TransferableTreeNode) dtde
351 .getTransferable().getTransferData(
352 TransferableTreeNode
353 .getJavaJVMLocalObjectFlavor());
354 JTree sourceTree = ttn.getSourceTree();
355 MutableTreeNode sourceNode = ttn.getSourceNode();
356 if (TreeDragAndDropWidget.this.mayDropHere(sourceTree,
357 sourceNode, TreeDragAndDropWidget.this.dropNode)) {
358 dtde
359 .dropComplete(this
360 .dropNodes(
361 ttn.getSourceTree(),
362 ttn.getSourceNode(),
363 TreeDragAndDropWidget.this.jcomp,
364 TreeDragAndDropWidget.this.dropNode,
365 (dtde.getDropAction() & DnDConstants.ACTION_MOVE) != 0));
366 if (ttn.isNodeWasExpanded()) {
367 DefaultTreeModel targetModel = (DefaultTreeModel) TreeDragAndDropWidget.this.jcomp
368 .getModel();
369 TreeDragAndDropWidget.this.jcomp
370 .expandPath(new TreePath(targetModel
371 .getPathToRoot(ttn.getSourceNode())));
372 }
373
374 TreeDragAndDropWidget.this.resetDragAndDrop();
375 } else {
376 try {
377 dtde.rejectDrop();
378 } catch (Exception e) {
379 // An exception may be thrown here if the user leaves
380 // and enters the window.
381 // This is an exceptional case.
382 }
383 dtde.dropComplete(false);
384 TreeDragAndDropWidget.this.resetDragAndDrop();
385 return;
386 }
387 } catch (UnsupportedFlavorException ufe) {
388 // So this is not a JVM local object, maybe it's a String...
389 try {
390 String droppedString = (String) dtde.getTransferable()
391 .getTransferData(DataFlavor.stringFlavor);
392 if (TreeDragAndDropWidget.this.mayDropHere(droppedString,
393 TreeDragAndDropWidget.this.jcomp, dropPath)) {
394 dtde.dropComplete(this.dropString(droppedString));
395 TreeDragAndDropWidget.this.resetDragAndDrop();
396 } else {
397 dtde.rejectDrop();
398 dtde.dropComplete(false);
399 TreeDragAndDropWidget.this.resetDragAndDrop();
400 return;
401 }
402 } catch (Exception exception) {
403 dtde.rejectDrop();
404 dtde.dropComplete(false);
405 TreeDragAndDropWidget.this.resetDragAndDrop();
406 return;
407 }
408 } catch (Exception e) {
409 // ClassCastException: So this is a JVM local object but not a
410 // TransferableTreeNode, right? Well, then just discard
411 // IOException: So there's a problem deserializing this, right?
412 // Well, then just discard
413 e.printStackTrace();
414 dtde.rejectDrop();
415 dtde.dropComplete(true);
416 TreeDragAndDropWidget.this.resetDragAndDrop();
417 return;
418 }
419 }
420
421 private boolean dropString(String droppedString) {
422 // Get the mutable TreeModel
423 DefaultTreeModel model = (DefaultTreeModel) TreeDragAndDropWidget.this.jcomp
424 .getModel();
425
426 // Ask the listeners to handle this drop
427 boolean doItOurselves = true;
428 EventListener[] listeners = TreeDragAndDropWidget.this.listeners
429 .getListeners(StringTreeDnDListener.class);
430 if ((listeners != null) && (listeners.length > 0)) {
431 try {
432 StringTreeDnDEvent event = new StringTreeDnDEvent(
433 droppedString, TreeDragAndDropWidget.this.jcomp,
434 TreeDragAndDropWidget.this.dropNode);
435 for (int i = 0; i < listeners.length; i++) {
436 ((StringTreeDnDListener) listeners[i]).drop(event);
437 }
438 } catch (DnDVetoException exception) {
439 doItOurselves = true;
440 }
441 }
442 if (doItOurselves) {
443 DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(
444 droppedString);
445
446 MutableTreeNode parent = (MutableTreeNode) TreeDragAndDropWidget.this.dropNode
447 .getParent();
448 if (TreeDragAndDropWidget.this.dropNode.isLeaf()) {
449 int index = parent
450 .getIndex(TreeDragAndDropWidget.this.dropNode);
451 model.insertNodeInto(newNode, parent, index + 1);
452 } else {
453 model.insertNodeInto(newNode,
454 TreeDragAndDropWidget.this.dropNode, 0); // dropNode.getChildCount()
455 // );
456 }
457 }
458 return true;
459 }
460
461 private boolean dropNodes(JTree aSourceTree,
462 MutableTreeNode aSourceNode, JTree aTargetTree,
463 MutableTreeNode aDropNode, boolean move) {
464 // Get the mutable TreeModel
465 DefaultTreeModel sourceModel = (DefaultTreeModel) aSourceTree
466 .getModel();
467 DefaultTreeModel targetModel = (DefaultTreeModel) aTargetTree
468 .getModel();
469
470 boolean doItOurselves = true;
471 EventListener[] listeners = TreeDragAndDropWidget.this.listeners
472 .getListeners(TreeTreeDnDListener.class);
473 if ((listeners != null) && (listeners.length > 0)) {
474 try {
475 TreeTreeDnDEvent event = new TreeTreeDnDEvent(aSourceTree,
476 aSourceNode, aTargetTree, aDropNode);
477 for (int i = 0; i < listeners.length; i++) {
478 ((TreeTreeDnDListener) listeners[i]).drop(event);
479 }
480 } catch (DnDVetoException exception) {
481 doItOurselves = false;
482 }
483 }
484 if (doItOurselves) {
485 MutableTreeNode sourceNodeCopy = aSourceNode;
486 if (move) {
487 sourceModel.removeNodeFromParent(aSourceNode);
488 } else {
489 sourceNodeCopy = this.recursivelyCopyNodes(targetModel,
490 aSourceNode);
491 }
492 // Attach the draggedNode into the new parent
493 MutableTreeNode parent = (MutableTreeNode) aDropNode
494 .getParent();
495 if (aDropNode.isLeaf() && (parent != null)) {
496 int index = parent.getIndex(aDropNode);
497 targetModel.insertNodeInto(sourceNodeCopy, parent,
498 index + 1);
499 } else {
500 targetModel.insertNodeInto(sourceNodeCopy, aDropNode, 0);// aDropNode.getChildCount()
501 // );
502 }
503 }
504 return true;
505 }
506
507 private DefaultMutableTreeNode recursivelyCopyNodes(
508 DefaultTreeModel aModel, TreeNode aNode) {
509 DefaultMutableTreeNode copy = new DefaultMutableTreeNode(aNode
510 .toString());
511 copy.setAllowsChildren(aNode.getAllowsChildren());
512 if (aNode.getChildCount() != 0) {
513 Enumeration children = aNode.children();
514 while (children.hasMoreElements()) {
515 TreeNode child = (TreeNode) children.nextElement();
516 DefaultMutableTreeNode childCopy = this
517 .recursivelyCopyNodes(aModel, child);
518 aModel
519 .insertNodeInto(childCopy, copy, copy
520 .getChildCount());
521 childCopy.setParent(copy);
522 }
523 }
524 return copy;
525 }
526
527 @Override
528 public void dragExit(DropTargetEvent dte) {
529 TreeDragAndDropWidget.this.dropNode = null;
530 TreeDragAndDropWidget.this.rendererProxy.setDropNode(null);
531 TreeDragAndDropWidget.this.jcomp.repaint();
532 }
533
534 @Override
535 public void dropActionChanged(DropTargetDragEvent dtde) {
536 }
537
538 private Transferable getTransferable(DropTargetDragEvent dtde) {
539 try {
540 DropTargetContext context = dtde.getDropTargetContext();
541 if (TreeDragAndDropWidget.getTransferableMethod == null) {
542 TreeDragAndDropWidget.getTransferableMethod = context
543 .getClass().getDeclaredMethod("getTransferable",
544 TreeDragAndDropWidget.EMPTY_CLASS_ARRAY);
545 TreeDragAndDropWidget.getTransferableMethod
546 .setAccessible(true);
547 }
548 return (Transferable) TreeDragAndDropWidget.getTransferableMethod
549 .invoke(context,
550 TreeDragAndDropWidget.EMPTY_OBJECT_ARRAY);
551 } catch (Exception e) {
552 e.printStackTrace(System.err);
553 return null;
554 }
555 }
556
557 /** This node to avoid too many invocations to dragOver */
558 private TreeNode lastDragOverNode = null;
559
560 @Override
561 public void dragOver(DropTargetDragEvent dtde) {
562 if (!TreeDragAndDropWidget.this.jcomp.isEnabled()) {
563 dtde.rejectDrag();
564 return;
565 }
566
567 // Is this a valid node for dropping?
568 TreePath dropPath = TreeDragAndDropWidget.this.jcomp
569 .getClosestPathForLocation(dtde.getLocation().x, dtde
570 .getLocation().y);
571
572 TreeNode currentDropNode = (TreeNode) dropPath
573 .getLastPathComponent();
574
575 if ((dropPath == null) || (currentDropNode == null)
576 || currentDropNode.equals(this.lastDragOverNode)) {
577 return;
578 } else {
579 this.lastDragOverNode = currentDropNode;
580 }
581
582 Transferable transferable = this.getTransferable(dtde); // dtde.getTransferable();
583
584 boolean mayDropHere = false;
585
586 try {
587 // WARNING: getTransferable available only on JDK 1.5
588 TransferableTreeNode transferredNode = (TransferableTreeNode) transferable
589 .getTransferData(TransferableTreeNode
590 .getJavaJVMLocalObjectFlavor());
591 JTree sourceTree = transferredNode.getSourceTree();
592 MutableTreeNode sourceNode = transferredNode.getSourceNode();
593 if (TreeDragAndDropWidget.this.mayDropHere(sourceTree,
594 sourceNode, dropPath)) {
595 TreeDragAndDropWidget.this.dropNode = (MutableTreeNode) dropPath
596 .getLastPathComponent();
597 if (!TreeDragAndDropWidget.this.jcomp.isExpanded(dropPath))
598 TreeDragAndDropWidget.this.jcomp.expandPath(dropPath);
599 mayDropHere = true;
600 } else {
601 TreeDragAndDropWidget.this.dropNode = null;
602 }
603 } catch (UnsupportedFlavorException ufe) {
604 // Oh, this is not a TransferableTreeNode, so maybe is a String,
605 // maybe?
606 try {
607 String sourceText = (String) transferable
608 .getTransferData(DataFlavor.stringFlavor);
609 if (TreeDragAndDropWidget.this.mayDropHere(sourceText,
610 TreeDragAndDropWidget.this.jcomp, dropPath)) {
611 TreeDragAndDropWidget.this.dropNode = (MutableTreeNode) dropPath
612 .getLastPathComponent();
613 if (!TreeDragAndDropWidget.this.jcomp
614 .isExpanded(dropPath))
615 TreeDragAndDropWidget.this.jcomp
616 .expandPath(dropPath);
617 mayDropHere = true;
618 } else {
619 TreeDragAndDropWidget.this.dropNode = null;
620 }
621 } catch (Exception e) {
622 // Well, whatever, just discard
623 TreeDragAndDropWidget.this.dropNode = null;
624 }
625 } catch (Exception e) {
626 // IOException: Oh, there's a problem with serialization. Maybe
627 // a classloader issue? Well, ummm... just discard this and say
628 // no
629 // ClassCastException: Oh, user is transferring a JVM object but
630 // not a TransferableTreeNode, well, umm... just discard and say
631 // no
632 TreeDragAndDropWidget.this.dropNode = null;
633 }
634 TreeDragAndDropWidget.this.rendererProxy
635 .setDropAllowed(mayDropHere);
636 TreeDragAndDropWidget.this.rendererProxy
637 .setDropNode((TreeNode) dropPath.getLastPathComponent());
638 TreeDragAndDropWidget.this.jcomp.repaint();
639 if (!mayDropHere) {
640 dtde.rejectDrag();
641 } else {
642 dtde.acceptDrag(dtde.getDropAction());
643 }
644 TreeDragAndDropWidget.this.jcomp.repaint();
645 }
646
647 @Override
648 public void dragEnter(DropTargetDragEvent dtde) {
649 this.dragOver(dtde);
650 // dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE );
651 }
652 }
653
654 /**
655 * Invoked to decide if a given String can be dropped in the last path
656 * component of the given path.
657 *
658 * @param aSourceString
659 * the String being dragged.
660 * @param aPath
661 * the path to drop into.
662 * @return true to allow the drop operation, false otherwise.
663 */
664 private boolean mayDropHere(String aSourceString, JTree aTargetTree,
665 TreePath aPath) {
666 return this.mayDropHere(aSourceString, aTargetTree, (TreeNode) aPath
667 .getLastPathComponent());
668 }
669
670 /**
671 * Invoked to decide if a given String can be dropped in the last path
672 * component of the given path.
673 *
674 * @param aSourceString
675 * the String being dragged.
676 * @return true to allow the drop operation, false otherwise.
677 */
678 private boolean mayDropHere(String aSourceString, JTree aTargetTree,
679 TreeNode aNode) {
680 EventListener[] listeners = this.listeners
681 .getListeners(StringTreeDnDListener.class);
682 if ((listeners != null) && (listeners.length > 0)) {
683 try {
684 StringTreeDnDEvent event = new StringTreeDnDEvent(
685 aSourceString, aTargetTree, aNode);
686 for (int i = 0; i < listeners.length; i++) {
687 ((StringTreeDnDListener) listeners[i]).mayDrop(event);
688 }
689 } catch (DnDVetoException exception) {
690 return false;
691 }
692 }
693 return true;
694 }
695
696 /**
697 * Invoked to decide if draggedNode can be dropped in the last path
698 * component of the given path.
699 *
700 * @param aSourceTree
701 * the source tree.
702 * @param aSourceNode
703 * the source node.
704 * @param aPath
705 * the path to drop into.
706 * @return true to allow the drop operation, false otherwise.
707 */
708 private boolean mayDropHere(JTree aSourceTree, MutableTreeNode aSourceNode,
709 TreePath aPath) {
710 if (aPath == null)
711 return false;
712 return this.mayDropHere(aSourceTree, aSourceNode, (TreeNode) aPath
713 .getLastPathComponent());
714 }
715
716 /**
717 * Invoked to decide if draggedNode can be dropped into aNode.
718 *
719 * @param aSourceTree
720 * the source tree.
721 * @param aSourceNode
722 * the source node.
723 * @param aNode
724 * the node to drop into.
725 * @return true to allow the drop operation, false to avoid it.
726 */
727 private boolean mayDropHere(JTree aSourceTree, MutableTreeNode aSourceNode,
728 TreeNode aNode) {
729 boolean mayDropHere = (aNode != aSourceNode)
730 && (aNode instanceof MutableTreeNode)
731 && ((aNode.getParent() == null) || (aNode.getParent() instanceof MutableTreeNode))
732 && (this.jcomp.getModel() instanceof DefaultTreeModel)
733 && !((this.jcomp == aSourceTree) && TreeDragAndDropWidget
734 .isAncestorOf(aSourceNode, aNode));
735
736 if (mayDropHere) {
737 // Ask listeners
738 EventListener[] listeners = this.listeners
739 .getListeners(TreeTreeDnDListener.class);
740 if ((listeners != null) && (listeners.length > 0)) {
741 try {
742 TreeTreeDnDEvent event = new TreeTreeDnDEvent(aSourceTree,
743 aSourceNode, this.jcomp, aNode);
744 for (int i = 0; i < listeners.length; i++) {
745 ((TreeTreeDnDListener) listeners[i]).mayDrop(event);
746 }
747 } catch (DnDVetoException exception) {
748 mayDropHere = false;
749 }
750 }
751 }
752
753 return mayDropHere;
754 }
755
756 /**
757 * See if aPossibleParent is ancestor of aNode
758 */
759 private static boolean isAncestorOf(TreeNode aPossibleParent, TreeNode aNode) {
760 if ((aPossibleParent == null) || (aNode.getParent() == null))
761 return false;
762 else if (aNode.getParent() == aPossibleParent)
763 return true;
764 else
765 return TreeDragAndDropWidget.isAncestorOf(aPossibleParent, aNode
766 .getParent());
767 }
768
769 private void resetDragAndDrop() {
770 this.dropNode = null;
771 this.rendererProxy.setDraggedNode(null);
772 this.rendererProxy.setDropAllowed(false);
773 this.rendererProxy.setDropNode(null);
774 this.jcomp.repaint();
775 }
776 }