KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > SpinnerListModel


1 /*
2  * @(#)SpinnerListModel.java 1.11 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing;
9
10 import java.util.*;
11 import java.io.Serializable JavaDoc;
12
13
14 /**
15  * A simple implementation of <code>SpinnerModel</code> whose
16  * values are defined by an array or a <code>List</code>.
17  * For example to create a model defined by
18  * an array of the names of the days of the week:
19  * <pre>
20  * String[] days = new DateFormatSymbols().getWeekdays();
21  * SpinnerModel model = new SpinnerListModel(Arrays.asList(days).subList(1, 8));
22  * </pre>
23  * This class only stores a reference to the array or <code>List</code>
24  * so if an element of the underlying sequence changes, it's up
25  * to the application to notify the <code>ChangeListeners</code> by calling
26  * <code>fireStateChanged</code>.
27  * <p>
28  * This model inherits a <code>ChangeListener</code>.
29  * The <code>ChangeListener</code>s are notified whenever the
30  * model's <code>value</code> or <code>list</code> properties changes.
31  *
32  * @see JSpinner
33  * @see SpinnerModel
34  * @see AbstractSpinnerModel
35  * @see SpinnerNumberModel
36  * @see SpinnerDateModel
37  *
38  * @version 1.11 05/05/04
39  * @author Hans Muller
40  * @since 1.4
41  */

42 public class SpinnerListModel extends AbstractSpinnerModel JavaDoc implements Serializable JavaDoc
43 {
44     private List list;
45     private int index;
46
47
48     /**
49      * Constructs a <code>SpinnerModel</code> whose sequence of
50      * values is defined by the specified <code>List</code>.
51      * The initial value (<i>current element</i>)
52      * of the model will be <code>values.get(0)</code>.
53      * If <code>values</code> is <code>null</code> or has zero
54      * size, an <code>IllegalArugmentException</code> is thrown.
55      *
56      * @param values the sequence this model represents
57      * @throws IllegalArugmentException if <code>values</code> is
58      * <code>null</code> or zero size
59      */

60     public SpinnerListModel(List<?> values) {
61         if (values == null || values.size() == 0) {
62             throw new IllegalArgumentException JavaDoc("SpinnerListModel(List) expects non-null non-empty List");
63         }
64     this.list = values;
65     this.index = 0;
66     }
67
68
69     /**
70      * Constructs a <code>SpinnerModel</code> whose sequence of values
71      * is defined by the specified array. The initial value of the model
72      * will be <code>values[0]</code>. If <code>values</code> is
73      * <code>null</code> or has zero length, an
74      * <code>IllegalArugmentException</code> is thrown.
75      *
76      * @param values the sequence this model represents
77      * @throws IllegalArugmentException if <code>values</code> is
78      * <code>null</code> or zero length
79      */

80     public SpinnerListModel(Object JavaDoc[] values) {
81         if (values == null || values.length == 0) {
82             throw new IllegalArgumentException JavaDoc("SpinnerListModel(Object[]) expects non-null non-empty Object[]");
83         }
84     this.list = Arrays.asList(values);
85         this.index = 0;
86     }
87
88
89     /**
90      * Constructs an effectively empty <code>SpinnerListModel</code>.
91      * The model's list will contain a single
92      * <code>"empty"</code> string element.
93      */

94     public SpinnerListModel() {
95     this(new Object JavaDoc[]{"empty"});
96     }
97
98
99     /**
100      * Returns the <code>List</code> that defines the sequence for this model.
101      *
102      * @return the value of the <code>list</code> property
103      * @see #setList
104      */

105     public List<?> getList() {
106     return list;
107     }
108
109
110     /**
111      * Changes the list that defines this sequence and resets the index
112      * of the models <code>value</code> to zero. Note that <code>list</code>
113      * is not copied, the model just stores a reference to it.
114      * <p>
115      * This method fires a <code>ChangeEvent</code> if <code>list</code> is
116      * not equal to the current list.
117      *
118      * @param list the sequence that this model represents
119      * @throws IllegalArgumentException if <code>list</code> is
120      * <code>null</code> or zero length
121      * @see #getList
122      */

123     public void setList(List<?> list) {
124     if ((list == null) || (list.size() == 0)) {
125         throw new IllegalArgumentException JavaDoc("invalid list");
126     }
127     if (!list.equals(this.list)) {
128         this.list = list;
129         index = 0;
130         fireStateChanged();
131     }
132     }
133
134
135     /**
136      * Returns the current element of the sequence.
137      *
138      * @return the <code>value</code> property
139      * @see SpinnerModel#getValue
140      * @see #setValue
141      */

142     public Object JavaDoc getValue() {
143     return list.get(index);
144     }
145
146
147     /**
148      * Changes the current element of the sequence and notifies
149      * <code>ChangeListeners</code>. If the specified
150      * value is not equal to an element of the underlying sequence
151      * then an <code>IllegalArgumentException</code> is thrown.
152      * In the following example the <code>setValue</code> call
153      * would cause an exception to be thrown:
154      * <pre>
155      * String[] values = {"one", "two", "free", "four"};
156      * SpinnerModel model = new SpinnerListModel(values);
157      * model.setValue("TWO");
158      * </pre>
159      *
160      * @param elt the sequence element that will be model's current value
161      * @throws IllegalArgumentException if the specified value isn't allowed
162      * @see SpinnerModel#setValue
163      * @see #getValue
164      */

165     public void setValue(Object JavaDoc elt) {
166     int index = list.indexOf(elt);
167     if (index == -1) {
168         throw new IllegalArgumentException JavaDoc("invalid sequence element");
169     }
170     else if (index != this.index) {
171         this.index = index;
172         fireStateChanged();
173     }
174     }
175
176
177     /**
178      * Returns the next legal value of the underlying sequence or
179      * <code>null</code> if value is already the last element.
180      *
181      * @return the next legal value of the underlying sequence or
182      * <code>null</code> if value is already the last element
183      * @see SpinnerModel#getNextValue
184      * @see #getPreviousValue
185      */

186     public Object JavaDoc getNextValue() {
187     return (index >= (list.size() - 1)) ? null : list.get(index + 1);
188     }
189
190
191     /**
192      * Returns the previous element of the underlying sequence or
193      * <code>null</code> if value is already the first element.
194      *
195      * @return the previous element of the underlying sequence or
196      * <code>null</code> if value is already the first element
197      * @see SpinnerModel#getPreviousValue
198      * @see #getNextValue
199      */

200     public Object JavaDoc getPreviousValue() {
201     return (index <= 0) ? null : list.get(index - 1);
202     }
203
204
205     /**
206      * Returns the next object that starts with <code>substring</code>.
207      *
208      * @param substring the string to be matched
209      * @return the match
210      */

211     Object JavaDoc findNextMatch(String JavaDoc substring) {
212         int max = list.size();
213
214         if (max == 0) {
215             return null;
216         }
217         int counter = index;
218
219         do {
220             Object JavaDoc value = list.get(counter);
221             String JavaDoc string = value.toString();
222
223             if (string != null && string.startsWith(substring)) {
224                 return value;
225             }
226             counter = (counter + 1) % max;
227         } while (counter != index);
228         return null;
229     }
230 }
231
232
Popular Tags