001 /*
002 * Copyright 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.awt.dnd;
027
028 import java.awt.Point;
029
030 import java.awt.datatransfer.DataFlavor;
031 import java.awt.datatransfer.Transferable;
032
033 import java.util.List;
034
035 /**
036 * The <code>DropTargetDragEvent</code> is delivered to a
037 * <code>DropTargetListener</code> via its
038 * dragEnter() and dragOver() methods.
039 * <p>
040 * The <code>DropTargetDragEvent</code> reports the <i>source drop actions</i>
041 * and the <i>user drop action</i> that reflect the current state of
042 * the drag operation.
043 * <p>
044 * <i>Source drop actions</i> is a bitwise mask of <code>DnDConstants</code>
045 * that represents the set of drop actions supported by the drag source for
046 * this drag operation.
047 * <p>
048 * <i>User drop action</i> depends on the drop actions supported by the drag
049 * source and the drop action selected by the user. The user can select a drop
050 * action by pressing modifier keys during the drag operation:
051 * <pre>
052 * Ctrl + Shift -> ACTION_LINK
053 * Ctrl -> ACTION_COPY
054 * Shift -> ACTION_MOVE
055 * </pre>
056 * If the user selects a drop action, the <i>user drop action</i> is one of
057 * <code>DnDConstants</code> that represents the selected drop action if this
058 * drop action is supported by the drag source or
059 * <code>DnDConstants.ACTION_NONE</code> if this drop action is not supported
060 * by the drag source.
061 * <p>
062 * If the user doesn't select a drop action, the set of
063 * <code>DnDConstants</code> that represents the set of drop actions supported
064 * by the drag source is searched for <code>DnDConstants.ACTION_MOVE</code>,
065 * then for <code>DnDConstants.ACTION_COPY</code>, then for
066 * <code>DnDConstants.ACTION_LINK</code> and the <i>user drop action</i> is the
067 * first constant found. If no constant is found the <i>user drop action</i>
068 * is <code>DnDConstants.ACTION_NONE</code>.
069 *
070 * @version 1.31, 05/05/07
071 * @since 1.2
072 */
073
074 public class DropTargetDragEvent extends DropTargetEvent {
075
076 private static final long serialVersionUID = -8422265619058953682L;
077
078 /**
079 * Construct a <code>DropTargetDragEvent</code> given the
080 * <code>DropTargetContext</code> for this operation,
081 * the location of the "Drag" <code>Cursor</code>'s hotspot
082 * in the <code>Component</code>'s coordinates, the
083 * user drop action, and the source drop actions.
084 * <P>
085 * @param dtc The DropTargetContext for this operation
086 * @param cursorLocn The location of the "Drag" Cursor's
087 * hotspot in Component coordinates
088 * @param dropAction The user drop action
089 * @param srcActions The source drop actions
090 *
091 * @throws NullPointerException if cursorLocn is null
092 * @throws <code>IllegalArgumentException</code> if dropAction is not one of
093 * <code>DnDConstants</code>.
094 * @throws <code>IllegalArgumentException</code> if srcActions is not
095 * a bitwise mask of <code>DnDConstants</code>.
096 * @throws <code>IllegalArgumentException</code> if dtc is <code>null</code>.
097 */
098
099 public DropTargetDragEvent(DropTargetContext dtc, Point cursorLocn,
100 int dropAction, int srcActions) {
101 super (dtc);
102
103 if (cursorLocn == null)
104 throw new NullPointerException("cursorLocn");
105
106 if (dropAction != DnDConstants.ACTION_NONE
107 && dropAction != DnDConstants.ACTION_COPY
108 && dropAction != DnDConstants.ACTION_MOVE
109 && dropAction != DnDConstants.ACTION_LINK)
110 throw new IllegalArgumentException("dropAction"
111 + dropAction);
112
113 if ((srcActions & ~(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK)) != 0)
114 throw new IllegalArgumentException("srcActions");
115
116 location = cursorLocn;
117 actions = srcActions;
118 this .dropAction = dropAction;
119 }
120
121 /**
122 * This method returns a <code>Point</code>
123 * indicating the <code>Cursor</code>'s current
124 * location within the <code>Component'</code>s
125 * coordinates.
126 * <P>
127 * @return the current cursor location in
128 * <code>Component</code>'s coords.
129 */
130
131 public Point getLocation() {
132 return location;
133 }
134
135 /**
136 * This method returns the current <code>DataFlavor</code>s from the
137 * <code>DropTargetContext</code>.
138 * <P>
139 * @return current DataFlavors from the DropTargetContext
140 */
141
142 public DataFlavor[] getCurrentDataFlavors() {
143 return getDropTargetContext().getCurrentDataFlavors();
144 }
145
146 /**
147 * This method returns the current <code>DataFlavor</code>s
148 * as a <code>java.util.List</code>
149 * <P>
150 * @return a <code>java.util.List</code> of the Current <code>DataFlavor</code>s
151 */
152
153 public List<DataFlavor> getCurrentDataFlavorsAsList() {
154 return getDropTargetContext().getCurrentDataFlavorsAsList();
155 }
156
157 /**
158 * This method returns a <code>boolean</code> indicating
159 * if the specified <code>DataFlavor</code> is supported.
160 * <P>
161 * @param df the <code>DataFlavor</code> to test
162 * <P>
163 * @return if a particular DataFlavor is supported
164 */
165
166 public boolean isDataFlavorSupported(DataFlavor df) {
167 return getDropTargetContext().isDataFlavorSupported(df);
168 }
169
170 /**
171 * This method returns the source drop actions.
172 *
173 * @return the source drop actions
174 */
175 public int getSourceActions() {
176 return actions;
177 }
178
179 /**
180 * This method returns the user drop action.
181 *
182 * @return the user drop action
183 */
184 public int getDropAction() {
185 return dropAction;
186 }
187
188 /**
189 * This method returns the Transferable object that represents
190 * the data associated with the current drag operation.
191 *
192 * @return the Transferable associated with the drag operation
193 * @throws InvalidDnDOperationException if the data associated with the drag
194 * operation is not available
195 *
196 * @since 1.5
197 */
198 public Transferable getTransferable() {
199 return getDropTargetContext().getTransferable();
200 }
201
202 /**
203 * Accepts the drag.
204 *
205 * This method should be called from a
206 * <code>DropTargetListeners</code> <code>dragEnter</code>,
207 * <code>dragOver</code>, and <code>dropActionChanged</code>
208 * methods if the implementation wishes to accept an operation
209 * from the srcActions other than the one selected by
210 * the user as represented by the <code>dropAction</code>.
211 *
212 * @param dragOperation the operation accepted by the target
213 */
214 public void acceptDrag(int dragOperation) {
215 getDropTargetContext().acceptDrag(dragOperation);
216 }
217
218 /**
219 * Rejects the drag as a result of examining either the
220 * <code>dropAction</code> or the available <code>DataFlavor</code>
221 * types.
222 */
223 public void rejectDrag() {
224 getDropTargetContext().rejectDrag();
225 }
226
227 /*
228 * fields
229 */
230
231 /**
232 * The location of the drag cursor's hotspot in Component coordinates.
233 *
234 * @serial
235 */
236 private Point location;
237
238 /**
239 * The source drop actions.
240 *
241 * @serial
242 */
243 private int actions;
244
245 /**
246 * The user drop action.
247 *
248 * @serial
249 */
250 private int dropAction;
251 }
|