KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > dnd > DropTarget


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  *******************************************************************************/

11 package org.eclipse.swt.dnd;
12
13 import org.eclipse.swt.*;
14 import org.eclipse.swt.widgets.*;
15 import org.eclipse.swt.internal.ole.win32.*;
16 import org.eclipse.swt.internal.win32.*;
17
18 /**
19  *
20  * Class <code>DropTarget</code> defines the target object for a drag and drop transfer.
21  *
22  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
23  *
24  * <p>This class identifies the <code>Control</code> over which the user must position the cursor
25  * in order to drop the data being transferred. It also specifies what data types can be dropped on
26  * this control and what operations can be performed. You may have several DropTragets in an
27  * application but there can only be a one to one mapping between a <code>Control</code> and a <code>DropTarget</code>.
28  * The DropTarget can receive data from within the same application or from other applications
29  * (such as text dragged from a text editor like Word).</p>
30  *
31  * <code><pre>
32  * int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
33  * Transfer[] types = new Transfer[] {TextTransfer.getInstance()};
34  * DropTarget target = new DropTarget(label, operations);
35  * target.setTransfer(types);
36  * </code></pre>
37  *
38  * <p>The application is notified of data being dragged over this control and of when a drop occurs by
39  * implementing the interface <code>DropTargetListener</code> which uses the class
40  * <code>DropTargetEvent</code>. The application can modify the type of drag being performed
41  * on this Control at any stage of the drag by modifying the <code>event.detail</code> field or the
42  * <code>event.currentDataType</code> field. When the data is dropped, it is the responsibility of
43  * the application to copy this data for its own purposes.
44  *
45  * <code><pre>
46  * target.addDropListener (new DropTargetListener() {
47  * public void dragEnter(DropTargetEvent event) {};
48  * public void dragOver(DropTargetEvent event) {};
49  * public void dragLeave(DropTargetEvent event) {};
50  * public void dragOperationChanged(DropTargetEvent event) {};
51  * public void dropAccept(DropTargetEvent event) {}
52  * public void drop(DropTargetEvent event) {
53  * // A drop has occurred, copy over the data
54  * if (event.data == null) { // no data to copy, indicate failure in event.detail
55  * event.detail = DND.DROP_NONE;
56  * return;
57  * }
58  * label.setText ((String) event.data); // data copied to label text
59  * }
60  * });
61  * </pre></code>
62  *
63  * <dl>
64  * <dt><b>Styles</b></dt> <dd>DND.DROP_NONE, DND.DROP_COPY, DND.DROP_MOVE, DND.DROP_LINK</dd>
65  * <dt><b>Events</b></dt> <dd>DND.DragEnter, DND.DragLeave, DND.DragOver, DND.DragOperationChanged,
66  * DND.DropAccept, DND.Drop </dd>
67  * </dl>
68  */

69 public class DropTarget extends Widget {
70     
71     Control control;
72     Listener controlListener;
73     Transfer[] transferAgents = new Transfer[0];
74     DropTargetEffect dropEffect;
75     
76     // Track application selections
77
TransferData selectedDataType;
78     int selectedOperation;
79     
80     // workaround - There is no event for "operation changed" so track operation based on key state
81
int keyOperation = -1;
82     
83     // workaround - The dataobject address is only passed as an argument in drag enter and drop.
84
// To allow applications to query the data values during the drag over operations,
85
// maintain a reference to it.
86
IDataObject iDataObject;
87     
88     // interfaces
89
COMObject iDropTarget;
90     int refCount;
91     
92     static final String JavaDoc DEFAULT_DROP_TARGET_EFFECT = "DEFAULT_DROP_TARGET_EFFECT"; //$NON-NLS-1$
93
static final String JavaDoc DROPTARGETID = "DropTarget"; //$NON-NLS-1$
94

95 /**
96  * Creates a new <code>DropTarget</code> to allow data to be dropped on the specified
97  * <code>Control</code>.
98  * Creating an instance of a DropTarget may cause system resources to be allocated
99  * depending on the platform. It is therefore mandatory that the DropTarget instance
100  * be disposed when no longer required.
101  *
102  * @param control the <code>Control</code> over which the user positions the cursor to drop the data
103  * @param style the bitwise OR'ing of allowed operations; this may be a combination of any of
104  * DND.DROP_NONE, DND.DROP_COPY, DND.DROP_MOVE, DND.DROP_LINK
105  *
106  * @exception SWTException <ul>
107  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
108  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
109  * </ul>
110  * @exception SWTError <ul>
111  * <li>ERROR_CANNOT_INIT_DROP - unable to initiate drop target; this will occur if more than one
112  * drop target is created for a control or if the operating system will not allow the creation
113  * of the drop target</li>
114  * </ul>
115  *
116  * <p>NOTE: ERROR_CANNOT_INIT_DROP should be an SWTException, since it is a
117  * recoverable error, but can not be changed due to backward compatibility.</p>
118  *
119  * @see Widget#dispose
120  * @see DropTarget#checkSubclass
121  * @see DND#DROP_NONE
122  * @see DND#DROP_COPY
123  * @see DND#DROP_MOVE
124  * @see DND#DROP_LINK
125  */

126 public DropTarget(Control control, int style) {
127     super (control, checkStyle(style));
128     this.control = control;
129     if (control.getData(DROPTARGETID) != null) {
130         DND.error(DND.ERROR_CANNOT_INIT_DROP);
131     }
132     control.setData(DROPTARGETID, this);
133     createCOMInterfaces();
134     this.AddRef();
135     
136     if (COM.CoLockObjectExternal(iDropTarget.getAddress(), true, true) != COM.S_OK)
137         DND.error(DND.ERROR_CANNOT_INIT_DROP);
138     if (COM.RegisterDragDrop( control.handle, iDropTarget.getAddress()) != COM.S_OK)
139         DND.error(DND.ERROR_CANNOT_INIT_DROP);
140
141     controlListener = new Listener () {
142         public void handleEvent (Event event) {
143             if (!DropTarget.this.isDisposed()){
144                 DropTarget.this.dispose();
145             }
146         }
147     };
148     control.addListener (SWT.Dispose, controlListener);
149     
150     this.addListener(SWT.Dispose, new Listener () {
151         public void handleEvent (Event event) {
152             onDispose();
153         }
154     });
155     
156     Object JavaDoc effect = control.getData(DEFAULT_DROP_TARGET_EFFECT);
157     if (effect instanceof DropTargetEffect) {
158         dropEffect = (DropTargetEffect) effect;
159     } else if (control instanceof Table) {
160         dropEffect = new TableDropTargetEffect((Table) control);
161     } else if (control instanceof Tree) {
162         dropEffect = new TreeDropTargetEffect((Tree) control);
163     }
164 }
165
166 static int checkStyle (int style) {
167     if (style == SWT.NONE) return DND.DROP_MOVE;
168     return style;
169 }
170
171 /**
172  * Adds the listener to the collection of listeners who will
173  * be notified when a drag and drop operation is in progress, by sending
174  * it one of the messages defined in the <code>DropTargetListener</code>
175  * interface.
176  *
177  * <p><ul>
178  * <li><code>dragEnter</code> is called when the cursor has entered the drop target boundaries
179  * <li><code>dragLeave</code> is called when the cursor has left the drop target boundaries and just before
180  * the drop occurs or is cancelled.
181  * <li><code>dragOperationChanged</code> is called when the operation being performed has changed
182  * (usually due to the user changing the selected modifier key(s) while dragging)
183  * <li><code>dragOver</code> is called when the cursor is moving over the drop target
184  * <li><code>dropAccept</code> is called just before the drop is performed. The drop target is given
185  * the chance to change the nature of the drop or veto the drop by setting the <code>event.detail</code> field
186  * <li><code>drop</code> is called when the data is being dropped
187  * </ul></p>
188  *
189  * @param listener the listener which should be notified
190  *
191  * @exception IllegalArgumentException <ul>
192  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
193  * </ul>
194  * @exception SWTException <ul>
195  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
196  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
197  * </ul>
198  *
199  * @see DropTargetListener
200  * @see #removeDropListener
201  * @see DropTargetEvent
202  */

203 public void addDropListener(DropTargetListener listener) {
204     if (listener == null) DND.error (SWT.ERROR_NULL_ARGUMENT);
205     DNDListener typedListener = new DNDListener (listener);
206     typedListener.dndWidget = this;
207     addListener (DND.DragEnter, typedListener);
208     addListener (DND.DragLeave, typedListener);
209     addListener (DND.DragOver, typedListener);
210     addListener (DND.DragOperationChanged, typedListener);
211     addListener (DND.Drop, typedListener);
212     addListener (DND.DropAccept, typedListener);
213 }
214
215 int AddRef() {
216     refCount++;
217     return refCount;
218 }
219
220 protected void checkSubclass () {
221     String JavaDoc name = getClass().getName ();
222     String JavaDoc validName = DropTarget.class.getName();
223     if (!validName.equals(name)) {
224         DND.error (SWT.ERROR_INVALID_SUBCLASS);
225     }
226 }
227
228 void createCOMInterfaces() {
229     // register each of the interfaces that this object implements
230
iDropTarget = new COMObject(new int[]{2, 0, 0, 5, 4, 0, 5}){
231         public int method0(int[] args) {return QueryInterface(args[0], args[1]);}
232         public int method1(int[] args) {return AddRef();}
233         public int method2(int[] args) {return Release();}
234         public int method3(int[] args) {return DragEnter(args[0], args[1], args[2], args[3], args[4]);}
235         public int method4(int[] args) {return DragOver(args[0], args[1], args[2], args[3]);}
236         public int method5(int[] args) {return DragLeave();}
237         public int method6(int[] args) {return Drop(args[0], args[1], args[2], args[3], args[4]);}
238     };
239
240 }
241
242 void disposeCOMInterfaces() {
243     if (iDropTarget != null)
244         iDropTarget.dispose();
245     iDropTarget = null;
246 }
247
248 int DragEnter(int pDataObject, int grfKeyState, int pt_x, int pt_y, int pdwEffect) {
249     selectedDataType = null;
250     selectedOperation = DND.DROP_NONE;
251     if (iDataObject != null) iDataObject.Release();
252     iDataObject = null;
253     
254     DNDEvent event = new DNDEvent();
255     if (!setEventData(event, pDataObject, grfKeyState, pt_x, pt_y, pdwEffect)) {
256         OS.MoveMemory(pdwEffect, new int[] {COM.DROPEFFECT_NONE}, 4);
257         return COM.S_FALSE;
258     }
259
260     // Remember the iDataObject because it is not passed into the DragOver callback
261
iDataObject = new IDataObject(pDataObject);
262     iDataObject.AddRef();
263     
264     int allowedOperations = event.operations;
265     TransferData[] allowedDataTypes = new TransferData[event.dataTypes.length];
266     System.arraycopy(event.dataTypes, 0, allowedDataTypes, 0, allowedDataTypes.length);
267     notifyListeners(DND.DragEnter, event);
268     refresh();
269     if (event.detail == DND.DROP_DEFAULT) {
270         event.detail = (allowedOperations & DND.DROP_MOVE) != 0 ? DND.DROP_MOVE : DND.DROP_NONE;
271     }
272     
273     selectedDataType = null;
274     for (int i = 0; i < allowedDataTypes.length; i++) {
275         if (TransferData.sameType(allowedDataTypes[i], event.dataType)){
276             selectedDataType = allowedDataTypes[i];
277             break;
278         }
279     }
280     
281     selectedOperation = DND.DROP_NONE;
282     if (selectedDataType != null && ((allowedOperations & event.detail) != 0)) {
283         selectedOperation = event.detail;
284     }
285     
286     OS.MoveMemory(pdwEffect, new int[] {opToOs(selectedOperation)}, 4);
287     return COM.S_OK;
288 }
289
290 int DragLeave() {
291     keyOperation = -1;
292
293     if (iDataObject == null) return COM.S_FALSE;
294
295     DNDEvent event = new DNDEvent();
296     event.widget = this;
297     event.time = OS.GetMessageTime();
298     event.detail = DND.DROP_NONE;
299     notifyListeners(DND.DragLeave, event);
300     refresh();
301     
302     iDataObject.Release();
303     iDataObject = null;
304     return COM.S_OK;
305 }
306
307 int DragOver(int grfKeyState, int pt_x, int pt_y, int pdwEffect) {
308     if (iDataObject == null) return COM.S_FALSE;
309     int oldKeyOperation = keyOperation;
310     
311     DNDEvent event = new DNDEvent();
312     if (!setEventData(event, iDataObject.getAddress(), grfKeyState, pt_x, pt_y, pdwEffect)) {
313         keyOperation = -1;
314         OS.MoveMemory(pdwEffect, new int[] {COM.DROPEFFECT_NONE}, 4);
315         return COM.S_FALSE;
316     }
317     
318     int allowedOperations = event.operations;
319     TransferData[] allowedDataTypes = new TransferData[event.dataTypes.length];
320     System.arraycopy(event.dataTypes, 0, allowedDataTypes, 0, allowedDataTypes.length);
321
322     if (keyOperation == oldKeyOperation) {
323         event.type = DND.DragOver;
324         event.dataType = selectedDataType;
325         event.detail = selectedOperation;
326     } else {
327         event.type = DND.DragOperationChanged;
328         event.dataType = selectedDataType;
329     }
330     notifyListeners(event.type, event);
331     refresh();
332     if (event.detail == DND.DROP_DEFAULT) {
333         event.detail = (allowedOperations & DND.DROP_MOVE) != 0 ? DND.DROP_MOVE : DND.DROP_NONE;
334     }
335     
336     selectedDataType = null;
337     for (int i = 0; i < allowedDataTypes.length; i++) {
338         if (TransferData.sameType(allowedDataTypes[i], event.dataType)){
339             selectedDataType = allowedDataTypes[i];
340             break;
341         }
342     }
343
344     selectedOperation = DND.DROP_NONE;
345     if (selectedDataType != null && ((allowedOperations & event.detail) == event.detail)) {
346         selectedOperation = event.detail;
347     }
348     
349     OS.MoveMemory(pdwEffect, new int[] {opToOs(selectedOperation)}, 4);
350     return COM.S_OK;
351 }
352
353 int Drop(int pDataObject, int grfKeyState, int pt_x, int pt_y, int pdwEffect) {
354     DNDEvent event = new DNDEvent();
355     event.widget = this;
356     event.time = OS.GetMessageTime();
357     if (dropEffect != null) {
358         event.item = dropEffect.getItem(pt_x, pt_y);
359     }
360     event.detail = DND.DROP_NONE;
361     notifyListeners(DND.DragLeave, event);
362     refresh();
363     
364     event = new DNDEvent();
365     if (!setEventData(event, pDataObject, grfKeyState, pt_x, pt_y, pdwEffect)) {
366         keyOperation = -1;
367         OS.MoveMemory(pdwEffect, new int[] {COM.DROPEFFECT_NONE}, 4);
368         return COM.S_FALSE;
369     }
370     keyOperation = -1;
371     int allowedOperations = event.operations;
372     TransferData[] allowedDataTypes = new TransferData[event.dataTypes.length];
373     System.arraycopy(event.dataTypes, 0, allowedDataTypes, 0, allowedDataTypes.length);
374     event.dataType = selectedDataType;
375     event.detail = selectedOperation;
376     notifyListeners(DND.DropAccept,event);
377     refresh();
378     
379     selectedDataType = null;
380     for (int i = 0; i < allowedDataTypes.length; i++) {
381         if (TransferData.sameType(allowedDataTypes[i], event.dataType)){
382             selectedDataType = allowedDataTypes[i];
383             break;
384         }
385     }
386     selectedOperation = DND.DROP_NONE;
387     if (selectedDataType != null && (allowedOperations & event.detail) == event.detail) {
388         selectedOperation = event.detail;
389     }
390
391     if (selectedOperation == DND.DROP_NONE){
392         OS.MoveMemory(pdwEffect, new int[] {COM.DROPEFFECT_NONE}, 4);
393         return COM.S_OK;
394     }
395     
396     // Get Data in a Java format
397
Object JavaDoc object = null;
398     for (int i = 0; i < transferAgents.length; i++){
399         Transfer transfer = transferAgents[i];
400         if (transfer != null && transfer.isSupportedType(selectedDataType)){
401             object = transfer.nativeToJava(selectedDataType);
402             break;
403         }
404     }
405     if (object == null){
406         selectedOperation = DND.DROP_NONE;
407     }
408     
409     event.detail = selectedOperation;
410     event.dataType = selectedDataType;
411     event.data = object;
412     OS.ImageList_DragShowNolock(false);
413     try {
414         notifyListeners(DND.Drop,event);
415     } finally {
416         OS.ImageList_DragShowNolock(true);
417     }
418     refresh();
419     selectedOperation = DND.DROP_NONE;
420     if ((allowedOperations & event.detail) == event.detail) {
421         selectedOperation = event.detail;
422     }
423     //notify source of action taken
424
OS.MoveMemory(pdwEffect, new int[] {opToOs(selectedOperation)}, 4);
425     return COM.S_OK;
426 }
427
428 /**
429  * Returns the Control which is registered for this DropTarget. This is the control over which the
430  * user positions the cursor to drop the data.
431  *
432  * @return the Control which is registered for this DropTarget
433  */

434 public Control getControl () {
435     return control;
436 }
437
438 /**
439  * Returns the drop effect for this DropTarget. This drop effect will be
440  * used during a drag and drop to display the drag under effect on the
441  * target widget.
442  *
443  * @return the drop effect that is registered for this DropTarget
444  *
445  * @since 3.3
446  */

447 public DropTargetEffect getDropTargetEffect() {
448     return dropEffect;
449 }
450
451 int getOperationFromKeyState(int grfKeyState) {
452     boolean ctrl = (grfKeyState & OS.MK_CONTROL) != 0;
453     boolean shift = (grfKeyState & OS.MK_SHIFT) != 0;
454     boolean alt = (grfKeyState & OS.MK_ALT) != 0;
455     if (alt) {
456         if (ctrl || shift) return DND.DROP_DEFAULT;
457         return DND.DROP_LINK;
458     }
459     if (ctrl && shift) return DND.DROP_LINK;
460     if (ctrl)return DND.DROP_COPY;
461     if (shift)return DND.DROP_MOVE;
462     return DND.DROP_DEFAULT;
463 }
464
465 /**
466  * Returns a list of the data types that can be transferred to this DropTarget.
467  *
468  * @return a list of the data types that can be transferred to this DropTarget
469  */

470 public Transfer[] getTransfer() {
471     return transferAgents;
472 }
473
474 void onDispose () {
475     if (control == null) return;
476
477     COM.RevokeDragDrop(control.handle);
478     
479     if (controlListener != null)
480         control.removeListener(SWT.Dispose, controlListener);
481     controlListener = null;
482     control.setData(DROPTARGETID, null);
483     transferAgents = null;
484     control = null;
485     
486     COM.CoLockObjectExternal(iDropTarget.getAddress(), false, true);
487     
488     this.Release();
489     
490     COM.CoFreeUnusedLibraries();
491 }
492
493 int opToOs(int operation) {
494     int osOperation = 0;
495     if ((operation & DND.DROP_COPY) != 0){
496         osOperation |= COM.DROPEFFECT_COPY;
497     }
498     if ((operation & DND.DROP_LINK) != 0) {
499         osOperation |= COM.DROPEFFECT_LINK;
500     }
501     if ((operation & DND.DROP_MOVE) != 0) {
502         osOperation |= COM.DROPEFFECT_MOVE;
503     }
504     return osOperation;
505 }
506
507 int osToOp(int osOperation){
508     int operation = 0;
509     if ((osOperation & COM.DROPEFFECT_COPY) != 0){
510         operation |= DND.DROP_COPY;
511     }
512     if ((osOperation & COM.DROPEFFECT_LINK) != 0) {
513         operation |= DND.DROP_LINK;
514     }
515     if ((osOperation & COM.DROPEFFECT_MOVE) != 0) {
516         operation |= DND.DROP_MOVE;
517     }
518     return operation;
519 }
520
521 /* QueryInterface([in] iid, [out] ppvObject)
522  * Ownership of ppvObject transfers from callee to caller so reference count on ppvObject
523  * must be incremented before returning. Caller is responsible for releasing ppvObject.
524  */

525 int QueryInterface(int riid, int ppvObject) {
526     
527     if (riid == 0 || ppvObject == 0)
528         return COM.E_INVALIDARG;
529     GUID guid = new GUID();
530     COM.MoveMemory(guid, riid, GUID.sizeof);
531     if (COM.IsEqualGUID(guid, COM.IIDIUnknown) || COM.IsEqualGUID(guid, COM.IIDIDropTarget)) {
532         OS.MoveMemory(ppvObject, new int[] {iDropTarget.getAddress()}, 4);
533         AddRef();
534         return COM.S_OK;
535     }
536
537     OS.MoveMemory(ppvObject, new int[] {0}, 4);
538     return COM.E_NOINTERFACE;
539 }
540
541 int Release() {
542     refCount--;
543     
544     if (refCount == 0) {
545         disposeCOMInterfaces();
546         COM.CoFreeUnusedLibraries();
547     }
548     
549     return refCount;
550 }
551
552 void refresh() {
553     if (control == null || control.isDisposed()) return;
554     int handle = control.handle;
555     RECT lpRect = new RECT();
556     if (OS.GetUpdateRect(handle, lpRect, false)) {
557         OS.ImageList_DragShowNolock(false);
558         OS.RedrawWindow(handle, lpRect, 0, OS.RDW_UPDATENOW | OS.RDW_INVALIDATE);
559         OS.ImageList_DragShowNolock(true);
560     }
561 }
562
563 /**
564  * Removes the listener from the collection of listeners who will
565  * be notified when a drag and drop operation is in progress.
566  *
567  * @param listener the listener which should be notified
568  *
569  * @exception IllegalArgumentException <ul>
570  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
571  * </ul>
572  * @exception SWTException <ul>
573  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
574  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
575  * </ul>
576  *
577  * @see DropTargetListener
578  * @see #addDropListener
579  */

580 public void removeDropListener(DropTargetListener listener) {
581     if (listener == null) DND.error (SWT.ERROR_NULL_ARGUMENT);
582     removeListener (DND.DragEnter, listener);
583     removeListener (DND.DragLeave, listener);
584     removeListener (DND.DragOver, listener);
585     removeListener (DND.DragOperationChanged, listener);
586     removeListener (DND.Drop, listener);
587     removeListener (DND.DropAccept, listener);
588 }
589
590 /**
591  * Specifies the drop effect for this DropTarget. This drop effect will be
592  * used during a drag and drop to display the drag under effect on the
593  * target widget.
594  *
595  * @param effect the drop effect that is registered for this DropTarget
596  *
597  * @since 3.3
598  */

599 public void setDropTargetEffect(DropTargetEffect effect) {
600     dropEffect = effect;
601 }
602
603 boolean setEventData(DNDEvent event, int pDataObject, int grfKeyState, int pt_x, int pt_y, int pdwEffect) {
604     if (pDataObject == 0 || pdwEffect == 0) return false;
605     
606     // get allowed operations
607
int style = getStyle();
608     int[] operations = new int[1];
609     OS.MoveMemory(operations, pdwEffect, 4);
610     operations[0] = osToOp(operations[0]) & style;
611     if (operations[0] == DND.DROP_NONE) return false;
612     
613     // get current operation
614
int operation = getOperationFromKeyState(grfKeyState);
615     keyOperation = operation;
616     if (operation == DND.DROP_DEFAULT) {
617         if ((style & DND.DROP_DEFAULT) == 0) {
618             operation = (operations[0] & DND.DROP_MOVE) != 0 ? DND.DROP_MOVE : DND.DROP_NONE;
619         }
620     } else {
621         if ((operation & operations[0]) == 0) operation = DND.DROP_NONE;
622     }
623     
624     // Get allowed transfer types
625
TransferData[] dataTypes = new TransferData[0];
626     IDataObject dataObject = new IDataObject(pDataObject);
627     dataObject.AddRef();
628     try {
629         int[] address = new int[1];
630         if (dataObject.EnumFormatEtc(COM.DATADIR_GET, address) != COM.S_OK) {
631             return false;
632         }
633         IEnumFORMATETC enumFormatetc = new IEnumFORMATETC(address[0]);
634         try {
635             // Loop over enumerator and save any types that match what we are looking for
636
int rgelt = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, FORMATETC.sizeof);
637             try {
638                 int[] pceltFetched = new int[1];
639                 enumFormatetc.Reset();
640                 while (enumFormatetc.Next(1, rgelt, pceltFetched) == COM.S_OK && pceltFetched[0] == 1) {
641                     TransferData transferData = new TransferData();
642                     transferData.formatetc = new FORMATETC();
643                     COM.MoveMemory(transferData.formatetc, rgelt, FORMATETC.sizeof);
644                     transferData.type = transferData.formatetc.cfFormat;
645                     transferData.pIDataObject = pDataObject;
646                     for (int i = 0; i < transferAgents.length; i++){
647                         Transfer transfer = transferAgents[i];
648                         if (transfer != null && transfer.isSupportedType(transferData)){
649                             TransferData[] newDataTypes = new TransferData[dataTypes.length + 1];
650                             System.arraycopy(dataTypes, 0, newDataTypes, 0, dataTypes.length);
651                             newDataTypes[dataTypes.length] = transferData;
652                             dataTypes = newDataTypes;
653                             break;
654                         }
655                     }
656                 }
657             } finally {
658                 OS.GlobalFree(rgelt);
659             }
660         } finally {
661             enumFormatetc.Release();
662         }
663     } finally {
664         dataObject.Release();
665     }
666     if (dataTypes.length == 0) return false;
667     
668     event.widget = this;
669     event.x = pt_x;
670     event.y = pt_y;
671     event.time = OS.GetMessageTime();
672     event.feedback = DND.FEEDBACK_SELECT;
673     event.dataTypes = dataTypes;
674     event.dataType = dataTypes[0];
675     if (dropEffect != null) {
676         event.item = dropEffect.getItem(pt_x, pt_y);
677     }
678     event.operations = operations[0];
679     event.detail = operation;
680     return true;
681 }
682
683 /**
684  * Specifies the data types that can be transferred to this DropTarget. If data is
685  * being dragged that does not match one of these types, the drop target will be notified of
686  * the drag and drop operation but the currentDataType will be null and the operation
687  * will be DND.NONE.
688  *
689  * @param transferAgents a list of Transfer objects which define the types of data that can be
690  * dropped on this target
691  *
692  * @exception IllegalArgumentException <ul>
693  * <li>ERROR_NULL_ARGUMENT - if transferAgents is null</li>
694  * </ul>
695  */

696 public void setTransfer(Transfer[] transferAgents){
697     if (transferAgents == null) DND.error(SWT.ERROR_NULL_ARGUMENT);
698     this.transferAgents = transferAgents;
699 }
700 }
701
Popular Tags