Mercurial > projects > dwt-addons
annotate dwtx/jface/action/SubContributionManager.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 |
---|---|
27 | 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.action.SubContributionManager; | |
14 | |
15 import dwtx.jface.action.IContributionManager; | |
16 import dwtx.jface.action.ActionContributionItem; | |
17 import dwtx.jface.action.IAction; | |
18 import dwtx.jface.action.SubContributionItem; | |
19 import dwtx.jface.action.IContributionItem; | |
20 import dwtx.jface.action.IContributionManagerOverrides; | |
21 | |
22 | |
23 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
|
24 import dwtx.dwtxhelper.Collection; |
27 | 25 |
26 /** | |
27 * A <code>SubContributionManager</code> is used to define a set of contribution | |
28 * items within a parent manager. Once defined, the visibility of the entire set can | |
29 * be changed as a unit. | |
30 */ | |
31 public abstract class SubContributionManager : IContributionManager { | |
32 /** | |
33 * The parent contribution manager. | |
34 */ | |
35 private IContributionManager parentMgr; | |
36 | |
37 /** | |
38 * Maps each item in the manager to a wrapper. The wrapper is used to | |
39 * control the visibility of each item. | |
40 */ | |
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
|
41 private Map mapItemToWrapper; |
27 | 42 |
43 /** | |
44 * The visibility of the manager, | |
45 */ | |
46 private bool visible = false; | |
47 | |
48 /** | |
49 * Constructs a new <code>SubContributionManager</code> | |
50 * | |
51 * @param mgr the parent contribution manager. All contributions made to the | |
52 * <code>SubContributionManager</code> are forwarded and appear in the | |
53 * parent manager. | |
54 */ | |
55 public this(IContributionManager mgr) { | |
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
|
56 mapItemToWrapper = new HashMap(); |
27 | 57 //super(); |
58 parentMgr = mgr; | |
59 } | |
60 | |
61 /* (non-Javadoc) | |
62 * Method declared on IContributionManager. | |
63 */ | |
64 public void add(IAction action) { | |
65 add(new ActionContributionItem(action)); | |
66 } | |
67 | |
68 /* (non-Javadoc) | |
69 * Method declared on IContributionManager. | |
70 */ | |
71 public void add(IContributionItem item) { | |
72 item.setParent(this); | |
73 SubContributionItem wrap = wrap(item); | |
74 wrap.setVisible(visible); | |
75 parentMgr.add(wrap); | |
76 itemAdded(item, wrap); | |
77 } | |
78 | |
79 /* (non-Javadoc) | |
80 * Method declared on IContributionManager. | |
81 */ | |
82 public void appendToGroup(String groupName, IAction action) { | |
83 appendToGroup(groupName, new ActionContributionItem(action)); | |
84 } | |
85 | |
86 /* (non-Javadoc) | |
87 * Method declared on IContributionManager. | |
88 */ | |
89 public void appendToGroup(String groupName, IContributionItem item) { | |
90 item.setParent(this); | |
91 SubContributionItem wrap = wrap(item); | |
92 wrap.setVisible(visible); | |
93 parentMgr.appendToGroup(groupName, wrap); | |
94 itemAdded(item, wrap); | |
95 } | |
96 | |
97 /** | |
98 * Disposes this sub contribution manager, removing all its items | |
99 * and cleaning up any other resources allocated by it. | |
100 * This must leave no trace of this sub contribution manager | |
101 * in the parent manager. Subclasses may extend. | |
102 * | |
103 * @since 3.0 | |
104 */ | |
105 public void disposeManager() { | |
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
|
106 Iterator it = mapItemToWrapper.values().iterator(); |
27 | 107 // Dispose items in addition to removing them. |
108 // See bugs 64024 and 73715 for details. | |
109 // Do not use getItems() here as subclasses can override that in bad ways. | |
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
|
110 while (it.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
|
111 IContributionItem item = cast(IContributionItem) it.next(); |
27 | 112 item.dispose(); |
113 } | |
114 removeAll(); | |
115 } | |
116 | |
117 /* (non-Javadoc) | |
118 * Method declared on IContributionManager. | |
119 * | |
120 * Returns the item passed to us, not the wrapper. | |
121 */ | |
122 public IContributionItem find(String id) { | |
123 IContributionItem item = parentMgr.find(id); | |
124 // Return the item passed to us, not the wrapper. | |
125 item = unwrap(item); | |
126 return item; | |
127 } | |
128 | |
129 /* (non-Javadoc) | |
130 * Method declared on IContributionManager. | |
131 * | |
132 * Returns the items passed to us, not the wrappers. | |
133 */ | |
134 public IContributionItem[] getItems() { | |
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
|
135 IContributionItem[] result = arraycast!(IContributionItem)(mapItemToWrapper.keySet().toArray()); |
27 | 136 return result; |
137 } | |
138 | |
139 /** | |
140 * Returns the parent manager. | |
141 * | |
142 * @return the parent manager | |
143 */ | |
144 public IContributionManager getParent() { | |
145 return parentMgr; | |
146 } | |
147 | |
148 /* (non-Javadoc) | |
149 * Method declared on IContributionManager. | |
150 */ | |
151 public IContributionManagerOverrides getOverrides() { | |
152 return parentMgr.getOverrides(); | |
153 } | |
154 | |
155 /* (non-Javadoc) | |
156 * Method declared on IContributionManager. | |
157 */ | |
158 public void insertAfter(String id, IAction action) { | |
159 insertAfter(id, new ActionContributionItem(action)); | |
160 } | |
161 | |
162 /* (non-Javadoc) | |
163 * Method declared on IContributionManager. | |
164 */ | |
165 public void insertAfter(String id, IContributionItem item) { | |
166 item.setParent(this); | |
167 SubContributionItem wrap = wrap(item); | |
168 wrap.setVisible(visible); | |
169 parentMgr.insertAfter(id, wrap); | |
170 itemAdded(item, wrap); | |
171 } | |
172 | |
173 /* (non-Javadoc) | |
174 * Method declared on IContributionManager. | |
175 */ | |
176 public void insertBefore(String id, IAction action) { | |
177 insertBefore(id, new ActionContributionItem(action)); | |
178 } | |
179 | |
180 /* (non-Javadoc) | |
181 * Method declared on IContributionManager. | |
182 */ | |
183 public void insertBefore(String id, IContributionItem item) { | |
184 item.setParent(this); | |
185 SubContributionItem wrap = wrap(item); | |
186 wrap.setVisible(visible); | |
187 parentMgr.insertBefore(id, wrap); | |
188 itemAdded(item, wrap); | |
189 } | |
190 | |
191 /* (non-Javadoc) | |
192 * Method declared on IContributionManager. | |
193 */ | |
194 public bool isDirty() { | |
195 return parentMgr.isDirty(); | |
196 } | |
197 | |
198 /* (non-Javadoc) | |
199 * Method declared on IContributionManager. | |
200 */ | |
201 public bool isEmpty() { | |
202 return parentMgr.isEmpty(); | |
203 } | |
204 | |
205 /** | |
206 * Returns whether the contribution list is visible. | |
207 * If the visibility is <code>true</code> then each item within the manager | |
208 * appears within the parent manager. Otherwise, the items are not visible. | |
209 * | |
210 * @return <code>true</code> if the manager is visible | |
211 */ | |
212 public bool isVisible() { | |
213 return visible; | |
214 } | |
215 | |
216 /** | |
217 * Notifies that an item has been added. | |
218 * <p> | |
219 * Subclasses are not expected to override this method. | |
220 * </p> | |
221 * | |
222 * @param item the item contributed by the client | |
223 * @param wrap the item contributed to the parent manager as a proxy for the item | |
224 * contributed by the client | |
225 */ | |
226 protected void itemAdded(IContributionItem item, SubContributionItem wrap) { | |
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
|
227 mapItemToWrapper.put(cast(Object)item, wrap); |
27 | 228 } |
229 | |
230 /** | |
231 * Notifies that an item has been removed. | |
232 * <p> | |
233 * Subclasses are not expected to override this method. | |
234 * </p> | |
235 * | |
236 * @param item the item contributed by the client | |
237 */ | |
238 protected void itemRemoved(IContributionItem item) { | |
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
|
239 mapItemToWrapper.remove(cast(Object)item); |
27 | 240 } |
241 | |
242 /** | |
243 * @return fetch all enumeration of wrappers for the item | |
244 * @deprecated Use getItems(String value) instead. | |
245 */ | |
246 public Enumeration items() { | |
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
|
247 return new class(mapItemToWrapper.values().iterator()) Enumeration { |
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
|
248 Iterator i; |
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
|
249 this(Iterator i_){ |
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
|
250 i = i_; |
27 | 251 } |
252 public bool hasMoreElements() { | |
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
|
253 return i.hasNext(); |
27 | 254 } |
255 | |
256 public Object nextElement() { | |
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
|
257 return i.next(); |
27 | 258 } |
259 }; | |
260 } | |
261 | |
262 /* (non-Javadoc) | |
263 * Method declared on IContributionManager. | |
264 */ | |
265 public void markDirty() { | |
266 parentMgr.markDirty(); | |
267 } | |
268 | |
269 /* (non-Javadoc) | |
270 * Method declared on IContributionManager. | |
271 */ | |
272 public void prependToGroup(String groupName, IAction action) { | |
273 prependToGroup(groupName, new ActionContributionItem(action)); | |
274 } | |
275 | |
276 /* (non-Javadoc) | |
277 * Method declared on IContributionManager. | |
278 */ | |
279 public void prependToGroup(String groupName, IContributionItem item) { | |
280 item.setParent(this); | |
281 SubContributionItem wrap = wrap(item); | |
282 wrap.setVisible(visible); | |
283 parentMgr.prependToGroup(groupName, wrap); | |
284 itemAdded(item, wrap); | |
285 } | |
286 | |
287 /* (non-Javadoc) | |
288 * Method declared on IContributionManager. | |
289 */ | |
290 public IContributionItem remove(String id) { | |
291 IContributionItem result = parentMgr.remove(id); | |
292 if (result !is null) { | |
293 itemRemoved(result); | |
294 } | |
295 return result; | |
296 } | |
297 | |
298 /* (non-Javadoc) | |
299 * Method declared on IContributionManager. | |
300 */ | |
301 public IContributionItem remove(IContributionItem item) { | |
302 SubContributionItem wrap = cast(SubContributionItem) mapItemToWrapper | |
303 .get(cast(Object)item); | |
304 if (wrap is null) { | |
305 return null; | |
306 } | |
307 IContributionItem result = parentMgr.remove(wrap); | |
308 if (result is null) { | |
309 return null; | |
310 } | |
311 itemRemoved(item); | |
312 return item; | |
313 } | |
314 | |
315 /* (non-Javadoc) | |
316 * Method declared on IContributionManager. | |
317 */ | |
318 public void removeAll() { | |
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
|
319 Iterator it = mapItemToWrapper.values().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
|
320 while (it.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
|
321 IContributionItem item = cast(IContributionItem) it.next(); |
27 | 322 parentMgr.remove(item); |
323 } | |
324 mapItemToWrapper.clear(); | |
325 } | |
326 | |
327 /** | |
328 * Sets the visibility of the manager. If the visibility is <code>true</code> | |
329 * then each item within the manager appears within the parent manager. | |
330 * Otherwise, the items are not visible. | |
331 * | |
332 * @param visible the new visibility | |
333 */ | |
334 public void setVisible(bool visible) { | |
335 this.visible = visible; | |
336 if (mapItemToWrapper.size() > 0) { | |
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
|
337 Iterator it = mapItemToWrapper.values().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
|
338 while (it.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
|
339 IContributionItem item = cast(IContributionItem) it.next(); |
27 | 340 item.setVisible(visible); |
341 } | |
342 parentMgr.markDirty(); | |
343 } | |
344 } | |
345 | |
346 /** | |
347 * Wraps a contribution item in a sub contribution item, and returns the new wrapper. | |
348 * @param item the contribution item to be wrapped | |
349 * @return the wrapped item | |
350 */ | |
351 protected SubContributionItem wrap(IContributionItem item) { | |
352 return new SubContributionItem(item); | |
353 } | |
354 | |
355 /** | |
356 * Unwraps a nested contribution item. If the contribution item is an | |
357 * instance of <code>SubContributionItem</code>, then its inner item is | |
358 * returned. Otherwise, the item itself is returned. | |
359 * | |
360 * @param item | |
361 * The item to unwrap; may be <code>null</code>. | |
362 * @return The inner item of <code>item</code>, if <code>item</code> is | |
363 * a <code>SubContributionItem</code>;<code>item</code> | |
364 * otherwise. | |
365 */ | |
366 protected IContributionItem unwrap(IContributionItem item) { | |
367 if (cast(SubContributionItem)item ) { | |
368 return (cast(SubContributionItem) item).getInnerItem(); | |
369 } | |
370 | |
371 return item; | |
372 } | |
373 } |