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 * Matthew Hall - bug 208858
|
|
11 *******************************************************************************/
|
|
12 module org.eclipse.jface.internal.databinding.swt.SWTObservableList;
|
|
13
|
|
14 import java.lang.all;
|
|
15
|
|
16 import java.util.ArrayList;
|
|
17 import java.util.Arrays;
|
|
18 import java.util.Collection;
|
|
19 import java.util.List;
|
|
20
|
|
21 import org.eclipse.core.databinding.BindingException;
|
|
22 import org.eclipse.core.databinding.observable.Diffs;
|
|
23 import org.eclipse.core.databinding.observable.ObservableTracker;
|
|
24 import org.eclipse.core.databinding.observable.Realm;
|
|
25 import org.eclipse.core.databinding.observable.list.AbstractObservableList;
|
|
26
|
|
27 /**
|
|
28 * Abstract base class of CComboObservableList, ComboObservableList, and
|
|
29 * ListObservableList.
|
|
30 *
|
|
31 * @since 3.2
|
|
32 *
|
|
33 */
|
|
34 public abstract class SWTObservableList : AbstractObservableList {
|
|
35
|
|
36 /**
|
|
37 *
|
|
38 */
|
|
39 public this() {
|
|
40 super();
|
|
41 }
|
|
42
|
|
43 /**
|
|
44 * @param realm
|
|
45 */
|
|
46 public this(Realm realm) {
|
|
47 super(realm);
|
|
48 }
|
|
49
|
|
50 public void add(int index, Object element) {
|
|
51 int size = doGetSize();
|
|
52 if (index < 0 || index > size)
|
|
53 index = size;
|
|
54 String[] newItems = new String[size + 1];
|
|
55 System.arraycopy(getItems(), 0, newItems, 0, index);
|
|
56 newItems[index] = cast(String) element;
|
|
57 System.arraycopy(getItems(), index, newItems, index + 1, size - index);
|
|
58 setItems(newItems);
|
|
59 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
|
|
60 true, element)));
|
|
61 }
|
|
62
|
|
63 public int doGetSize() {
|
|
64 return getItemCount();
|
|
65 }
|
|
66
|
|
67 public Object get(int index) {
|
|
68 getterCalled();
|
|
69 return getItem(index);
|
|
70 }
|
|
71
|
|
72 public Object getElementType() {
|
|
73 return String.classinfo;
|
|
74 }
|
|
75
|
|
76 /**
|
|
77 * @param index
|
|
78 * @return the item at the given index
|
|
79 */
|
|
80 protected abstract String getItem(int index);
|
|
81
|
|
82 /**
|
|
83 * @return the item count
|
|
84 */
|
|
85 protected abstract int getItemCount();
|
|
86
|
|
87 /**
|
|
88 * @return the items
|
|
89 */
|
|
90 protected abstract String[] getItems();
|
|
91
|
|
92 private void getterCalled() {
|
|
93 ObservableTracker.getterCalled(this);
|
|
94 }
|
|
95
|
|
96 public Object remove(int index) {
|
|
97 getterCalled();
|
|
98 int size = doGetSize();
|
|
99 if (index < 0 || index > size - 1)
|
|
100 throw new BindingException(
|
|
101 "Request to remove an element out of the collection bounds"); //$NON-NLS-1$
|
|
102
|
|
103 String[] newItems = new String[size - 1];
|
|
104 String oldElement = getItem(index);
|
|
105 if (newItems.length > 0) {
|
|
106 System.arraycopy(getItems(), 0, newItems, 0, index);
|
|
107 if (size - 1 > index) {
|
|
108 System.arraycopy(getItems(), index + 1, newItems, index, size
|
|
109 - index - 1);
|
|
110 }
|
|
111 }
|
|
112 setItems(newItems);
|
|
113 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
|
|
114 false, oldElement)));
|
|
115 return oldElement;
|
|
116 }
|
|
117
|
|
118 public Object set(int index, Object element) {
|
|
119 String oldElement = getItem(index);
|
|
120 setItem(index, cast(String) element);
|
|
121 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
|
|
122 false, oldElement), Diffs.createListDiffEntry(index, true,
|
|
123 element)));
|
|
124 return oldElement;
|
|
125 }
|
|
126
|
|
127 public Object move(int oldIndex, int newIndex) {
|
|
128 checkRealm();
|
|
129 if (oldIndex is newIndex)
|
|
130 return get(oldIndex);
|
|
131 int size = doGetSize();
|
|
132 if (oldIndex < 0 || oldIndex >= size)
|
|
133 throw new IndexOutOfBoundsException(
|
|
134 "oldIndex: " + oldIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$
|
|
135 if (newIndex < 0 || newIndex >= size)
|
|
136 throw new IndexOutOfBoundsException(
|
|
137 "newIndex: " + newIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$
|
|
138
|
|
139 String[] items = getItems();
|
|
140 String[] newItems = new String[size];
|
|
141 String element = items[oldIndex];
|
|
142 if (newItems.length > 0) {
|
|
143 System.arraycopy(items, 0, newItems, 0, size);
|
|
144 if (oldIndex < newIndex) {
|
|
145 System.arraycopy(items, oldIndex + 1, newItems, oldIndex,
|
|
146 newIndex - oldIndex);
|
|
147 } else {
|
|
148 System.arraycopy(items, newIndex, newItems, newIndex + 1,
|
|
149 oldIndex - newIndex);
|
|
150 }
|
|
151 newItems[newIndex] = element;
|
|
152 }
|
|
153 setItems(newItems);
|
|
154 fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(oldIndex,
|
|
155 false, element), Diffs.createListDiffEntry(newIndex, true,
|
|
156 element)));
|
|
157 return element;
|
|
158 }
|
|
159
|
|
160 public bool removeAll(Collection c) {
|
|
161 checkRealm();
|
|
162 List oldItems = Arrays.asList(getItems());
|
|
163 List newItems = new ArrayList(oldItems);
|
|
164 bool removedAll = newItems.removeAll(c);
|
|
165 if (removedAll) {
|
|
166 setItems(cast(String[]) newItems.toArray(new String[newItems.size()]));
|
|
167 fireListChange(Diffs.computeListDiff(oldItems, newItems));
|
|
168 }
|
|
169 return removedAll;
|
|
170 }
|
|
171
|
|
172 public bool retainAll(Collection c) {
|
|
173 checkRealm();
|
|
174 List oldItems = Arrays.asList(getItems());
|
|
175 List newItems = new ArrayList(oldItems);
|
|
176 bool retainedAll = newItems.retainAll(c);
|
|
177 if (retainedAll) {
|
|
178 setItems(cast(String[]) newItems.toArray(new String[newItems.size()]));
|
|
179 fireListChange(Diffs.computeListDiff(oldItems, newItems));
|
|
180 }
|
|
181 return retainedAll;
|
|
182 }
|
|
183
|
|
184 /**
|
|
185 * @param index
|
|
186 * @param string
|
|
187 */
|
|
188 protected abstract void setItem(int index, String string);
|
|
189
|
|
190 /**
|
|
191 * @param newItems
|
|
192 */
|
|
193 protected abstract void setItems(String[] newItems);
|
|
194
|
|
195 }
|