1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56:
57:
61: public class DropTarget
62: implements DropTargetListener, EventListener, Serializable
63: {
64:
67: private static final long serialVersionUID = -6283860791671019047L;
68:
69: protected static class DropTargetAutoScroller
70: implements ActionListener
71: {
72: private Component component;
73: private Point point;
74:
75: protected DropTargetAutoScroller (Component c, Point p)
76: {
77: component = c;
78: point = p;
79: }
80:
81: protected void updateLocation (Point newLocn)
82: {
83: point = newLocn;
84: }
85:
86: protected void stop ()
87: throws NotImplementedException
88: {
89:
90: }
91:
92: public void actionPerformed (ActionEvent e)
93: throws NotImplementedException
94: {
95:
96: }
97: }
98:
99: private Component component;
100: private FlavorMap flavorMap;
101: private int actions;
102: private DropTargetPeer peer;
103: private DropTargetContext dropTargetContext;
104: private DropTargetListener dropTargetListener;
105: private DropTarget.DropTargetAutoScroller autoscroller;
106: private boolean active = true;
107:
108:
114: public DropTarget ()
115: {
116: this (null, 0, null, true, null);
117: }
118:
119:
125: public DropTarget (Component c, DropTargetListener dtl)
126: {
127: this (c, 0, dtl, true, null);
128: }
129:
130:
136: public DropTarget (Component c, int i, DropTargetListener dtl)
137: {
138: this (c, i, dtl, true, null);
139: }
140:
141:
147: public DropTarget (Component c, int i, DropTargetListener dtl, boolean b)
148: {
149: this (c, i, dtl, b, null);
150: }
151:
152:
158: public DropTarget (Component c, int i, DropTargetListener dtl, boolean b,
159: FlavorMap fm)
160: {
161: if (GraphicsEnvironment.isHeadless ())
162: throw new HeadlessException ();
163:
164: setComponent(c);
165: setDefaultActions(i);
166: dropTargetListener = dtl;
167: flavorMap = fm;
168:
169: setActive (b);
170:
171: if (c != null)
172: c.setDropTarget(this);
173: }
174:
175:
178: public void setComponent (Component c)
179: {
180: component = c;
181: }
182:
183:
186: public Component getComponent ()
187: {
188: return component;
189: }
190:
191:
194: public void setDefaultActions (int ops)
195: {
196: actions = ops;
197: }
198:
199:
202: public int getDefaultActions ()
203: {
204: return actions;
205: }
206:
207: public void setActive (boolean active)
208: {
209: this.active = active;
210: }
211:
212: public boolean isActive()
213: {
214: return active;
215: }
216:
217:
225: public void addDropTargetListener (DropTargetListener dtl)
226: throws TooManyListenersException
227: {
228: if (dropTargetListener != null)
229: throw new TooManyListenersException ();
230:
231: dropTargetListener = dtl;
232: }
233:
234: public void removeDropTargetListener(DropTargetListener dtl)
235: {
236: if (dropTargetListener != null)
237: dropTargetListener = null;
238: }
239:
240: public void dragEnter(DropTargetDragEvent dtde)
241: {
242: if (dropTargetListener != null)
243: dropTargetListener.dragEnter(dtde);
244: }
245:
246: public void dragOver(DropTargetDragEvent dtde)
247: {
248: if (dropTargetListener != null)
249: dropTargetListener.dragOver(dtde);
250: }
251:
252: public void dropActionChanged(DropTargetDragEvent dtde)
253: {
254: if (dropTargetListener != null)
255: dropTargetListener.dropActionChanged(dtde);
256: }
257:
258: public void dragExit(DropTargetEvent dte)
259: {
260: if (dropTargetListener != null)
261: dropTargetListener.dragExit(dte);
262: }
263:
264: public void drop(DropTargetDropEvent dtde)
265: {
266: if (dropTargetListener != null)
267: dropTargetListener.drop(dtde);
268: }
269:
270: public FlavorMap getFlavorMap()
271: {
272: return flavorMap;
273: }
274:
275: public void setFlavorMap(FlavorMap fm)
276: {
277: flavorMap = fm;
278: }
279:
280: public void addNotify(ComponentPeer p)
281: {
282: Component c = component;
283: while (c != null && p instanceof LightweightPeer)
284: {
285: p = c.getPeer();
286: c = c.getParent();
287: }
288:
289: if (p instanceof DropTargetPeer)
290: {
291: peer = ((DropTargetPeer) p);
292: peer.addDropTarget(this);
293: }
294: else
295: peer = null;
296: }
297:
298: public void removeNotify(ComponentPeer p)
299: {
300: ((DropTargetPeer) peer).removeDropTarget(this);
301: peer = null;
302: p = null;
303: }
304:
305: public DropTargetContext getDropTargetContext()
306: {
307: if (dropTargetContext == null)
308: dropTargetContext = createDropTargetContext ();
309:
310: return dropTargetContext;
311: }
312:
313: protected DropTargetContext createDropTargetContext()
314: {
315: if (dropTargetContext == null)
316: dropTargetContext = new DropTargetContext (this);
317:
318: return dropTargetContext;
319: }
320:
321: protected DropTarget.DropTargetAutoScroller createDropTargetAutoScroller
322: (Component c, Point p)
323: {
324: if (autoscroller == null)
325: autoscroller = new DropTarget.DropTargetAutoScroller (c, p);
326:
327: return autoscroller;
328: }
329:
330: protected void initializeAutoscrolling(Point p)
331: {
332: createDropTargetAutoScroller (component, p);
333: }
334:
335: protected void updateAutoscroll(Point dragCursorLocn)
336: {
337: if (autoscroller != null)
338: autoscroller.updateLocation(dragCursorLocn);
339: }
340:
341: protected void clearAutoscroll()
342: {
343: autoscroller = null;
344: }
345: }