KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > observable > set > ObservableSet


1 /*******************************************************************************
2  * Copyright (c) 2006 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  ******************************************************************************/

11
12 package org.eclipse.jface.internal.databinding.provisional.observable.set;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.jface.internal.databinding.provisional.observable.AbstractObservable;
21 import org.eclipse.jface.internal.databinding.provisional.observable.ObservableTracker;
22
23 /**
24  *
25  * Abstract implementation of {@link IObservableSet}.
26  *
27  * @since 1.0
28  *
29  */

30 public abstract class ObservableSet extends AbstractObservable implements
31         IObservableSet {
32
33     protected Set JavaDoc wrappedSet;
34
35     private boolean stale = false;
36
37     private Object JavaDoc setChangeListeners;
38
39     protected Object JavaDoc elementType;
40
41     protected ObservableSet(Set JavaDoc wrappedSet, Object JavaDoc elementType) {
42         this.wrappedSet = wrappedSet;
43         this.elementType = elementType;
44     }
45
46     public void addSetChangeListener(ISetChangeListener listener) {
47         if (setChangeListeners == null) {
48             boolean hadListeners = hasListeners();
49             setChangeListeners = listener;
50             if (!hadListeners) {
51                 firstListenerAdded();
52             }
53             return;
54         }
55
56         Collection JavaDoc listenerList;
57         if (setChangeListeners instanceof Collection JavaDoc) {
58             listenerList = (Collection JavaDoc) setChangeListeners;
59         } else {
60             ISetChangeListener l = (ISetChangeListener) setChangeListeners;
61             
62             listenerList = new ArrayList JavaDoc();
63             listenerList.add(l);
64             setChangeListeners = listenerList;
65         }
66
67         if (listenerList.size() > 16) {
68             HashSet JavaDoc listenerSet = new HashSet JavaDoc();
69             listenerSet.addAll(listenerList);
70             setChangeListeners = listenerList;
71         }
72
73         listenerList.add(listener);
74     }
75
76     public void removeSetChangeListener(ISetChangeListener listener) {
77
78         if (setChangeListeners == listener) {
79             setChangeListeners = null;
80             if (!hasListeners()) {
81                 lastListenerRemoved();
82             }
83             return;
84         }
85
86         if (setChangeListeners instanceof Collection JavaDoc) {
87             Collection JavaDoc listenerList = (Collection JavaDoc) setChangeListeners;
88             listenerList.remove(listener);
89             if (listenerList.size() == 0) {
90                 setChangeListeners = null;
91                 if (!hasListeners()) {
92                     lastListenerRemoved();
93                 }
94             }
95         }
96     }
97
98     /**
99      * @return
100      */

101     protected boolean hasListeners() {
102         return super.hasListeners() || setChangeListeners!=null;
103     }
104
105     protected void fireSetChange(SetDiff diff) {
106         // fire general change event first
107
super.fireChange();
108
109         if (setChangeListeners == null) {
110             return;
111         }
112         
113         if (setChangeListeners instanceof ISetChangeListener) {
114             ((ISetChangeListener) setChangeListeners).handleSetChange(this, diff);
115             return;
116         }
117         
118         Collection JavaDoc changeListenerCollection = (Collection JavaDoc) setChangeListeners;
119         
120         ISetChangeListener[] listeners = (ISetChangeListener[]) (changeListenerCollection)
121         .toArray(new ISetChangeListener[changeListenerCollection.size()]);
122         for (int i = 0; i < listeners.length; i++) {
123             listeners[i].handleSetChange(this, diff);
124         }
125     }
126     
127     public boolean contains(Object JavaDoc o) {
128         getterCalled();
129         return wrappedSet.contains(o);
130     }
131
132     public boolean containsAll(Collection JavaDoc c) {
133         getterCalled();
134         return wrappedSet.containsAll(c);
135     }
136
137     public boolean equals(Object JavaDoc o) {
138         getterCalled();
139         return wrappedSet.equals(o);
140     }
141
142     public int hashCode() {
143         getterCalled();
144         return wrappedSet.hashCode();
145     }
146
147     public boolean isEmpty() {
148         getterCalled();
149         return wrappedSet.isEmpty();
150     }
151
152     public Iterator JavaDoc iterator() {
153         final Iterator JavaDoc wrappedIterator = wrappedSet.iterator();
154         return new Iterator JavaDoc() {
155
156             public void remove() {
157                 throw new UnsupportedOperationException JavaDoc();
158             }
159
160             public boolean hasNext() {
161                 ObservableTracker.getterCalled(ObservableSet.this);
162                 return wrappedIterator.hasNext();
163             }
164
165             public Object JavaDoc next() {
166                 ObservableTracker.getterCalled(ObservableSet.this);
167                 return wrappedIterator.next();
168             }
169         };
170     }
171
172     public int size() {
173         getterCalled();
174         return wrappedSet.size();
175     }
176
177     public Object JavaDoc[] toArray() {
178         getterCalled();
179         return wrappedSet.toArray();
180     }
181
182     public Object JavaDoc[] toArray(Object JavaDoc[] a) {
183         getterCalled();
184         return wrappedSet.toArray(a);
185     }
186
187     public String JavaDoc toString() {
188         getterCalled();
189         return wrappedSet.toString();
190     }
191
192     protected void getterCalled() {
193         ObservableTracker.getterCalled(this);
194     }
195
196     public boolean add(Object JavaDoc o) {
197         throw new UnsupportedOperationException JavaDoc();
198     }
199
200     public boolean addAll(Collection JavaDoc c) {
201         throw new UnsupportedOperationException JavaDoc();
202     }
203
204     public boolean remove(Object JavaDoc o) {
205         throw new UnsupportedOperationException JavaDoc();
206     }
207
208     public boolean removeAll(Collection JavaDoc c) {
209         throw new UnsupportedOperationException JavaDoc();
210     }
211
212     public boolean retainAll(Collection JavaDoc c) {
213         throw new UnsupportedOperationException JavaDoc();
214     }
215
216     public void clear() {
217         throw new UnsupportedOperationException JavaDoc();
218     }
219
220     /**
221      * @return Returns the stale state.
222      */

223     public boolean isStale() {
224         return stale;
225     }
226
227     /**
228      * @param stale
229      * The stale state to set. This will fire a stale event if the
230      * given boolean is true and this observable set was not already
231      * stale.
232      */

233     public void setStale(boolean stale) {
234         boolean wasStale = this.stale;
235         this.stale = stale;
236         if (!wasStale && stale) {
237             fireStale();
238         }
239     }
240
241     /**
242      * @param wrappedSet The wrappedSet to set.
243      */

244     protected void setWrappedSet(Set JavaDoc wrappedSet) {
245         this.wrappedSet = wrappedSet;
246     }
247
248     protected void fireChange() {
249         throw new RuntimeException JavaDoc("fireChange should not be called, use fireSetChange() instead"); //$NON-NLS-1$
250
}
251     
252     /* (non-Javadoc)
253      * @see org.eclipse.jface.provisional.databinding.observable.AbstractObservable#dispose()
254      */

255     public void dispose() {
256         setChangeListeners = null;
257         super.dispose();
258     }
259     
260     public Object JavaDoc getElementType() {
261         return elementType;
262     }
263 }
264
Popular Tags