Mercurial > projects > dwt-addons
comparison dwtx/jface/viewers/AbstractTableViewer.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 |
---|---|
23 import dwtx.jface.viewers.ViewerColumn; | 23 import dwtx.jface.viewers.ViewerColumn; |
24 import dwtx.jface.viewers.ViewerCell; | 24 import dwtx.jface.viewers.ViewerCell; |
25 import dwtx.jface.viewers.ViewerComparator; | 25 import dwtx.jface.viewers.ViewerComparator; |
26 import dwtx.jface.viewers.IStructuredContentProvider; | 26 import dwtx.jface.viewers.IStructuredContentProvider; |
27 | 27 |
28 import tango.util.collection.model.SeqView; | |
29 import tango.util.collection.ArraySeq; | |
30 import tango.util.collection.HashSet; | |
31 | 28 |
32 import dwt.DWT; | 29 import dwt.DWT; |
33 import dwt.widgets.Control; | 30 import dwt.widgets.Control; |
34 import dwt.widgets.Event; | 31 import dwt.widgets.Event; |
35 import dwt.widgets.Item; | 32 import dwt.widgets.Item; |
36 import dwt.widgets.Listener; | 33 import dwt.widgets.Listener; |
37 import dwt.widgets.Widget; | 34 import dwt.widgets.Widget; |
38 import dwtx.core.runtime.Assert; | 35 import dwtx.core.runtime.Assert; |
39 | 36 |
40 import dwt.dwthelper.utils; | 37 import dwt.dwthelper.utils; |
38 import dwtx.dwtxhelper.Collection; | |
41 import dwt.dwthelper.Runnable; | 39 import dwt.dwthelper.Runnable; |
42 import tango.core.Array; | 40 import tango.core.Array; |
43 | 41 |
44 /** | 42 /** |
45 * This is a widget independent class implementors of | 43 * This is a widget independent class implementors of |
72 | 70 |
73 /** | 71 /** |
74 * Add the listener for SetData on the table | 72 * Add the listener for SetData on the table |
75 */ | 73 */ |
76 private void addTableListener() { | 74 private void addTableListener() { |
77 getControl().addListener(DWT.SetData, new class Listener { | 75 getControl().addListener(DWT.SetData, dgListener( (Event event){ |
78 /* | 76 /* |
79 * (non-Javadoc) | 77 * (non-Javadoc) |
80 * | 78 * |
81 * @see dwt.widgets.Listener#handleEvent(dwt.widgets.Event) | 79 * @see dwt.widgets.Listener#handleEvent(dwt.widgets.Event) |
82 */ | 80 */ |
83 public void handleEvent(Event event) { | 81 Item item = cast(Item) event.item; |
84 Item item = cast(Item) event.item; | 82 final int index = doIndexOf(item); |
85 final int index = doIndexOf(item); | 83 Object element = resolveElement(index); |
86 Object element = resolveElement(index); | 84 if (element is null) { |
87 if (element is null) { | 85 // Didn't find it so make a request |
88 // Didn't find it so make a request | 86 // Keep looking if it is not in the cache. |
89 // Keep looking if it is not in the cache. | 87 IContentProvider contentProvider = getContentProvider(); |
90 IContentProvider contentProvider = getContentProvider(); | 88 // If we are building lazily then request lookup now |
91 // If we are building lazily then request lookup now | 89 if (auto lcp = cast(ILazyContentProvider)contentProvider ) { |
92 if (auto lcp = cast(ILazyContentProvider)contentProvider ) { | 90 lcp.updateElement(index); |
93 lcp.updateElement(index); | 91 return; |
94 return; | |
95 } | |
96 } | 92 } |
97 | 93 } |
98 associate(element, item); | 94 |
99 updateItem(item, element); | 95 associate(element, item); |
100 } | 96 updateItem(item, element); |
101 | 97 })); |
102 }); | |
103 } | 98 } |
104 | 99 |
105 /** | 100 /** |
106 * Get the element at index.Resolve it lazily if this is available. | 101 * Get the element at index.Resolve it lazily if this is available. |
107 * | 102 * |
477 /* | 472 /* |
478 * (non-Javadoc) | 473 * (non-Javadoc) |
479 * | 474 * |
480 * @see dwtx.jface.viewers.StructuredViewer#getSelectionFromWidget() | 475 * @see dwtx.jface.viewers.StructuredViewer#getSelectionFromWidget() |
481 */ | 476 */ |
482 protected override SeqView!(Object) getSelectionFromWidget() { | 477 protected override List getSelectionFromWidget() { |
483 if (virtualManager !is null) { | 478 if (virtualManager !is null) { |
484 return getVirtualSelection(); | 479 return getVirtualSelection(); |
485 } | 480 } |
486 Widget[] items = doGetSelection(); | 481 Widget[] items = doGetSelection(); |
487 auto list = new ArraySeq!(Object); | 482 ArrayList list = new ArrayList(items.length); |
488 list.capacity(items.length); | |
489 for (int i = 0; i < items.length; i++) { | 483 for (int i = 0; i < items.length; i++) { |
490 Widget item = items[i]; | 484 Widget item = items[i]; |
491 Object e = item.getData(); | 485 Object e = item.getData(); |
492 if (e !is null) { | 486 if (e !is null) { |
493 list.append(e); | 487 list.add(e); |
494 } | 488 } |
495 } | 489 } |
496 return list; | 490 return list; |
497 } | 491 } |
498 | 492 |
501 * extra widget creation. | 495 * extra widget creation. |
502 * | 496 * |
503 * @return List of Object | 497 * @return List of Object |
504 */ | 498 */ |
505 | 499 |
506 private SeqView!(Object) getVirtualSelection() { | 500 private List getVirtualSelection() { |
507 | 501 |
508 auto result = new ArraySeq!(Object); | 502 List result = new ArrayList(); |
509 int[] selectionIndices = doGetSelectionIndices(); | 503 int[] selectionIndices = doGetSelectionIndices(); |
510 if (auto lazy_ = cast(ILazyContentProvider) getContentProvider() ) { | 504 if (auto lazy_ = cast(ILazyContentProvider) getContentProvider() ) { |
511 for (int i = 0; i < selectionIndices.length; i++) { | 505 for (int i = 0; i < selectionIndices.length; i++) { |
512 int selectionIndex = selectionIndices[i]; | 506 int selectionIndex = selectionIndices[i]; |
513 lazy_.updateElement(selectionIndex);// Start the update | 507 lazy_.updateElement(selectionIndex);// Start the update |
514 Object element = doGetItem(selectionIndex).getData(); | 508 Object element = doGetItem(selectionIndex).getData(); |
515 // Only add the element if it got updated. | 509 // Only add the element if it got updated. |
516 // If this is done deferred the selection will | 510 // If this is done deferred the selection will |
517 // be incomplete until selection is finished. | 511 // be incomplete until selection is finished. |
518 if (element !is null) { | 512 if (element !is null) { |
519 result.append(element); | 513 result.add(element); |
520 } | 514 } |
521 } | 515 } |
522 } else { | 516 } else { |
523 for (int i = 0; i < selectionIndices.length; i++) { | 517 for (int i = 0; i < selectionIndices.length; i++) { |
524 Object element = null; | 518 Object element = null; |
531 // Not cached so try the item's data | 525 // Not cached so try the item's data |
532 Item item = doGetItem(selectionIndex); | 526 Item item = doGetItem(selectionIndex); |
533 element = item.getData(); | 527 element = item.getData(); |
534 } | 528 } |
535 if (element !is null) { | 529 if (element !is null) { |
536 result.append(element); | 530 result.add(element); |
537 } | 531 } |
538 } | 532 } |
539 | 533 |
540 } | 534 } |
541 return result; | 535 return result; |
585 * java.lang.Object) | 579 * java.lang.Object) |
586 */ | 580 */ |
587 protected override void inputChanged(Object input, Object oldInput) { | 581 protected override void inputChanged(Object input, Object oldInput) { |
588 getControl().setRedraw(false); | 582 getControl().setRedraw(false); |
589 try { | 583 try { |
590 preservingSelection(new class Runnable { | 584 preservingSelection(dgRunnable( { |
591 public void run() { | 585 internalRefresh(getRoot()); |
592 internalRefresh(getRoot()); | 586 })); |
593 } | |
594 }); | |
595 } finally { | 587 } finally { |
596 getControl().setRedraw(true); | 588 getControl().setRedraw(true); |
597 } | 589 } |
598 } | 590 } |
599 | 591 |
826 if (checkBusy()) | 818 if (checkBusy()) |
827 return; | 819 return; |
828 if (elements.length is 0) { | 820 if (elements.length is 0) { |
829 return; | 821 return; |
830 } | 822 } |
831 preservingSelection(new class(elements) Runnable { | 823 preservingSelection(dgRunnable( (Object[] elements_){ |
832 Object[] elements_; | 824 internalRemove(elements_); |
833 this(Object[] a){ | 825 }, elements)); |
834 elements_=a; | |
835 } | |
836 public void run() { | |
837 internalRemove(elements_); | |
838 } | |
839 }); | |
840 } | 826 } |
841 | 827 |
842 /** | 828 /** |
843 * Removes the given element from this table viewer. The selection is | 829 * Removes the given element from this table viewer. The selection is |
844 * updated if necessary. | 830 * updated if necessary. |
876 * (non-Javadoc) | 862 * (non-Javadoc) |
877 * | 863 * |
878 * @see dwtx.jface.viewers.StructuredViewer#setSelectionToWidget(java.util.List, | 864 * @see dwtx.jface.viewers.StructuredViewer#setSelectionToWidget(java.util.List, |
879 * bool) | 865 * bool) |
880 */ | 866 */ |
881 protected override void setSelectionToWidget(SeqView!(Object) list, bool reveal) { | 867 protected override void setSelectionToWidget(List list, bool reveal) { |
882 if (list is null) { | 868 if (list is null) { |
883 doDeselectAll(); | 869 doDeselectAll(); |
884 return; | 870 return; |
885 } | 871 } |
886 | 872 |
893 // move the focus to this row (See bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=198665) | 879 // move the focus to this row (See bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=198665) |
894 if (reveal) { | 880 if (reveal) { |
895 int size = list.size(); | 881 int size = list.size(); |
896 Item[] items = new Item[size]; | 882 Item[] items = new Item[size]; |
897 int count = 0; | 883 int count = 0; |
898 foreach( o; list ) { | 884 for (int i = 0; i < size; ++i) { |
885 Object o = list.get(i); | |
899 Widget w = findItem(o); | 886 Widget w = findItem(o); |
900 if (auto item = cast(Item)w ) { | 887 if (auto item = cast(Item)w ) { |
901 items[count++] = item; | 888 items[count++] = item; |
902 } | 889 } |
903 } | 890 } |
905 System.arraycopy(items, 0, items = new Item[count], 0, count); | 892 System.arraycopy(items, 0, items = new Item[count], 0, count); |
906 } | 893 } |
907 doSetSelection(items); | 894 doSetSelection(items); |
908 } else { | 895 } else { |
909 doDeselectAll(); // Clear the selection | 896 doDeselectAll(); // Clear the selection |
910 if( ! list.drained() ) { | 897 if( ! list.isEmpty() ) { |
911 int[] indices = new int[list.size()]; | 898 int[] indices = new int[list.size()]; |
912 | 899 |
900 Iterator it = list.iterator(); | |
913 Item[] items = doGetItems(); | 901 Item[] items = doGetItems(); |
902 Object modelElement; | |
914 | 903 |
915 int count = 0; | 904 int count = 0; |
916 foreach( modelElement; list ){ | 905 while( it.hasNext() ) { |
906 modelElement = it.next(); | |
917 bool found = false; | 907 bool found = false; |
918 for (int i = 0; i < items.length && !found; i++) { | 908 for (int i = 0; i < items.length && !found; i++) { |
919 if (opEquals(modelElement, items[i].getData())) { | 909 if (opEquals(modelElement, items[i].getData())) { |
920 indices[count++] = i; | 910 indices[count++] = i; |
921 found = true; | 911 found = true; |
938 * @param list | 928 * @param list |
939 * The elements to set | 929 * The elements to set |
940 * @param reveal | 930 * @param reveal |
941 * Whether or not reveal the first item. | 931 * Whether or not reveal the first item. |
942 */ | 932 */ |
943 private void virtualSetSelectionToWidget(SeqView!(Object) list, bool reveal) { | 933 private void virtualSetSelectionToWidget(List list, bool reveal) { |
944 int size = list.size(); | 934 int size = list.size(); |
945 int[] indices = new int[list.size()]; | 935 int[] indices = new int[list.size()]; |
946 | 936 |
947 Item firstItem = null; | 937 Item firstItem = null; |
948 int count = 0; | 938 int count = 0; |
949 auto virtualElements = new HashSet!(Object); | 939 HashSet virtualElements = new HashSet(); |
950 foreach( o; list ){ | 940 for (int i = 0; i < size; ++i) { |
941 Object o = list.get(i); | |
951 Widget w = findItem(o); | 942 Widget w = findItem(o); |
952 if (auto item = cast(Item)w ) { | 943 if (auto item = cast(Item)w ) { |
953 indices[count++] = doIndexOf(item); | 944 indices[count++] = doIndexOf(item); |
954 if (firstItem is null) { | 945 if (firstItem is null) { |
955 firstItem = item; | 946 firstItem = item; |
1038 | 1029 |
1039 /** | 1030 /** |
1040 * Replace the element at the given index with the given element. This | 1031 * Replace the element at the given index with the given element. This |
1041 * method will not call the content provider to verify. <strong>Note that | 1032 * method will not call the content provider to verify. <strong>Note that |
1042 * this method will materialize a TableItem the given index.</strong>. | 1033 * this method will materialize a TableItem the given index.</strong>. |
1043 * | 1034 * |
1044 * @param element | 1035 * @param element |
1045 * @param index | 1036 * @param index |
1046 * @see ILazyContentProvider | 1037 * @see ILazyContentProvider |
1047 * | 1038 * |
1048 * @since 3.1 | 1039 * @since 3.1 |
1049 */ | 1040 */ |
1050 public void replace(Object element, int index) { | 1041 public void replace(Object element, int index) { |
1051 if (checkBusy()) | 1042 if (checkBusy()) |
1052 return; | 1043 return; |