View Javadoc

1   package org.catacomb.druid.swing.dnd;
2   
3   
4   import org.catacomb.druid.swing.DTextCanvas;
5   
6   import java.awt.*;
7   import java.awt.dnd.*;
8   import java.awt.image.BufferedImage;
9   
10  
11  public class RegionDragSource implements DragSourceListener, DragSourceMotionListener,
12      DragGestureListener {
13  
14      DragSource source;
15      DragGestureRecognizer recognizer;
16      RegionBoard regionBoard;
17      DTextCanvas sourceCanvas;
18  
19  
20  
21      public RegionDragSource(RegionBoard rb, int actions) {
22          regionBoard = rb;
23          sourceCanvas = rb.getCanvas();
24          source = new DragSource();
25          source.addDragSourceMotionListener(this);
26          recognizer = source.createDefaultDragGestureRecognizer(sourceCanvas, actions, this);
27      }
28  
29  
30  
31      public void dragGestureRecognized(DragGestureEvent dge) {
32          Region reg = regionBoard.getHoverRegion();
33          if (reg == null) {
34  //              E.info("drag recognized, but no region");
35          } else {
36              initiateDrag(reg, dge.getDragOrigin(), dge);
37          }
38      }
39  
40  
41  
42      public void initiateDrag(Region reg, Point ptDragOrigin, DragGestureEvent trigger) {
43          String sdrag = reg.getText();
44  
45          DdndTransferable transferable = new DdndTransferable(null); // ERR
46          Rectangle raPath = new Rectangle(reg.getX(), reg.getY(), reg.getW(), reg.getH());
47  
48          int rw = (int)(raPath.getWidth());
49          int rh = (int)(raPath.getHeight());
50          BufferedImage dragImg = new BufferedImage(rw, rh, BufferedImage.TYPE_INT_ARGB);
51          Graphics2D g2 = dragImg.createGraphics();
52  
53          rw = g2.getFontMetrics().stringWidth(sdrag) + 8;
54          dragImg = new BufferedImage(rw, rh, BufferedImage.TYPE_INT_ARGB);
55          g2 = dragImg.createGraphics();
56  
57  
58          g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0.5f));
59  
60          g2.setColor(Color.white);
61          g2.fillRect(0, 0, rw, rh);
62  
63          g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));
64          g2.setColor(Color.blue);
65          g2.drawString(sdrag, 4, 16);
66  
67          Point imgOffset = new Point(ptDragOrigin.x - raPath.x, ptDragOrigin.y - raPath.y);
68  
69  
70          DragAndDrop.getDnD().setDragSource(reg.getRef());
71          DragAndDrop.getDnD().setDragImage(dragImg, imgOffset);
72  
73  
74  
75          // source.startDrag(dge, DragSource.DefaultLinkDrop, dragImg,
76          // imgOffset, transferable, this);
77          source.startDrag(trigger, Cursor.getPredefinedCursor(Cursor.HAND_CURSOR), dragImg, imgOffset, transferable, this);
78  
79      }
80  
81  
82  
83      /*
84       * Drag Event Handlers
85       */
86      public void dragEnter(DragSourceDragEvent dsde) {
87      }
88  
89  
90      public void dragExit(DragSourceEvent dse) {
91      }
92  
93  
94      public void dragOver(DragSourceDragEvent dsde) {
95      }
96  
97  
98      public void dropActionChanged(DragSourceDragEvent dsde) {
99          System.out.println("Action: " + dsde.getDropAction());
100         System.out.println("Target Action: " + dsde.getTargetActions());
101         System.out.println("User Action: " + dsde.getUserAction());
102     }
103 
104 
105 
106     public void dragDropEnd(DragSourceDropEvent dsde) {
107 
108         // if was move, remove source
109 
110         /*
111         E.info("Drop Action: " + dsde.getDropAction());
112         if (dsde.getDropSuccess() && (dsde.getDropAction() == DnDConstants.ACTION_MOVE)) {
113            E.info("was move - should remove original");
114         }
115         */
116 
117     }
118 
119 
120     public void dragMouseMoved(DragSourceDragEvent dsde) {
121         // E.info("drag moved " + dsde);
122 
123     }
124 }