78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2008 Matthew Hall 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 * Matthew Hall - initial API and implementation (bug 215531)
|
|
10 * Matthew Hall - bug 230267
|
|
11 ******************************************************************************/
|
|
12
|
|
13 module org.eclipse.jface.internal.databinding.viewers.ObservableViewerElementSet;
|
85
|
14 import org.eclipse.jface.internal.databinding.viewers.ViewerElementSet;
|
78
|
15
|
|
16 import java.lang.all;
|
|
17
|
|
18 import java.util.Collection;
|
|
19 import java.util.Collections;
|
|
20 import java.util.Iterator;
|
|
21 import java.util.Set;
|
|
22
|
|
23 import org.eclipse.core.databinding.observable.Diffs;
|
|
24 import org.eclipse.core.databinding.observable.Realm;
|
|
25 import org.eclipse.core.databinding.observable.set.AbstractObservableSet;
|
|
26 import org.eclipse.core.databinding.observable.set.IObservableSet;
|
|
27 import org.eclipse.core.databinding.observable.set.WritableSet;
|
|
28 import org.eclipse.core.runtime.Assert;
|
|
29 import org.eclipse.jface.viewers.IElementComparer;
|
|
30 import org.eclipse.jface.viewers.StructuredViewer;
|
|
31
|
|
32 /**
|
|
33 * An {@link IObservableSet} of elements in a {@link StructuredViewer}.
|
|
34 * Elements of the set are compared using an {@link IElementComparer} instead of
|
|
35 * {@link #equals(Object)}.
|
|
36 * <p>
|
|
37 * This class is <i>not</i> a strict implementation the {@link IObservableSet}
|
|
38 * interface. It intentionally violates the {@link Set} contract, which requires
|
|
39 * the use of {@link #equals(Object)} when comparing elements. This class is
|
|
40 * designed for use with {@link StructuredViewer} which uses
|
|
41 * {@link IElementComparer} for element comparisons.
|
|
42 *
|
|
43 *
|
|
44 * @since 1.2
|
|
45 */
|
|
46 public class ObservableViewerElementSet : AbstractObservableSet {
|
|
47 private Set wrappedSet;
|
|
48 private Object elementType;
|
|
49 private IElementComparer comparer;
|
|
50
|
|
51 /**
|
|
52 * Constructs an ObservableViewerElementSet on the given {@link Realm} which
|
|
53 * uses the given {@link IElementComparer} to compare elements.
|
|
54 *
|
|
55 * @param realm
|
|
56 * the realm of the constructed set.
|
|
57 * @param elementType
|
|
58 * the element type of the constructed set.
|
|
59 * @param comparer
|
|
60 * the {@link IElementComparer} used to compare elements.
|
|
61 */
|
|
62 public this(Realm realm, Object elementType,
|
|
63 IElementComparer comparer) {
|
|
64 super(realm);
|
|
65
|
85
|
66 Assert.isNotNull(cast(Object)comparer);
|
78
|
67 this.wrappedSet = new ViewerElementSet(comparer);
|
|
68 this.elementType = elementType;
|
|
69 this.comparer = comparer;
|
|
70 }
|
|
71
|
|
72 protected Set getWrappedSet() {
|
|
73 return wrappedSet;
|
|
74 }
|
|
75
|
|
76 public Object getElementType() {
|
|
77 return elementType;
|
|
78 }
|
|
79
|
|
80 public Iterator iterator() {
|
|
81 getterCalled();
|
|
82 final Iterator wrappedIterator = wrappedSet.iterator();
|
|
83 return new class() Iterator {
|
|
84 Object last;
|
|
85
|
|
86 public bool hasNext() {
|
|
87 getterCalled();
|
|
88 return wrappedIterator.hasNext();
|
|
89 }
|
|
90
|
|
91 public Object next() {
|
|
92 getterCalled();
|
|
93 return last = wrappedIterator.next();
|
|
94 }
|
|
95
|
|
96 public void remove() {
|
|
97 getterCalled();
|
|
98 wrappedIterator.remove();
|
|
99 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET,
|
|
100 Collections.singleton(last)));
|
|
101 }
|
|
102 };
|
|
103 }
|
|
104
|
|
105 public bool add(Object o) {
|
|
106 getterCalled();
|
|
107 bool changed = wrappedSet.add(o);
|
|
108 if (changed)
|
|
109 fireSetChange(Diffs.createSetDiff(Collections.singleton(o),
|
|
110 Collections.EMPTY_SET));
|
|
111 return changed;
|
|
112 }
|
|
113
|
|
114 public bool addAll(Collection c) {
|
|
115 getterCalled();
|
|
116 Set additions = new ViewerElementSet(comparer);
|
|
117 for (Iterator iterator = c.iterator(); iterator.hasNext();) {
|
|
118 Object element = iterator.next();
|
|
119 if (wrappedSet.add(element))
|
|
120 additions.add(element);
|
|
121 }
|
|
122 bool changed = !additions.isEmpty();
|
|
123 if (changed)
|
|
124 fireSetChange(Diffs.createSetDiff(additions, Collections.EMPTY_SET));
|
|
125 return changed;
|
|
126 }
|
|
127
|
|
128 public bool remove(Object o) {
|
|
129 getterCalled();
|
|
130 bool changed = wrappedSet.remove(o);
|
|
131 if (changed)
|
|
132 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET,
|
|
133 Collections.singleton(o)));
|
|
134 return changed;
|
|
135 }
|
|
136
|
|
137 public bool removeAll(Collection c) {
|
|
138 getterCalled();
|
|
139 Set removals = new ViewerElementSet(comparer);
|
|
140 for (Iterator iterator = c.iterator(); iterator.hasNext();) {
|
|
141 Object element = iterator.next();
|
|
142 if (wrappedSet.remove(element))
|
|
143 removals.add(element);
|
|
144 }
|
|
145 bool changed = !removals.isEmpty();
|
|
146 if (changed)
|
|
147 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removals));
|
|
148 return changed;
|
|
149 }
|
|
150
|
|
151 public bool retainAll(Collection c) {
|
|
152 getterCalled();
|
|
153 Set removals = new ViewerElementSet(comparer);
|
|
154 Object[] toRetain = c.toArray();
|
|
155 outer: for (Iterator iterator = wrappedSet.iterator(); iterator
|
|
156 .hasNext();) {
|
|
157 Object element = iterator.next();
|
|
158 // Cannot rely on c.contains(element) because we must compare
|
|
159 // elements using IElementComparer.
|
|
160 for (int i = 0; i < toRetain.length; i++) {
|
85
|
161 if (comparer.opEquals(element, toRetain[i]))
|
78
|
162 continue outer;
|
|
163 }
|
|
164 iterator.remove();
|
|
165 removals.add(element);
|
|
166 }
|
|
167 bool changed = !removals.isEmpty();
|
|
168 if (changed)
|
|
169 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removals));
|
|
170 return changed;
|
|
171 }
|
|
172
|
|
173 public void clear() {
|
|
174 getterCalled();
|
|
175 if (!wrappedSet.isEmpty()) {
|
|
176 Set removals = wrappedSet;
|
|
177 wrappedSet = new ViewerElementSet(comparer);
|
|
178 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removals));
|
|
179 }
|
|
180 }
|
|
181
|
|
182 /**
|
|
183 * Returns an {@link IObservableSet} for holding viewer elements, using the
|
|
184 * given {@link IElementComparer} for comparisons.
|
|
185 *
|
|
186 * @param realm
|
|
187 * the realm of the returned observable
|
|
188 * @param elementType
|
|
189 * the element type of the returned set
|
|
190 * @param comparer
|
|
191 * the element comparer to use in element comparisons (may be
|
|
192 * null). If null, the returned set will compare elements
|
|
193 * according to the standard contract for {@link Set} interface
|
|
194 * contract.
|
|
195 * @return a Set for holding viewer elements, using the given
|
|
196 * {@link IElementComparer} for comparisons.
|
|
197 */
|
|
198 public static IObservableSet withComparer(Realm realm, Object elementType,
|
|
199 IElementComparer comparer) {
|
|
200 if (comparer is null)
|
|
201 return new WritableSet(realm, Collections.EMPTY_SET, elementType);
|
|
202 return new ObservableViewerElementSet(realm, elementType, comparer);
|
|
203 }
|
|
204 }
|