KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > DefaultListModel


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: DefaultListModel.java,v $
11    Revision 1.1 2004/01/27 09:05:10 bobintetley
12    ListModel and List Selection implemented. ScrollPane fix so all components
13       scrollable
14
15
16 */

17
18 package swingwtx.swing;
19
20 import java.util.Enumeration JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 public class DefaultListModel extends AbstractListModel {
24     private Vector JavaDoc v = new Vector JavaDoc();
25     public int getSize() {
26     return v.size();
27     }
28     public Object JavaDoc getElementAt(int index) {
29     return v.elementAt(index);
30     }
31     public void copyInto(Object JavaDoc anArray[]) {
32     v.copyInto(anArray);
33     }
34     public void trimToSize() {
35     v.trimToSize();
36     }
37     public void ensureCapacity(int minCapacity) {
38     v.ensureCapacity(minCapacity);
39     }
40     public void setSize(int newSize) {
41     int oldSize = v.size();
42     v.setSize(newSize);
43     if (oldSize > newSize) {
44         fireIntervalRemoved(this, newSize, oldSize-1);
45     }
46     else if (oldSize < newSize) {
47         fireIntervalAdded(this, oldSize, newSize-1);
48     }
49     }
50     public int capacity() {
51     return v.capacity();
52     }
53     public int size() {
54     return v.size();
55     }
56     public boolean isEmpty() {
57     return v.isEmpty();
58     }
59     public Enumeration JavaDoc elements() {
60     return v.elements();
61     }
62     public boolean contains(Object JavaDoc elem) {
63     return v.contains(elem);
64     }
65     public int indexOf(Object JavaDoc elem) {
66     return v.indexOf(elem);
67     }
68      public int indexOf(Object JavaDoc elem, int index) {
69     return v.indexOf(elem, index);
70     }
71     public int lastIndexOf(Object JavaDoc elem) {
72     return v.lastIndexOf(elem);
73     }
74     public int lastIndexOf(Object JavaDoc elem, int index) {
75     return v.lastIndexOf(elem, index);
76     }
77     public Object JavaDoc elementAt(int index) {
78     return v.elementAt(index);
79     }
80     public Object JavaDoc firstElement() {
81     return v.firstElement();
82     }
83     public Object JavaDoc lastElement() {
84     return v.lastElement();
85     }
86     public void setElementAt(Object JavaDoc obj, int index) {
87     v.setElementAt(obj, index);
88     fireContentsChanged(this, index, index);
89     }
90     public void removeElementAt(int index) {
91     v.removeElementAt(index);
92     fireIntervalRemoved(this, index, index);
93     }
94     public void insertElementAt(Object JavaDoc obj, int index) {
95     v.insertElementAt(obj, index);
96     fireIntervalAdded(this, index, index);
97     }
98     public void addElement(Object JavaDoc obj) {
99     int index = v.size();
100     v.addElement(obj);
101     fireIntervalAdded(this, index, index);
102     }
103     public boolean removeElement(Object JavaDoc obj) {
104     int index = indexOf(obj);
105     boolean rv = v.removeElement(obj);
106     if (index >= 0) {
107         fireIntervalRemoved(this, index, index);
108     }
109     return rv;
110     }
111     public void removeAllElements() {
112     int index1 = v.size()-1;
113     v.removeAllElements();
114     if (index1 >= 0) {
115         fireIntervalRemoved(this, 0, index1);
116     }
117     }
118     public String JavaDoc toString() {
119     return v.toString();
120     }
121     public Object JavaDoc[] toArray() {
122     Object JavaDoc[] rv = new Object JavaDoc[v.size()];
123     v.copyInto(rv);
124     return rv;
125     }
126     public Object JavaDoc get(int index) {
127     return v.elementAt(index);
128     }
129     public Object JavaDoc set(int index, Object JavaDoc element) {
130     Object JavaDoc rv = v.elementAt(index);
131     v.setElementAt(element, index);
132     fireContentsChanged(this, index, index);
133     return rv;
134     }
135     public void add(int index, Object JavaDoc element) {
136     v.insertElementAt(element, index);
137     fireIntervalAdded(this, index, index);
138     }
139     public Object JavaDoc remove(int index) {
140     Object JavaDoc rv = v.elementAt(index);
141     v.removeElementAt(index);
142     fireIntervalRemoved(this, index, index);
143     return rv;
144     }
145     public void clear() {
146     int index1 = v.size()-1;
147     v.removeAllElements();
148     if (index1 >= 0) {
149         fireIntervalRemoved(this, 0, index1);
150     }
151     }
152     public void removeRange(int fromIndex, int toIndex) {
153     for(int i = toIndex; i >= fromIndex; i--) {
154         v.removeElementAt(i);
155     }
156     fireIntervalRemoved(this, fromIndex, toIndex);
157     }
158 }
159
Popular Tags