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