KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > databinding > observable > list > AbstractObservableList


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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  * Brad Reynolds - bug 164653
11  * Brad Reynolds - bug 167204
12  *******************************************************************************/

13
14 package org.eclipse.core.databinding.observable.list;
15
16 import java.util.AbstractList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import org.eclipse.core.databinding.observable.ChangeEvent;
21 import org.eclipse.core.databinding.observable.ChangeSupport;
22 import org.eclipse.core.databinding.observable.IChangeListener;
23 import org.eclipse.core.databinding.observable.IStaleListener;
24 import org.eclipse.core.databinding.observable.ObservableTracker;
25 import org.eclipse.core.databinding.observable.Realm;
26 import org.eclipse.core.databinding.observable.StaleEvent;
27 import org.eclipse.core.runtime.Assert;
28 import org.eclipse.core.runtime.AssertionFailedException;
29
30 /**
31  * Subclasses should override at least get(int index) and size().
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 AbstractObservableList extends AbstractList JavaDoc implements
43         IObservableList {
44
45     private ChangeSupport changeSupport;
46
47     /**
48      * @param realm
49      *
50      */

51     public AbstractObservableList(Realm realm) {
52         Assert.isNotNull(realm);
53         changeSupport = new ChangeSupport(realm){
54             protected void firstListenerAdded() {
55                 AbstractObservableList.this.firstListenerAdded();
56             }
57             protected void lastListenerRemoved() {
58                 AbstractObservableList.this.lastListenerRemoved();
59             }
60         };
61     }
62
63     /**
64      *
65      */

66     public AbstractObservableList() {
67         this(Realm.getDefault());
68     }
69     
70     public boolean isStale() {
71         return false;
72     }
73
74     public synchronized void addListChangeListener(IListChangeListener listener) {
75         changeSupport.addListener(ListChangeEvent.TYPE, listener);
76     }
77
78     public synchronized void removeListChangeListener(IListChangeListener listener) {
79         changeSupport.removeListener(ListChangeEvent.TYPE, listener);
80     }
81
82     protected void fireListChange(ListDiff diff) {
83         // fire general change event first
84
fireChange();
85         changeSupport.fireEvent(new ListChangeEvent(this, diff));
86     }
87
88     public synchronized void addChangeListener(IChangeListener listener) {
89         changeSupport.addChangeListener(listener);
90     }
91
92     public synchronized void removeChangeListener(IChangeListener listener) {
93         changeSupport.removeChangeListener(listener);
94     }
95
96     public synchronized void addStaleListener(IStaleListener listener) {
97         changeSupport.addStaleListener(listener);
98     }
99
100     public synchronized void removeStaleListener(IStaleListener listener) {
101         changeSupport.removeStaleListener(listener);
102     }
103
104     /**
105      * Fires change event. Must be invoked from the current realm.
106      */

107     protected void fireChange() {
108         checkRealm();
109         changeSupport.fireEvent(new ChangeEvent(this));
110     }
111
112     /**
113      * Fires stale event. Must be invoked from the current realm.
114      */

115     protected void fireStale() {
116         checkRealm();
117         changeSupport.fireEvent(new StaleEvent(this));
118     }
119
120     /**
121      *
122      */

123     protected void firstListenerAdded() {
124     }
125
126     /**
127      *
128      */

129     protected void lastListenerRemoved() {
130     }
131
132     /**
133      *
134      */

135     public synchronized void dispose() {
136         changeSupport = null;
137         lastListenerRemoved();
138     }
139
140     public final int size() {
141         getterCalled();
142         return doGetSize();
143     }
144
145     /**
146      * @return the size
147      */

148     protected abstract int doGetSize();
149
150     /**
151      *
152      */

153     private void getterCalled() {
154         ObservableTracker.getterCalled(this);
155     }
156
157     public boolean isEmpty() {
158         getterCalled();
159         return super.isEmpty();
160     }
161
162     public boolean contains(Object JavaDoc o) {
163         getterCalled();
164         return super.contains(o);
165     }
166
167     public Iterator JavaDoc iterator() {
168         getterCalled();
169         final Iterator JavaDoc wrappedIterator = super.iterator();
170         return new Iterator JavaDoc() {
171             public void remove() {
172                 wrappedIterator.remove();
173             }
174
175             public boolean hasNext() {
176                 return wrappedIterator.hasNext();
177             }
178
179             public Object JavaDoc next() {
180                 return wrappedIterator.next();
181             }
182         };
183     }
184
185     public Object JavaDoc[] toArray() {
186         getterCalled();
187         return super.toArray();
188     }
189
190     public Object JavaDoc[] toArray(Object JavaDoc a[]) {
191         getterCalled();
192         return super.toArray(a);
193     }
194
195     // Modification Operations
196

197     public boolean add(Object JavaDoc o) {
198         getterCalled();
199         return super.add(o);
200     }
201
202     public boolean remove(Object JavaDoc o) {
203         getterCalled();
204         return super.remove(o);
205     }
206
207     // Bulk Modification Operations
208

209     public boolean containsAll(Collection JavaDoc c) {
210         getterCalled();
211         return super.containsAll(c);
212     }
213
214     public boolean addAll(Collection JavaDoc c) {
215         getterCalled();
216         return super.addAll(c);
217     }
218
219     public boolean addAll(int index, Collection JavaDoc c) {
220         getterCalled();
221         return super.addAll(c);
222     }
223
224     public boolean removeAll(Collection JavaDoc c) {
225         getterCalled();
226         return super.removeAll(c);
227     }
228
229     public boolean retainAll(Collection JavaDoc c) {
230         getterCalled();
231         return super.retainAll(c);
232     }
233
234     // Comparison and hashing
235

236     public boolean equals(Object JavaDoc o) {
237         getterCalled();
238         return super.equals(o);
239     }
240
241     public int hashCode() {
242         getterCalled();
243         return super.hashCode();
244     }
245
246     public int indexOf(Object JavaDoc o) {
247         getterCalled();
248         return super.indexOf(o);
249     }
250
251     public int lastIndexOf(Object JavaDoc o) {
252         getterCalled();
253         return super.lastIndexOf(o);
254     }
255
256     public Realm getRealm() {
257         return changeSupport.getRealm();
258     }
259     
260     /**
261      * Asserts that the realm is the current realm.
262      *
263      * @see Realm#isCurrent()
264      * @throws AssertionFailedException
265      * if the realm is not the current realm
266      */

267     protected void checkRealm() {
268         Assert.isTrue(getRealm().isCurrent());
269     }
270 }
271
Popular Tags