1 |
/* DefaultListModel.java -- |
/* DefaultListModel.java -- |
2 |
Copyright (C) 2002 Free Software Foundation, Inc. |
Copyright (C) 2002 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
37 |
|
|
38 |
package javax.swing; |
package javax.swing; |
39 |
|
|
40 |
public class DefaultListModel extends AbstractListModel |
// Imports |
41 |
{ |
import java.util.*; |
42 |
DefaultListModel() |
|
43 |
{ |
/** |
44 |
} |
* DefaultListModel |
45 |
} |
* @author Andrew Selkirk |
46 |
|
* @version 1.0 |
47 |
|
*/ |
48 |
|
public class DefaultListModel extends AbstractListModel { |
49 |
|
|
50 |
|
//------------------------------------------------------------- |
51 |
|
// Variables -------------------------------------------------- |
52 |
|
//------------------------------------------------------------- |
53 |
|
|
54 |
|
/** |
55 |
|
* elements. Note: Sun obviously implemented the storage as a |
56 |
|
* Vector according to the similar API on this class. I choose |
57 |
|
* instead to implement the model with a proper collection object. |
58 |
|
* Is this a good choice? Probably not (ya..I know there are |
59 |
|
* sync issues by doing this) |
60 |
|
*/ |
61 |
|
private ArrayList elements = new ArrayList(); |
62 |
|
|
63 |
|
|
64 |
|
//------------------------------------------------------------- |
65 |
|
// Initialization --------------------------------------------- |
66 |
|
//------------------------------------------------------------- |
67 |
|
|
68 |
|
/** |
69 |
|
* Constructor DefaultListModel |
70 |
|
*/ |
71 |
|
public DefaultListModel() { |
72 |
|
// TODO |
73 |
|
} // DefaultListModel() |
74 |
|
|
75 |
|
|
76 |
|
//------------------------------------------------------------- |
77 |
|
// Methods ---------------------------------------------------- |
78 |
|
//------------------------------------------------------------- |
79 |
|
|
80 |
|
/** |
81 |
|
* elementAt |
82 |
|
* @param index TODO |
83 |
|
* @returns Object |
84 |
|
*/ |
85 |
|
public Object elementAt(int index) { |
86 |
|
return elements.get(index); |
87 |
|
} // elementAt() |
88 |
|
|
89 |
|
/** |
90 |
|
* toString |
91 |
|
* @returns String |
92 |
|
*/ |
93 |
|
public String toString() { |
94 |
|
return elements.toString(); |
95 |
|
} // toString() |
96 |
|
|
97 |
|
/** |
98 |
|
* indexOf |
99 |
|
* @param element TODO |
100 |
|
* @returns int |
101 |
|
*/ |
102 |
|
public int indexOf(Object element) { |
103 |
|
return elements.indexOf(element); |
104 |
|
} // indexOf() |
105 |
|
|
106 |
|
/** |
107 |
|
* indexOf |
108 |
|
* @param element TODO |
109 |
|
* @param startIndex TODO |
110 |
|
* @returns int |
111 |
|
*/ |
112 |
|
public int indexOf(Object element, int startIndex) { |
113 |
|
|
114 |
|
// Variables |
115 |
|
int index; |
116 |
|
Object test; |
117 |
|
|
118 |
|
// Process Elements |
119 |
|
for (index = startIndex; index < elements.size(); index++) { |
120 |
|
test = elements.get(index); |
121 |
|
if (test.equals(element) == true) { |
122 |
|
return index; |
123 |
|
} // if |
124 |
|
} // for |
125 |
|
return -1; |
126 |
|
|
127 |
|
} // indexOf() |
128 |
|
|
129 |
|
/** |
130 |
|
* lastIndexOf |
131 |
|
* @param element TODO |
132 |
|
* @returns int |
133 |
|
*/ |
134 |
|
public int lastIndexOf(Object element) { |
135 |
|
return elements.lastIndexOf(element); |
136 |
|
} // lastIndexOf() |
137 |
|
|
138 |
|
/** |
139 |
|
* lastIndexOf |
140 |
|
* @param element TODO |
141 |
|
* @param endIndex TODO |
142 |
|
* @returns int |
143 |
|
*/ |
144 |
|
public int lastIndexOf(Object element, int endIndex) { |
145 |
|
|
146 |
|
// Variables |
147 |
|
int index; |
148 |
|
Object test; |
149 |
|
|
150 |
|
// Process Elements |
151 |
|
for (index = endIndex; index >= 0; index--) { |
152 |
|
test = elements.get(index); |
153 |
|
if (test.equals(element) == true) { |
154 |
|
return index; |
155 |
|
} // if |
156 |
|
} // for |
157 |
|
return -1; |
158 |
|
|
159 |
|
} // lastIndexOf() |
160 |
|
|
161 |
|
/** |
162 |
|
* get |
163 |
|
* @param index TODO |
164 |
|
* @returns Object |
165 |
|
*/ |
166 |
|
public Object get(int index) { |
167 |
|
return elements.get(index); |
168 |
|
} // get() |
169 |
|
|
170 |
|
/** |
171 |
|
* set |
172 |
|
* @param index TODO |
173 |
|
* @param element TODO |
174 |
|
* @returns Object |
175 |
|
*/ |
176 |
|
public Object set(int index, Object element) { |
177 |
|
|
178 |
|
// Variables |
179 |
|
Object result; |
180 |
|
|
181 |
|
// Process Action |
182 |
|
result = elements.set(index, element); |
183 |
|
|
184 |
|
// Send event |
185 |
|
fireContentsChanged(this, index, index); |
186 |
|
|
187 |
|
return result; |
188 |
|
|
189 |
|
} // set() |
190 |
|
|
191 |
|
/** |
192 |
|
* add |
193 |
|
* @param index TODO |
194 |
|
* @param element TODO |
195 |
|
*/ |
196 |
|
public void add(int index, Object element) { |
197 |
|
|
198 |
|
// Process Action |
199 |
|
elements.add(index, element); |
200 |
|
|
201 |
|
// Send event |
202 |
|
fireContentsChanged(this, index, index); |
203 |
|
|
204 |
|
} // add() |
205 |
|
|
206 |
|
/** |
207 |
|
* addElement |
208 |
|
* @param element TODO |
209 |
|
*/ |
210 |
|
public void addElement(Object element) { |
211 |
|
|
212 |
|
// Process Action |
213 |
|
elements.add(element); |
214 |
|
|
215 |
|
// Send event |
216 |
|
fireIntervalAdded(this, elements.size(), elements.size()); |
217 |
|
|
218 |
|
} // addElement() |
219 |
|
|
220 |
|
/** |
221 |
|
* size |
222 |
|
* @returns int |
223 |
|
*/ |
224 |
|
public int size() { |
225 |
|
return elements.size(); |
226 |
|
} // size() |
227 |
|
|
228 |
|
/** |
229 |
|
* toArray |
230 |
|
* @returns Object[] |
231 |
|
*/ |
232 |
|
public Object[] toArray() { |
233 |
|
return elements.toArray(); |
234 |
|
} // toArray() |
235 |
|
|
236 |
|
/** |
237 |
|
* contains |
238 |
|
* @param element TODO |
239 |
|
* @returns boolean |
240 |
|
*/ |
241 |
|
public boolean contains(Object element) { |
242 |
|
return elements.contains(element); |
243 |
|
} // contains() |
244 |
|
|
245 |
|
/** |
246 |
|
* copyInto |
247 |
|
* @param array TODO |
248 |
|
*/ |
249 |
|
public void copyInto(Object[] array) { |
250 |
|
|
251 |
|
// Variables |
252 |
|
int index; |
253 |
|
int size; |
254 |
|
Object[] srcArray; |
255 |
|
|
256 |
|
// Initialize |
257 |
|
size = size(); |
258 |
|
srcArray = toArray(); |
259 |
|
|
260 |
|
// Process Elements |
261 |
|
for (index = 0; index < size; index++) { |
262 |
|
array[index] = srcArray[index]; |
263 |
|
} // for |
264 |
|
|
265 |
|
} // copyInto() |
266 |
|
|
267 |
|
/** |
268 |
|
* clear |
269 |
|
*/ |
270 |
|
public void clear() { |
271 |
|
|
272 |
|
// Process Action |
273 |
|
elements.clear(); |
274 |
|
|
275 |
|
// Send event |
276 |
|
fireIntervalRemoved(this, 0, elements.size()); |
277 |
|
|
278 |
|
} // clear() |
279 |
|
|
280 |
|
/** |
281 |
|
* remove |
282 |
|
* @param index TODO |
283 |
|
* @returns Object |
284 |
|
*/ |
285 |
|
public Object remove(int index) { |
286 |
|
|
287 |
|
// Variables |
288 |
|
Object result; |
289 |
|
|
290 |
|
// Process Action |
291 |
|
result = elements.remove(index); |
292 |
|
|
293 |
|
// Send event |
294 |
|
fireIntervalRemoved(this, index, index); |
295 |
|
|
296 |
|
return result; |
297 |
|
|
298 |
|
} // remove() |
299 |
|
|
300 |
|
/** |
301 |
|
* isEmpty |
302 |
|
* @returns boolean |
303 |
|
*/ |
304 |
|
public boolean isEmpty() { |
305 |
|
return elements.isEmpty(); |
306 |
|
} // isEmpty() |
307 |
|
|
308 |
|
/** |
309 |
|
* elements |
310 |
|
* @returns Enumeration |
311 |
|
*/ |
312 |
|
public Enumeration elements() { |
313 |
|
|
314 |
|
// TODO |
315 |
|
// Note: This is a pathetic implementation. If Vector |
316 |
|
// was used for storage, this wouldn't be an issue. I'll |
317 |
|
// have to implement an Enumeration inner class sometime. |
318 |
|
|
319 |
|
// Variables |
320 |
|
Vector vector; |
321 |
|
|
322 |
|
// Get Enumeration |
323 |
|
vector = new Vector(elements); |
324 |
|
return vector.elements(); |
325 |
|
|
326 |
|
} // elements() |
327 |
|
|
328 |
|
/** |
329 |
|
* trimToSize |
330 |
|
*/ |
331 |
|
public void trimToSize() { |
332 |
|
elements.trimToSize(); |
333 |
|
} // trimToSize() |
334 |
|
|
335 |
|
/** |
336 |
|
* ensureCapacity |
337 |
|
* @param size TODO |
338 |
|
*/ |
339 |
|
public void ensureCapacity(int size) { |
340 |
|
elements.ensureCapacity(size); |
341 |
|
} // ensureCapacity() |
342 |
|
|
343 |
|
/** |
344 |
|
* setSize |
345 |
|
* @param size TODO |
346 |
|
*/ |
347 |
|
public void setSize(int size) { |
348 |
|
elements.ensureCapacity(size); |
349 |
|
} // setSize() |
350 |
|
|
351 |
|
/** |
352 |
|
* capacity |
353 |
|
* @returns int |
354 |
|
*/ |
355 |
|
public int capacity() { |
356 |
|
return elements.size(); |
357 |
|
} // capacity() |
358 |
|
|
359 |
|
/** |
360 |
|
* firstElement |
361 |
|
* @returns Object |
362 |
|
*/ |
363 |
|
public Object firstElement() { |
364 |
|
|
365 |
|
// Variables |
366 |
|
Object element; |
367 |
|
|
368 |
|
try { |
369 |
|
element = elements.get(0); |
370 |
|
return element; |
371 |
|
} catch (IndexOutOfBoundsException e) { |
372 |
|
throw new NoSuchElementException(); |
373 |
|
} // try |
374 |
|
|
375 |
|
} // firstElement() |
376 |
|
|
377 |
|
/** |
378 |
|
* lastElement |
379 |
|
* @returns Object |
380 |
|
*/ |
381 |
|
public Object lastElement() { |
382 |
|
|
383 |
|
// Variables |
384 |
|
Object element; |
385 |
|
|
386 |
|
try { |
387 |
|
element = elements.get(elements.size() - 1); |
388 |
|
return element; |
389 |
|
} catch (ArrayIndexOutOfBoundsException e) { |
390 |
|
throw new NoSuchElementException(); |
391 |
|
} // try |
392 |
|
|
393 |
|
} // lastElement() |
394 |
|
|
395 |
|
/** |
396 |
|
* setElementAt |
397 |
|
* @param element TODO |
398 |
|
* @param index TODO |
399 |
|
*/ |
400 |
|
public void setElementAt(Object element, int index) { |
401 |
|
|
402 |
|
// Process Action |
403 |
|
elements.set(index, element); |
404 |
|
|
405 |
|
// Send event |
406 |
|
fireContentsChanged(this, index, index); |
407 |
|
|
408 |
|
} // setElementAt() |
409 |
|
|
410 |
|
/** |
411 |
|
* removeElementAt |
412 |
|
* @param index TODO |
413 |
|
*/ |
414 |
|
public void removeElementAt(int index) { |
415 |
|
|
416 |
|
// Process Action |
417 |
|
elements.remove(index); |
418 |
|
|
419 |
|
// Send event |
420 |
|
fireIntervalRemoved(this, index, index); |
421 |
|
|
422 |
|
} // removeElementAt() |
423 |
|
|
424 |
|
/** |
425 |
|
* insertElementAt |
426 |
|
* @param element TODO |
427 |
|
* @param index TODO |
428 |
|
*/ |
429 |
|
public void insertElementAt(Object element, int index) { |
430 |
|
|
431 |
|
// Process Action |
432 |
|
elements.add(index, element); |
433 |
|
|
434 |
|
// Send event |
435 |
|
fireIntervalRemoved(this, index, index); |
436 |
|
|
437 |
|
} // insertElementAt() |
438 |
|
|
439 |
|
/** |
440 |
|
* removeElement |
441 |
|
* @param element TODO |
442 |
|
* @returns boolean |
443 |
|
*/ |
444 |
|
public boolean removeElement(Object element) { |
445 |
|
|
446 |
|
// Variables |
447 |
|
int index; |
448 |
|
|
449 |
|
index = elements.indexOf(element); |
450 |
|
if (index != -1) { |
451 |
|
elements.remove(index); |
452 |
|
|
453 |
|
// Send event |
454 |
|
fireIntervalRemoved(this, index, index); |
455 |
|
|
456 |
|
return true; |
457 |
|
|
458 |
|
} // if |
459 |
|
|
460 |
|
return false; |
461 |
|
|
462 |
|
} // removeElement() |
463 |
|
|
464 |
|
/** |
465 |
|
* removeAllElements |
466 |
|
*/ |
467 |
|
public void removeAllElements() { |
468 |
|
|
469 |
|
// Variables |
470 |
|
int size; |
471 |
|
|
472 |
|
size = size(); |
473 |
|
|
474 |
|
if (size > 0) { |
475 |
|
|
476 |
|
// Process Action |
477 |
|
elements.clear(); |
478 |
|
|
479 |
|
// Send event |
480 |
|
fireIntervalRemoved(this, 0, size - 1); |
481 |
|
|
482 |
|
} // if |
483 |
|
|
484 |
|
} // removeAllElements() |
485 |
|
|
486 |
|
/** |
487 |
|
* removeRange |
488 |
|
* @param startIndex TODO |
489 |
|
* @param endIndex TODO |
490 |
|
*/ |
491 |
|
public void removeRange(int startIndex, int endIndex) { |
492 |
|
|
493 |
|
// Variables |
494 |
|
int index; |
495 |
|
|
496 |
|
// Check Indices |
497 |
|
if (startIndex > endIndex) { |
498 |
|
throw new IllegalArgumentException(); |
499 |
|
} // if |
500 |
|
|
501 |
|
// Process Elements |
502 |
|
for (index = endIndex; index >= startIndex; index--) { |
503 |
|
elements.remove(index); |
504 |
|
} // for |
505 |
|
|
506 |
|
// Send event |
507 |
|
fireIntervalRemoved(this, startIndex, endIndex); |
508 |
|
|
509 |
|
} // removeRange() |
510 |
|
|
511 |
|
/** |
512 |
|
* getSize |
513 |
|
* @returns int |
514 |
|
*/ |
515 |
|
public int getSize() { |
516 |
|
return elements.size(); |
517 |
|
} // getSize() |
518 |
|
|
519 |
|
/** |
520 |
|
* getElementAt |
521 |
|
* @param index TODO |
522 |
|
* @returns Object |
523 |
|
*/ |
524 |
|
public Object getElementAt(int index) { |
525 |
|
return elements.get(index); |
526 |
|
} // getElementAt() |
527 |
|
|
528 |
|
|
529 |
|
} // DefaultListModel |