|
@@ -0,0 +1,262 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2011 DMDirc Developers
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package com.dmdirc.util;
|
|
24
|
+
|
|
25
|
+import java.util.Collection;
|
|
26
|
+import java.util.Iterator;
|
|
27
|
+import java.util.List;
|
|
28
|
+import java.util.ListIterator;
|
|
29
|
+
|
|
30
|
+/**
|
|
31
|
+ * Decorates a {@link List} to add observable functionality.
|
|
32
|
+ *
|
|
33
|
+ * @param <T> The type of object the list contains
|
|
34
|
+ */
|
|
35
|
+public class ObservableListDecorator<T> implements ObservableList<T> {
|
|
36
|
+
|
|
37
|
+ /** The list being decorated. */
|
|
38
|
+ private final List<T> list;
|
|
39
|
+
|
|
40
|
+ /** The listeners for this list. */
|
|
41
|
+ private final ListenerList listeners = new ListenerList();
|
|
42
|
+
|
|
43
|
+ /**
|
|
44
|
+ * Creates a new {@link ObservableListDecorator} which will decorate the
|
|
45
|
+ * given list.
|
|
46
|
+ *
|
|
47
|
+ * @param list The list to be decorated
|
|
48
|
+ */
|
|
49
|
+ public ObservableListDecorator(final List<T> list) {
|
|
50
|
+ this.list = list;
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ /** {@inheritDoc} */
|
|
54
|
+ @Override
|
|
55
|
+ public void addListListener(final ListObserver listener) {
|
|
56
|
+ listeners.add(ListObserver.class, listener);
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ /** {@inheritDoc} */
|
|
60
|
+ @Override
|
|
61
|
+ public void removeListListener(final ListObserver listener) {
|
|
62
|
+ listeners.remove(ListObserver.class, listener);
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ /** {@inheritDoc} */
|
|
66
|
+ @Override
|
|
67
|
+ public int size() {
|
|
68
|
+ return list.size();
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ /** {@inheritDoc} */
|
|
72
|
+ @Override
|
|
73
|
+ public boolean isEmpty() {
|
|
74
|
+ return list.isEmpty();
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ /** {@inheritDoc} */
|
|
78
|
+ @Override
|
|
79
|
+ public boolean contains(final Object o) {
|
|
80
|
+ return list.contains(o);
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ /** {@inheritDoc} */
|
|
84
|
+ @Override
|
|
85
|
+ public Iterator<T> iterator() {
|
|
86
|
+ return list.iterator();
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ /** {@inheritDoc} */
|
|
90
|
+ @Override
|
|
91
|
+ public Object[] toArray() {
|
|
92
|
+ return list.toArray();
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ /** {@inheritDoc} */
|
|
96
|
+ @Override
|
|
97
|
+ public <T> T[] toArray(final T[] a) {
|
|
98
|
+ return list.toArray(a);
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ /** {@inheritDoc} */
|
|
102
|
+ @Override
|
|
103
|
+ public boolean add(final T e) {
|
|
104
|
+ list.add(e);
|
|
105
|
+
|
|
106
|
+ listeners.getCallable(ListObserver.class).onItemsAdded(this,
|
|
107
|
+ list.size() - 1, list.size() - 1);
|
|
108
|
+
|
|
109
|
+ return true;
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ /** {@inheritDoc} */
|
|
113
|
+ @Override
|
|
114
|
+ public boolean remove(final Object o) {
|
|
115
|
+ final int index = list.indexOf(o);
|
|
116
|
+
|
|
117
|
+ if (list.remove(o)) {
|
|
118
|
+ listeners.getCallable(ListObserver.class).onItemsRemoved(this,
|
|
119
|
+ index, index);
|
|
120
|
+
|
|
121
|
+ return true;
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ return false;
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ /** {@inheritDoc} */
|
|
128
|
+ @Override
|
|
129
|
+ public boolean containsAll(final Collection<?> c) {
|
|
130
|
+ return list.containsAll(c);
|
|
131
|
+ }
|
|
132
|
+
|
|
133
|
+ /** {@inheritDoc} */
|
|
134
|
+ @Override
|
|
135
|
+ public boolean addAll(final Collection<? extends T> c) {
|
|
136
|
+ if (list.addAll(c)) {
|
|
137
|
+ listeners.getCallable(ListObserver.class).onItemsAdded(this,
|
|
138
|
+ list.size() - c.size(), list.size() - 1);
|
|
139
|
+ return true;
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+ return false;
|
|
143
|
+ }
|
|
144
|
+
|
|
145
|
+ /** {@inheritDoc} */
|
|
146
|
+ @Override
|
|
147
|
+ public boolean addAll(final int index, final Collection<? extends T> c) {
|
|
148
|
+ if (list.addAll(index, c)) {
|
|
149
|
+ listeners.getCallable(ListObserver.class).onItemsAdded(this,
|
|
150
|
+ index, index + c.size());
|
|
151
|
+ return true;
|
|
152
|
+ }
|
|
153
|
+
|
|
154
|
+ return false;
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+ /** {@inheritDoc} */
|
|
158
|
+ @Override
|
|
159
|
+ public boolean removeAll(final Collection<?> c) {
|
|
160
|
+ final int length = list.size();
|
|
161
|
+
|
|
162
|
+ if (list.removeAll(c)) {
|
|
163
|
+ listeners.getCallable(ListObserver.class).onItemsChanged(this, 0,
|
|
164
|
+ length - 1);
|
|
165
|
+
|
|
166
|
+ return true;
|
|
167
|
+ }
|
|
168
|
+
|
|
169
|
+ return false;
|
|
170
|
+ }
|
|
171
|
+
|
|
172
|
+ /** {@inheritDoc} */
|
|
173
|
+ @Override
|
|
174
|
+ public boolean retainAll(final Collection<?> c) {
|
|
175
|
+ final int length = list.size();
|
|
176
|
+
|
|
177
|
+ if (list.retainAll(c)) {
|
|
178
|
+ listeners.getCallable(ListObserver.class).onItemsChanged(this, 0,
|
|
179
|
+ length - 1);
|
|
180
|
+
|
|
181
|
+ return true;
|
|
182
|
+ }
|
|
183
|
+
|
|
184
|
+ return false;
|
|
185
|
+ }
|
|
186
|
+
|
|
187
|
+ /** {@inheritDoc} */
|
|
188
|
+ @Override
|
|
189
|
+ public void clear() {
|
|
190
|
+ final int length = list.size();
|
|
191
|
+
|
|
192
|
+ list.clear();
|
|
193
|
+
|
|
194
|
+ listeners.getCallable(ListObserver.class).onItemsRemoved(this, 0,
|
|
195
|
+ length - 1);
|
|
196
|
+ }
|
|
197
|
+
|
|
198
|
+ /** {@inheritDoc} */
|
|
199
|
+ @Override
|
|
200
|
+ public T get(final int index) {
|
|
201
|
+ return list.get(index);
|
|
202
|
+ }
|
|
203
|
+
|
|
204
|
+ /** {@inheritDoc} */
|
|
205
|
+ @Override
|
|
206
|
+ public T set(final int index, final T element) {
|
|
207
|
+ final T res = list.set(index, element);
|
|
208
|
+
|
|
209
|
+ listeners.getCallable(ListObserver.class).onItemsChanged(this, index, index);
|
|
210
|
+
|
|
211
|
+ return res;
|
|
212
|
+ }
|
|
213
|
+
|
|
214
|
+ /** {@inheritDoc} */
|
|
215
|
+ @Override
|
|
216
|
+ public void add(final int index, final T element) {
|
|
217
|
+ list.add(index, element);
|
|
218
|
+
|
|
219
|
+ listeners.getCallable(ListObserver.class).onItemsAdded(this, index, index);
|
|
220
|
+ }
|
|
221
|
+
|
|
222
|
+ /** {@inheritDoc} */
|
|
223
|
+ @Override
|
|
224
|
+ public T remove(final int index) {
|
|
225
|
+ final T res = list.remove(index);
|
|
226
|
+
|
|
227
|
+ listeners.getCallable(ListObserver.class).onItemsRemoved(this, index, index);
|
|
228
|
+
|
|
229
|
+ return res;
|
|
230
|
+ }
|
|
231
|
+
|
|
232
|
+ /** {@inheritDoc} */
|
|
233
|
+ @Override
|
|
234
|
+ public int indexOf(final Object o) {
|
|
235
|
+ return list.indexOf(o);
|
|
236
|
+ }
|
|
237
|
+
|
|
238
|
+ /** {@inheritDoc} */
|
|
239
|
+ @Override
|
|
240
|
+ public int lastIndexOf(final Object o) {
|
|
241
|
+ return list.lastIndexOf(o);
|
|
242
|
+ }
|
|
243
|
+
|
|
244
|
+ /** {@inheritDoc} */
|
|
245
|
+ @Override
|
|
246
|
+ public ListIterator<T> listIterator() {
|
|
247
|
+ return list.listIterator();
|
|
248
|
+ }
|
|
249
|
+
|
|
250
|
+ /** {@inheritDoc} */
|
|
251
|
+ @Override
|
|
252
|
+ public ListIterator<T> listIterator(final int index) {
|
|
253
|
+ return list.listIterator(index);
|
|
254
|
+ }
|
|
255
|
+
|
|
256
|
+ /** {@inheritDoc} */
|
|
257
|
+ @Override
|
|
258
|
+ public List<T> subList(final int fromIndex, final int toIndex) {
|
|
259
|
+ return list.subList(fromIndex, toIndex);
|
|
260
|
+ }
|
|
261
|
+
|
|
262
|
+}
|