View Javadoc

1   package org.catacomb.druid.swing;
2   
3   
4   
5   import org.catacomb.druid.swing.dnd.DdndTransferable;
6   import org.catacomb.druid.swing.dnd.DragAndDrop;
7   import org.catacomb.report.E;
8   
9   import java.awt.dnd.*;
10  
11  import javax.swing.tree.TreePath;
12  
13  import java.awt.Point;
14  import java.awt.Rectangle;
15  
16  import java.awt.image.*;
17  import java.awt.Graphics2D;
18  import java.awt.Color;
19  import java.awt.AlphaComposite;
20  import java.awt.Cursor;
21  
22  public class DTreeDragSource implements DragSourceListener, DragSourceMotionListener, DragGestureListener {
23  
24      DragSource source;
25  
26      DragGestureRecognizer recognizer;
27  
28      DdndTransferable transferable;
29  
30  
31      DTree sourceTree;
32  
33      BufferedImage dragImg;
34      Point imgOffset;
35  
36  
37      public DTreeDragSource(DTree tree, int actions) {
38          sourceTree = tree;
39          source = new DragSource();
40          source.addDragSourceMotionListener(this);
41          recognizer = source.createDefaultDragGestureRecognizer(sourceTree,
42                       actions, this);
43      }
44  
45      /*
46       * Drag Gesture Handler
47       */
48      public void dragGestureRecognized(DragGestureEvent dge) {
49          TreePath path = sourceTree.getSelectionPath();
50          if ((path == null) || (path.getPathCount() <= 1)) {
51              // We can't move the root node or an empty selection
52              return;
53          }
54  
55          String sdrag = path.getLastPathComponent().toString();
56  
57  
58  
59          transferable = new DdndTransferable(path);
60  
61          Point ptDragOrigin = dge.getDragOrigin();
62          //  TreePath path = getPathForLocation(ptDragOrigin.x, ptDragOrigin.y);
63          Rectangle raPath = sourceTree.getPathBounds(path);
64  
65  
66          int rw = (int)(raPath.getWidth());
67          int rh = (int)(raPath.getHeight());
68          dragImg = new BufferedImage(rw, rh, BufferedImage.TYPE_INT_ARGB);
69  
70          Graphics2D g2 = dragImg.createGraphics();
71  
72          g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0.5f));
73  
74          g2.setColor(Color.white);
75          g2.fillRect(0, 0, rw, rh);
76  
77          g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));
78          g2.setColor(Color.blue);
79          g2.drawString(sdrag, 14, 16);
80  
81          imgOffset = new Point(ptDragOrigin.x-raPath.x, ptDragOrigin.y-raPath.y);
82  
83          DragAndDrop.getDnD().setDragImage(dragImg, imgOffset);
84  
85          // source.startDrag(dge, DragSource.DefaultLinkDrop, dragImg, imgOffset, transferable, this);
86          source.startDrag(dge, Cursor.getPredefinedCursor(Cursor.HAND_CURSOR), dragImg, imgOffset, transferable, this);
87  
88      }
89  
90  
91  
92  
93  
94      /*
95       * Drag Event Handlers
96       */
97      public void dragEnter(DragSourceDragEvent dsde) {
98      }
99  
100     public void dragExit(DragSourceEvent dse) {
101     }
102 
103     public void dragOver(DragSourceDragEvent dsde) {
104     }
105 
106     public void dropActionChanged(DragSourceDragEvent dsde) {
107         System.out.println("Action: " + dsde.getDropAction());
108         System.out.println("Target Action: " + dsde.getTargetActions());
109         System.out.println("User Action: " + dsde.getUserAction());
110     }
111 
112 
113 
114     public void dragDropEnd(DragSourceDropEvent dsde) {
115         /*
116          * to support move or copy, we have to check which occurred:
117          */
118         E.info("Drop Action: " + dsde.getDropAction());
119         if (dsde.getDropSuccess()
120                 && (dsde.getDropAction() == DnDConstants.ACTION_MOVE)) {
121             E.info("was move - should remove original");
122         }
123 
124         /*
125          * to support move only... if (dsde.getDropSuccess()) {
126          * ((DefaultTreeModel)sourceTree.getModel()).removeNodeFromParent(oldNode); }
127          */
128     }
129 
130     public void dragMouseMoved(DragSourceDragEvent dsde) {
131         //E.info("drag moved " + dsde);
132 
133     }
134 }
135 
136