1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  * Port to the D programming language:
11  *     Frank Benoit <benoit@tionex.de>
12  *******************************************************************************/
13 module org.eclipse.swt.dnd.DND;
14 
15 import java.lang.all;
16 
17 
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.SWTError;
20 import org.eclipse.swt.SWTException;
21 
22 version(Tango){
23     import tango.util.Convert;
24 } else { // Phobos
25     import std.conv;
26 }
27 
28 /**
29  *
30  * Class DND contains all the constants used in defining a
31  * DragSource or a DropTarget.
32  *
33  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
34  */
35 public class DND {
36 
37     /**
38      * The transfer mechanism for data that is being cut
39      * and then pasted or copied and then pasted (value is 1).
40      *
41      * @see Clipboard
42      *
43      * @since 3.1
44      */
45     public const static int CLIPBOARD = 1 << 0;
46 
47     /**
48      * The transfer mechanism for clients that use the selection
49      * mechanism (value is 2).
50      *
51      * @see Clipboard
52      *
53      * @since 3.1
54      */
55     public const static int SELECTION_CLIPBOARD = 1 << 1;
56 
57     /**
58      * Drag and Drop Operation: no drag/drop operation performed
59      * (value is 0).
60      */
61     public const static int DROP_NONE = 0;
62 
63     /**
64      * Drag and Drop Operation: a copy of the data in the drag source is
65      * added to the drop target (value is 1 &lt;&lt; 0).
66      */
67     public const static int DROP_COPY = 1 << 0;
68 
69     /**
70      * Drag and Drop Operation: a copy of the data is added to the drop target and
71      * the original data is removed from the drag source (value is 1 &lt;&lt; 1).
72      */
73     public const static int DROP_MOVE = 1 << 1;
74 
75     /**
76      * Drag and Drop Operation: the drop target makes a link to the data in
77      * the drag source (value is 1 &lt;&lt; 2).
78      */
79     public const static int DROP_LINK = 1 << 2;
80 
81     /**
82      * Drag and Drop Operation: the drop target moves the data and the drag source removes
83      * any references to the data and updates its display.  This is not available on all platforms
84      * and is only used when a non-SWT application is the drop target.  In this case, the SWT
85      * drag source is informed in the dragFinished event that the drop target has moved the data.
86      * (value is 1 &lt;&lt; 3).
87      *
88      * @see DragSourceListener#dragFinished
89      */
90     public const static int DROP_TARGET_MOVE = 1 << 3;
91 
92     /**
93      * Drag and Drop Operation: During a dragEnter event or a dragOperationChanged, if no modifier keys
94      * are pressed, the operation is set to DROP_DEFAULT.  The application can choose what the default
95      * operation should be by setting a new value in the operation field.  If no value is choosen, the
96      * default operation for the platform will be selected (value is 1 &lt;&lt; 4).
97      *
98      * @see DropTargetListener#dragEnter
99      * @see DropTargetListener#dragOperationChanged
100      * @since 2.0
101      */
102     public const static int DROP_DEFAULT = 1 << 4;
103 
104     /**
105      * DragSource Event: the drop has successfully completed or has been terminated (such as hitting
106      * the ESC key); perform cleanup such as removing data on a move operation (value is 2000).
107      */
108     public static const int DragEnd     = 2000;
109 
110     /**
111      * DragSource Event: the data to be dropped is required from the drag source (value is 2001).
112      */
113     public static const int DragSetData = 2001;
114 
115     /**
116      * DropTarget Event: the cursor has entered the drop target boundaries (value is 2002).
117      */
118     public static const int DragEnter   = 2002;
119 
120     /**
121      * DropTarget Event: the cursor has left the drop target boundaries OR the drop
122      * operation has been cancelled (such as by hitting ECS) OR the drop is about to
123      * happen (user has released the mouse button over this target) (value is 2003).
124      */
125     public static const int DragLeave   = 2003;
126 
127     /**
128      * DropTarget Event: the cursor is over the drop target (value is 2004).
129      */
130     public static const int DragOver    = 2004;
131 
132     /**
133      * DropTarget Event: the operation being performed has changed usually due to the user
134      * changing the selected modifier keys while dragging (value is 2005).
135      */
136     public static const int DragOperationChanged = 2005;
137 
138     /**
139      * DropTarget Event: the data has been dropped (value is 2006).
140      */
141     public static const int Drop = 2006;
142 
143     /**
144      * DropTarget Event: the drop target is given a last chance to modify the drop (value is 2007).
145      */
146     public static const int DropAccept  = 2007;
147 
148     /**
149      * DragSource Event: a drag is about to begin (value is 2008).
150      */
151     public static const int DragStart = 2008;
152 
153     /**
154      * DropTarget drag under effect: No effect is shown (value is 0).
155      */
156     public static const int FEEDBACK_NONE = 0;
157 
158     /**
159      * DropTarget drag under effect: The item under the cursor is selected; applies to tables
160      * and trees (value is 1).
161      */
162     public static const int FEEDBACK_SELECT = 1;
163 
164     /**
165      * DropTarget drag under effect: An insertion mark is shown before the item under the cursor; applies to
166      * trees (value is 2).
167      */
168     public static const int FEEDBACK_INSERT_BEFORE = 2;
169 
170     /**
171      * DropTarget drag under effect:An insertion mark is shown after the item under the cursor; applies to
172      * trees (value is 4).
173      */
174     public static const int FEEDBACK_INSERT_AFTER = 4;
175 
176     /**
177      * DropTarget drag under effect: The widget is scrolled up or down to allow the user to drop on items that
178      * are not currently visible;  applies to tables and trees (value is 8).
179      */
180     public static const int FEEDBACK_SCROLL = 8;
181 
182     /**
183      * DropTarget drag under effect: The item currently under the cursor is expanded to allow the user to
184      * select a drop target from a sub item; applies to trees (value is 16).
185      */
186     public static const int FEEDBACK_EXPAND = 16;
187 
188     /**
189      * Error code: drag source can not be initialized (value is 2000).
190      */
191     public static const int ERROR_CANNOT_INIT_DRAG = 2000;
192 
193     /**
194      * Error code: drop target cannot be initialized (value is 2001).
195      */
196     public static const int ERROR_CANNOT_INIT_DROP = 2001;
197 
198     /**
199      * Error code: Data can not be set on system clipboard (value is 2002).
200      */
201     public static const int ERROR_CANNOT_SET_CLIPBOARD = 2002;
202 
203     /**
204      * Error code: Data does not have correct format for type (value is 2003).
205      * @since 3.1
206      */
207     public static const int ERROR_INVALID_DATA = 2003;
208 
209     /**
210      * DropTarget Key: The string constant for looking up the drop target 
211      * for a control using <code>getData(String)</code>. When a drop target 
212      * is created for a control, it is stored as a property in the control 
213      * using <code>setData(String, Object)</code>.
214      * 
215      * @since 3.4
216      */
217     public static const String DROP_TARGET_KEY = "DropTarget"; //$NON-NLS-1$
218     
219     /**
220      * DragSource Key: The string constant for looking up the drag source 
221      * for a control using <code>getData(String)</code>. When a drag source 
222      * is created for a control, it is stored as a property in the control 
223      * using <code>setData(String, Object)</code>.
224      * 
225      * @since 3.4
226      */
227     public static const String DRAG_SOURCE_KEY = "DragSource"; //$NON-NLS-1$
228 
229     static const String INIT_DRAG_MESSAGE = "Cannot initialize Drag"; //$NON-NLS-1$
230     static const String INIT_DROP_MESSAGE = "Cannot initialize Drop"; //$NON-NLS-1$
231     static const String CANNOT_SET_CLIPBOARD_MESSAGE = "Cannot set data in clipboard"; //$NON-NLS-1$
232     static const String INVALID_DATA_MESSAGE = "Data does not have correct format for type"; //$NON-NLS-1$
233 
234 /**
235  * Throws an appropriate exception based on the passed in error code.
236  *
237  * @param code the DND error code
238  */
239 public static void error (int code) {
240     error (code, 0);
241 }
242 
243 /**
244  * Throws an appropriate exception based on the passed in error code.
245  * The <code>hresult</code> argument should be either 0, or the
246  * platform specific error code.
247  * <p>
248  * In DND, errors are reported by throwing one of three exceptions:
249  * <dl>
250  * <dd>java.lang.IllegalArgumentException</dd>
251  * <dt>thrown whenever one of the API methods is invoked with an illegal argument</dt>
252  * <dd>org.eclipse.swt.SWTException (extends java.lang.RuntimeException)</dd>
253  * <dt>thrown whenever a recoverable error happens internally in SWT</dt>
254  * <dd>org.eclipse.swt.SWTError (extends java.lang.Error)</dd>
255  * <dt>thrown whenever a <b>non-recoverable</b> error happens internally in SWT</dt>
256  * </dl>
257  * This method provides the logic which maps between error codes
258  * and one of the above exceptions.
259  * </p>
260  *
261  * @param code the DND error code.
262  * @param hresult the platform specific error code.
263  *
264  * @see SWTError
265  * @see SWTException
266  * @see IllegalArgumentException
267  */
268 public static void error (int code, int hresult) {
269     switch (code) {
270         /* OS Failure/Limit (fatal, may occur only on some platforms) */
271         case DND.ERROR_CANNOT_INIT_DRAG:{
272             String msg = DND.INIT_DRAG_MESSAGE;
273             if (hresult !is 0) msg ~= " result = "~to!(String)(hresult); //$NON-NLS-1$
274             throw new SWTError (code, msg);
275         }
276         case DND.ERROR_CANNOT_INIT_DROP:{
277             String msg = DND.INIT_DROP_MESSAGE;
278             if (hresult !is 0) msg ~= " result = "~to!(String)(hresult); //$NON-NLS-1$
279             throw new SWTError (code, msg);
280         }
281         case DND.ERROR_CANNOT_SET_CLIPBOARD:{
282             String msg = DND.CANNOT_SET_CLIPBOARD_MESSAGE;
283             if (hresult !is 0) msg ~= " result = "~to!(String)(hresult); //$NON-NLS-1$
284             throw new SWTError (code, msg);
285         }
286         case DND.ERROR_INVALID_DATA:{
287             String msg = DND.INVALID_DATA_MESSAGE;
288             if (hresult !is 0) msg ~= " result = "~to!(String)(hresult); //$NON-NLS-1$
289             throw new SWTException (code, msg);
290         }
291         default:
292     }
293 
294     /* Unknown/Undefined Error */
295     SWT.error(code);
296 }
297 
298 }