1 /* DragSourceContext.java --
2 Copyright (C) 2002 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.awt.Component;
42 import java.awt.Cursor;
43 import java.awt.Image;
44 import java.awt.Point;
45 import java.awt.datatransfer.Transferable;
46 import java.awt.dnd.peer.DragSourceContextPeer;
47 import java.io.Serializable;
48 import java.util.TooManyListenersException;
53 public class DragSourceContext
54 implements DragSourceListener, DragSourceMotionListener, Serializable
57 * Compatible with JDK 1.2+
59 static final long serialVersionUID = -115407898692194719L;
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;
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;
77 * Initializes a drag source context.
79 * @exception IllegalArgumentException If Component or DragSource of trigger
80 * are null, the drag action for the trigger event is DnDConstants.ACTION_NONE
81 * or if the source actions for the DragGestureRecognizer associated with the
82 * trigger event are equal to DnDConstants.ACTION_NONE.
83 * @exception NullPointerException If peer, trans or trigger is null or if the
84 * image is not null but the offset is.
86 public DragSourceContext (DragSourceContextPeer peer,
87 DragGestureEvent trigger, Cursor cursor,
88 Image image, Point offset, Transferable trans,
89 DragSourceListener dsl)
92 || trigger == null || trans == null
93 || (image != null && offset == null))
94 throw new NullPointerException ();
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 ();
104 this.trigger = trigger;
105 this.cursor = cursor;
107 this.offset = offset;
108 this.transferable = trans;
109 this.dragSourceListener = dsl;
110 this.sourceActions = trigger.getSourceAsDragGestureRecognizer().getSourceActions();
113 updateCurrentCursor(trigger.getDragAction(), sourceActions, DEFAULT);
117 * Returns the DragSource object associated with the
120 * @return the DragSource associated with the trigger.
122 public DragSource getDragSource()
124 return trigger.getDragSource ();
128 * Returns the component associated with this.
130 * @return the component associated with the trigger.
132 public Component getComponent()
134 return trigger.getComponent ();
138 * Gets the trigger associated with this.
140 * @return the trigger.
142 public DragGestureEvent getTrigger()
148 * Returns the source actions for the DragGestureRecognizer.
150 * @return the source actions for DragGestureRecognizer.
152 public int getSourceActions()
154 if (sourceActions == 0)
155 sourceActions = trigger.getSourceAsDragGestureRecognizer().getSourceActions();
156 return sourceActions;
160 * Sets the cursor for this drag operation to the specified cursor.
162 * @param cursor c - the Cursor to use, or null to use the default drag
165 public void setCursor(Cursor cursor)
168 useCustomCursor = false;
170 useCustomCursor = true;
171 this.cursor = cursor;
172 peer.setCursor(cursor);
176 * Returns the current cursor or null if the default
177 * drag cursor is used.
179 * @return the current cursor or null.
181 public Cursor getCursor()
187 * Adds a <code>DragSourceListener</code>.
189 * @exception TooManyListenersException If a <code>DragSourceListener</code>
190 * has already been added.
192 public void addDragSourceListener (DragSourceListener dsl)
193 throws TooManyListenersException
195 if (dragSourceListener != null)
196 throw new TooManyListenersException ();
198 dragSourceListener = dsl;
201 public void removeDragSourceListener (DragSourceListener dsl)
203 if (dragSourceListener == dsl)
204 dragSourceListener = null;
208 * This function tells the peer that the DataFlavors have been modified.
210 public void transferablesFlavorsChanged()
212 peer.transferablesFlavorsChanged();
216 * Calls dragEnter on the listeners registered with this
217 * and with the DragSource.
219 * @param e - the DragSourceDragEvent
221 public void dragEnter(DragSourceDragEvent e)
223 if (dragSourceListener != null)
224 dragSourceListener.dragEnter(e);
226 DragSource ds = getDragSource();
227 DragSourceListener[] dsl = ds.getDragSourceListeners();
228 for (int i = 0; i < dsl.length; i++)
231 updateCurrentCursor(e.getDropAction(), e.getTargetActions(), ENTER);
235 * Calls dragOver on the listeners registered with this
236 * and with the DragSource.
238 * @param e - the DragSourceDragEvent
240 public void dragOver(DragSourceDragEvent e)
242 if (dragSourceListener != null)
243 dragSourceListener.dragOver(e);
245 DragSource ds = getDragSource();
246 DragSourceListener[] dsl = ds.getDragSourceListeners();
247 for (int i = 0; i < dsl.length; i++)
250 updateCurrentCursor(e.getDropAction(), e.getTargetActions(), OVER);
254 * Calls dragExit on the listeners registered with this
255 * and with the DragSource.
257 * @param e - the DragSourceEvent
259 public void dragExit(DragSourceEvent e)
261 if (dragSourceListener != null)
262 dragSourceListener.dragExit(e);
264 DragSource ds = getDragSource();
265 DragSourceListener[] dsl = ds.getDragSourceListeners();
266 for (int i = 0; i < dsl.length; i++)
269 updateCurrentCursor(DnDConstants.ACTION_NONE, DnDConstants.ACTION_NONE,
274 * Calls dropActionChanged on the listeners registered with this
275 * and with the DragSource.
277 * @param e - the DragSourceDragEvent
279 public void dropActionChanged(DragSourceDragEvent e)
281 if (dragSourceListener != null)
282 dragSourceListener.dropActionChanged(e);
284 DragSource ds = getDragSource();
285 DragSourceListener[] dsl = ds.getDragSourceListeners();
286 for (int i = 0; i < dsl.length; i++)
287 dsl[i].dropActionChanged(e);
289 updateCurrentCursor(e.getDropAction(), e.getTargetActions(), CHANGED);
293 * Calls dragDropEnd on the listeners registered with this
294 * and with the DragSource.
296 * @param e - the DragSourceDropEvent
298 public void dragDropEnd(DragSourceDropEvent e)
300 if (dragSourceListener != null)
301 dragSourceListener.dragDropEnd(e);
303 DragSource ds = getDragSource();
304 DragSourceListener[] dsl = ds.getDragSourceListeners();
305 for (int i = 0; i < dsl.length; i++)
306 dsl[i].dragDropEnd(e);
310 * Calls dragMouseMoved on the listeners registered with the DragSource.
312 * @param e - the DragSourceDragEvent
314 public void dragMouseMoved(DragSourceDragEvent e)
316 DragSource ds = getDragSource();
317 DragSourceMotionListener[] dsml = ds.getDragSourceMotionListeners();
318 for (int i = 0; i < dsml.length; i++)
319 dsml[i].dragMouseMoved(e);
323 * Returns the Transferable set with this object.
325 * @return the transferable.
327 public Transferable getTransferable()
333 * This function sets the drag cursor for the specified operation, actions and
334 * status if the default drag cursor is active. Otherwise, the cursor is not
335 * updated in any way.
337 * @param dropOp - the current operation.
338 * @param targetAct - the supported actions.
339 * @param status - the status of the cursor (constant).
341 protected void updateCurrentCursor(int dropOp, int targetAct, int status)
343 if (! useCustomCursor)
345 Cursor newCursor = null;
349 targetAct = DnDConstants.ACTION_NONE;
353 int action = dropOp & targetAct;
354 if (action == DnDConstants.ACTION_NONE)
356 if ((dropOp & DnDConstants.ACTION_LINK) != 0)
357 newCursor = DragSource.DefaultLinkNoDrop;
358 else if ((dropOp & DnDConstants.ACTION_MOVE) != 0)
359 newCursor = DragSource.DefaultMoveNoDrop;
361 newCursor = DragSource.DefaultCopyNoDrop;
365 if ((dropOp & DnDConstants.ACTION_LINK) != 0)
366 newCursor = DragSource.DefaultLinkDrop;
367 else if ((dropOp & DnDConstants.ACTION_MOVE) != 0)
368 newCursor = DragSource.DefaultMoveDrop;
370 newCursor = DragSource.DefaultCopyDrop;
374 if (cursor == null || ! cursor.equals(newCursor))
377 DragSourceContextPeer p = peer;
383 } // class DragSourceContext