Mercurial > projects > dwt-addons
annotate dwtx/jface/util/DelegatingDropAdapter.d @ 104:04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
These new wrappers now use the tango.util.containers instead of the tango.util.collections.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 07 Aug 2008 15:01:33 +0200 |
parents | 644f1334b451 |
children |
rev | line source |
---|---|
30 | 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.util.DelegatingDropAdapter; | |
14 | |
15 import dwtx.jface.util.TransferDropTargetListener; | |
16 import dwtx.jface.util.SafeRunnable; | |
17 | |
18 | |
19 import dwt.dnd.DND; | |
20 import dwt.dnd.DropTargetEvent; | |
21 import dwt.dnd.DropTargetListener; | |
22 import dwt.dnd.Transfer; | |
23 import dwt.dnd.TransferData; | |
24 | |
25 import dwt.dwthelper.utils; | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
26 import dwtx.dwtxhelper.Collection; |
30 | 27 |
28 /** | |
29 * A <code>DelegatingDropAdapter</code> is a <code>DropTargetListener</code> that | |
30 * maintains and delegates to a set of {@link TransferDropTargetListener}s. Each | |
31 * <code>TransferDropTargetListener</code> can then be implemented as if it were | |
32 * the DropTarget's only <code>DropTargetListener</code>. | |
33 * <p> | |
34 * On <code>dragEnter</code>, <code>dragOperationChanged</code>, <code>dragOver</code> | |
35 * and <code>drop</code>, a <i>current</i> listener is obtained from the set of all | |
36 * <code>TransferDropTargetListeners</code>. The current listener is the first listener | |
37 * to return <code>true</code> for | |
38 * {@link TransferDropTargetListener#isEnabled(DropTargetEvent)}. | |
39 * The current listener is forwarded all <code>DropTargetEvents</code> until some other | |
40 * listener becomes the current listener, or the drop terminates. | |
41 * </p> | |
42 * <p> | |
43 * After adding all <code>TransferDropTargetListeners</code> to the | |
44 * <code>DelegatingDropAdapter</code> the combined set of <code>Transfers</code> should | |
45 * be set in the DWT <code>DropTarget</code>. <code>#getTransfers()</code> provides the | |
46 * set of <code>Transfer</code> types of all <code>TransferDropTargetListeners</code>. | |
47 * </p> | |
48 * <p> | |
49 * The following example snippet shows a <code>DelegatingDropAdapter</code> with two | |
50 * <code>TransferDropTargetListeners</code>. One supports dropping resources and | |
51 * demonstrates how a listener can be disabled in the isEnabled method. | |
52 * The other listener supports text transfer. | |
53 * </p> | |
54 * <code><pre> | |
55 * final TreeViewer viewer = new TreeViewer(shell, DWT.NONE); | |
56 * DelegatingDropAdapter dropAdapter = new DelegatingDropAdapter(); | |
57 * dropAdapter.addDropTargetListener(new TransferDropTargetListener() { | |
58 * public Transfer getTransfer() { | |
59 * return ResourceTransfer.getInstance(); | |
60 * } | |
61 * public bool isEnabled(DropTargetEvent event) { | |
62 * // disable drop listener if there is no viewer selection | |
63 * if (viewer.getSelection().isEmpty()) | |
64 * return false; | |
65 * return true; | |
66 * } | |
67 * public void dragEnter(DropTargetEvent event) {} | |
68 * public void dragLeave(DropTargetEvent event) {} | |
69 * public void dragOperationChanged(DropTargetEvent event) {} | |
70 * public void dragOver(DropTargetEvent event) {} | |
71 * public void drop(DropTargetEvent event) { | |
72 * if (event.data is null) | |
73 * return; | |
74 * IResource[] resources = (IResource[]) event.data; | |
75 * if (event.detail is DND.DROP_COPY) { | |
76 * // copy resources | |
77 * } else { | |
78 * // move resources | |
79 * } | |
80 * | |
81 * } | |
82 * public void dropAccept(DropTargetEvent event) {} | |
83 * }); | |
84 * dropAdapter.addDropTargetListener(new TransferDropTargetListener() { | |
85 * public Transfer getTransfer() { | |
86 * return TextTransfer.getInstance(); | |
87 * } | |
88 * public bool isEnabled(DropTargetEvent event) { | |
89 * return true; | |
90 * } | |
91 * public void dragEnter(DropTargetEvent event) {} | |
92 * public void dragLeave(DropTargetEvent event) {} | |
93 * public void dragOperationChanged(DropTargetEvent event) {} | |
94 * public void dragOver(DropTargetEvent event) {} | |
95 * public void drop(DropTargetEvent event) { | |
96 * if (event.data is null) | |
97 * return; | |
98 * System.out.println(event.data); | |
99 * } | |
100 * public void dropAccept(DropTargetEvent event) {} | |
101 * }); | |
102 * viewer.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, dropAdapter.getTransfers(), dropAdapter); | |
103 * </pre></code> | |
104 * @since 3.0 | |
105 */ | |
106 public class DelegatingDropAdapter : DropTargetListener { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
107 private List listeners; |
30 | 108 |
109 private TransferDropTargetListener currentListener; | |
110 | |
111 private int originalDropType; | |
112 | |
113 this(){ | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
114 listeners = new ArrayList(); |
30 | 115 } |
116 | |
117 /** | |
118 * Adds the given <code>TransferDropTargetListener</code>. | |
119 * | |
120 * @param listener the new listener | |
121 */ | |
122 public void addDropTargetListener(TransferDropTargetListener listener) { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
123 listeners.add(cast(Object)listener); |
30 | 124 } |
125 | |
126 /** | |
127 * The cursor has entered the drop target boundaries. The current listener is | |
128 * updated, and <code>#dragEnter()</code> is forwarded to the current listener. | |
129 * | |
130 * @param event the drop target event | |
131 * @see DropTargetListener#dragEnter(DropTargetEvent) | |
132 */ | |
133 public void dragEnter(DropTargetEvent event) { | |
134 // if (Policy.DEBUG_DRAG_DROP) | |
135 // System.out.println("Drag Enter: " + toString()); //$NON-NLS-1$ | |
136 originalDropType = event.detail; | |
137 updateCurrentListener(event); | |
138 } | |
139 | |
140 /** | |
141 * The cursor has left the drop target boundaries. The event is forwarded to the | |
142 * current listener. | |
143 * | |
144 * @param event the drop target event | |
145 * @see DropTargetListener#dragLeave(DropTargetEvent) | |
146 */ | |
147 public void dragLeave(DropTargetEvent event) { | |
148 // if (Policy.DEBUG_DRAG_DROP) | |
149 // System.out.println("Drag Leave: " + toString()); //$NON-NLS-1$ | |
150 setCurrentListener(null, event); | |
151 } | |
152 | |
153 /** | |
154 * The operation being performed has changed (usually due to the user changing | |
155 * a drag modifier key while dragging). Updates the current listener and forwards | |
156 * this event to that listener. | |
157 * | |
158 * @param event the drop target event | |
159 * @see DropTargetListener#dragOperationChanged(DropTargetEvent) | |
160 */ | |
161 public void dragOperationChanged(DropTargetEvent event) { | |
162 // if (Policy.DEBUG_DRAG_DROP) | |
163 // System.out.println("Drag Operation Changed to: " + event.detail); //$NON-NLS-1$ | |
164 originalDropType = event.detail; | |
165 TransferDropTargetListener oldListener = getCurrentListener(); | |
166 updateCurrentListener(event); | |
167 TransferDropTargetListener newListener = getCurrentListener(); | |
168 // only notify the current listener if it hasn't changed based on the | |
169 // operation change. otherwise the new listener would get a dragEnter | |
170 // followed by a dragOperationChanged with the exact same event. | |
171 if (newListener !is null && newListener is oldListener) { | |
39 | 172 SafeRunnable.run(new class(event,newListener) SafeRunnable { |
30 | 173 DropTargetEvent event_; |
174 TransferDropTargetListener newListener_; | |
39 | 175 this(DropTargetEvent a,TransferDropTargetListener b){ |
176 event_=a; | |
177 newListener_=b; | |
30 | 178 } |
179 public void run() { | |
180 newListener_.dragOperationChanged(event_); | |
181 } | |
182 }); | |
183 } | |
184 } | |
185 | |
186 /** | |
187 * The cursor is moving over the drop target. Updates the current listener and | |
188 * forwards this event to that listener. If no listener can handle the drag | |
189 * operation the <code>event.detail</code> field is set to <code>DND.DROP_NONE</code> | |
190 * to indicate an invalid drop. | |
191 * | |
192 * @param event the drop target event | |
193 * @see DropTargetListener#dragOver(DropTargetEvent) | |
194 */ | |
195 public void dragOver(DropTargetEvent event) { | |
196 TransferDropTargetListener oldListener = getCurrentListener(); | |
197 updateCurrentListener(event); | |
198 TransferDropTargetListener newListener = getCurrentListener(); | |
199 | |
200 // only notify the current listener if it hasn't changed based on the | |
201 // drag over. otherwise the new listener would get a dragEnter | |
202 // followed by a dragOver with the exact same event. | |
203 if (newListener !is null && newListener is oldListener) { | |
39 | 204 SafeRunnable.run(new class(event,newListener) SafeRunnable { |
30 | 205 DropTargetEvent event_; |
206 TransferDropTargetListener newListener_; | |
39 | 207 this(DropTargetEvent a,TransferDropTargetListener b){ |
208 event_=a; | |
209 newListener_=b; | |
30 | 210 } |
211 public void run() { | |
212 newListener_.dragOver(event_); | |
213 } | |
214 }); | |
215 } | |
216 } | |
217 | |
218 /** | |
219 * Forwards this event to the current listener, if there is one. Sets the | |
220 * current listener to <code>null</code> afterwards. | |
221 * | |
222 * @param event the drop target event | |
223 * @see DropTargetListener#drop(DropTargetEvent) | |
224 */ | |
225 public void drop(DropTargetEvent event) { | |
226 // if (Policy.DEBUG_DRAG_DROP) | |
227 // System.out.println("Drop: " + toString()); //$NON-NLS-1$ | |
228 updateCurrentListener(event); | |
229 if (getCurrentListener() !is null) { | |
39 | 230 SafeRunnable.run(new class(event) SafeRunnable { |
30 | 231 DropTargetEvent event_; |
39 | 232 this(DropTargetEvent a){ event_=a;} |
30 | 233 public void run() { |
234 getCurrentListener().drop(event_); | |
235 } | |
236 }); | |
237 } | |
238 setCurrentListener(null, event); | |
239 } | |
240 | |
241 /** | |
242 * Forwards this event to the current listener if there is one. | |
243 * | |
244 * @param event the drop target event | |
245 * @see DropTargetListener#dropAccept(DropTargetEvent) | |
246 */ | |
247 public void dropAccept(DropTargetEvent event) { | |
248 // if (Policy.DEBUG_DRAG_DROP) | |
249 // System.out.println("Drop Accept: " + toString()); //$NON-NLS-1$ | |
250 if (getCurrentListener() !is null) { | |
39 | 251 SafeRunnable.run(new class(event) SafeRunnable { |
30 | 252 DropTargetEvent event_; |
39 | 253 this(DropTargetEvent a){ event_=a;} |
30 | 254 public void run() { |
255 getCurrentListener().dropAccept(event_); | |
256 } | |
257 }); | |
258 } | |
259 } | |
260 | |
261 /** | |
262 * Returns the listener which currently handles drop events. | |
263 * | |
264 * @return the <code>TransferDropTargetListener</code> which currently | |
265 * handles drop events. | |
266 */ | |
267 private TransferDropTargetListener getCurrentListener() { | |
268 return currentListener; | |
269 } | |
270 | |
271 /** | |
272 * Returns the transfer data type supported by the given listener. | |
273 * Returns <code>null</code> if the listener does not support any of the | |
274 * specified data types. | |
275 * | |
276 * @param dataTypes available data types | |
277 * @param listener <code>TransferDropTargetListener</code> to use for testing | |
278 * supported data types. | |
279 * @return the transfer data type supported by the given listener or | |
280 * <code>null</code>. | |
281 */ | |
282 private TransferData getSupportedTransferType(TransferData[] dataTypes, | |
283 TransferDropTargetListener listener) { | |
284 for (int i = 0; i < dataTypes.length; i++) { | |
285 if (listener.getTransfer().isSupportedType(dataTypes[i])) { | |
286 return dataTypes[i]; | |
287 } | |
288 } | |
289 return null; | |
290 } | |
291 | |
292 /** | |
293 * Returns the combined set of <code>Transfer</code> types of all | |
294 * <code>TransferDropTargetListeners</code>. | |
295 * | |
296 * @return the combined set of <code>Transfer</code> types | |
297 */ | |
298 public Transfer[] getTransfers() { | |
299 Transfer[] types = new Transfer[listeners.size()]; | |
300 for (int i = 0; i < listeners.size(); i++) { | |
301 TransferDropTargetListener listener = cast(TransferDropTargetListener) listeners | |
302 .get(i); | |
303 types[i] = listener.getTransfer(); | |
304 } | |
305 return types; | |
306 } | |
307 | |
308 /** | |
309 * Returns <code>true</code> if there are no listeners to delegate events to. | |
310 * | |
311 * @return <code>true</code> if there are no <code>TransferDropTargetListeners</code> | |
312 * <code>false</code> otherwise | |
313 */ | |
314 public bool isEmpty() { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
315 return listeners.isEmpty(); |
30 | 316 } |
317 | |
318 /** | |
319 * Removes the given <code>TransferDropTargetListener</code>. | |
320 * Listeners should not be removed while a drag and drop operation is in progress. | |
321 * | |
322 * @param listener the listener to remove | |
323 */ | |
324 public void removeDropTargetListener(TransferDropTargetListener listener) { | |
325 if (currentListener is listener) { | |
326 currentListener = null; | |
327 } | |
328 listeners.remove(cast(Object)listener); | |
329 } | |
330 | |
331 /** | |
332 * Sets the current listener to <code>listener</code>. Sends the given | |
333 * <code>DropTargetEvent</code> if the current listener changes. | |
334 * | |
335 * @return <code>true</code> if the new listener is different than the previous | |
336 * <code>false</code> otherwise | |
337 */ | |
338 private bool setCurrentListener(TransferDropTargetListener listener, | |
339 DropTargetEvent event) { | |
340 if (currentListener is listener) { | |
341 return false; | |
342 } | |
343 if (currentListener !is null) { | |
39 | 344 SafeRunnable.run(new class(event) SafeRunnable { |
30 | 345 DropTargetEvent event_; |
39 | 346 this(DropTargetEvent a){ event_=a;} |
30 | 347 public void run() { |
348 currentListener.dragLeave(event_); | |
349 } | |
350 }); | |
351 } | |
352 currentListener = listener; | |
353 // if (Policy.DEBUG_DRAG_DROP) | |
354 // System.out.println("Current drop listener: " + listener); //$NON-NLS-1$ | |
355 if (currentListener !is null) { | |
39 | 356 SafeRunnable.run(new class(event) SafeRunnable { |
30 | 357 DropTargetEvent event_; |
39 | 358 this(DropTargetEvent a){ event_=a;} |
30 | 359 public void run() { |
360 currentListener.dragEnter(event_); | |
361 } | |
362 }); | |
363 } | |
364 return true; | |
365 } | |
366 | |
367 /** | |
368 * Updates the current listener to one that can handle the drop. There can be many | |
369 * listeners and each listener may be able to handle many <code>TransferData</code> | |
370 * types. The first listener found that can handle a drop of one of the given | |
371 * <code>TransferData</code> types will be selected. | |
372 * If no listener can handle the drag operation the <code>event.detail</code> field | |
373 * is set to <code>DND.DROP_NONE</code> to indicate an invalid drop. | |
374 * | |
375 * @param event the drop target event | |
376 */ | |
377 private void updateCurrentListener(DropTargetEvent event) { | |
378 int originalDetail = event.detail; | |
379 // revert the detail to the "original" drop type that the User indicated. | |
380 // this is necessary because the previous listener may have changed the detail | |
381 // to something other than what the user indicated. | |
382 event.detail = originalDropType; | |
383 | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
384 Iterator iter = listeners.iterator(); |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
385 while (iter.hasNext()) { |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
386 TransferDropTargetListener listener = cast(TransferDropTargetListener) iter |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
387 .next(); |
30 | 388 TransferData dataType = getSupportedTransferType(event.dataTypes, |
389 listener); | |
390 if (dataType !is null) { | |
391 TransferData originalDataType = event.currentDataType; | |
392 // set the data type supported by the drop listener | |
393 event.currentDataType = dataType; | |
394 if (listener.isEnabled(event)) { | |
395 // if the listener stays the same, set its previously determined | |
396 // event detail | |
397 if (!setCurrentListener(listener, event)) { | |
398 event.detail = originalDetail; | |
399 } | |
400 return; | |
401 } | |
402 event.currentDataType = originalDataType; | |
403 } | |
404 } | |
405 setCurrentListener(null, event); | |
406 event.detail = DND.DROP_NONE; | |
407 | |
408 // -always- ensure that expand/scroll are on...otherwise | |
409 // if a valid drop target is a child of an invalid one | |
410 // you can't get there... | |
411 event.feedback = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL; | |
412 } | |
413 } |