Mercurial > projects > dwt-addons
comparison dwtx/jface/action/ContributionManager.d @ 16:e0f0aaf75edd
PopupDialog, bindings and actions
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 01 Apr 2008 08:00:31 +0200 |
parents | |
children | da5ad8eedf5d |
comparison
equal
deleted
inserted
replaced
15:db8940420ed8 | 16:e0f0aaf75edd |
---|---|
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.action.ContributionManager; | |
14 | |
15 import dwtx.jface.action.ActionContributionItem; | |
16 | |
17 import dwtx.jface.action.IContributionManager; | |
18 import dwtx.jface.action.IContributionItem; | |
19 import dwtx.jface.action.IContributionManagerOverrides; | |
20 import dwtx.jface.action.IAction; | |
21 | |
22 import tango.util.collection.ArraySeq; | |
23 import tango.util.collection.model.Seq; | |
24 | |
25 import dwtx.jface.util.Policy; | |
26 | |
27 import dwt.dwthelper.utils; | |
28 import dwt.dwthelper.Integer; | |
29 import tango.io.Stdout; | |
30 import tango.core.Exception; | |
31 import tango.text.convert.Format; | |
32 | |
33 /** | |
34 * Abstract base class for all contribution managers, and standard | |
35 * implementation of <code>IContributionManager</code>. This class provides | |
36 * functionality common across the specific managers defined by this framework. | |
37 * <p> | |
38 * This class maintains a list of contribution items and a dirty flag, both as | |
39 * internal state. In addition to providing implementations of most | |
40 * <code>IContributionManager</code> methods, this class automatically | |
41 * coalesces adjacent separators, hides beginning and ending separators, and | |
42 * deals with dynamically changing sets of contributions. When the set of | |
43 * contributions does change dynamically, the changes are propagated to the | |
44 * control via the <code>update</code> method, which subclasses must | |
45 * implement. | |
46 * </p> | |
47 * <p> | |
48 * Note: A <code>ContributionItem</code> cannot be shared between different | |
49 * <code>ContributionManager</code>s. | |
50 * </p> | |
51 */ | |
52 public abstract class ContributionManager : IContributionManager { | |
53 | |
54 // Internal debug flag. | |
55 // protected static final bool DEBUG = false; | |
56 | |
57 /** | |
58 * The list of contribution items. | |
59 */ | |
60 private Seq!(IContributionItem) contributions; | |
61 | |
62 /** | |
63 * Indicates whether the widgets are in sync with the contributions. | |
64 */ | |
65 private bool isDirty_ = true; | |
66 | |
67 /** | |
68 * Number of dynamic contribution items. | |
69 */ | |
70 private int dynamicItems = 0; | |
71 | |
72 /** | |
73 * The overrides for items of this manager | |
74 */ | |
75 private IContributionManagerOverrides overrides; | |
76 | |
77 /** | |
78 * Creates a new contribution manager. | |
79 */ | |
80 protected this() { | |
81 contributions = new ArraySeq!(IContributionItem); | |
82 // Do nothing. | |
83 } | |
84 | |
85 /* | |
86 * (non-Javadoc) Method declared on IContributionManager. | |
87 */ | |
88 public void add(IAction action) { | |
89 add(new ActionContributionItem(action)); | |
90 } | |
91 | |
92 /* | |
93 * (non-Javadoc) Method declared on IContributionManager. | |
94 */ | |
95 public void add(IContributionItem item) { | |
96 if (allowItem(item)) { | |
97 contributions.append(item); | |
98 itemAdded(item); | |
99 } | |
100 } | |
101 | |
102 /** | |
103 * Adds a contribution item to the start or end of the group with the given | |
104 * name. | |
105 * | |
106 * @param groupName | |
107 * the name of the group | |
108 * @param item | |
109 * the contribution item | |
110 * @param append | |
111 * <code>true</code> to add to the end of the group, and | |
112 * <code>false</code> to add the beginning of the group | |
113 * @exception IllegalArgumentException | |
114 * if there is no group with the given name | |
115 */ | |
116 private void addToGroup(String groupName, IContributionItem item, | |
117 bool append) { | |
118 int i; | |
119 auto items = contributions.elements(); | |
120 for (i = 0; items.more(); i++) { | |
121 IContributionItem o = cast(IContributionItem) items.get(); | |
122 if (o.isGroupMarker()) { | |
123 String id = o.getId(); | |
124 if (id !is null && id.equalsIgnoreCase(groupName)) { | |
125 i++; | |
126 if (append) { | |
127 for (; items.more(); i++) { | |
128 IContributionItem ci = cast(IContributionItem) items | |
129 .get(); | |
130 if (ci.isGroupMarker()) { | |
131 break; | |
132 } | |
133 } | |
134 } | |
135 if (allowItem(item)) { | |
136 //TODO: does this corrupt the iterator? | |
137 contributions.addAt(i, item); | |
138 itemAdded(item); | |
139 } | |
140 return; | |
141 } | |
142 } | |
143 } | |
144 throw new IllegalArgumentException("Group not found: " ~ groupName);//$NON-NLS-1$ | |
145 } | |
146 | |
147 /* | |
148 * (non-Javadoc) Method declared on IContributionManager. | |
149 */ | |
150 public void appendToGroup(String groupName, IAction action) { | |
151 addToGroup(groupName, new ActionContributionItem(action), true); | |
152 } | |
153 | |
154 /* | |
155 * (non-Javadoc) Method declared on IContributionManager. | |
156 */ | |
157 public void appendToGroup(String groupName, IContributionItem item) { | |
158 addToGroup(groupName, item, true); | |
159 } | |
160 | |
161 /** | |
162 * This method allows subclasses of <code>ContributionManager</code> to | |
163 * prevent certain items in the contributions list. | |
164 * <code>ContributionManager</code> will either block or allow an addition | |
165 * based on the result of this method call. This can be used to prevent | |
166 * duplication, for example. | |
167 * | |
168 * @param itemToAdd | |
169 * The contribution item to be added; may be <code>null</code>. | |
170 * @return <code>true</code> if the addition should be allowed; | |
171 * <code>false</code> otherwise. The default implementation allows | |
172 * all items. | |
173 * @since 3.0 | |
174 */ | |
175 protected bool allowItem(IContributionItem itemToAdd) { | |
176 return true; | |
177 } | |
178 | |
179 /** | |
180 * Internal debug method for printing statistics about this manager to | |
181 * <code>System.out</code>. | |
182 */ | |
183 protected void dumpStatistics() { | |
184 int size = 0; | |
185 if (contributions !is null) { | |
186 size = contributions.size(); | |
187 } | |
188 | |
189 Stdout.formatln(this.toString()); | |
190 Stdout.formatln(" Number of elements: {}", size);//$NON-NLS-1$ | |
191 int sum = 0; | |
192 for (int i = 0; i < size; i++) { | |
193 if ((cast(IContributionItem) contributions.get(i)).isVisible()) { | |
194 sum++; | |
195 } | |
196 } | |
197 Stdout.formatln(" Number of visible elements: {}", sum);//$NON-NLS-1$ | |
198 Stdout.formatln(" Is dirty: {}", isDirty()); //$NON-NLS-1$ | |
199 } | |
200 | |
201 /* | |
202 * (non-Javadoc) Method declared on IContributionManager. | |
203 */ | |
204 public IContributionItem find(String id) { | |
205 auto e = contributions.elements(); | |
206 while (e.more()) { | |
207 IContributionItem item = cast(IContributionItem) e.get(); | |
208 String itemId = item.getId(); | |
209 if (itemId !is null && itemId.equalsIgnoreCase(id)) { | |
210 return item; | |
211 } | |
212 } | |
213 return null; | |
214 } | |
215 | |
216 /* | |
217 * (non-Javadoc) Method declared on IContributionManager. | |
218 */ | |
219 public IContributionItem[] getItems() { | |
220 return contributions.toArray(); | |
221 } | |
222 | |
223 /** | |
224 * Return the number of contributions in this manager. | |
225 * | |
226 * @return the number of contributions in this manager | |
227 * @since 3.3 | |
228 */ | |
229 public int getSize() { | |
230 return contributions.size(); | |
231 } | |
232 | |
233 /** | |
234 * The <code>ContributionManager</code> implementation of this method | |
235 * declared on <code>IContributionManager</code> returns the current | |
236 * overrides. If there is no overrides it lazily creates one which overrides | |
237 * no item state. | |
238 * | |
239 * @since 2.0 | |
240 */ | |
241 public IContributionManagerOverrides getOverrides() { | |
242 if (overrides is null) { | |
243 overrides = new class IContributionManagerOverrides { | |
244 public ValueWrapperBool getEnabled(IContributionItem item) { | |
245 return null; | |
246 } | |
247 | |
248 public ValueWrapperInt getAccelerator(IContributionItem item) { | |
249 return null; | |
250 } | |
251 | |
252 public String getAcceleratorText(IContributionItem item) { | |
253 return null; | |
254 } | |
255 | |
256 public String getText(IContributionItem item) { | |
257 return null; | |
258 } | |
259 }; | |
260 } | |
261 return overrides; | |
262 } | |
263 | |
264 /** | |
265 * Returns whether this contribution manager contains dynamic items. A | |
266 * dynamic contribution item contributes items conditionally, dependent on | |
267 * some internal state. | |
268 * | |
269 * @return <code>true</code> if this manager contains dynamic items, and | |
270 * <code>false</code> otherwise | |
271 */ | |
272 protected bool hasDynamicItems() { | |
273 return (dynamicItems > 0); | |
274 } | |
275 | |
276 /** | |
277 * Returns the index of the item with the given id. | |
278 * | |
279 * @param id | |
280 * The id of the item whose index is requested. | |
281 * | |
282 * @return <code>int</code> the index or -1 if the item is not found | |
283 */ | |
284 public int indexOf(String id) { | |
285 for (int i = 0; i < contributions.size(); i++) { | |
286 IContributionItem item = cast(IContributionItem) contributions.get(i); | |
287 String itemId = item.getId(); | |
288 if (itemId !is null && itemId.equalsIgnoreCase(id)) { | |
289 return i; | |
290 } | |
291 } | |
292 return -1; | |
293 } | |
294 | |
295 /** | |
296 * Returns the index of the object in the internal structure. This is | |
297 * different from <code>indexOf(String id)</code> since some contribution | |
298 * items may not have an id. | |
299 * | |
300 * @param item | |
301 * The contribution item | |
302 * @return the index, or -1 if the item is not found | |
303 * @since 3.0 | |
304 */ | |
305 protected int indexOf(IContributionItem item) { | |
306 int res = -1; | |
307 int idx = 0; | |
308 foreach( e; contributions ){ | |
309 if( e == item ) { | |
310 res = idx; | |
311 break; | |
312 } | |
313 idx++; | |
314 } | |
315 return res; | |
316 } | |
317 | |
318 /** | |
319 * Insert the item at the given index. | |
320 * | |
321 * @param index | |
322 * The index to be used for insertion | |
323 * @param item | |
324 * The item to be inserted | |
325 */ | |
326 public void insert(int index, IContributionItem item) { | |
327 if (index > contributions.size()) { | |
328 throw new IndexOutOfBoundsException( Format( | |
329 "inserting {} at {}", item.getId(), index)); //$NON-NLS-1$ //$NON-NLS-2$ | |
330 } | |
331 if (allowItem(item)) { | |
332 contributions.addAt(index, item); | |
333 itemAdded(item); | |
334 } | |
335 } | |
336 | |
337 /* | |
338 * (non-Javadoc) Method declared on IContributionManager. | |
339 */ | |
340 public void insertAfter(String ID, IAction action) { | |
341 insertAfter(ID, new ActionContributionItem(action)); | |
342 } | |
343 | |
344 /* | |
345 * (non-Javadoc) Method declared on IContributionManager. | |
346 */ | |
347 public void insertAfter(String ID, IContributionItem item) { | |
348 IContributionItem ci = find(ID); | |
349 if (ci is null) { | |
350 throw new IllegalArgumentException(Format("can't find ID{}", ID));//$NON-NLS-1$ | |
351 } | |
352 int ix = SeqIndexOf!(IContributionItem)( contributions, ci ); | |
353 if (ix >= 0) { | |
354 // System.out.println("insert after: " + ix); | |
355 if (allowItem(item)) { | |
356 contributions.addAt(ix + 1, item); | |
357 itemAdded(item); | |
358 } | |
359 } | |
360 } | |
361 | |
362 /* | |
363 * (non-Javadoc) Method declared on IContributionManager. | |
364 */ | |
365 public void insertBefore(String ID, IAction action) { | |
366 insertBefore(ID, new ActionContributionItem(action)); | |
367 } | |
368 | |
369 /* | |
370 * (non-Javadoc) Method declared on IContributionManager. | |
371 */ | |
372 public void insertBefore(String ID, IContributionItem item) { | |
373 IContributionItem ci = find(ID); | |
374 if (ci is null) { | |
375 throw new IllegalArgumentException(Format("can't find ID {}", ID));//$NON-NLS-1$ | |
376 } | |
377 int ix = SeqIndexOf!(IContributionItem)(contributions,ci); | |
378 if (ix >= 0) { | |
379 // System.out.println("insert before: " + ix); | |
380 if (allowItem(item)) { | |
381 contributions.addAt(ix, item); | |
382 itemAdded(item); | |
383 } | |
384 } | |
385 } | |
386 | |
387 /* | |
388 * (non-Javadoc) Method declared on IContributionManager. | |
389 */ | |
390 public bool isDirty() { | |
391 if (isDirty_) { | |
392 return true; | |
393 } | |
394 if (hasDynamicItems()) { | |
395 foreach( e; contributions ){ | |
396 IContributionItem item = cast(IContributionItem) e; | |
397 if (item.isDirty()) { | |
398 return true; | |
399 } | |
400 } | |
401 } | |
402 return false; | |
403 } | |
404 | |
405 /* | |
406 * (non-Javadoc) Method declared on IContributionManager. | |
407 */ | |
408 public bool isEmpty() { | |
409 return contributions.drained(); | |
410 } | |
411 | |
412 /** | |
413 * The given item was added to the list of contributions. Marks the manager | |
414 * as dirty and updates the number of dynamic items, and the memento. | |
415 * | |
416 * @param item | |
417 * the item to be added | |
418 * | |
419 */ | |
420 protected void itemAdded(IContributionItem item) { | |
421 item.setParent(this); | |
422 markDirty(); | |
423 if (item.isDynamic()) { | |
424 dynamicItems++; | |
425 } | |
426 } | |
427 | |
428 /** | |
429 * The given item was removed from the list of contributions. Marks the | |
430 * manager as dirty and updates the number of dynamic items. | |
431 * | |
432 * @param item | |
433 * remove given parent from list of contributions | |
434 */ | |
435 protected void itemRemoved(IContributionItem item) { | |
436 item.setParent(null); | |
437 markDirty(); | |
438 if (item.isDynamic()) { | |
439 dynamicItems--; | |
440 } | |
441 } | |
442 | |
443 /* | |
444 * (non-Javadoc) Method declared on IContributionManager. | |
445 */ | |
446 public void markDirty() { | |
447 setDirty(true); | |
448 } | |
449 | |
450 /* | |
451 * (non-Javadoc) Method declared on IContributionManager. | |
452 */ | |
453 public void prependToGroup(String groupName, IAction action) { | |
454 addToGroup(groupName, new ActionContributionItem(action), false); | |
455 } | |
456 | |
457 /* | |
458 * (non-Javadoc) Method declared on IContributionManager. | |
459 */ | |
460 public void prependToGroup(String groupName, IContributionItem item) { | |
461 addToGroup(groupName, item, false); | |
462 } | |
463 | |
464 /* | |
465 * (non-Javadoc) Method declared on IContributionManager. | |
466 */ | |
467 public IContributionItem remove(String ID) { | |
468 IContributionItem ci = find(ID); | |
469 if (ci is null) { | |
470 return null; | |
471 } | |
472 return remove(ci); | |
473 } | |
474 | |
475 /* | |
476 * (non-Javadoc) Method declared on IContributionManager. | |
477 */ | |
478 public IContributionItem remove(IContributionItem item) { | |
479 bool contained = contributions.contains(item); | |
480 contributions.remove(item); | |
481 if (contained) { | |
482 itemRemoved(item); | |
483 return item; | |
484 } | |
485 return null; | |
486 } | |
487 | |
488 /* | |
489 * (non-Javadoc) Method declared on IContributionManager. | |
490 */ | |
491 public void removeAll() { | |
492 IContributionItem[] items = getItems(); | |
493 contributions.clear(); | |
494 for (int i = 0; i < items.length; i++) { | |
495 IContributionItem item = items[i]; | |
496 itemRemoved(item); | |
497 } | |
498 dynamicItems = 0; | |
499 markDirty(); | |
500 } | |
501 | |
502 /** | |
503 * Replaces the item of the given identifier with another contribution item. | |
504 * This can be used, for example, to replace large contribution items with | |
505 * placeholders to avoid memory leaks. If the identifier cannot be found in | |
506 * the current list of items, then this does nothing. If multiple | |
507 * occurrences are found, then the replacement items is put in the first | |
508 * position and the other positions are removed. | |
509 * | |
510 * @param identifier | |
511 * The identifier to look for in the list of contributions; | |
512 * should not be <code>null</code>. | |
513 * @param replacementItem | |
514 * The contribution item to replace the old item; must not be | |
515 * <code>null</code>. Use | |
516 * {@link dwtx.jface.action.ContributionManager#remove(java.lang.String) remove} | |
517 * if that is what you want to do. | |
518 * @return <code>true</code> if the given identifier can be; <code> | |
519 * @since 3.0 | |
520 */ | |
521 public bool replaceItem(String identifier, | |
522 IContributionItem replacementItem) { | |
523 if (identifier is null) { | |
524 return false; | |
525 } | |
526 | |
527 int index = indexOf(identifier); | |
528 if (index < 0) { | |
529 return false; // couldn't find the item. | |
530 } | |
531 | |
532 // Remove the old item. | |
533 IContributionItem oldItem = cast(IContributionItem) contributions | |
534 .get(index); | |
535 itemRemoved(oldItem); | |
536 | |
537 // Add the new item. | |
538 contributions.replaceAt(index, replacementItem); | |
539 itemAdded(replacementItem); // throws NPE if (replacementItem is null) | |
540 | |
541 // Go through and remove duplicates. | |
542 for (int i = contributions.size() - 1; i > index; i--) { | |
543 IContributionItem item = cast(IContributionItem) contributions.get(i); | |
544 if ((item !is null) && (identifier.equals(item.getId()))) { | |
545 if (Policy.TRACE_TOOLBAR) { | |
546 Stdout.formatln("Removing duplicate on replace: {}", identifier); //$NON-NLS-1$ | |
547 } | |
548 contributions.removeAt(i); | |
549 itemRemoved(item); | |
550 } | |
551 } | |
552 | |
553 return true; // success | |
554 } | |
555 | |
556 /** | |
557 * Sets whether this manager is dirty. When dirty, the list of contributions | |
558 * is not accurately reflected in the corresponding widgets. | |
559 * | |
560 * @param dirty | |
561 * <code>true</code> if this manager is dirty, and | |
562 * <code>false</code> if it is up-to-date | |
563 */ | |
564 protected void setDirty(bool dirty) { | |
565 isDirty_ = dirty; | |
566 } | |
567 | |
568 /** | |
569 * Sets the overrides for this contribution manager | |
570 * | |
571 * @param newOverrides | |
572 * the overrides for the items of this manager | |
573 * @since 2.0 | |
574 */ | |
575 public void setOverrides(IContributionManagerOverrides newOverrides) { | |
576 overrides = newOverrides; | |
577 } | |
578 | |
579 /** | |
580 * An internal method for setting the order of the contribution items. | |
581 * | |
582 * @param items | |
583 * the contribution items in the specified order | |
584 * @since 3.0 | |
585 */ | |
586 protected void internalSetItems(IContributionItem[] items) { | |
587 contributions.clear(); | |
588 for (int i = 0; i < items.length; i++) { | |
589 if (allowItem(items[i])) { | |
590 contributions.append(items[i]); | |
591 } | |
592 } | |
593 } | |
594 } |