78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006, 2008 IBM Corporation 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 * IBM Corporation - initial API and implementation
|
|
10 * Matthew Hall - bug 208332
|
|
11 *******************************************************************************/
|
|
12
|
|
13 module org.eclipse.core.databinding.observable.set.ObservableSet;
|
81
|
14 import org.eclipse.core.databinding.observable.set.ISetChangeListener;
|
|
15 import org.eclipse.core.databinding.observable.set.SetDiff;
|
|
16 import org.eclipse.core.databinding.observable.set.SetChangeEvent;
|
|
17 import org.eclipse.core.databinding.observable.set.IObservableSet;
|
78
|
18
|
|
19 import java.lang.all;
|
|
20
|
|
21 import java.util.Collection;
|
|
22 import java.util.Iterator;
|
|
23 import java.util.Set;
|
|
24
|
|
25 import org.eclipse.core.databinding.observable.AbstractObservable;
|
|
26 import org.eclipse.core.databinding.observable.ObservableTracker;
|
|
27 import org.eclipse.core.databinding.observable.Realm;
|
|
28
|
|
29 /**
|
|
30 *
|
|
31 * Abstract implementation of {@link IObservableSet}.
|
|
32 *
|
|
33 * <p>
|
|
34 * This class is thread safe. All state accessing methods must be invoked from
|
|
35 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
|
|
36 * listeners may be invoked from any thread.
|
|
37 * </p>
|
|
38 *
|
|
39 * @since 1.0
|
|
40 *
|
|
41 */
|
|
42 public abstract class ObservableSet : AbstractObservable ,
|
|
43 IObservableSet {
|
85
|
44 // DWT start: additional methods in Set
|
|
45 public bool add(String o) {
|
|
46 return add(stringcast(o));
|
|
47 }
|
|
48 public bool remove(String o) {
|
|
49 return remove(stringcast(o));
|
|
50 }
|
|
51 public bool contains(String o) {
|
|
52 return contains(stringcast(o));
|
|
53 }
|
|
54 public int opApply (int delegate(ref Object value) dg){
|
|
55 auto it = iterator();
|
|
56 while(it.hasNext()){
|
|
57 auto v = it.next();
|
|
58 int res = dg( v );
|
|
59 if( res ) return res;
|
|
60 }
|
|
61 return 0;
|
|
62 }
|
78
|
63
|
85
|
64 // DWT end: additional methods in Set
|
78
|
65 protected Set wrappedSet;
|
|
66
|
|
67 private bool stale = false;
|
|
68
|
|
69 protected Object elementType;
|
|
70
|
|
71 protected this(Set wrappedSet, Object elementType) {
|
|
72 this(Realm.getDefault(), wrappedSet, elementType);
|
|
73 }
|
|
74
|
|
75 protected this(Realm realm, Set wrappedSet, Object elementType) {
|
|
76 super(realm);
|
|
77 this.wrappedSet = wrappedSet;
|
|
78 this.elementType = elementType;
|
|
79 }
|
|
80
|
|
81 public synchronized void addSetChangeListener(ISetChangeListener listener) {
|
|
82 addListener(SetChangeEvent.TYPE, listener);
|
|
83 }
|
|
84
|
|
85 public synchronized void removeSetChangeListener(ISetChangeListener listener) {
|
|
86 removeListener(SetChangeEvent.TYPE, listener);
|
|
87 }
|
|
88
|
|
89 protected void fireSetChange(SetDiff diff) {
|
|
90 // fire general change event first
|
|
91 super.fireChange();
|
|
92
|
|
93 fireEvent(new SetChangeEvent(this, diff));
|
|
94 }
|
|
95
|
|
96 public bool contains(Object o) {
|
|
97 getterCalled();
|
|
98 return wrappedSet.contains(o);
|
|
99 }
|
|
100
|
|
101 public bool containsAll(Collection c) {
|
|
102 getterCalled();
|
|
103 return wrappedSet.containsAll(c);
|
|
104 }
|
|
105
|
85
|
106 public override equals_t opEquals(Object o) {
|
78
|
107 getterCalled();
|
85
|
108 return wrappedSet.opEquals(o);
|
78
|
109 }
|
|
110
|
85
|
111 public hash_t toHash() {
|
78
|
112 getterCalled();
|
85
|
113 return wrappedSet.toHash();
|
78
|
114 }
|
|
115
|
|
116 public bool isEmpty() {
|
|
117 getterCalled();
|
|
118 return wrappedSet.isEmpty();
|
|
119 }
|
|
120
|
|
121 public Iterator iterator() {
|
|
122 getterCalled();
|
|
123 final Iterator wrappedIterator = wrappedSet.iterator();
|
|
124 return new class() Iterator {
|
|
125
|
|
126 public void remove() {
|
|
127 throw new UnsupportedOperationException();
|
|
128 }
|
|
129
|
|
130 public bool hasNext() {
|
|
131 ObservableTracker.getterCalled(this.outer);
|
|
132 return wrappedIterator.hasNext();
|
|
133 }
|
|
134
|
|
135 public Object next() {
|
|
136 ObservableTracker.getterCalled(this.outer);
|
|
137 return wrappedIterator.next();
|
|
138 }
|
|
139 };
|
|
140 }
|
|
141
|
|
142 public int size() {
|
|
143 getterCalled();
|
|
144 return wrappedSet.size();
|
|
145 }
|
|
146
|
|
147 public Object[] toArray() {
|
|
148 getterCalled();
|
|
149 return wrappedSet.toArray();
|
|
150 }
|
|
151
|
|
152 public Object[] toArray(Object[] a) {
|
|
153 getterCalled();
|
|
154 return wrappedSet.toArray(a);
|
|
155 }
|
|
156
|
|
157 public String toString() {
|
|
158 getterCalled();
|
|
159 return wrappedSet.toString();
|
|
160 }
|
|
161
|
|
162 protected void getterCalled() {
|
|
163 ObservableTracker.getterCalled(this);
|
|
164 }
|
|
165
|
|
166 public bool add(Object o) {
|
|
167 throw new UnsupportedOperationException();
|
|
168 }
|
|
169
|
|
170 public bool addAll(Collection c) {
|
|
171 throw new UnsupportedOperationException();
|
|
172 }
|
|
173
|
|
174 public bool remove(Object o) {
|
|
175 throw new UnsupportedOperationException();
|
|
176 }
|
|
177
|
|
178 public bool removeAll(Collection c) {
|
|
179 throw new UnsupportedOperationException();
|
|
180 }
|
|
181
|
|
182 public bool retainAll(Collection c) {
|
|
183 throw new UnsupportedOperationException();
|
|
184 }
|
|
185
|
|
186 public void clear() {
|
|
187 throw new UnsupportedOperationException();
|
|
188 }
|
|
189
|
|
190 /**
|
|
191 * @return Returns the stale state.
|
|
192 */
|
|
193 public bool isStale() {
|
|
194 getterCalled();
|
|
195 return stale;
|
|
196 }
|
|
197
|
|
198 /**
|
|
199 * @param stale
|
|
200 * The stale state to set. This will fire a stale event if the
|
|
201 * given bool is true and this observable set was not already
|
|
202 * stale.
|
|
203 */
|
|
204 public void setStale(bool stale) {
|
|
205 checkRealm();
|
|
206 bool wasStale = this.stale;
|
|
207 this.stale = stale;
|
|
208 if (!wasStale && stale) {
|
|
209 fireStale();
|
|
210 }
|
|
211 }
|
|
212
|
|
213 /**
|
|
214 * @param wrappedSet The wrappedSet to set.
|
|
215 */
|
|
216 protected void setWrappedSet(Set wrappedSet) {
|
|
217 this.wrappedSet = wrappedSet;
|
|
218 }
|
|
219
|
|
220 protected void fireChange() {
|
|
221 throw new RuntimeException("fireChange should not be called, use fireSetChange() instead"); //$NON-NLS-1$
|
|
222 }
|
|
223
|
|
224 /* (non-Javadoc)
|
|
225 * @see org.eclipse.jface.provisional.databinding.observable.AbstractObservable#dispose()
|
|
226 */
|
|
227 public synchronized void dispose() {
|
|
228 super.dispose();
|
|
229 }
|
|
230
|
|
231 public Object getElementType() {
|
|
232 return elementType;
|
|
233 }
|
|
234 }
|