1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
/* * @(#)SpinnerListModel.java 1.12 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.swing; import java.util.*; import java.io.Serializable; /** * A simple implementation of <code>SpinnerModel</code> whose * values are defined by an array or a <code>List</code>. * For example to create a model defined by * an array of the names of the days of the week: * <pre> * String[] days = new DateFormatSymbols().getWeekdays(); * SpinnerModel model = new SpinnerListModel(Arrays.asList(days).subList(1, 8)); * </pre> * This class only stores a reference to the array or <code>List</code> * so if an element of the underlying sequence changes, it's up * to the application to notify the <code>ChangeListeners</code> by calling * <code>fireStateChanged</code>. * <p> * This model inherits a <code>ChangeListener</code>. * The <code>ChangeListener</code>s are notified whenever the * model's <code>value</code> or <code>list</code> properties changes. * * @see JSpinner * @see SpinnerModel * @see AbstractSpinnerModel * @see SpinnerNumberModel * @see SpinnerDateModel * * @version 1.12 11/17/05 * @author Hans Muller * @since 1.4 */ public class SpinnerListModel extends AbstractSpinnerModel implements Serializable { private List list; private int index; /** * Constructs a <code>SpinnerModel</code> whose sequence of * values is defined by the specified <code>List</code>. * The initial value (<i>current element</i>) * of the model will be <code>values.get(0)</code>. * If <code>values</code> is <code>null</code> or has zero * size, an <code>IllegalArugmentException</code> is thrown. * * @param values the sequence this model represents * @throws IllegalArugmentException if <code>values</code> is * <code>null</code> or zero size */ public SpinnerListModel(List<?> values) { if (values == null || values.size() == 0) { throw new IllegalArgumentException("SpinnerListModel(List) expects non-null non-empty List"); } this.list = values; this.index = 0; } /** * Constructs a <code>SpinnerModel</code> whose sequence of values * is defined by the specified array. The initial value of the model * will be <code>values[0]</code>. If <code>values</code> is * <code>null</code> or has zero length, an * <code>IllegalArugmentException</code> is thrown. * * @param values the sequence this model represents * @throws IllegalArugmentException if <code>values</code> is * <code>null</code> or zero length */ public SpinnerListModel(Object[] values) { if (values == null || values.length == 0) { throw new IllegalArgumentException("SpinnerListModel(Object[]) expects non-null non-empty Object[]"); } this.list = Arrays.asList(values); this.index = 0; } /** * Constructs an effectively empty <code>SpinnerListModel</code>. * The model's list will contain a single * <code>"empty"</code> string element. */ public SpinnerListModel() { this(new Object[]{"empty"}); } /** * Returns the <code>List</code> that defines the sequence for this model. * * @return the value of the <code>list</code> property * @see #setList */ public List<?> getList() { return list; } /** * Changes the list that defines this sequence and resets the index * of the models <code>value</code> to zero. Note that <code>list</code> * is not copied, the model just stores a reference to it. * <p> * This method fires a <code>ChangeEvent</code> if <code>list</code> is * not equal to the current list. * * @param list the sequence that this model represents * @throws IllegalArgumentException if <code>list</code> is * <code>null</code> or zero length * @see #getList */ public void setList(List<?> list) { if ((list == null) || (list.size() == 0)) { throw new IllegalArgumentException("invalid list"); } if (!list.equals(this.list)) { this.list = list; index = 0; fireStateChanged(); } } /** * Returns the current element of the sequence. * * @return the <code>value</code> property * @see SpinnerModel#getValue * @see #setValue */ public Object getValue() { return list.get(index); } /** * Changes the current element of the sequence and notifies * <code>ChangeListeners</code>. If the specified * value is not equal to an element of the underlying sequence * then an <code>IllegalArgumentException</code> is thrown. * In the following example the <code>setValue</code> call * would cause an exception to be thrown: * <pre> * String[] values = {"one", "two", "free", "four"}; * SpinnerModel model = new SpinnerListModel(values); * model.setValue("TWO"); * </pre> * * @param elt the sequence element that will be model's current value * @throws IllegalArgumentException if the specified value isn't allowed * @see SpinnerModel#setValue * @see #getValue */ public void setValue(Object elt) { int index = list.indexOf(elt); if (index == -1) { throw new IllegalArgumentException("invalid sequence element"); } else if (index != this.index) { this.index = index; fireStateChanged(); } } /** * Returns the next legal value of the underlying sequence or * <code>null</code> if value is already the last element. * * @return the next legal value of the underlying sequence or * <code>null</code> if value is already the last element * @see SpinnerModel#getNextValue * @see #getPreviousValue */ public Object getNextValue() { return (index >= (list.size() - 1)) ? null : list.get(index + 1); } /** * Returns the previous element of the underlying sequence or * <code>null</code> if value is already the first element. * * @return the previous element of the underlying sequence or * <code>null</code> if value is already the first element * @see SpinnerModel#getPreviousValue * @see #getNextValue */ public Object getPreviousValue() { return (index <= 0) ? null : list.get(index - 1); } /** * Returns the next object that starts with <code>substring</code>. * * @param substring the string to be matched * @return the match */ Object findNextMatch(String substring) { int max = list.size(); if (max == 0) { return null; } int counter = index; do { Object value = list.get(counter); String string = value.toString(); if (string != null && string.startsWith(substring)) { return value; } counter = (counter + 1) % max; } while (counter != index); return null; } }