View Javadoc

1   package org.catacomb.druid.swing.dnd;
2   
3   
4   import org.catacomb.druid.swing.DTextCanvas;
5   import org.catacomb.druid.swing.ImageDragSource;
6   import org.catacomb.report.E;
7   
8   import java.awt.Color;
9   import java.awt.Graphics;
10  import java.awt.Point;
11  import java.awt.Rectangle;
12  import java.awt.dnd.*;
13  import java.awt.image.BufferedImage;
14  
15  
16  
17  
18  public class RegionDropTarget implements DropTargetListener {
19  
20      DropTarget target;
21  
22      RegionBoard regionBoard;
23  
24      DTextCanvas textCanvas;
25  
26      Rectangle dragBounds;
27  
28  
29      Region previousDragRegion;
30  
31  
32      public RegionDropTarget(RegionBoard rb) {
33          regionBoard = rb;
34          textCanvas = rb.getCanvas();
35          target = new DropTarget(textCanvas, this);
36      }
37  
38  
39      public void dragEnter(DropTargetDragEvent dtde) {
40  
41          dtde.acceptDrag(dtde.getDropAction());
42  
43          /*
44          TreeNode node = getNodeForEvent(dtde);
45          if (node.isLeaf()) {
46           dtde.rejectDrag();
47          } else {
48           // start by supporting move operations
49           //dtde.acceptDrag(DnDConstants.ACTION_MOVE);
50           dtde.acceptDrag(dtde.getDropAction());
51          }
52          */
53      }
54  
55      public void dragOver(DropTargetDragEvent dtde) {
56  
57          if (false) { // (DragSource.isDragImageSupported()) {
58  
59          } else {
60  
61              if (dragBounds != null) {
62                  textCanvas.paintImmediately(dragBounds);
63              } else {
64                  dragBounds = new Rectangle();
65              }
66  
67              Point p = dtde.getLocation();
68  
69              Object obj = DragAndDrop.getDnD();
70  
71              if (obj instanceof ImageDragSource) {
72                  ImageDragSource ids = (ImageDragSource)obj;
73  
74                  BufferedImage bim = ids.getDragImage();
75                  Point poff = ids.getDragImageOffset();
76  
77                  int bw = bim.getWidth();
78                  int bh = bim.getHeight();
79  
80                  if (bim == null) {
81                      E.warning("no drag image?");
82                  } else {
83  
84                      // And remember where we are about to draw the new ghost image
85                      dragBounds.setRect(p.x - poff.x, p.y - poff.y, bw, bh);
86  
87                      Graphics g = textCanvas.getGraphics();
88  
89                      dragAt(g, p.x - poff.x + bw/2, p.y - poff.y + bh/2);
90  
91                      g.drawImage(bim, (int)(dragBounds.getX()), (int)(dragBounds.getY()), null);
92  
93  
94                  }
95              } else {
96                  E.warning("wrong type of source " + obj);
97              }
98          }
99          dtde.acceptDrag(dtde.getDropAction());
100 
101     }
102 
103 
104 
105 
106     public void dragAt(Graphics g, int i, int j) {
107         clearDragEcho();
108 
109         Region reg = regionBoard.getDragOverRegion(i, j);
110         if (reg != null) {
111             if (reg.acceptsDrops()) {
112                 g.setColor(Color.green);
113                 g.drawRect(reg.getX(), reg.getY(), reg.getW(), reg.getH());
114                 g.drawRect(reg.getX()+1, reg.getY()+1, reg.getW()-2, reg.getH()-2);
115             }
116             previousDragRegion = reg;
117         }
118     }
119 
120 
121     public void clearDragEcho() {
122         if (previousDragRegion != null) {
123             regionBoard.unecho(previousDragRegion);
124             previousDragRegion = null;
125         }
126     }
127 
128 
129 
130 
131     public void dragExit(DropTargetEvent dte) {
132         if (dragBounds != null) {
133             textCanvas.paintImmediately(dragBounds);
134         }
135     }
136 
137 
138     public void dropActionChanged(DropTargetDragEvent dtde) {
139     }
140 
141 
142     public void drop(DropTargetDropEvent dtde) {
143 
144 
145         //   Point pt = dtde.getLocation();
146         //   DropTargetContext dtc = dtde.getDropTargetContext();
147 
148         Region dropReg = regionBoard.getDragOverRegion();
149         Object dropee = DragAndDrop.getDnD().getDragSource();
150 
151         if (dropReg == null) {
152             regionBoard.dropGeneral(dropee);
153 
154         } else if (dropReg.acceptsDrops()) {
155             regionBoard.dropOn(dropee, dropReg);
156         }
157 
158     }
159 
160 
161 }
162