1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49:
50:
53: public class DragSourceContext
54: implements DragSourceListener, DragSourceMotionListener, Serializable
55: {
56:
59: static final long serialVersionUID = -115407898692194719L;
60:
61: protected static final int DEFAULT = 0;
62: protected static final int ENTER = 1;
63: protected static final int OVER = 2;
64: protected static final int CHANGED = 3;
65:
66: private DragSourceContextPeer peer;
67: private Cursor cursor;
68: private Transferable transferable;
69: private DragGestureEvent trigger;
70: private DragSourceListener dragSourceListener;
71: private boolean useCustomCursor;
72: private int sourceActions;
73: private Image image;
74: private Point offset;
75:
76:
86: public DragSourceContext (DragSourceContextPeer peer,
87: DragGestureEvent trigger, Cursor cursor,
88: Image image, Point offset, Transferable trans,
89: DragSourceListener dsl)
90: {
91: if (peer == null
92: || trigger == null || trans == null
93: || (image != null && offset == null))
94: throw new NullPointerException ();
95:
96: if (trigger.getComponent () == null
97: || trigger.getDragSource () == null
98: || trigger.getDragAction () == DnDConstants.ACTION_NONE
99: || trigger.getSourceAsDragGestureRecognizer ()
100: .getSourceActions () == DnDConstants.ACTION_NONE)
101: throw new IllegalArgumentException ();
102:
103: this.peer = peer;
104: this.trigger = trigger;
105: this.cursor = cursor;
106: this.image = image;
107: this.offset = offset;
108: this.transferable = trans;
109: this.dragSourceListener = dsl;
110: this.sourceActions = trigger.getSourceAsDragGestureRecognizer().getSourceActions();
111:
112: setCursor(cursor);
113: updateCurrentCursor(trigger.getDragAction(), sourceActions, DEFAULT);
114: }
115:
116:
122: public DragSource getDragSource()
123: {
124: return trigger.getDragSource ();
125: }
126:
127:
132: public Component getComponent()
133: {
134: return trigger.getComponent ();
135: }
136:
137:
142: public DragGestureEvent getTrigger()
143: {
144: return trigger;
145: }
146:
147:
152: public int getSourceActions()
153: {
154: if (sourceActions == 0)
155: sourceActions = trigger.getSourceAsDragGestureRecognizer().getSourceActions();
156: return sourceActions;
157: }
158:
159:
165: public void setCursor(Cursor cursor)
166: {
167: if (cursor == null)
168: useCustomCursor = false;
169: else
170: useCustomCursor = true;
171: this.cursor = cursor;
172: peer.setCursor(cursor);
173: }
174:
175:
181: public Cursor getCursor()
182: {
183: return cursor;
184: }
185:
186:
192: public void addDragSourceListener (DragSourceListener dsl)
193: throws TooManyListenersException
194: {
195: if (dragSourceListener != null)
196: throw new TooManyListenersException ();
197:
198: dragSourceListener = dsl;
199: }
200:
201: public void removeDragSourceListener (DragSourceListener dsl)
202: {
203: if (dragSourceListener == dsl)
204: dragSourceListener = null;
205: }
206:
207:
210: public void transferablesFlavorsChanged()
211: {
212: peer.transferablesFlavorsChanged();
213: }
214:
215:
221: public void dragEnter(DragSourceDragEvent e)
222: {
223: if (dragSourceListener != null)
224: dragSourceListener.dragEnter(e);
225:
226: DragSource ds = getDragSource();
227: DragSourceListener[] dsl = ds.getDragSourceListeners();
228: for (int i = 0; i < dsl.length; i++)
229: dsl[i].dragEnter(e);
230:
231: updateCurrentCursor(e.getDropAction(), e.getTargetActions(), ENTER);
232: }
233:
234:
240: public void dragOver(DragSourceDragEvent e)
241: {
242: if (dragSourceListener != null)
243: dragSourceListener.dragOver(e);
244:
245: DragSource ds = getDragSource();
246: DragSourceListener[] dsl = ds.getDragSourceListeners();
247: for (int i = 0; i < dsl.length; i++)
248: dsl[i].dragOver(e);
249:
250: updateCurrentCursor(e.getDropAction(), e.getTargetActions(), OVER);
251: }
252:
253:
259: public void dragExit(DragSourceEvent e)
260: {
261: if (dragSourceListener != null)
262: dragSourceListener.dragExit(e);
263:
264: DragSource ds = getDragSource();
265: DragSourceListener[] dsl = ds.getDragSourceListeners();
266: for (int i = 0; i < dsl.length; i++)
267: dsl[i].dragExit(e);
268:
269: updateCurrentCursor(DnDConstants.ACTION_NONE, DnDConstants.ACTION_NONE,
270: DEFAULT);
271: }
272:
273:
279: public void dropActionChanged(DragSourceDragEvent e)
280: {
281: if (dragSourceListener != null)
282: dragSourceListener.dropActionChanged(e);
283:
284: DragSource ds = getDragSource();
285: DragSourceListener[] dsl = ds.getDragSourceListeners();
286: for (int i = 0; i < dsl.length; i++)
287: dsl[i].dropActionChanged(e);
288:
289: updateCurrentCursor(e.getDropAction(), e.getTargetActions(), CHANGED);
290: }
291:
292:
298: public void dragDropEnd(DragSourceDropEvent e)
299: {
300: if (dragSourceListener != null)
301: dragSourceListener.dragDropEnd(e);
302:
303: DragSource ds = getDragSource();
304: DragSourceListener[] dsl = ds.getDragSourceListeners();
305: for (int i = 0; i < dsl.length; i++)
306: dsl[i].dragDropEnd(e);
307: }
308:
309:
314: public void dragMouseMoved(DragSourceDragEvent e)
315: {
316: DragSource ds = getDragSource();
317: DragSourceMotionListener[] dsml = ds.getDragSourceMotionListeners();
318: for (int i = 0; i < dsml.length; i++)
319: dsml[i].dragMouseMoved(e);
320: }
321:
322:
327: public Transferable getTransferable()
328: {
329: return transferable;
330: }
331:
332:
341: protected void updateCurrentCursor(int dropOp, int targetAct, int status)
342: {
343: if (! useCustomCursor)
344: {
345: Cursor newCursor = null;
346: switch (status)
347: {
348: default:
349: targetAct = DnDConstants.ACTION_NONE;
350: case ENTER:
351: case CHANGED:
352: case OVER:
353: int action = dropOp & targetAct;
354: if (action == DnDConstants.ACTION_NONE)
355: {
356: if ((dropOp & DnDConstants.ACTION_LINK) != 0)
357: newCursor = DragSource.DefaultLinkNoDrop;
358: else if ((dropOp & DnDConstants.ACTION_MOVE) != 0)
359: newCursor = DragSource.DefaultMoveNoDrop;
360: else
361: newCursor = DragSource.DefaultCopyNoDrop;
362: }
363: else
364: {
365: if ((dropOp & DnDConstants.ACTION_LINK) != 0)
366: newCursor = DragSource.DefaultLinkDrop;
367: else if ((dropOp & DnDConstants.ACTION_MOVE) != 0)
368: newCursor = DragSource.DefaultMoveDrop;
369: else
370: newCursor = DragSource.DefaultCopyDrop;
371: }
372: }
373:
374: if (cursor == null || ! cursor.equals(newCursor))
375: {
376: cursor = newCursor;
377: DragSourceContextPeer p = peer;
378: if (p != null)
379: p.setCursor(cursor);
380: }
381: }
382: }
383: }