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:
52:
55: public class DragSourceContext
56: implements DragSourceListener, DragSourceMotionListener, Serializable
57: {
58:
61: static final long serialVersionUID = -115407898692194719L;
62:
63: protected static final int DEFAULT = 0;
64: protected static final int ENTER = 1;
65: protected static final int OVER = 2;
66: protected static final int CHANGED = 3;
67:
68: private DragSourceContextPeer peer;
69: private Cursor cursor;
70: private Transferable transferable;
71: private DragGestureEvent trigger;
72: private DragSourceListener dragSourceListener;
73: private boolean useCustomCursor;
74: private int sourceActions;
75: private Image image;
76: private Point offset;
77:
78:
88: public DragSourceContext (DragSourceContextPeer peer,
89: DragGestureEvent trigger, Cursor cursor,
90: Image image, Point offset, Transferable trans,
91: DragSourceListener dsl)
92: {
93: if (peer == null
94: || trigger == null || trans == null
95: || (image != null && offset == null))
96: throw new NullPointerException ();
97:
98: if (trigger.getComponent () == null
99: || trigger.getDragSource () == null
100: || trigger.getDragAction () == DnDConstants.ACTION_NONE
101: || trigger.getSourceAsDragGestureRecognizer ()
102: .getSourceActions () == DnDConstants.ACTION_NONE)
103: throw new IllegalArgumentException ();
104:
105: this.peer = peer;
106: this.trigger = trigger;
107: this.cursor = cursor;
108: this.image = image;
109: this.offset = offset;
110: this.transferable = trans;
111: this.dragSourceListener = dsl;
112: this.sourceActions = trigger.getSourceAsDragGestureRecognizer().getSourceActions();
113:
114: setCursor(cursor);
115: updateCurrentCursor(trigger.getDragAction(), sourceActions, DEFAULT);
116: }
117:
118:
124: public DragSource getDragSource()
125: {
126: return trigger.getDragSource ();
127: }
128:
129:
134: public Component getComponent()
135: {
136: return trigger.getComponent ();
137: }
138:
139:
144: public DragGestureEvent getTrigger()
145: {
146: return trigger;
147: }
148:
149:
154: public int getSourceActions()
155: {
156: if (sourceActions == 0)
157: sourceActions = trigger.getSourceAsDragGestureRecognizer().getSourceActions();
158: return sourceActions;
159: }
160:
161:
167: public void setCursor(Cursor cursor)
168: {
169: if (cursor == null)
170: useCustomCursor = false;
171: else
172: useCustomCursor = true;
173: this.cursor = cursor;
174: peer.setCursor(cursor);
175: }
176:
177:
183: public Cursor getCursor()
184: {
185: return cursor;
186: }
187:
188:
194: public void addDragSourceListener (DragSourceListener dsl)
195: throws TooManyListenersException
196: {
197: if (dragSourceListener != null)
198: throw new TooManyListenersException ();
199:
200: dragSourceListener = dsl;
201: }
202:
203: public void removeDragSourceListener (DragSourceListener dsl)
204: {
205: if (dragSourceListener == dsl)
206: dragSourceListener = null;
207: }
208:
209:
212: public void transferablesFlavorsChanged()
213: {
214: peer.transferablesFlavorsChanged();
215: }
216:
217:
223: public void dragEnter(DragSourceDragEvent e)
224: {
225: if (dragSourceListener != null)
226: dragSourceListener.dragEnter(e);
227:
228: DragSource ds = getDragSource();
229: DragSourceListener[] dsl = ds.getDragSourceListeners();
230: for (int i = 0; i < dsl.length; i++)
231: dsl[i].dragEnter(e);
232:
233: updateCurrentCursor(e.getDropAction(), e.getTargetActions(), ENTER);
234: }
235:
236:
242: public void dragOver(DragSourceDragEvent e)
243: {
244: if (dragSourceListener != null)
245: dragSourceListener.dragOver(e);
246:
247: DragSource ds = getDragSource();
248: DragSourceListener[] dsl = ds.getDragSourceListeners();
249: for (int i = 0; i < dsl.length; i++)
250: dsl[i].dragOver(e);
251:
252: updateCurrentCursor(e.getDropAction(), e.getTargetActions(), OVER);
253: }
254:
255:
261: public void dragExit(DragSourceEvent e)
262: {
263: if (dragSourceListener != null)
264: dragSourceListener.dragExit(e);
265:
266: DragSource ds = getDragSource();
267: DragSourceListener[] dsl = ds.getDragSourceListeners();
268: for (int i = 0; i < dsl.length; i++)
269: dsl[i].dragExit(e);
270:
271: updateCurrentCursor(0, 0, DEFAULT);
272: }
273:
274:
280: public void dropActionChanged(DragSourceDragEvent e)
281: {
282: if (dragSourceListener != null)
283: dragSourceListener.dropActionChanged(e);
284:
285: DragSource ds = getDragSource();
286: DragSourceListener[] dsl = ds.getDragSourceListeners();
287: for (int i = 0; i < dsl.length; i++)
288: dsl[i].dropActionChanged(e);
289:
290: updateCurrentCursor(e.getDropAction(), e.getTargetActions(), CHANGED);
291: }
292:
293:
299: public void dragDropEnd(DragSourceDropEvent e)
300: {
301: if (dragSourceListener != null)
302: dragSourceListener.dragDropEnd(e);
303:
304: DragSource ds = getDragSource();
305: DragSourceListener[] dsl = ds.getDragSourceListeners();
306: for (int i = 0; i < dsl.length; i++)
307: dsl[i].dragDropEnd(e);
308: }
309:
310:
315: public void dragMouseMoved(DragSourceDragEvent e)
316: {
317: DragSource ds = getDragSource();
318: DragSourceMotionListener[] dsml = ds.getDragSourceMotionListeners();
319: for (int i = 0; i < dsml.length; i++)
320: dsml[i].dragMouseMoved(e);
321: }
322:
323:
328: public Transferable getTransferable()
329: {
330: return transferable;
331: }
332:
333:
342: protected void updateCurrentCursor(int dropOp, int targetAct, int status)
343: throws NotImplementedException
344: {
345:
346: if (!useCustomCursor)
347: {
348: Cursor cursor = null;
349: switch (status)
350: {
351: case ENTER:
352: break;
353: case CHANGED:
354: break;
355: case OVER:
356: break;
357: default:
358: break;
359: }
360:
361: this.cursor = cursor;
362: peer.setCursor(cursor);
363: }
364: }
365: }