Mercurial > projects > dwt-addons
comparison dwtx/jface/viewers/AbstractListViewer.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 |
comparison
equal
deleted
inserted
replaced
103:2d6540440fe6 | 104:04b47443bb01 |
---|---|
20 import dwtx.jface.viewers.ILabelProvider; | 20 import dwtx.jface.viewers.ILabelProvider; |
21 import dwtx.jface.viewers.IBaseLabelProvider; | 21 import dwtx.jface.viewers.IBaseLabelProvider; |
22 import dwtx.jface.viewers.ViewerComparator; | 22 import dwtx.jface.viewers.ViewerComparator; |
23 import dwtx.jface.viewers.IElementComparer; | 23 import dwtx.jface.viewers.IElementComparer; |
24 | 24 |
25 import tango.util.collection.ArraySeq; | |
26 import tango.util.collection.model.Seq; | |
27 import tango.util.collection.model.SeqView; | |
28 | 25 |
29 import dwt.widgets.Control; | 26 import dwt.widgets.Control; |
30 import dwt.widgets.Widget; | 27 import dwt.widgets.Widget; |
31 import dwtx.core.runtime.Assert; | 28 import dwtx.core.runtime.Assert; |
32 | 29 |
33 import dwt.dwthelper.utils; | 30 import dwt.dwthelper.utils; |
31 import dwtx.dwtxhelper.Collection; | |
34 import dwt.dwthelper.Runnable; | 32 import dwt.dwthelper.Runnable; |
35 | 33 |
36 /** | 34 /** |
37 * Abstract base class for viewers that contain lists of items (such as a combo or list). | 35 * Abstract base class for viewers that contain lists of items (such as a combo or list). |
38 * Most of the viewer implementation is in this base class, except for the minimal code that | 36 * Most of the viewer implementation is in this base class, except for the minimal code that |
48 alias StructuredViewer.setSelectionToWidget setSelectionToWidget; | 46 alias StructuredViewer.setSelectionToWidget setSelectionToWidget; |
49 | 47 |
50 /** | 48 /** |
51 * A list of viewer elements (element type: <code>Object</code>). | 49 * A list of viewer elements (element type: <code>Object</code>). |
52 */ | 50 */ |
53 private Seq!(Object) listMap; | 51 private dwtx.dwtxhelper.Collection.List listMap; |
54 | 52 |
55 /** | 53 /** |
56 * Adds the given string to the underlying widget at the given index | 54 * Adds the given string to the underlying widget at the given index |
57 * | 55 * |
58 * @param string the string to add | 56 * @param string the string to add |
130 * Deselects all selected items in the underlying widget. | 128 * Deselects all selected items in the underlying widget. |
131 */ | 129 */ |
132 protected abstract void listDeselectAll(); | 130 protected abstract void listDeselectAll(); |
133 | 131 |
134 public this(){ | 132 public this(){ |
135 listMap = new ArraySeq!(Object); | 133 listMap = new ArrayList(); |
136 } | 134 } |
137 | 135 |
138 /** | 136 /** |
139 * Adds the given elements to this list viewer. | 137 * Adds the given elements to this list viewer. |
140 * If this viewer does not have a sorter, the elements are added at the end | 138 * If this viewer does not have a sorter, the elements are added at the end |
158 } | 156 } |
159 } | 157 } |
160 | 158 |
161 private void insertItem(ILabelProvider labelProvider, Object element, int index) { | 159 private void insertItem(ILabelProvider labelProvider, Object element, int index) { |
162 listAdd(getLabelProviderText(labelProvider, element), index); | 160 listAdd(getLabelProviderText(labelProvider, element), index); |
163 listMap.addAt(index, element); | 161 listMap.add(index, element); |
164 mapElement(element, getControl()); // must map it, since findItem only looks in map, if enabled | 162 mapElement(element, getControl()); // must map it, since findItem only looks in map, if enabled |
165 } | 163 } |
166 | 164 |
167 /** | 165 /** |
168 * Inserts the given element into this list viewer at the given position. | 166 * Inserts the given element into this list viewer at the given position. |
292 * Method declared on Viewer. | 290 * Method declared on Viewer. |
293 */ | 291 */ |
294 /* (non-Javadoc) | 292 /* (non-Javadoc) |
295 * Method declared on StructuredViewer. | 293 * Method declared on StructuredViewer. |
296 */ | 294 */ |
297 protected override SeqView!(Object) getSelectionFromWidget() { | 295 protected override List getSelectionFromWidget() { |
298 int[] ixs = listGetSelectionIndices(); | 296 int[] ixs = listGetSelectionIndices(); |
299 ArraySeq!(Object) list = new ArraySeq!(Object); | 297 ArrayList list = new ArrayList(ixs.length); |
300 list.capacity(ixs.length); | |
301 for (int i = 0; i < ixs.length; i++) { | 298 for (int i = 0; i < ixs.length; i++) { |
302 Object e = getElementAt(ixs[i]); | 299 Object e = getElementAt(ixs[i]); |
303 if (e !is null) { | 300 if (e !is null) { |
304 list.append(e); | 301 list.add(e); |
305 } | 302 } |
306 } | 303 } |
307 return list; | 304 return list; |
308 } | 305 } |
309 | 306 |
354 listRemoveAll(); | 351 listRemoveAll(); |
355 String[] labels = new String[size]; | 352 String[] labels = new String[size]; |
356 for (int i = 0; i < size; i++) { | 353 for (int i = 0; i < size; i++) { |
357 Object el = children[i]; | 354 Object el = children[i]; |
358 labels[i] = getLabelProviderText(cast(ILabelProvider) getLabelProvider(),el); | 355 labels[i] = getLabelProviderText(cast(ILabelProvider) getLabelProvider(),el); |
359 listMap.append(el); | 356 listMap.add(el); |
360 mapElement(el, getControl()); // must map it, since findItem only looks in map, if enabled | 357 mapElement(el, getControl()); // must map it, since findItem only looks in map, if enabled |
361 } | 358 } |
362 listSetItems(labels); | 359 listSetItems(labels); |
363 } | 360 } |
364 | 361 |
371 // the parent | 368 // the parent |
372 if (listMap !is null) { | 369 if (listMap !is null) { |
373 listMap.clear(); | 370 listMap.clear(); |
374 } | 371 } |
375 unmapAllElements(); | 372 unmapAllElements(); |
376 auto selection = getSelectionFromWidget(); | 373 List selection = getSelectionFromWidget(); |
377 | 374 |
378 int topIndex = -1; | 375 int topIndex = -1; |
379 if (selection is null || selection.drained()) { | 376 if (selection is null || selection.isEmpty()) { |
380 topIndex = listGetTopIndex(); | 377 topIndex = listGetTopIndex(); |
381 } | 378 } |
382 | 379 |
383 Object[] children = null; | 380 Object[] children = null; |
384 list.setRedraw(false); | 381 list.setRedraw(false); |
385 try { | 382 try { |
386 listRemoveAll(); | 383 listRemoveAll(); |
387 | 384 |
388 children = getSortedChildren(getRoot()); | 385 children = getSortedChildren(getRoot()); |
389 String[] items = new String[children.length]; | 386 String[] items = new String[children.length]; |
390 | 387 |
391 ILabelProvider labelProvider = cast(ILabelProvider) getLabelProvider(); | 388 ILabelProvider labelProvider = cast(ILabelProvider) getLabelProvider(); |
392 | 389 |
393 for (int i = 0; i < items.length; i++) { | 390 for (int i = 0; i < items.length; i++) { |
394 Object el = children[i]; | 391 Object el = children[i]; |
395 items[i] = getLabelProviderText(labelProvider, el); | 392 items[i] = getLabelProviderText(labelProvider, el); |
396 listMap.append(el); | 393 listMap.add(el); |
397 mapElement(el, list); // must map it, since findItem only looks in map, if enabled | 394 mapElement(el, list); // must map it, since findItem only looks in map, if enabled |
398 } | 395 } |
399 | 396 |
400 listSetItems(items); | 397 listSetItems(items); |
401 } finally { | 398 } finally { |
402 list.setRedraw(true); | 399 list.setRedraw(true); |
403 } | 400 } |
404 | 401 |
448 return; | 445 return; |
449 } | 446 } |
450 int ix = getElementIndex(elements[i]); | 447 int ix = getElementIndex(elements[i]); |
451 if (ix >= 0) { | 448 if (ix >= 0) { |
452 listRemove(ix); | 449 listRemove(ix); |
453 listMap.removeAt(ix); | 450 listMap.remove(ix); |
454 unmapElement(elements[i], getControl()); | 451 unmapElement(elements[i], getControl()); |
455 } | 452 } |
456 } | 453 } |
457 } | 454 } |
458 | 455 |
470 public void remove(Object[] elements) { | 467 public void remove(Object[] elements) { |
471 assertElementsNotNull(elements); | 468 assertElementsNotNull(elements); |
472 if (elements.length is 0) { | 469 if (elements.length is 0) { |
473 return; | 470 return; |
474 } | 471 } |
475 preservingSelection(new class(elements) Runnable { | 472 preservingSelection( dgRunnable( (Object[] elements_){ |
476 Object[] elements_; | 473 internalRemove(elements_); |
477 this(Object[] a){ | 474 }, elements)); |
478 elements_= a; | |
479 } | |
480 public void run() { | |
481 internalRemove(elements_); | |
482 } | |
483 }); | |
484 } | 475 } |
485 | 476 |
486 /** | 477 /** |
487 * Removes the given element from this list viewer. | 478 * Removes the given element from this list viewer. |
488 * The selection is updated if necessary. | 479 * The selection is updated if necessary. |
514 } | 505 } |
515 | 506 |
516 /* (non-Javadoc) | 507 /* (non-Javadoc) |
517 * Method declared on StructuredViewer. | 508 * Method declared on StructuredViewer. |
518 */ | 509 */ |
519 protected override void setSelectionToWidget(SeqView!(Object) in_, bool reveal) { | 510 protected override void setSelectionToWidget(List in_, bool reveal) { |
520 if (in_ is null || in_.size() is 0) { // clear selection | 511 if (in_ is null || in_.size() is 0) { // clear selection |
521 listDeselectAll(); | 512 listDeselectAll(); |
522 } else { | 513 } else { |
523 int n = in_.size(); | 514 int n = in_.size(); |
524 int[] ixs = new int[n]; | 515 int[] ixs = new int[n]; |
548 * @return the index | 539 * @return the index |
549 */ | 540 */ |
550 int getElementIndex(Object element) { | 541 int getElementIndex(Object element) { |
551 IElementComparer comparer = getComparer(); | 542 IElementComparer comparer = getComparer(); |
552 if (comparer is null) { | 543 if (comparer is null) { |
553 int idx = 0; | 544 return listMap.indexOf(element); |
554 foreach( e; listMap ){ | |
555 if( e == element ){ | |
556 return idx; | |
557 } | |
558 idx++; | |
559 } | |
560 return -1; | |
561 } | 545 } |
562 int size = listMap.size(); | 546 int size = listMap.size(); |
563 for (int i = 0; i < size; i++) { | 547 for (int i = 0; i < size; i++) { |
564 if (comparer.opEquals(element, listMap.get(i))) | 548 if (comparer.opEquals(element, listMap.get(i))) |
565 return i; | 549 return i; |