78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2005, 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 * Brad Reynolds - bug 164653
|
|
11 * Brad Reynolds - bug 167204
|
|
12 * Gautam Saggar - bug 169529
|
|
13 * Brad Reynolds - bug 147515
|
|
14 * Matthew Hall - bug 208858, 213145
|
|
15 *******************************************************************************/
|
|
16 module org.eclipse.core.databinding.observable.list.WritableList;
|
81
|
17 import org.eclipse.core.databinding.observable.list.ObservableList;
|
|
18 import org.eclipse.core.databinding.observable.list.ListDiffEntry;
|
78
|
19
|
|
20 import java.lang.all;
|
|
21
|
|
22 import java.util.ArrayList;
|
|
23 import java.util.Collection;
|
|
24 import java.util.Iterator;
|
|
25 import java.util.List;
|
|
26
|
|
27 import org.eclipse.core.databinding.observable.Diffs;
|
|
28 import org.eclipse.core.databinding.observable.Realm;
|
|
29
|
|
30 /**
|
|
31 * Mutable observable list backed by an ArrayList.
|
|
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 public class WritableList : ObservableList {
|
|
42
|
|
43 /**
|
|
44 * Creates an empty writable list in the default realm with a
|
|
45 * <code>null</code> element type.
|
|
46 *
|
|
47 */
|
|
48 public this() {
|
|
49 this(Realm.getDefault());
|
|
50 }
|
|
51
|
|
52 /**
|
|
53 * Creates an empty writable list with a <code>null</code> element type.
|
|
54 *
|
|
55 * @param realm
|
|
56 */
|
|
57 public this(Realm realm) {
|
|
58 this(realm, new ArrayList(), null);
|
|
59 }
|
|
60
|
|
61 /**
|
|
62 * Constructs a new instance with the default realm.
|
|
63 *
|
|
64 * @param toWrap
|
|
65 * @param elementType
|
|
66 * can be <code>null</code>
|
|
67 */
|
|
68 public this(List toWrap, Object elementType) {
|
|
69 this(Realm.getDefault(), toWrap, elementType);
|
|
70 }
|
|
71
|
|
72 /**
|
|
73 * Creates a writable list containing elements of the given type, wrapping
|
|
74 * an existing client-supplied list.
|
|
75 *
|
|
76 * @param realm
|
|
77 * @param toWrap
|
|
78 * The java.utilList to wrap
|
|
79 * @param elementType
|
|
80 * can be <code>null</code>
|
|
81 */
|
|
82 public this(Realm realm, List toWrap, Object elementType) {
|
|
83 super(realm, toWrap, elementType);
|
|
84 }
|
|
85
|
|
86 public Object set(int index, Object element) {
|
|
87 checkRealm();
|
|
88 Object oldElement = wrappedList.set(index, element);
|
|
89 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
|
|
90 false, oldElement), Diffs.createListDiffEntry(index, true,
|
|
91 element)));
|
|
92 return oldElement;
|
|
93 }
|
|
94
|
|
95 /**
|
|
96 * @since 1.1
|
|
97 */
|
|
98 public Object move(int oldIndex, int newIndex) {
|
|
99 checkRealm();
|
|
100 int size = wrappedList.size();
|
|
101 if (oldIndex < 0 || oldIndex >= size)
|
|
102 throw new IndexOutOfBoundsException(
|
|
103 "oldIndex: " + oldIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$
|
|
104 if (newIndex < 0 || newIndex >= size)
|
|
105 throw new IndexOutOfBoundsException(
|
|
106 "newIndex: " + newIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$
|
|
107 if (oldIndex is newIndex)
|
|
108 return wrappedList.get(oldIndex);
|
|
109 Object element = wrappedList.remove(oldIndex);
|
|
110 wrappedList.add(newIndex, element);
|
|
111 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(oldIndex,
|
|
112 false, element), Diffs.createListDiffEntry(newIndex, true,
|
|
113 element)));
|
|
114 return element;
|
|
115 }
|
|
116
|
|
117 public Object remove(int index) {
|
|
118 checkRealm();
|
|
119 Object oldElement = wrappedList.remove(index);
|
|
120 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
|
|
121 false, oldElement)));
|
|
122 return oldElement;
|
|
123 }
|
|
124
|
|
125 public bool add(Object element) {
|
|
126 checkRealm();
|
|
127 bool added = wrappedList.add(element);
|
|
128 if (added) {
|
|
129 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
|
|
130 wrappedList.size() - 1, true, element)));
|
|
131 }
|
|
132 return added;
|
|
133 }
|
|
134
|
|
135 public void add(int index, Object element) {
|
|
136 checkRealm();
|
|
137 wrappedList.add(index, element);
|
|
138 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
|
|
139 true, element)));
|
|
140 }
|
|
141
|
|
142 public bool addAll(Collection c) {
|
|
143 checkRealm();
|
|
144 ListDiffEntry[] entries = new ListDiffEntry[c.size()];
|
|
145 int i = 0;
|
|
146 int addIndex = wrappedList.size();
|
|
147 for (Iterator it = c.iterator(); it.hasNext();) {
|
|
148 Object element = it.next();
|
|
149 entries[i++] = Diffs.createListDiffEntry(addIndex++, true, element);
|
|
150 }
|
|
151 bool added = wrappedList.addAll(c);
|
|
152 fireListChange(Diffs.createListDiff(entries));
|
|
153 return added;
|
|
154 }
|
|
155
|
|
156 public bool addAll(int index, Collection c) {
|
|
157 checkRealm();
|
|
158 ListDiffEntry[] entries = new ListDiffEntry[c.size()];
|
|
159 int i = 0;
|
|
160 int addIndex = index;
|
|
161 for (Iterator it = c.iterator(); it.hasNext();) {
|
|
162 Object element = it.next();
|
|
163 entries[i++] = Diffs.createListDiffEntry(addIndex++, true, element);
|
|
164 }
|
|
165 bool added = wrappedList.addAll(index, c);
|
|
166 fireListChange(Diffs.createListDiff(entries));
|
|
167 return added;
|
|
168 }
|
|
169
|
|
170 public bool remove(Object o) {
|
|
171 checkRealm();
|
|
172 int index = wrappedList.indexOf(o);
|
|
173 if (index is -1) {
|
|
174 return false;
|
|
175 }
|
|
176 wrappedList.remove(index);
|
|
177 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
|
|
178 false, o)));
|
|
179 return true;
|
|
180 }
|
|
181
|
|
182 public bool removeAll(Collection c) {
|
|
183 checkRealm();
|
|
184 List entries = new ArrayList();
|
|
185 for (Iterator it = c.iterator(); it.hasNext();) {
|
|
186 Object element = it.next();
|
|
187 int removeIndex = wrappedList.indexOf(element);
|
|
188 if (removeIndex !is -1) {
|
|
189 wrappedList.remove(removeIndex);
|
|
190 entries.add(Diffs.createListDiffEntry(removeIndex, false,
|
|
191 element));
|
|
192 }
|
|
193 }
|
|
194 if (entries.size() > 0)
|
|
195 fireListChange(Diffs.createListDiff(cast(ListDiffEntry[]) entries
|
|
196 .toArray(new ListDiffEntry[entries.size()])));
|
|
197 return entries.size() > 0;
|
|
198 }
|
|
199
|
|
200 public bool retainAll(Collection c) {
|
|
201 checkRealm();
|
|
202 List entries = new ArrayList();
|
|
203 int removeIndex = 0;
|
|
204 for (Iterator it = wrappedList.iterator(); it.hasNext();) {
|
|
205 Object element = it.next();
|
|
206 if (!c.contains(element)) {
|
|
207 entries.add(Diffs.createListDiffEntry(removeIndex, false,
|
|
208 element));
|
|
209 it.remove();
|
|
210 } else {
|
|
211 // only increment if we haven't removed the current element
|
|
212 removeIndex++;
|
|
213 }
|
|
214 }
|
|
215 if (entries.size() > 0)
|
|
216 fireListChange(Diffs.createListDiff(cast(ListDiffEntry[]) entries
|
|
217 .toArray(new ListDiffEntry[entries.size()])));
|
|
218 return entries.size() > 0;
|
|
219 }
|
|
220
|
|
221 public void clear() {
|
|
222 checkRealm();
|
|
223 List entries = new ArrayList();
|
|
224 for (Iterator it = wrappedList.iterator(); it.hasNext();) {
|
|
225 Object element = it.next();
|
|
226 // always report 0 as the remove index
|
|
227 entries.add(Diffs.createListDiffEntry(0, false, element));
|
|
228 it.remove();
|
|
229 }
|
|
230 fireListChange(Diffs.createListDiff(cast(ListDiffEntry[]) entries
|
|
231 .toArray(new ListDiffEntry[entries.size()])));
|
|
232 }
|
|
233
|
|
234 /**
|
|
235 * @param elementType
|
|
236 * can be <code>null</code>
|
|
237 * @return new list with the default realm.
|
|
238 */
|
|
239 public static WritableList withElementType(Object elementType) {
|
|
240 return new WritableList(Realm.getDefault(), new ArrayList(),
|
|
241 elementType);
|
|
242 }
|
|
243 }
|