Mercurial > projects > dwt-addons
annotate dwtx/jface/action/SubMenuManager.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 | 46a6e0e6ccd4 |
children |
rev | line source |
---|---|
28 | 1 /******************************************************************************* |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
2 * Copyright (c) 2000, 2007 IBM Corporation and others. |
28 | 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.SubMenuManager; | |
14 | |
15 import dwtx.jface.action.SubContributionManager; | |
16 import dwtx.jface.action.IMenuManager; | |
17 import dwtx.jface.action.IMenuListener; | |
18 import dwtx.jface.action.IContributionItem; | |
19 import dwtx.jface.action.IContributionManager; | |
20 import dwtx.jface.action.SubContributionItem; | |
21 | |
22 | |
23 import dwt.widgets.Composite; | |
24 import dwt.widgets.CoolBar; | |
25 import dwt.widgets.Menu; | |
26 import dwt.widgets.ToolBar; | |
27 import dwtx.core.runtime.Assert; | |
28 import dwtx.core.runtime.ListenerList; | |
29 | |
30 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:
70
diff
changeset
|
31 import dwtx.dwtxhelper.Collection; |
28 | 32 |
33 /** | |
34 * A <code>SubMenuManager</code> is used to define a set of contribution | |
35 * items within a parent manager. Once defined, the visibility of the entire set can | |
36 * be changed as a unit. | |
37 * <p> | |
38 * A client may ask for and make additions to a submenu. The visibility of these items | |
39 * is also controlled by the visibility of the <code>SubMenuManager</code>. | |
40 * </p> | |
41 */ | |
42 public class SubMenuManager : SubContributionManager, | |
43 IMenuManager { | |
44 | |
45 public bool isDirty() { | |
46 return super.isDirty(); | |
47 } | |
48 public bool isVisible() { | |
49 return super.isVisible(); | |
50 } | |
51 | |
52 /** | |
53 * Maps each submenu in the manager to a wrapper. The wrapper is used to | |
54 * monitor additions and removals. If the visibility of the manager is modified | |
55 * the visibility of the submenus is also modified. | |
56 */ | |
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:
70
diff
changeset
|
57 private Map mapMenuToWrapper; |
28 | 58 |
59 /** | |
60 * List of registered menu listeners (element type: <code>IMenuListener</code>). | |
61 */ | |
62 private ListenerList menuListeners; | |
63 | |
64 /** | |
65 * The menu listener added to the parent. Lazily initialized | |
66 * in addMenuListener. | |
67 */ | |
68 private IMenuListener menuListener; | |
69 | |
70 /** | |
71 * Constructs a new manager. | |
72 * | |
73 * @param mgr the parent manager. All contributions made to the | |
74 * <code>SubMenuManager</code> are forwarded and appear in the | |
75 * parent manager. | |
76 */ | |
77 public this(IMenuManager mgr) { | |
78 menuListeners = new ListenerList(); | |
79 super(mgr); | |
80 } | |
81 | |
82 /* (non-Javadoc) | |
83 * @see dwtx.jface.action.IMenuManager#addMenuListener(dwtx.jface.action.IMenuListener) | |
84 */ | |
85 public void addMenuListener(IMenuListener listener) { | |
86 menuListeners.add(cast(Object)listener); | |
87 if (menuListener is null) { | |
88 menuListener = new class IMenuListener { | |
89 public void menuAboutToShow(IMenuManager manager) { | |
90 Object[] listeners = menuListeners.getListeners(); | |
91 for (int i = 0; i < listeners.length; ++i) { | |
92 (cast(IMenuListener) listeners[i]) | |
93 .menuAboutToShow(this.outer); | |
94 } | |
95 } | |
96 }; | |
97 } | |
98 getParentMenuManager().addMenuListener(menuListener); | |
99 } | |
100 | |
101 /** | |
102 * The default implementation of this <code>IContributionItem</code> | |
103 * method does nothing. Subclasses may override. | |
104 */ | |
105 public void dispose() { | |
106 // do nothing | |
107 } | |
108 | |
109 /* (non-Javadoc) | |
110 * @see dwtx.jface.action.SubContributionManager#disposeManager() | |
111 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
112 public override void disposeManager() { |
28 | 113 if (menuListener !is null) { |
114 getParentMenuManager().removeMenuListener(menuListener); | |
115 menuListener = null; | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
116 menuListeners.clear(); |
28 | 117 } |
118 // Dispose wrapped menus in addition to removing them. | |
119 // See bugs 64024 and 73715 for details. | |
120 // important to dispose menu wrappers before call to super, | |
121 // otherwise super's call to removeAll will remove them | |
122 // before they can be disposed | |
123 if (mapMenuToWrapper !is null) { | |
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:
70
diff
changeset
|
124 Iterator iter = mapMenuToWrapper.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:
70
diff
changeset
|
125 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:
70
diff
changeset
|
126 SubMenuManager wrapper = cast(SubMenuManager) iter.next(); |
28 | 127 wrapper.disposeManager(); |
128 } | |
129 mapMenuToWrapper.clear(); | |
130 mapMenuToWrapper = null; | |
131 } | |
132 super.disposeManager(); | |
133 } | |
134 | |
135 /* (non-Javadoc) | |
136 * @see dwtx.jface.action.IContributionItem#fill(dwt.widgets.Composite) | |
137 */ | |
138 public void fill(Composite parent) { | |
139 if (isVisible()) { | |
140 getParentMenuManager().fill(parent); | |
141 } | |
142 } | |
143 | |
144 /* (non-Javadoc) | |
145 * @see dwtx.jface.action.IContributionItem#fill(dwt.widgets.CoolBar, int) | |
146 */ | |
147 public void fill(CoolBar parent, int index) { | |
148 // do nothing | |
149 } | |
150 | |
151 /* (non-Javadoc) | |
152 * @see dwtx.jface.action.IContributionItem#fill(dwt.widgets.Menu, int) | |
153 */ | |
154 public void fill(Menu parent, int index) { | |
155 if (isVisible()) { | |
156 getParentMenuManager().fill(parent, index); | |
157 } | |
158 } | |
159 | |
160 /* (non-Javadoc) | |
161 * @see dwtx.jface.action.IContributionItem#fill(dwt.widgets.ToolBar, int) | |
162 */ | |
163 public void fill(ToolBar parent, int index) { | |
164 if (isVisible()) { | |
165 getParentMenuManager().fill(parent, index); | |
166 } | |
167 } | |
168 | |
169 /* (non-Javadoc) | |
170 * Method declared on IContributionManager. | |
171 * | |
172 * Returns the item passed to us, not the wrapper. | |
173 * In the case of menu's not added by this manager, | |
174 * ensure that we return a wrapper for the menu. | |
175 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
176 public override IContributionItem find(String id) { |
28 | 177 IContributionItem item = getParentMenuManager().find(id); |
178 if (cast(SubContributionItem)item ) { | |
179 // Return the item passed to us, not the wrapper. | |
180 item = unwrap(item); | |
181 } | |
182 | |
183 if (cast(IMenuManager)item ) { | |
184 // if it is a menu manager wrap it before returning | |
185 IMenuManager menu = cast(IMenuManager) item; | |
186 item = getWrapper(menu); | |
187 } | |
188 | |
189 return item; | |
190 } | |
191 | |
192 /** | |
193 * <p> | |
194 * The menu returned is wrapped within a <code>SubMenuManager</code> to | |
195 * monitor additions and removals. If the visibility of this menu is modified | |
196 * the visibility of the submenus is also modified. | |
197 * </p> | |
198 */ | |
199 public IMenuManager findMenuUsingPath(String path) { | |
200 IContributionItem item = findUsingPath(path); | |
201 if (cast(IMenuManager)item ) { | |
202 return cast(IMenuManager) item; | |
203 } | |
204 return null; | |
205 } | |
206 | |
207 /* (non-Javadoc) | |
208 * Method declared on IMenuManager. | |
209 * | |
210 * Returns the item passed to us, not the wrapper. | |
211 * | |
212 * We use use the same algorithm as MenuManager.findUsingPath, but unwrap | |
213 * submenus along so that SubMenuManagers are visible. | |
214 */ | |
215 public IContributionItem findUsingPath(String path) { | |
216 String id = path; | |
217 String rest = null; | |
218 int separator = path.indexOf('/'); | |
219 if (separator !is -1) { | |
220 id = path.substring(0, separator); | |
221 rest = path.substring(separator + 1); | |
222 } | |
223 IContributionItem item = find(id); // unwraps item | |
224 if (rest !is null && cast(IMenuManager)item ) { | |
225 IMenuManager menu = cast(IMenuManager) item; | |
226 item = menu.findUsingPath(rest); | |
227 } | |
228 return item; | |
229 } | |
230 | |
231 /* (non-Javadoc) | |
232 * @see dwtx.jface.action.IContributionItem#getId() | |
233 */ | |
234 public String getId() { | |
235 return getParentMenuManager().getId(); | |
236 } | |
237 | |
238 /** | |
239 * @return the parent menu manager that this sub-manager contributes to. | |
240 */ | |
241 protected final IMenuManager getParentMenuManager() { | |
242 // Cast is ok because that's the only | |
243 // thing we accept in the construtor. | |
244 return cast(IMenuManager) getParent(); | |
245 } | |
246 | |
247 /* (non-Javadoc) | |
248 * @see dwtx.jface.action.IMenuManager#getRemoveAllWhenShown() | |
249 */ | |
250 public bool getRemoveAllWhenShown() { | |
251 return false; | |
252 } | |
253 | |
254 /** | |
255 * Returns the menu wrapper for a menu manager. | |
256 * <p> | |
257 * The sub menus within this menu are wrapped within a <code>SubMenuManager</code> to | |
258 * monitor additions and removals. If the visibility of this menu is modified | |
259 * the visibility of the sub menus is also modified. | |
260 * <p> | |
261 * @param mgr the menu manager to be wrapped | |
262 * | |
263 * @return the menu wrapper | |
264 */ | |
265 protected IMenuManager getWrapper(IMenuManager mgr) { | |
266 if (mapMenuToWrapper is null) { | |
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:
70
diff
changeset
|
267 mapMenuToWrapper = new HashMap(4); |
28 | 268 } |
269 SubMenuManager wrapper = cast(SubMenuManager) mapMenuToWrapper.get(cast(Object)mgr); | |
270 if (wrapper is null) { | |
271 wrapper = wrapMenu(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:
70
diff
changeset
|
272 mapMenuToWrapper.put(cast(Object)mgr, wrapper); |
28 | 273 } |
274 return wrapper; | |
275 } | |
276 | |
277 /* (non-Javadoc) | |
278 * @see dwtx.jface.action.IContributionItem#isDynamic() | |
279 */ | |
280 public bool isDynamic() { | |
281 return getParentMenuManager().isDynamic(); | |
282 } | |
283 | |
284 /* (non-Javadoc) | |
285 * @see dwtx.jface.action.IContributionItem#isEnabled() | |
286 */ | |
287 public bool isEnabled() { | |
288 return isVisible() && getParentMenuManager().isEnabled(); | |
289 } | |
290 | |
291 /* (non-Javadoc) | |
292 * @see dwtx.jface.action.IContributionItem#isGroupMarker() | |
293 */ | |
294 public bool isGroupMarker() { | |
295 return getParentMenuManager().isGroupMarker(); | |
296 } | |
297 | |
298 /* (non-Javadoc) | |
299 * @see dwtx.jface.action.IContributionItem#isSeparator() | |
300 */ | |
301 public bool isSeparator() { | |
302 return getParentMenuManager().isSeparator(); | |
303 } | |
304 | |
305 /** | |
306 * Remove all contribution items. | |
307 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
308 public override void removeAll() { |
28 | 309 super.removeAll(); |
310 if (mapMenuToWrapper !is null) { | |
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:
70
diff
changeset
|
311 Iterator iter = mapMenuToWrapper.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:
70
diff
changeset
|
312 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:
70
diff
changeset
|
313 SubMenuManager wrapper = cast(SubMenuManager) iter.next(); |
28 | 314 wrapper.removeAll(); |
315 } | |
316 mapMenuToWrapper.clear(); | |
317 mapMenuToWrapper = null; | |
318 } | |
319 } | |
320 | |
321 /* (non-Javadoc) | |
322 * @see dwtx.jface.action.IMenuManager#removeMenuListener(dwtx.jface.action.IMenuListener) | |
323 */ | |
324 public void removeMenuListener(IMenuListener listener) { | |
325 menuListeners.remove(cast(Object)listener); | |
326 } | |
327 | |
328 /* (non-Javadoc) | |
329 * @see dwtx.jface.action.IContributionItem#saveWidgetState() | |
330 */ | |
331 public void saveWidgetState() { | |
332 // do nothing | |
333 } | |
334 | |
335 /* (non-Javadoc) | |
336 * @see dwtx.jface.action.IContributionItem#setParent(dwtx.jface.action.IContributionManager) | |
337 */ | |
338 public void setParent(IContributionManager parent) { | |
339 // do nothing, our "parent manager's" parent | |
340 // is set when it is added to a manager | |
341 } | |
342 | |
343 /* (non-Javadoc) | |
344 * @see dwtx.jface.action.IMenuManager#setRemoveAllWhenShown(bool) | |
345 */ | |
346 public void setRemoveAllWhenShown(bool removeAll) { | |
347 Assert.isTrue(false, "Should not be called on submenu manager"); //$NON-NLS-1$ | |
348 } | |
349 | |
350 /* (non-Javadoc) | |
351 * @see dwtx.jface.action.SubContributionManager#setVisible(bool) | |
352 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
353 public override void setVisible(bool visible) { |
28 | 354 super.setVisible(visible); |
355 if (mapMenuToWrapper !is null) { | |
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:
70
diff
changeset
|
356 Iterator iter = mapMenuToWrapper.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:
70
diff
changeset
|
357 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:
70
diff
changeset
|
358 SubMenuManager wrapper = cast(SubMenuManager) iter.next(); |
28 | 359 wrapper.setVisible(visible); |
360 } | |
361 } | |
362 } | |
363 | |
364 /* (non-Javadoc) | |
365 * @see dwtx.jface.action.IContributionItem#update() | |
366 */ | |
367 public void update() { | |
368 // This method is not governed by visibility. The client may | |
369 // call <code>setVisible</code> and then force an update. At that | |
370 // point we need to update the parent. | |
371 getParentMenuManager().update(); | |
372 } | |
373 | |
374 /* (non-Javadoc) | |
375 * @see dwtx.jface.action.IContributionManager#update(bool) | |
376 */ | |
377 public void update(bool force) { | |
378 // This method is not governed by visibility. The client may | |
379 // call <code>setVisible</code> and then force an update. At that | |
380 // point we need to update the parent. | |
381 getParentMenuManager().update(force); | |
382 } | |
383 | |
384 /* (non-Javadoc) | |
385 * @see dwtx.jface.action.IContributionItem#update(java.lang.String) | |
386 */ | |
387 public void update(String id) { | |
388 getParentMenuManager().update(id); | |
389 } | |
390 | |
391 /* (non-Javadoc) | |
392 * @see dwtx.jface.action.IMenuManager#updateAll(bool) | |
393 */ | |
394 public void updateAll(bool force) { | |
395 // This method is not governed by visibility. The client may | |
396 // call <code>setVisible</code> and then force an update. At that | |
397 // point we need to update the parent. | |
398 getParentMenuManager().updateAll(force); | |
399 } | |
400 | |
401 /** | |
402 * Wraps a menu manager in a sub menu manager, and returns the new wrapper. | |
403 * @param menu the menu manager to wrap | |
404 * @return the new wrapped menu manager | |
405 */ | |
406 protected SubMenuManager wrapMenu(IMenuManager menu) { | |
407 SubMenuManager mgr = new SubMenuManager(menu); | |
408 mgr.setVisible(isVisible()); | |
409 return mgr; | |
410 } | |
411 } |