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 {
|
|
44
|
|
45 protected Set wrappedSet;
|
|
46
|
|
47 private bool stale = false;
|
|
48
|
|
49 protected Object elementType;
|
|
50
|
|
51 protected this(Set wrappedSet, Object elementType) {
|
|
52 this(Realm.getDefault(), wrappedSet, elementType);
|
|
53 }
|
|
54
|
|
55 protected this(Realm realm, Set wrappedSet, Object elementType) {
|
|
56 super(realm);
|
|
57 this.wrappedSet = wrappedSet;
|
|
58 this.elementType = elementType;
|
|
59 }
|
|
60
|
|
61 public synchronized void addSetChangeListener(ISetChangeListener listener) {
|
|
62 addListener(SetChangeEvent.TYPE, listener);
|
|
63 }
|
|
64
|
|
65 public synchronized void removeSetChangeListener(ISetChangeListener listener) {
|
|
66 removeListener(SetChangeEvent.TYPE, listener);
|
|
67 }
|
|
68
|
|
69 protected void fireSetChange(SetDiff diff) {
|
|
70 // fire general change event first
|
|
71 super.fireChange();
|
|
72
|
|
73 fireEvent(new SetChangeEvent(this, diff));
|
|
74 }
|
|
75
|
|
76 public bool contains(Object o) {
|
|
77 getterCalled();
|
|
78 return wrappedSet.contains(o);
|
|
79 }
|
|
80
|
|
81 public bool containsAll(Collection c) {
|
|
82 getterCalled();
|
|
83 return wrappedSet.containsAll(c);
|
|
84 }
|
|
85
|
|
86 public override bool opEquals(Object o) {
|
|
87 getterCalled();
|
|
88 return wrappedSet.equals(o);
|
|
89 }
|
|
90
|
|
91 public int hashCode() {
|
|
92 getterCalled();
|
|
93 return wrappedSet.hashCode();
|
|
94 }
|
|
95
|
|
96 public bool isEmpty() {
|
|
97 getterCalled();
|
|
98 return wrappedSet.isEmpty();
|
|
99 }
|
|
100
|
|
101 public Iterator iterator() {
|
|
102 getterCalled();
|
|
103 final Iterator wrappedIterator = wrappedSet.iterator();
|
|
104 return new class() Iterator {
|
|
105
|
|
106 public void remove() {
|
|
107 throw new UnsupportedOperationException();
|
|
108 }
|
|
109
|
|
110 public bool hasNext() {
|
|
111 ObservableTracker.getterCalled(this.outer);
|
|
112 return wrappedIterator.hasNext();
|
|
113 }
|
|
114
|
|
115 public Object next() {
|
|
116 ObservableTracker.getterCalled(this.outer);
|
|
117 return wrappedIterator.next();
|
|
118 }
|
|
119 };
|
|
120 }
|
|
121
|
|
122 public int size() {
|
|
123 getterCalled();
|
|
124 return wrappedSet.size();
|
|
125 }
|
|
126
|
|
127 public Object[] toArray() {
|
|
128 getterCalled();
|
|
129 return wrappedSet.toArray();
|
|
130 }
|
|
131
|
|
132 public Object[] toArray(Object[] a) {
|
|
133 getterCalled();
|
|
134 return wrappedSet.toArray(a);
|
|
135 }
|
|
136
|
|
137 public String toString() {
|
|
138 getterCalled();
|
|
139 return wrappedSet.toString();
|
|
140 }
|
|
141
|
|
142 protected void getterCalled() {
|
|
143 ObservableTracker.getterCalled(this);
|
|
144 }
|
|
145
|
|
146 public bool add(Object o) {
|
|
147 throw new UnsupportedOperationException();
|
|
148 }
|
|
149
|
|
150 public bool addAll(Collection c) {
|
|
151 throw new UnsupportedOperationException();
|
|
152 }
|
|
153
|
|
154 public bool remove(Object o) {
|
|
155 throw new UnsupportedOperationException();
|
|
156 }
|
|
157
|
|
158 public bool removeAll(Collection c) {
|
|
159 throw new UnsupportedOperationException();
|
|
160 }
|
|
161
|
|
162 public bool retainAll(Collection c) {
|
|
163 throw new UnsupportedOperationException();
|
|
164 }
|
|
165
|
|
166 public void clear() {
|
|
167 throw new UnsupportedOperationException();
|
|
168 }
|
|
169
|
|
170 /**
|
|
171 * @return Returns the stale state.
|
|
172 */
|
|
173 public bool isStale() {
|
|
174 getterCalled();
|
|
175 return stale;
|
|
176 }
|
|
177
|
|
178 /**
|
|
179 * @param stale
|
|
180 * The stale state to set. This will fire a stale event if the
|
|
181 * given bool is true and this observable set was not already
|
|
182 * stale.
|
|
183 */
|
|
184 public void setStale(bool stale) {
|
|
185 checkRealm();
|
|
186 bool wasStale = this.stale;
|
|
187 this.stale = stale;
|
|
188 if (!wasStale && stale) {
|
|
189 fireStale();
|
|
190 }
|
|
191 }
|
|
192
|
|
193 /**
|
|
194 * @param wrappedSet The wrappedSet to set.
|
|
195 */
|
|
196 protected void setWrappedSet(Set wrappedSet) {
|
|
197 this.wrappedSet = wrappedSet;
|
|
198 }
|
|
199
|
|
200 protected void fireChange() {
|
|
201 throw new RuntimeException("fireChange should not be called, use fireSetChange() instead"); //$NON-NLS-1$
|
|
202 }
|
|
203
|
|
204 /* (non-Javadoc)
|
|
205 * @see org.eclipse.jface.provisional.databinding.observable.AbstractObservable#dispose()
|
|
206 */
|
|
207 public synchronized void dispose() {
|
|
208 super.dispose();
|
|
209 }
|
|
210
|
|
211 public Object getElementType() {
|
|
212 return elementType;
|
|
213 }
|
|
214 }
|