30
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 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 dwtx.jface.util.DelegatingDragAdapter;
|
|
14
|
|
15 import dwtx.jface.util.TransferDragSourceListener;
|
|
16 import dwtx.jface.util.SafeRunnable;
|
|
17
|
|
18 import tango.util.collection.ArraySeq;
|
|
19
|
|
20 import dwt.dnd.DragSource;
|
|
21 import dwt.dnd.DragSourceEvent;
|
|
22 import dwt.dnd.DragSourceListener;
|
|
23 import dwt.dnd.Transfer;
|
|
24 import dwt.dnd.TransferData;
|
|
25
|
|
26 import dwt.dwthelper.utils;
|
|
27
|
|
28 /**
|
|
29 * A <code>DelegatingDragAdapter</code> is a <code>DragSourceListener</code> that
|
|
30 * maintains and delegates to a set of {@link TransferDragSourceListener}s. Each
|
|
31 * TransferDragSourceListener can then be implemented as if it were the
|
|
32 * <code>DragSource's</code> only DragSourceListener.
|
|
33 * <p>
|
|
34 * When a drag is started, a subset of all <code>TransferDragSourceListeners</code>
|
|
35 * is generated and stored in a list of <i>active</i> listeners. This subset is
|
|
36 * calculated by forwarding {@link DragSourceListener#dragStart(DragSourceEvent)} to
|
|
37 * every listener, and checking if the {@link DragSourceEvent#doit doit} field is left
|
|
38 * set to <code>true</code>.
|
|
39 * </p>
|
|
40 * The <code>DragSource</code>'s set of supported Transfer types ({@link
|
|
41 * DragSource#setTransfer(Transfer[])}) is updated to reflect the Transfer types
|
|
42 * corresponding to the active listener subset.
|
|
43 * <p>
|
|
44 * If and when {@link #dragSetData(DragSourceEvent)} is called, a single
|
|
45 * <code>TransferDragSourceListener</code> is chosen, and only it is allowed to set the
|
|
46 * drag data. The chosen listener is the first listener in the subset of active listeners
|
|
47 * whose Transfer supports ({@link Transfer#isSupportedType(TransferData)}) the
|
|
48 * <code>dataType</code> in the <code>DragSourceEvent</code>.
|
|
49 * </p>
|
|
50 * <p>
|
|
51 * The following example snippet shows a <code>DelegatingDragAdapter</code> with two
|
|
52 * <code>TransferDragSourceListeners</code>. One implements drag of text strings,
|
|
53 * the other supports file transfer and demonstrates how a listener can be disabled using
|
|
54 * the dragStart method.
|
|
55 * </p>
|
|
56 * <code><pre>
|
|
57 * final TreeViewer viewer = new TreeViewer(shell, DWT.NONE);
|
|
58 *
|
|
59 * DelegatingDragAdapter dragAdapter = new DelegatingDragAdapter();
|
|
60 * dragAdapter.addDragSourceListener(new TransferDragSourceListener() {
|
|
61 * public Transfer getTransfer() {
|
|
62 * return TextTransfer.getInstance();
|
|
63 * }
|
|
64 * public void dragStart(DragSourceEvent event) {
|
|
65 * // always enabled, can control enablement based on selection etc.
|
|
66 * }
|
|
67 * public void dragSetData(DragSourceEvent event) {
|
|
68 * event.data = "Transfer data";
|
|
69 * }
|
|
70 * public void dragFinished(DragSourceEvent event) {
|
|
71 * // no clean-up required
|
|
72 * }
|
|
73 * });
|
|
74 * dragAdapter.addDragSourceListener(new TransferDragSourceListener() {
|
|
75 * public Transfer getTransfer() {
|
|
76 * return FileTransfer.getInstance();
|
|
77 * }
|
|
78 * public void dragStart(DragSourceEvent event) {
|
|
79 * // enable drag listener if there is a viewer selection
|
|
80 * event.doit = !viewer.getSelection().isEmpty();
|
|
81 * }
|
|
82 * public void dragSetData(DragSourceEvent event) {
|
|
83 * File file1 = new File("C:/temp/file1");
|
|
84 * File file2 = new File("C:/temp/file2");
|
|
85 * event.data = new String[] {file1.getAbsolutePath(), file2.getAbsolutePath()};
|
|
86 * }
|
|
87 * public void dragFinished(DragSourceEvent event) {
|
|
88 * // no clean-up required
|
|
89 * }
|
|
90 * });
|
|
91 * viewer.addDragSupport(DND.DROP_COPY | DND.DROP_MOVE, dragAdapter.getTransfers(), dragAdapter);
|
|
92 * </pre></code>
|
|
93 * @since 3.0
|
|
94 */
|
|
95 public class DelegatingDragAdapter : DragSourceListener {
|
|
96 private ArraySeq!(Object) listeners;
|
|
97
|
|
98 private ArraySeq!(Object) activeListeners;
|
|
99
|
|
100 private TransferDragSourceListener currentListener;
|
|
101
|
|
102 this(){
|
|
103 listeners = new ArraySeq!(Object);
|
|
104 activeListeners = new ArraySeq!(Object);
|
|
105 }
|
|
106
|
|
107 /**
|
|
108 * Adds the given <code>TransferDragSourceListener</code>.
|
|
109 *
|
|
110 * @param listener the new listener
|
|
111 */
|
|
112 public void addDragSourceListener(TransferDragSourceListener listener) {
|
|
113 listeners.append(cast(Object)listener);
|
|
114 }
|
|
115
|
|
116 /**
|
|
117 * The drop has successfully completed. This event is forwarded to the current
|
|
118 * drag listener.
|
|
119 * Doesn't update the current listener, since the current listener is already the one
|
|
120 * that completed the drag operation.
|
|
121 *
|
|
122 * @param event the drag source event
|
|
123 * @see DragSourceListener#dragFinished(DragSourceEvent)
|
|
124 */
|
|
125 public void dragFinished(DragSourceEvent event) {
|
|
126 // if (Policy.DEBUG_DRAG_DROP)
|
|
127 // System.out.println("Drag Finished: " + toString()); //$NON-NLS-1$
|
39
|
128 SafeRunnable.run(new class(event) SafeRunnable {
|
30
|
129 DragSourceEvent event_;
|
39
|
130 this(DragSourceEvent a){
|
|
131 event_=a;
|
30
|
132 }
|
|
133 public void run() {
|
|
134 if (currentListener !is null) {
|
|
135 // there is a listener that can handle the drop, delegate the event
|
|
136 currentListener.dragFinished(event_);
|
|
137 } else {
|
|
138 // The drag was canceled and currentListener was never set, so send the
|
|
139 // dragFinished event to all the active listeners.
|
|
140 foreach( e; activeListeners ){
|
|
141 (cast(TransferDragSourceListener) e)
|
|
142 .dragFinished(event);
|
|
143 }
|
|
144 }
|
|
145 }
|
|
146 });
|
|
147 currentListener = null;
|
|
148 activeListeners.clear();
|
|
149 }
|
|
150
|
|
151 /**
|
|
152 * The drop data is requested.
|
|
153 * Updates the current listener and then forwards the event to it.
|
|
154 *
|
|
155 * @param event the drag source event
|
|
156 * @see DragSourceListener#dragSetData(DragSourceEvent)
|
|
157 */
|
|
158 public void dragSetData(DragSourceEvent event) {
|
|
159 // if (Policy.DEBUG_DRAG_DROP)
|
|
160 // System.out.println("Drag Set Data: " + toString()); //$NON-NLS-1$
|
|
161
|
|
162 updateCurrentListener(event); // find a listener that can provide the given data type
|
|
163 if (currentListener !is null) {
|
39
|
164 SafeRunnable.run(new class(event) SafeRunnable {
|
30
|
165 DragSourceEvent event_;
|
39
|
166 this(DragSourceEvent a){
|
|
167 event_=a;
|
30
|
168 }
|
|
169 public void run() {
|
|
170 currentListener.dragSetData(event_);
|
|
171 }
|
|
172 });
|
|
173 }
|
|
174 }
|
|
175
|
|
176 /**
|
|
177 * A drag operation has started.
|
|
178 * Forwards this event to each listener. A listener must set <code>event.doit</code>
|
|
179 * to <code>false</code> if it cannot handle the drag operation. If a listener can
|
|
180 * handle the drag, it is added to the list of active listeners.
|
|
181 * The drag is aborted if there are no listeners that can handle it.
|
|
182 *
|
|
183 * @param event the drag source event
|
|
184 * @see DragSourceListener#dragStart(DragSourceEvent)
|
|
185 */
|
|
186 public void dragStart(DragSourceEvent event) {
|
|
187 // if (Policy.DEBUG_DRAG_DROP)
|
|
188 // System.out.println("Drag Start: " + toString()); //$NON-NLS-1$
|
|
189 bool doit = false; // true if any one of the listeners can handle the drag
|
|
190 auto transfers = new ArraySeq!(Object);
|
|
191 transfers.capacity(listeners.size());
|
|
192
|
|
193 activeListeners.clear();
|
|
194 for (int i = 0; i < listeners.size(); i++) {
|
|
195 TransferDragSourceListener listener = cast(TransferDragSourceListener) listeners
|
|
196 .get(i);
|
|
197 event.doit = true; // restore event.doit
|
39
|
198 SafeRunnable.run(new class(event,listener) SafeRunnable {
|
30
|
199 TransferDragSourceListener listener_;
|
|
200 DragSourceEvent event_;
|
39
|
201 this(DragSourceEvent a,TransferDragSourceListener b){
|
|
202 event_=a;
|
|
203 listener_=b;
|
30
|
204 }
|
|
205 public void run() {
|
|
206 listener_.dragStart(event_);
|
|
207 }
|
|
208 });
|
|
209 if (event.doit) { // the listener can handle this drag
|
|
210 transfers.append(listener.getTransfer());
|
|
211 activeListeners.append(cast(Object)listener);
|
|
212 }
|
|
213 doit |= event.doit;
|
|
214 }
|
|
215
|
|
216 if (doit) {
|
|
217 (cast(DragSource) event.widget).setTransfer(arraycast!(Transfer)( transfers
|
|
218 .toArray()));
|
|
219 }
|
|
220
|
|
221 event.doit = doit;
|
|
222 }
|
|
223
|
|
224 /**
|
|
225 * Returns the <code>Transfer<code>s from every <code>TransferDragSourceListener</code>.
|
|
226 *
|
|
227 * @return the combined <code>Transfer</code>s
|
|
228 */
|
|
229 public Transfer[] getTransfers() {
|
|
230 Transfer[] types = new Transfer[listeners.size()];
|
|
231 for (int i = 0; i < listeners.size(); i++) {
|
|
232 TransferDragSourceListener listener = cast(TransferDragSourceListener) listeners
|
|
233 .get(i);
|
|
234 types[i] = listener.getTransfer();
|
|
235 }
|
|
236 return types;
|
|
237 }
|
|
238
|
|
239 /**
|
|
240 * Returns <code>true</code> if there are no listeners to delegate drag events to.
|
|
241 *
|
|
242 * @return <code>true</code> if there are no <code>TransferDragSourceListeners</code>
|
|
243 * <code>false</code> otherwise.
|
|
244 */
|
|
245 public bool isEmpty() {
|
|
246 return listeners.drained();
|
|
247 }
|
|
248
|
|
249 /**
|
|
250 * Removes the given <code>TransferDragSourceListener</code>.
|
|
251 * Listeners should not be removed while a drag and drop operation is in progress.
|
|
252 *
|
|
253 * @param listener the <code>TransferDragSourceListener</code> to remove
|
|
254 */
|
|
255 public void removeDragSourceListener(TransferDragSourceListener listener) {
|
|
256 listeners.remove(cast(Object)listener);
|
|
257 if (currentListener is listener) {
|
|
258 currentListener = null;
|
|
259 }
|
|
260 if (activeListeners.contains(cast(Object)listener)) {
|
|
261 activeListeners.remove(cast(Object)listener);
|
|
262 }
|
|
263 }
|
|
264
|
|
265 /**
|
|
266 * Updates the current listener to one that can handle the drag. There can
|
|
267 * be many listeners and each listener may be able to handle many <code>TransferData</code>
|
|
268 * types. The first listener found that supports one of the <code>TransferData</ode>
|
|
269 * types specified in the <code>DragSourceEvent</code> will be selected.
|
|
270 *
|
|
271 * @param event the drag source event
|
|
272 */
|
|
273 private void updateCurrentListener(DragSourceEvent event) {
|
|
274 currentListener = null;
|
|
275 if (event.dataType is null) {
|
|
276 return;
|
|
277 }
|
|
278 foreach( e; activeListeners ){
|
|
279 TransferDragSourceListener listener = cast(TransferDragSourceListener)e;
|
|
280
|
|
281 if (listener.getTransfer().isSupportedType(event.dataType)) {
|
|
282 // if (Policy.DEBUG_DRAG_DROP)
|
|
283 // System.out.println("Current drag listener: " + listener); //$NON-NLS-1$
|
|
284 currentListener = listener;
|
|
285 return;
|
|
286 }
|
|
287 }
|
|
288 }
|
|
289
|
|
290 }
|