source: trunk/gcc/libjava/javax/swing/DefaultListModel.java@ 2446

Last change on this file since 2446 was 1389, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 10.0 KB
Line 
1/* DefaultListModel.java --
2 Copyright (C) 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38package javax.swing;
39
40// Imports
41import java.util.*;
42
43/**
44 * DefaultListModel
45 * @author Andrew Selkirk
46 * @version 1.0
47 */
48public 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 /**