1 /*
2  * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package java.awt.dnd;
27 
28 import java.awt.Point;
29 import java.awt.datatransfer.DataFlavor;
30 import java.awt.datatransfer.Transferable;
31 import java.io.Serial;
32 import java.util.List;
33 
34 /**
35  * The {@code DropTargetDragEvent} is delivered to a
36  * {@code DropTargetListener} via its
37  * dragEnter() and dragOver() methods.
38  * <p>
39  * The {@code DropTargetDragEvent} reports the <i>source drop actions</i>
40  * and the <i>user drop action</i> that reflect the current state of
41  * the drag operation.
42  * <p>
43  * <i>Source drop actions</i> is a bitwise mask of {@code DnDConstants}
44  * that represents the set of drop actions supported by the drag source for
45  * this drag operation.
46  * <p>
47  * <i>User drop action</i> depends on the drop actions supported by the drag
48  * source and the drop action selected by the user. The user can select a drop
49  * action by pressing modifier keys during the drag operation:
50  * <pre>
51  *   Ctrl + Shift -&gt; ACTION_LINK
52  *   Ctrl         -&gt; ACTION_COPY
53  *   Shift        -&gt; ACTION_MOVE
54  * </pre>
55  * If the user selects a drop action, the <i>user drop action</i> is one of
56  * {@code DnDConstants} that represents the selected drop action if this
57  * drop action is supported by the drag source or
58  * {@code DnDConstants.ACTION_NONE} if this drop action is not supported
59  * by the drag source.
60  * <p>
61  * If the user doesn't select a drop action, the set of
62  * {@code DnDConstants} that represents the set of drop actions supported
63  * by the drag source is searched for {@code DnDConstants.ACTION_MOVE},
64  * then for {@code DnDConstants.ACTION_COPY}, then for
65  * {@code DnDConstants.ACTION_LINK} and the <i>user drop action</i> is the
66  * first constant found. If no constant is found the <i>user drop action</i>
67  * is {@code DnDConstants.ACTION_NONE}.
68  *
69  * @since 1.2
70  */
71 
72 public class DropTargetDragEvent extends DropTargetEvent {
73 
74     /**
75      * Use serialVersionUID from JDK 1.4 for interoperability.
76      */
77     @Serial
78     private static final long serialVersionUID = -8422265619058953682L;
79 
80     /**
81      * Construct a {@code DropTargetDragEvent} given the
82      * {@code DropTargetContext} for this operation,
83      * the location of the "Drag" {@code Cursor}'s hotspot
84      * in the {@code Component}'s coordinates, the
85      * user drop action, and the source drop actions.
86      *
87      * @param dtc        The DropTargetContext for this operation
88      * @param cursorLocn The location of the "Drag" Cursor's
89      * hotspot in Component coordinates
90      * @param dropAction The user drop action
91      * @param srcActions The source drop actions
92      *
93      * @throws NullPointerException if cursorLocn is null
94      * @throws IllegalArgumentException if dropAction is not one of
95      *         {@code DnDConstants}.
96      * @throws IllegalArgumentException if srcActions is not
97      *         a bitwise mask of {@code DnDConstants}.
98      * @throws IllegalArgumentException if dtc is {@code null}.
99      */
100 
DropTargetDragEvent(DropTargetContext dtc, Point cursorLocn, int dropAction, int srcActions)101     public DropTargetDragEvent(DropTargetContext dtc, Point cursorLocn, int dropAction, int srcActions)  {
102         super(dtc);
103 
104         if (cursorLocn == null) 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" + dropAction);
111 
112         if ((srcActions & ~(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK)) != 0) throw new IllegalArgumentException("srcActions");
113 
114         location        = cursorLocn;
115         actions         = srcActions;
116         this.dropAction = dropAction;
117     }
118 
119     /**
120      * This method returns a {@code Point}
121      * indicating the {@code Cursor}'s current
122      * location within the {@code Component'}s
123      * coordinates.
124      *
125      * @return the current cursor location in
126      * {@code Component}'s coords.
127      */
128 
getLocation()129     public Point getLocation() {
130         return location;
131     }
132 
133 
134     /**
135      * This method returns the current {@code DataFlavor}s from the
136      * {@code DropTargetContext}.
137      *
138      * @return current DataFlavors from the DropTargetContext
139      */
140 
getCurrentDataFlavors()141     public DataFlavor[] getCurrentDataFlavors() {
142         return getDropTargetContext().getCurrentDataFlavors();
143     }
144 
145     /**
146      * This method returns the current {@code DataFlavor}s
147      * as a {@code java.util.List}
148      *
149      * @return a {@code java.util.List} of the Current {@code DataFlavor}s
150      */
151 
getCurrentDataFlavorsAsList()152     public List<DataFlavor> getCurrentDataFlavorsAsList() {
153         return getDropTargetContext().getCurrentDataFlavorsAsList();
154     }
155 
156     /**
157      * This method returns a {@code boolean} indicating
158      * if the specified {@code DataFlavor} is supported.
159      *
160      * @param df the {@code DataFlavor} to test
161      *
162      * @return if a particular DataFlavor is supported
163      */
164 
isDataFlavorSupported(DataFlavor df)165     public boolean isDataFlavorSupported(DataFlavor df) {
166         return getDropTargetContext().isDataFlavorSupported(df);
167     }
168 
169     /**
170      * This method returns the source drop actions.
171      *
172      * @return the source drop actions
173      */
getSourceActions()174     public int getSourceActions() { return actions; }
175 
176     /**
177      * This method returns the user drop action.
178      *
179      * @return the user drop action
180      */
getDropAction()181     public int getDropAction() { return dropAction; }
182 
183     /**
184      * This method returns the Transferable object that represents
185      * the data associated with the current drag operation.
186      *
187      * @return the Transferable associated with the drag operation
188      * @throws InvalidDnDOperationException if the data associated with the drag
189      *         operation is not available
190      *
191      * @since 1.5
192      */
getTransferable()193     public Transferable getTransferable() {
194         return getDropTargetContext().getTransferable();
195     }
196 
197     /**
198      * Accepts the drag.
199      *
200      * This method should be called from a
201      * {@code DropTargetListeners dragEnter},
202      * {@code dragOver}, and {@code dropActionChanged}
203      * methods if the implementation wishes to accept an operation
204      * from the srcActions other than the one selected by
205      * the user as represented by the {@code dropAction}.
206      *
207      * @param dragOperation the operation accepted by the target
208      */
acceptDrag(int dragOperation)209     public void acceptDrag(int dragOperation) {
210         getDropTargetContext().acceptDrag(dragOperation);
211     }
212 
213     /**
214      * Rejects the drag as a result of examining either the
215      * {@code dropAction} or the available {@code DataFlavor}
216      * types.
217      */
rejectDrag()218     public void rejectDrag() {
219         getDropTargetContext().rejectDrag();
220     }
221 
222     /*
223      * fields
224      */
225 
226     /**
227      * The location of the drag cursor's hotspot in Component coordinates.
228      *
229      * @serial
230      */
231     private Point               location;
232 
233     /**
234      * The source drop actions.
235      *
236      * @serial
237      */
238     private int                 actions;
239 
240     /**
241      * The user drop action.
242      *
243      * @serial
244      */
245     private int                 dropAction;
246 }
247