Mercurial > projects > dwt-addons
annotate dwtx/jface/util/DelegatingDragAdapter.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, 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 | |
19 import dwt.dnd.DragSource; | |
20 import dwt.dnd.DragSourceEvent; | |
21 import dwt.dnd.DragSourceListener; | |
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>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 { | |
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
|
96 private List listeners; |
30 | 97 |
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
|
98 private List activeListeners; |
30 | 99 |
100 private TransferDragSourceListener currentListener; | |
101 | |
102 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
|
103 listeners = new ArrayList(); |
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
|
104 activeListeners = new ArrayList(); |
30 | 105 } |
106 | |
107 /** | |
108 * Adds the given <code>TransferDragSourceListener</code>. | |
109 * | |
110 * @param listener the new listener | |
111 */ | |
112 public void addDragSourceListener(TransferDragSourceListener 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
|
113 listeners.add(cast(Object)listener); |
30 | 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. | |
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
|
140 Iterator iterator = activeListeners.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
|
141 while (iterator.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
|
142 (cast(TransferDragSourceListener) iterator.next()) |
30 | 143 .dragFinished(event); |
144 } | |
145 } | |
146 } | |
147 }); | |
148 currentListener = null; | |
149 activeListeners.clear(); | |
150 } | |
151 | |
152 /** | |
153 * The drop data is requested. | |
154 * Updates the current listener and then forwards the event to it. | |
155 * | |
156 * @param event the drag source event | |
157 * @see DragSourceListener#dragSetData(DragSourceEvent) | |
158 */ | |
159 public void dragSetData(DragSourceEvent event) { | |
160 // if (Policy.DEBUG_DRAG_DROP) | |
161 // System.out.println("Drag Set Data: " + toString()); //$NON-NLS-1$ | |
162 | |
163 updateCurrentListener(event); // find a listener that can provide the given data type | |
164 if (currentListener !is null) { | |
39 | 165 SafeRunnable.run(new class(event) SafeRunnable { |
30 | 166 DragSourceEvent event_; |
39 | 167 this(DragSourceEvent a){ |
168 event_=a; | |
30 | 169 } |
170 public void run() { | |
171 currentListener.dragSetData(event_); | |
172 } | |
173 }); | |
174 } | |
175 } | |
176 | |
177 /** | |
178 * A drag operation has started. | |
179 * Forwards this event to each listener. A listener must set <code>event.doit</code> | |
180 * to <code>false</code> if it cannot handle the drag operation. If a listener can | |
181 * handle the drag, it is added to the list of active listeners. | |
182 * The drag is aborted if there are no listeners that can handle it. | |
183 * | |
184 * @param event the drag source event | |
185 * @see DragSourceListener#dragStart(DragSourceEvent) | |
186 */ | |
187 public void dragStart(DragSourceEvent event) { | |
188 // if (Policy.DEBUG_DRAG_DROP) | |
189 // System.out.println("Drag Start: " + toString()); //$NON-NLS-1$ | |
190 bool doit = false; // true if any one of the listeners can handle the drag | |
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
|
191 List transfers = new ArrayList(listeners.size()); |
30 | 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 | |
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
|
210 transfers.add(listener.getTransfer()); |
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
|
211 activeListeners.add(cast(Object)listener); |
30 | 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() { | |
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
|
246 return listeners.isEmpty(); |
30 | 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 } | |
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
|
278 Iterator iterator = activeListeners.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
|
279 while (iterator.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
|
280 TransferDragSourceListener listener = cast(TransferDragSourceListener) 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
|
281 .next(); |
30 | 282 |
283 if (listener.getTransfer().isSupportedType(event.dataType)) { | |
284 // if (Policy.DEBUG_DRAG_DROP) | |
285 // System.out.println("Current drag listener: " + listener); //$NON-NLS-1$ | |
286 currentListener = listener; | |
287 return; | |
288 } | |
289 } | |
290 } | |
291 | |
292 } |