1 package org.catacomb.druid.swing.dnd;
2
3
4 import org.catacomb.druid.swing.DDropTextField;
5 import org.catacomb.druid.swing.ImageDragSource;
6 import org.catacomb.report.E;
7
8 import java.awt.Graphics;
9 import java.awt.Point;
10 import java.awt.Rectangle;
11 import java.awt.dnd.*;
12 import java.awt.image.BufferedImage;
13
14 import javax.swing.JTextField;
15
16
17 public class TextFieldDropTarget implements DropTargetListener {
18
19 DropTarget target;
20
21 DDropTextField ddtField;
22
23 JTextField jtField;
24
25 Rectangle dragBounds;
26
27
28 public TextFieldDropTarget(JTextField jtf, DDropTextField ddtf) {
29 jtField = jtf;
30 ddtField = ddtf;
31 target = new DropTarget(jtf, this);
32 }
33
34
35 public void dragEnter(DropTargetDragEvent dtde) {
36
37 dtde.acceptDrag(dtde.getDropAction());
38
39 }
40
41 public void dragOver(DropTargetDragEvent dtde) {
42
43 if (false) {
44
45 } else {
46
47 if (dragBounds != null) {
48 jtField.paintImmediately(dragBounds);
49 } else {
50 dragBounds = new Rectangle();
51 }
52
53 Point p = dtde.getLocation();
54
55 Object obj = DragAndDrop.getDnD();
56
57 if (obj instanceof ImageDragSource) {
58 ImageDragSource ids = (ImageDragSource)obj;
59
60 BufferedImage bim = ids.getDragImage();
61 Point poff = ids.getDragImageOffset();
62
63 int bw = bim.getWidth();
64 int bh = bim.getHeight();
65
66 if (bim == null) {
67 E.warning("no drag image?");
68 } else {
69
70
71 dragBounds.setRect(p.x - poff.x, p.y - poff.y, bw, bh);
72
73 Graphics g = jtField.getGraphics();
74
75
76
77 g.drawImage(bim, (int)(dragBounds.getX()), (int)(dragBounds.getY()), null);
78
79
80 }
81 } else {
82 E.warning("wrong type of source " + obj);
83 }
84 }
85 dtde.acceptDrag(dtde.getDropAction());
86
87 }
88
89
90
91
92
93
94 public void dragExit(DropTargetEvent dte) {
95 if (dragBounds != null) {
96 jtField.paintImmediately(dragBounds);
97 }
98 }
99
100
101 public void dropActionChanged(DropTargetDragEvent dtde) {
102 }
103
104
105 public void drop(DropTargetDropEvent dtde) {
106
107 Object obj = DragAndDrop.getDnD().getDragSource();
108 ddtField.setDropee(obj);
109
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 }
141
142