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