KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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  * Gautam Saggar - bug 169529
13  * Brad Reynolds - bug 147515
14  *******************************************************************************/

15 package org.eclipse.core.databinding.observable.list;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.eclipse.core.databinding.observable.Diffs;
23 import org.eclipse.core.databinding.observable.Realm;
24
25 /**
26  * Mutable observable list backed by an ArrayList.
27  *
28  * <p>
29  * This class is thread safe. All state accessing methods must be invoked from
30  * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
31  * listeners may be invoked from any thread.
32  * </p>
33  *
34  * @since 1.0
35  */

36 public class WritableList extends ObservableList {
37
38     /**
39      * Creates an empty writable list in the default realm with a
40      * <code>null</code> element type.
41      *
42      */

43     public WritableList() {
44         this(Realm.getDefault());
45     }
46
47     /**
48      * Creates an empty writable list with a <code>null</code> element type.
49      *
50      * @param realm
51      */

52     public WritableList(Realm realm) {
53         this(realm, new ArrayList JavaDoc(), null);
54     }
55
56     /**
57      * Construts a new instance with the default realm.
58      *
59      * @param toWrap
60      * @param elementType
61      * can be <code>null</code>
62      */

63     public WritableList(List JavaDoc toWrap, Object JavaDoc elementType) {
64         this(Realm.getDefault(), toWrap, elementType);
65     }
66
67     /**
68      * Creates a writable list containing elements of the given type, wrapping
69      * an existing client-supplied list.
70      *
71      * @param realm
72      * @param toWrap
73      * The java.utilList to wrap
74      * @param elementType
75      * can be <code>null</code>
76      */

77     public WritableList(Realm realm, List JavaDoc toWrap, Object JavaDoc elementType) {
78         super(realm, toWrap, elementType);
79     }
80
81     public Object JavaDoc set(int index, Object JavaDoc element) {
82         checkRealm();
83         Object JavaDoc oldElement = wrappedList.set(index, element);
84         fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
85                 false, oldElement), Diffs.createListDiffEntry(index, true,
86                 element)));
87         return oldElement;
88     }
89
90     public Object JavaDoc remove(int index) {
91         checkRealm();
92         Object JavaDoc oldElement = wrappedList.remove(index);
93         fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
94                 false, oldElement)));
95         return oldElement;
96     }
97
98     public boolean add(Object JavaDoc element) {
99         checkRealm();
100         boolean added = wrappedList.add(element);
101         if (added) {
102             fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
103                     wrappedList.size() - 1, true, element)));
104         }
105         return added;
106     }
107
108     public void add(int index, Object JavaDoc element) {
109         checkRealm();
110         wrappedList.add(index, element);
111         fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
112                 true, element)));
113     }
114
115     public boolean addAll(Collection JavaDoc c) {
116         checkRealm();
117         ListDiffEntry[] entries = new ListDiffEntry[c.size()];
118         int i = 0;
119         int addIndex = wrappedList.size();
120         for (Iterator JavaDoc it = c.iterator(); it.hasNext();) {
121             Object JavaDoc element = it.next();
122             entries[i++] = Diffs.createListDiffEntry(addIndex++, true, element);
123         }
124         boolean added = wrappedList.addAll(c);
125         fireListChange(Diffs.createListDiff(entries));
126         return added;
127     }
128
129     public boolean addAll(int index, Collection JavaDoc c) {
130         checkRealm();
131         ListDiffEntry[] entries = new ListDiffEntry[c.size()];
132         int i = 0;
133         int addIndex = index;
134         for (Iterator JavaDoc it = c.iterator(); it.hasNext();) {
135             Object JavaDoc element = it.next();
136             entries[i++] = Diffs.createListDiffEntry(addIndex++, true, element);
137         }
138         boolean added = wrappedList.addAll(index, c);
139         fireListChange(Diffs.createListDiff(entries));
140         return added;
141     }
142
143     public boolean remove(Object JavaDoc o) {
144         checkRealm();
145         int index = wrappedList.indexOf(o);
146         if (index == -1) {
147             return false;
148         }
149         wrappedList.remove(index);
150         fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
151                 false, o)));
152         return true;
153     }
154
155     public boolean removeAll(Collection JavaDoc c) {
156         checkRealm();
157         List JavaDoc entries = new ArrayList JavaDoc();
158         for (Iterator JavaDoc it = c.iterator(); it.hasNext();) {
159             Object JavaDoc element = it.next();
160             int removeIndex = wrappedList.indexOf(element);
161             if (removeIndex != -1) {
162                 wrappedList.remove(removeIndex);
163                 entries.add(Diffs.createListDiffEntry(removeIndex, false,
164                         element));
165             }
166         }
167         fireListChange(Diffs.createListDiff((ListDiffEntry[]) entries
168                 .toArray(new ListDiffEntry[entries.size()])));
169         return entries.size() > 0;
170     }
171
172     public boolean retainAll(Collection JavaDoc c) {
173         checkRealm();
174         List JavaDoc entries = new ArrayList JavaDoc();
175         int removeIndex = 0;
176         for (Iterator JavaDoc it = wrappedList.iterator(); it.hasNext();) {
177             Object JavaDoc element = it.next();
178             if (!c.contains(element)) {
179                 entries.add(Diffs.createListDiffEntry(removeIndex, false,
180                         element));
181                 it.remove();
182             } else {
183                 // only increment if we haven't removed the current element
184
removeIndex++;
185             }
186         }
187         fireListChange(Diffs.createListDiff((ListDiffEntry[]) entries
188                 .toArray(new ListDiffEntry[entries.size()])));
189         return entries.size() > 0;
190     }
191
192     public void clear() {
193         checkRealm();
194         List JavaDoc entries = new ArrayList JavaDoc();
195         for (Iterator JavaDoc it = wrappedList.iterator(); it.hasNext();) {
196             Object JavaDoc element = it.next();
197             // always report 0 as the remove index
198
entries.add(Diffs.createListDiffEntry(0, false, element));
199             it.remove();
200         }
201         fireListChange(Diffs.createListDiff((ListDiffEntry[]) entries
202                 .toArray(new ListDiffEntry[entries.size()])));
203     }
204
205     /**
206      * @param elementType
207      * can be <code>null</code>
208      * @return new list with the default realm.
209      */

210     public static WritableList withElementType(Object JavaDoc elementType) {
211         return new WritableList(Realm.getDefault(), new ArrayList JavaDoc(),
212                 elementType);
213     }
214 }
215
Popular Tags