01: package jimm.datavision.gui;
02:
03: import javax.swing.AbstractListModel;
04: import java.util.ArrayList;
05: import java.util.Iterator;
06:
07: /**
08: * Implements a sorted list model suitable for use with a <code>JList</code>.
09: *
10: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
11: */
12: public class SortedListModel extends AbstractListModel {
13:
14: ArrayList list;
15:
16: public SortedListModel() {
17: list = new ArrayList();
18: }
19:
20: public void add(Comparable obj) {
21: int size = list.size();
22: if (size == 0) {
23: list.add(obj);
24: fireIntervalAdded(this , 0, 0);
25: return;
26: }
27:
28: // Add in list, sorted
29: for (int i = 0; i < list.size(); ++i) {
30: if (obj.compareTo(list.get(i)) < 0) {
31: list.add(i, obj);
32: fireIntervalAdded(this , i, i);
33: return;
34: }
35: }
36:
37: list.add(obj);
38: fireIntervalAdded(this , size, size);
39: }
40:
41: public void remove(int index) {
42: list.remove(index);
43: fireIntervalRemoved(this , index, index);
44: }
45:
46: public void removeAllElements() {
47: int size = list.size();
48: if (size > 0) {
49: list.clear();
50: fireIntervalRemoved(this , 0, size - 1);
51: }
52: }
53:
54: public Iterator iterator() {
55: return list.iterator();
56: }
57:
58: public int getSize() {
59: return list.size();
60: }
61:
62: public Object getElementAt(int index) {
63: return list.get(index);
64: }
65:
66: public Object[] toArray() {
67: return list.toArray();
68: }
69:
70: }
|