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