comparison dwtx/jface/action/ContributionManager.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 7ffeace6c47f
children
comparison
equal deleted inserted replaced
103:2d6540440fe6 104:04b47443bb01
17 import dwtx.jface.action.IContributionManager; 17 import dwtx.jface.action.IContributionManager;
18 import dwtx.jface.action.IContributionItem; 18 import dwtx.jface.action.IContributionItem;
19 import dwtx.jface.action.IContributionManagerOverrides; 19 import dwtx.jface.action.IContributionManagerOverrides;
20 import dwtx.jface.action.IAction; 20 import dwtx.jface.action.IAction;
21 21
22 import tango.util.collection.ArraySeq;
23 import tango.util.collection.model.Seq;
24 22
25 import dwtx.core.runtime.Assert; 23 import dwtx.core.runtime.Assert;
26 import dwtx.jface.util.Policy; 24 import dwtx.jface.util.Policy;
27 25
28 import dwt.dwthelper.utils; 26 import dwt.dwthelper.utils;
27 import dwtx.dwtxhelper.Collection;
29 import tango.io.Stdout; 28 import tango.io.Stdout;
30 import tango.core.Exception; 29 import tango.core.Exception;
31 import tango.text.convert.Format; 30 import tango.text.convert.Format;
32 31
33 /** 32 /**
55 // protected static final bool DEBUG = false; 54 // protected static final bool DEBUG = false;
56 55
57 /** 56 /**
58 * The list of contribution items. 57 * The list of contribution items.
59 */ 58 */
60 private Seq!(IContributionItem) contributions; 59 private List contributions;
61 60
62 /** 61 /**
63 * Indicates whether the widgets are in sync with the contributions. 62 * Indicates whether the widgets are in sync with the contributions.
64 */ 63 */
65 private bool isDirty_ = true; 64 private bool isDirty_ = true;
76 75
77 /** 76 /**
78 * Creates a new contribution manager. 77 * Creates a new contribution manager.
79 */ 78 */
80 protected this() { 79 protected this() {
81 contributions = new ArraySeq!(IContributionItem); 80 contributions = new ArrayList();
82 // Do nothing. 81 // Do nothing.
83 } 82 }
84 83
85 /* 84 /*
86 * (non-Javadoc) Method declared on IContributionManager. 85 * (non-Javadoc) Method declared on IContributionManager.
94 * (non-Javadoc) Method declared on IContributionManager. 93 * (non-Javadoc) Method declared on IContributionManager.
95 */ 94 */
96 public void add(IContributionItem item) { 95 public void add(IContributionItem item) {
97 Assert.isNotNull( cast(Object)item, "Item must not be null"); //$NON-NLS-1$ 96 Assert.isNotNull( cast(Object)item, "Item must not be null"); //$NON-NLS-1$
98 if (allowItem(item)) { 97 if (allowItem(item)) {
99 contributions.append(item); 98 contributions.add(cast(Object)item);
100 itemAdded(item); 99 itemAdded(item);
101 } 100 }
102 } 101 }
103 102
104 /** 103 /**
116 * if there is no group with the given name 115 * if there is no group with the given name
117 */ 116 */
118 private void addToGroup(String groupName, IContributionItem item, 117 private void addToGroup(String groupName, IContributionItem item,
119 bool append) { 118 bool append) {
120 int i; 119 int i;
121 auto items = contributions.elements(); 120 auto items = contributions.iterator();
122 for (i = 0; items.more(); i++) { 121 for (i = 0; items.hasNext(); i++) {
123 IContributionItem o = cast(IContributionItem) items.get(); 122 IContributionItem o = cast(IContributionItem) items.next();
124 if (o.isGroupMarker()) { 123 if (o.isGroupMarker()) {
125 String id = o.getId(); 124 String id = o.getId();
126 if (id !is null && id.equalsIgnoreCase(groupName)) { 125 if (id !is null && id.equalsIgnoreCase(groupName)) {
127 i++; 126 i++;
128 if (append) { 127 if (append) {
129 for (; items.more(); i++) { 128 for (; items.hasNext(); i++) {
130 IContributionItem ci = cast(IContributionItem) items 129 IContributionItem ci = cast(IContributionItem) items
131 .get(); 130 .next();
132 if (ci.isGroupMarker()) { 131 if (ci.isGroupMarker()) {
133 break; 132 break;
134 } 133 }
135 } 134 }
136 } 135 }
137 if (allowItem(item)) { 136 if (allowItem(item)) {
138 //TODO: does this corrupt the iterator? 137 contributions.add(i, cast(Object)item);
139 contributions.addAt(i, item);
140 itemAdded(item); 138 itemAdded(item);
141 } 139 }
142 return; 140 return;
143 } 141 }
144 } 142 }
202 200
203 /* 201 /*
204 * (non-Javadoc) Method declared on IContributionManager. 202 * (non-Javadoc) Method declared on IContributionManager.
205 */ 203 */
206 public IContributionItem find(String id) { 204 public IContributionItem find(String id) {
207 auto e = contributions.elements(); 205 auto e = contributions.iterator();
208 while (e.more()) { 206 while (e.hasNext()) {
209 IContributionItem item = cast(IContributionItem) e.get(); 207 IContributionItem item = cast(IContributionItem) e.next();
210 String itemId = item.getId(); 208 String itemId = item.getId();
211 if (itemId !is null && itemId.equalsIgnoreCase(id)) { 209 if (itemId !is null && itemId.equalsIgnoreCase(id)) {
212 return item; 210 return item;
213 } 211 }
214 } 212 }
217 215
218 /* 216 /*
219 * (non-Javadoc) Method declared on IContributionManager. 217 * (non-Javadoc) Method declared on IContributionManager.
220 */ 218 */
221 public IContributionItem[] getItems() { 219 public IContributionItem[] getItems() {
222 return contributions.toArray(); 220 IContributionItem[] items = arraycast!(IContributionItem)(contributions.toArray());
221 return items;
223 } 222 }
224 223
225 /** 224 /**
226 * Return the number of contributions in this manager. 225 * Return the number of contributions in this manager.
227 * 226 *
245 overrides = new class IContributionManagerOverrides { 244 overrides = new class IContributionManagerOverrides {
246 public Boolean getEnabled(IContributionItem item) { 245 public Boolean getEnabled(IContributionItem item) {
247 return null; 246 return null;
248 } 247 }
249 248
250 public ValueWrapperInt getAccelerator(IContributionItem item) { 249 public Integer getAccelerator(IContributionItem item) {
251 return null; 250 return null;
252 } 251 }
253 252
254 public String getAcceleratorText(IContributionItem item) { 253 public String getAcceleratorText(IContributionItem item) {
255 return null; 254 return null;
303 * The contribution item 302 * The contribution item
304 * @return the index, or -1 if the item is not found 303 * @return the index, or -1 if the item is not found
305 * @since 3.0 304 * @since 3.0
306 */ 305 */
307 protected int indexOf(IContributionItem item) { 306 protected int indexOf(IContributionItem item) {
308 int res = -1; 307 return contributions.indexOf(cast(Object)item);
309 int idx = 0;
310 foreach( e; contributions ){
311 if( e == item ) {
312 res = idx;
313 break;
314 }
315 idx++;
316 }
317 return res;
318 } 308 }
319 309
320 /** 310 /**
321 * Insert the item at the given index. 311 * Insert the item at the given index.
322 * 312 *
329 if (index > contributions.size()) { 319 if (index > contributions.size()) {
330 throw new IndexOutOfBoundsException( Format( 320 throw new IndexOutOfBoundsException( Format(
331 "inserting {} at {}", item.getId(), index)); //$NON-NLS-1$ //$NON-NLS-2$ 321 "inserting {} at {}", item.getId(), index)); //$NON-NLS-1$ //$NON-NLS-2$
332 } 322 }
333 if (allowItem(item)) { 323 if (allowItem(item)) {
334 contributions.addAt(index, item); 324 contributions.add(index, cast(Object)item);
335 itemAdded(item); 325 itemAdded(item);
336 } 326 }
337 } 327 }
338 328
339 /* 329 /*
349 public void insertAfter(String ID, IContributionItem item) { 339 public void insertAfter(String ID, IContributionItem item) {
350 IContributionItem ci = find(ID); 340 IContributionItem ci = find(ID);
351 if (ci is null) { 341 if (ci is null) {
352 throw new IllegalArgumentException(Format("can't find ID{}", ID));//$NON-NLS-1$ 342 throw new IllegalArgumentException(Format("can't find ID{}", ID));//$NON-NLS-1$
353 } 343 }
354 int ix = SeqIndexOf!(IContributionItem)( contributions, ci ); 344 int ix = contributions.indexOf(cast(Object)ci);
355 if (ix >= 0) { 345 if (ix >= 0) {
356 // System.out.println("insert after: " + ix); 346 // System.out.println("insert after: " + ix);
357 if (allowItem(item)) { 347 if (allowItem(item)) {
358 contributions.addAt(ix + 1, item); 348 contributions.add(ix + 1,cast(Object) item);
359 itemAdded(item); 349 itemAdded(item);
360 } 350 }
361 } 351 }
362 } 352 }
363 353
374 public void insertBefore(String ID, IContributionItem item) { 364 public void insertBefore(String ID, IContributionItem item) {
375 IContributionItem ci = find(ID); 365 IContributionItem ci = find(ID);
376 if (ci is null) { 366 if (ci is null) {
377 throw new IllegalArgumentException(Format("can't find ID {}", ID));//$NON-NLS-1$ 367 throw new IllegalArgumentException(Format("can't find ID {}", ID));//$NON-NLS-1$
378 } 368 }
379 int ix = SeqIndexOf!(IContributionItem)(contributions,ci); 369 int ix = contributions.indexOf(cast(Object)ci);
380 if (ix >= 0) { 370 if (ix >= 0) {
381 // System.out.println("insert before: " + ix); 371 // System.out.println("insert before: " + ix);
382 if (allowItem(item)) { 372 if (allowItem(item)) {
383 contributions.addAt(ix, item); 373 contributions.add(ix, cast(Object)item);
384 itemAdded(item); 374 itemAdded(item);
385 } 375 }
386 } 376 }
387 } 377 }
388 378
392 public bool isDirty() { 382 public bool isDirty() {
393 if (isDirty_) { 383 if (isDirty_) {
394 return true; 384 return true;
395 } 385 }
396 if (hasDynamicItems()) { 386 if (hasDynamicItems()) {
397 foreach( e; contributions ){ 387 for (Iterator iter = contributions.iterator(); iter.hasNext();) {
398 IContributionItem item = cast(IContributionItem) e; 388 IContributionItem item = cast(IContributionItem) iter.next();
399 if (item.isDirty()) { 389 if (item.isDirty()) {
400 return true; 390 return true;
401 } 391 }
402 } 392 }
403 } 393 }
406 396
407 /* 397 /*
408 * (non-Javadoc) Method declared on IContributionManager. 398 * (non-Javadoc) Method declared on IContributionManager.
409 */ 399 */
410 public bool isEmpty() { 400 public bool isEmpty() {
411 return contributions.drained(); 401 return contributions.isEmpty();
412 } 402 }
413 403
414 /** 404 /**
415 * The given item was added to the list of contributions. Marks the manager 405 * The given item was added to the list of contributions. Marks the manager
416 * as dirty and updates the number of dynamic items, and the memento. 406 * as dirty and updates the number of dynamic items, and the memento.
476 466
477 /* 467 /*
478 * (non-Javadoc) Method declared on IContributionManager. 468 * (non-Javadoc) Method declared on IContributionManager.
479 */ 469 */
480 public IContributionItem remove(IContributionItem item) { 470 public IContributionItem remove(IContributionItem item) {
481 bool contained = contributions.contains(item); 471 if (contributions.remove(cast(Object)item)) {
482 contributions.remove(item);
483 if (contained) {
484 itemRemoved(item); 472 itemRemoved(item);
485 return item; 473 return item;
486 } 474 }
487 return null; 475 return null;
488 } 476 }
535 IContributionItem oldItem = cast(IContributionItem) contributions 523 IContributionItem oldItem = cast(IContributionItem) contributions
536 .get(index); 524 .get(index);
537 itemRemoved(oldItem); 525 itemRemoved(oldItem);
538 526
539 // Add the new item. 527 // Add the new item.
540 contributions.replaceAt(index, replacementItem); 528 contributions.set(index, cast(Object)replacementItem);
541 itemAdded(replacementItem); // throws NPE if (replacementItem is null) 529 itemAdded(replacementItem); // throws NPE if (replacementItem is null)
542 530
543 // Go through and remove duplicates. 531 // Go through and remove duplicates.
544 for (int i = contributions.size() - 1; i > index; i--) { 532 for (int i = contributions.size() - 1; i > index; i--) {
545 IContributionItem item = cast(IContributionItem) contributions.get(i); 533 IContributionItem item = cast(IContributionItem) contributions.get(i);
546 if ((item !is null) && (identifier.equals(item.getId()))) { 534 if ((item !is null) && (identifier.equals(item.getId()))) {
547 if (Policy.TRACE_TOOLBAR) { 535 if (Policy.TRACE_TOOLBAR) {
548 Stdout.formatln("Removing duplicate on replace: {}", identifier); //$NON-NLS-1$ 536 Stdout.formatln("Removing duplicate on replace: {}", identifier); //$NON-NLS-1$
549 } 537 }
550 contributions.removeAt(i); 538 contributions.remove(i);
551 itemRemoved(item); 539 itemRemoved(item);
552 } 540 }
553 } 541 }
554 542
555 return true; // success 543 return true; // success
587 */ 575 */
588 protected void internalSetItems(IContributionItem[] items) { 576 protected void internalSetItems(IContributionItem[] items) {
589 contributions.clear(); 577 contributions.clear();
590 for (int i = 0; i < items.length; i++) { 578 for (int i = 0; i < items.length; i++) {
591 if (allowItem(items[i])) { 579 if (allowItem(items[i])) {
592 contributions.append(items[i]); 580 contributions.add(cast(Object)items[i]);
593 } 581 }
594 } 582 }
595 } 583 }
596 } 584 }