1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.aequo;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
18
19 import javax.swing.DefaultListSelectionModel;
20 import javax.swing.ListSelectionModel;
21 import javax.swing.event.ListDataEvent;
22 import javax.swing.event.ListDataListener;
23
24
25
26
27
28
29
30
31
32
33 public class DefaultCompareModel<T, CompareEntryType extends CompareEntry<T>> implements CompareModel<T, CompareEntryType> {
34
35
36 protected List<CompareEntryType> entries;
37
38
39 protected ArrayList<ListDataListener> listeners = new ArrayList<ListDataListener>();
40
41
42 protected DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
43
44
45 protected int width;
46
47
48
49
50
51
52 public DefaultCompareModel(int width) {
53 this.width = width;
54 entries = new ArrayList<CompareEntryType>();
55 }
56
57
58
59
60
61
62 public DefaultCompareModel(List<CompareEntryType> entries) {
63 this.entries = entries;
64 if (entries.size() == 0) {
65 throw new IllegalArgumentException("Supplied list must not be empty");
66 }
67 width = entries.get(0).getData().length;
68 ensureWidth(entries);
69 }
70
71
72
73
74
75 public int getWidth() {
76 return width;
77 }
78
79
80
81
82
83
84 protected void ensureWidth(CompareEntryType entry) throws IllegalArgumentException {
85 if (entry.getData().length != width) {
86 throw new IllegalArgumentException("Not all elements are of the required width; width=" + width);
87 }
88 }
89
90
91
92
93
94
95 protected void ensureWidth(Collection<? extends CompareEntryType> entries) throws IllegalArgumentException {
96 for (CompareEntryType entry : entries) {
97 if (entry.getData().length != width) {
98 throw new IllegalArgumentException("Not all elements are of the required width; width=" + width);
99 }
100 }
101 }
102
103
104
105
106
107 public ListSelectionModel getSelectionModel() {
108 return selectionModel;
109 }
110
111
112
113
114
115
116 public List<CompareEntryType> getAllEntries() {
117 return entries;
118 }
119
120
121
122
123
124
125 protected static void notifyListeners(Collection<ListDataListener> listeners, ListDataEvent event) {
126 if (event.getType() == ListDataEvent.CONTENTS_CHANGED) {
127 for (ListDataListener listener : listeners) {
128 listener.contentsChanged(event);
129 }
130 } else if (event.getType() == ListDataEvent.INTERVAL_ADDED) {
131 for (ListDataListener listener : listeners) {
132 listener.intervalAdded(event);
133 }
134 } else {
135 for (ListDataListener listener : listeners) {
136 listener.intervalRemoved(event);
137 }
138 }
139 }
140
141
142
143
144
145
146 protected void fireConentChangedEvent(int from, int to) {
147 ListDataEvent event = new ListDataEvent(DefaultCompareModel.this, ListDataEvent.CONTENTS_CHANGED, from, to);
148 notifyListeners(listeners, event);
149 }
150
151
152
153
154
155
156 protected void fireIntervalAddedEvent(int from, int to) {
157 ListDataEvent event = new ListDataEvent(DefaultCompareModel.this, ListDataEvent.INTERVAL_ADDED, from, to);
158 notifyListeners(listeners, event);
159 }
160
161
162
163
164
165
166 protected void fireIntervalRemovedEvent(int from, int to) {
167 ListDataEvent event = new ListDataEvent(DefaultCompareModel.this, ListDataEvent.INTERVAL_REMOVED, from, to);
168 notifyListeners(listeners, event);
169 }
170
171
172
173
174
175
176 public void notifyUpdate(int from, int to) {
177 ListDataEvent event = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, from, to);
178 notifyListeners(listeners, event);
179 }
180
181
182
183
184
185 public void addListDataListener(ListDataListener l) {
186 listeners.add(l);
187 }
188
189
190
191
192
193 public void removeListDataListener(ListDataListener l) {
194 listeners.remove(l);
195 }
196
197
198
199
200
201
202 public List<T> getAsList(int column) {
203 ArrayList<T> res = new ArrayList<T>();
204 for (CompareEntryType entry : entries) {
205 T o = entry.getData()[column];
206 if (o != null) {
207 res.add(o);
208 }
209 }
210 return res;
211 }
212
213
214
215
216
217
218
219
220
221 public List<T> getAsSubList(int column, int start, int end) {
222 ArrayList<T> res = new ArrayList<T>();
223 for (int i = start; i <= end; i++) {
224 CompareEntryType entry = get(i);
225 T o = entry.getData()[column];
226 if (o != null) {
227 res.add(o);
228 }
229 }
230 return res;
231 }
232
233
234
235
236
237
238 public void addImpl(int index, CompareEntryType entry) {
239 entries.add(index, entry);
240 }
241
242
243
244
245
246
247 public boolean addImpl(CompareEntryType entry) {
248 return entries.add(entry);
249 }
250
251
252
253
254
255
256 public CompareEntryType removeImpl(int index) {
257 return entries.remove(index);
258 }
259
260
261
262
263
264
265
266
267 public boolean add(CompareEntryType entry) {
268 ensureWidth(entry);
269 int i = entries.size();
270 boolean res = addImpl(entry);
271 if (res) {
272 fireIntervalAddedEvent(i, i);
273 }
274 return res;
275 }
276
277
278
279
280
281 public CompareEntryType get(int index) {
282 return entries.get(index);
283 }
284
285
286
287
288
289
290 public CompareEntryType remove(int index) {
291 CompareEntryType res = removeImpl(index);
292 fireIntervalRemovedEvent(index, index);
293 return res;
294 }
295
296
297
298
299
300
301
302
303 public Object getElementAt(int index) {
304 return get(index);
305 }
306
307
308
309
310
311 public int getSize() {
312 return entries.size();
313 }
314
315 }