Added
Link Here
|
1 |
package org.eclipse.core.databinding.observable.list; |
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Arrays; |
5 |
import java.util.List; |
6 |
|
7 |
import org.eclipse.core.databinding.observable.Diffs; |
8 |
import org.eclipse.core.databinding.observable.value.IObservableValue; |
9 |
|
10 |
/** |
11 |
* @since 1.0 |
12 |
* |
13 |
*/ |
14 |
public abstract class FilteringList extends ObservableList { |
15 |
|
16 |
private final IObservableList unfiltered; |
17 |
|
18 |
private final IObservableValue textObservable; |
19 |
|
20 |
private IListChangeListener listChangeListener = new IListChangeListener() { |
21 |
public void handleListChange(IObservableList source, ListDiff diff) { |
22 |
ListDiffEntry[] differences = diff.getDifferences(); |
23 |
List listDiffEntries = new ArrayList(); |
24 |
for (int i = 0; i < differences.length; i++) { |
25 |
ListDiffEntry diffEntry = differences[i]; |
26 |
Object element = diffEntry.getElement(); |
27 |
if (diffEntry.isAddition()) { |
28 |
if (select(element)) { |
29 |
int index = internalAddElement(element, diffEntry |
30 |
.getPosition()); |
31 |
if (index >= 0) { |
32 |
listDiffEntries.add(Diffs.createListDiffEntry( |
33 |
index, true, element)); |
34 |
} |
35 |
} |
36 |
} else { |
37 |
if (select(element)) { |
38 |
int index = internalRemoveElement(element, diffEntry |
39 |
.getPosition()); |
40 |
if (index >= 0) { |
41 |
listDiffEntries.add(Diffs.createListDiffEntry( |
42 |
index, false, element)); |
43 |
} |
44 |
} |
45 |
} |
46 |
} |
47 |
fireListChange(Diffs |
48 |
.createListDiff((ListDiffEntry[]) listDiffEntries |
49 |
.toArray(new ListDiffEntry[listDiffEntries.size()]))); |
50 |
} |
51 |
}; |
52 |
|
53 |
protected FilteringList(IObservableList list, |
54 |
IObservableValue textObservable) { |
55 |
super(list.getRealm(), new ArrayList(), list.getElementType()); |
56 |
this.unfiltered = list; |
57 |
unfiltered.addListChangeListener(listChangeListener); |
58 |
refilter(); |
59 |
} |
60 |
|
61 |
protected void refilter() { |
62 |
List listDiffEntries = new ArrayList(); |
63 |
for (int i = 0; i < unfiltered.size(); i++) { |
64 |
Object element = unfiltered.get(i); |
65 |
if (select(element)) { |
66 |
int newIndex = internalAddElement(element, i); |
67 |
if (newIndex >= 0) { |
68 |
listDiffEntries.add(Diffs.createListDiffEntry(newIndex, |
69 |
true, element)); |
70 |
} |
71 |
} else { |
72 |
int oldIndex = internalRemoveElement(element, i); |
73 |
if (oldIndex >= 0) { |
74 |
listDiffEntries.add(Diffs.createListDiffEntry(oldIndex, |
75 |
false, element)); |
76 |
} |
77 |
} |
78 |
} |
79 |
fireListChange(Diffs.createListDiff((ListDiffEntry[]) listDiffEntries |
80 |
.toArray(new ListDiffEntry[listDiffEntries.size()]))); |
81 |
} |
82 |
|
83 |
private int internalRemoveElement(Object element, int childIndex) { |
84 |
int location = Arrays.binarySearch(filteredIndices, childIndex); |
85 |
if (location >= 0) { |
86 |
wrappedList.remove(location); |
87 |
if (location == 0) { |
88 |
if (filteredIndices.length == 1) { |
89 |
filteredIndices = new int[0]; |
90 |
} else { |
91 |
int[] next = new int[filteredIndices.length - 1]; |
92 |
System.arraycopy(filteredIndices, 1, next, 0, next.length); |
93 |
filteredIndices = next; |
94 |
} |
95 |
} else if (location == (filteredIndices.length - 1)) { |
96 |
int[] next = new int[filteredIndices.length - 1]; |
97 |
System.arraycopy(filteredIndices, 0, next, 0, location); |
98 |
filteredIndices = next; |
99 |
} else { |
100 |
int[] next = new int[filteredIndices.length - 1]; |
101 |
System.arraycopy(filteredIndices, 0, next, 0, location); |
102 |
System.arraycopy(filteredIndices, location + 1, next, location, |
103 |
next.length - location); |
104 |
filteredIndices = next; |
105 |
} |
106 |
} |
107 |
return location; |
108 |
} |
109 |
|
110 |
private int internalAddElement(Object element, int index) { |
111 |
int location = Arrays.binarySearch(filteredIndices, index); |
112 |
if (location >= 0) { |
113 |
return -1; |
114 |
} |
115 |
location = 0 - (location + 1); |
116 |
wrappedList.add(location, element); |
117 |
int[] next = new int[filteredIndices.length + 1]; |
118 |
if (location == 0) { |
119 |
next[0] = index; |
120 |
System.arraycopy(filteredIndices, 0, next, 1, |
121 |
filteredIndices.length); |
122 |
} else if (location == filteredIndices.length) { |
123 |
next[filteredIndices.length] = index; |
124 |
System.arraycopy(filteredIndices, 0, next, 0, |
125 |
filteredIndices.length); |
126 |
} else { |
127 |
System.arraycopy(filteredIndices, 0, next, 0, location); |
128 |
next[location] = index; |
129 |
System.arraycopy(filteredIndices, location, next, location + 1, |
130 |
filteredIndices.length - location); |
131 |
} |
132 |
filteredIndices = next; |
133 |
return location; |
134 |
} |
135 |
|
136 |
protected boolean select(Object element) { |
137 |
return stringMatcher.match(getText(element)); |
138 |
} |
139 |
|
140 |
protected abstract String getText(Object element); |
141 |
|
142 |
} |