KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > dnd > DropTargetDragEvent


1 /*
2  * @(#)DropTargetDragEvent.java 1.24 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.dnd;
9
10 import java.awt.Point JavaDoc;
11
12 import java.awt.datatransfer.DataFlavor JavaDoc;
13 import java.awt.datatransfer.Transferable JavaDoc;
14
15 import java.util.List JavaDoc;
16
17 /**
18  * The <code>DropTargetDragEvent</code> is delivered to a
19  * <code>DropTargetListener</code> via its
20  * dragEnter() and dragOver() methods.
21  * <p>
22  * The <code>DropTargetDragEvent</code> reports the <i>source drop actions</i>
23  * and the <i>user drop action</i> that reflect the current state of
24  * the drag operation.
25  * <p>
26  * <i>Source drop actions</i> is a bitwise mask of <code>DnDConstants</code>
27  * that represents the set of drop actions supported by the drag source for
28  * this drag operation.
29  * <p>
30  * <i>User drop action</i> depends on the drop actions supported by the drag
31  * source and the drop action selected by the user. The user can select a drop
32  * action by pressing modifier keys during the drag operation:
33  * <pre>
34  * Ctrl + Shift -> ACTION_LINK
35  * Ctrl -> ACTION_COPY
36  * Shift -> ACTION_MOVE
37  * </pre>
38  * If the user selects a drop action, the <i>user drop action</i> is one of
39  * <code>DnDConstants</code> that represents the selected drop action if this
40  * drop action is supported by the drag source or
41  * <code>DnDConstants.ACTION_NONE</code> if this drop action is not supported
42  * by the drag source.
43  * <p>
44  * If the user doesn't select a drop action, the set of
45  * <code>DnDConstants</code> that represents the set of drop actions supported
46  * by the drag source is searched for <code>DnDConstants.ACTION_MOVE</code>,
47  * then for <code>DnDConstants.ACTION_COPY</code>, then for
48  * <code>DnDConstants.ACTION_LINK</code> and the <i>user drop action</i> is the
49  * first constant found. If no constant is found the <i>user drop action</i>
50  * is <code>DnDConstants.ACTION_NONE</code>.
51  *
52  * @version 1.24, 05/05/04
53  * @since 1.2
54  */

55
56 public class DropTargetDragEvent extends DropTargetEvent JavaDoc {
57
58     private static final long serialVersionUID = -8422265619058953682L;
59
60     /**
61      * Construct a <code>DropTargetDragEvent</code> given the
62      * <code>DropTargetContext</code> for this operation,
63      * the location of the "Drag" <code>Cursor</code>'s hotspot
64      * in the <code>Component</code>'s coordinates, the
65      * user drop action, and the source drop actions.
66      * <P>
67      * @param dtc The DropTargetContext for this operation
68      * @param cursorLocn The location of the "Drag" Cursor's
69      * hotspot in Component coordinates
70      * @param dropAction The user drop action
71      * @param srcActions The source drop actions
72      *
73      * @throws NullPointerException if cursorLocn is null
74      * @throws <code>IllegalArgumentException</code> if dropAction is not one of
75      * <code>DnDConstants</code>.
76      * @throws <code>IllegalArgumentException</code> if srcActions is not
77      * a bitwise mask of <code>DnDConstants</code>.
78      * @throws <code>IllegalArgumentException</code> if dtc is <code>null</code>.
79      */

80
81     public DropTargetDragEvent(DropTargetContext JavaDoc dtc, Point JavaDoc cursorLocn, int dropAction, int srcActions) {
82     super(dtc);
83
84     if (cursorLocn == null) throw new NullPointerException JavaDoc("cursorLocn");
85
86     if (dropAction != DnDConstants.ACTION_NONE &&
87         dropAction != DnDConstants.ACTION_COPY &&
88         dropAction != DnDConstants.ACTION_MOVE &&
89         dropAction != DnDConstants.ACTION_LINK
90     ) throw new IllegalArgumentException JavaDoc("dropAction" + dropAction);
91
92     if ((srcActions & ~(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK)) != 0) throw new IllegalArgumentException JavaDoc("srcActions");
93
94     location = cursorLocn;
95     actions = srcActions;
96     this.dropAction = dropAction;
97     }
98
99     /**
100      * This method returns a <code>Point</code>
101      * indicating the <code>Cursor</code>'s current
102      * location within the <code>Component'</code>s
103      * coordinates.
104      * <P>
105      * @return the current cursor location in
106      * <code>Component</code>'s coords.
107      */

108
109     public Point JavaDoc getLocation() {
110     return location;
111     }
112
113
114     /**
115      * This method returns the current <code>DataFlavor</code>s from the
116      * <code>DropTargetContext</code>.
117      * <P>
118      * @return current DataFlavors from the DropTargetContext
119      */

120
121     public DataFlavor JavaDoc[] getCurrentDataFlavors() {
122     return getDropTargetContext().getCurrentDataFlavors();
123     }
124
125     /**
126      * This method returns the current <code>DataFlavor</code>s
127      * as a <code>java.util.List</code>
128      * <P>
129      * @return a <code>java.util.List</code> of the Current <code>DataFlavor</code>s
130      */

131
132     public List JavaDoc<DataFlavor JavaDoc> getCurrentDataFlavorsAsList() {
133     return getDropTargetContext().getCurrentDataFlavorsAsList();
134     }
135
136     /**
137      * This method returns a <code>boolean</code> indicating
138      * if the specified <code>DataFlavor</code> is supported.
139      * <P>
140      * @param df the <code>DataFlavor</code> to test
141      * <P>
142      * @return if a particular DataFlavor is supported
143      */

144
145     public boolean isDataFlavorSupported(DataFlavor JavaDoc df) {
146     return getDropTargetContext().isDataFlavorSupported(df);
147     }
148
149     /**
150      * This method returns the source drop actions.
151      *
152      * @return the source drop actions
153      */

154     public int getSourceActions() { return actions; }
155
156     /**
157      * This method returns the user drop action.
158      *
159      * @return the user drop action
160      */

161     public int getDropAction() { return dropAction; }
162
163     /**
164      * This method returns the Transferable object that represents
165      * the data associated with the current drag operation.
166      *
167      * @return the Transferable associated with the drag operation
168      * @throws InvalidDnDOperationException if the data associated with the drag
169      * operation is not available
170      *
171      * @since 1.5
172      */

173     public Transferable JavaDoc getTransferable() {
174     return getDropTargetContext().getTransferable();
175     }
176
177     /**
178      * Accepts the drag.
179      *
180      * This method should be called from a
181      * <code>DropTargetListeners</code> <code>dragEnter</code>,
182      * <code>dragOver</code>, and <code>dropActionChanged</code>
183      * methods if the implementation wishes to accept an operation
184      * from the srcActions other than the one selected by
185      * the user as represented by the <code>dropAction</code>.
186      *
187      * @param dragOperation the operation accepted by the target
188      */

189     public void acceptDrag(int dragOperation) {
190     getDropTargetContext().acceptDrag(dragOperation);
191     }
192
193     /**
194      * Rejects the drag as a result of examining either the
195      * <code>dropAction</code> or the available <code>DataFlavor</code>
196      * types.
197      */

198     public void rejectDrag() {
199     getDropTargetContext().rejectDrag();
200     }
201
202     /*
203      * fields
204      */

205
206     /**
207      * The location of the drag cursor's hotspot in Component coordinates.
208      *
209      * @serial
210      */

211     private Point JavaDoc location;
212
213     /**
214      * The source drop actions.
215      *
216      * @serial
217      */

218     private int actions;
219
220     /**
221      * The user drop action.
222      *
223      * @serial
224      */

225     private int dropAction;
226 }
227
Popular Tags