1 7 8 package javax.swing; 9 10 import java.util.*; 11 import java.io.Serializable ; 12 13 14 42 public class SpinnerListModel extends AbstractSpinnerModel implements Serializable 43 { 44 private List list; 45 private int index; 46 47 48 60 public SpinnerListModel(List<?> values) { 61 if (values == null || values.size() == 0) { 62 throw new IllegalArgumentException ("SpinnerListModel(List) expects non-null non-empty List"); 63 } 64 this.list = values; 65 this.index = 0; 66 } 67 68 69 80 public SpinnerListModel(Object [] values) { 81 if (values == null || values.length == 0) { 82 throw new IllegalArgumentException ("SpinnerListModel(Object[]) expects non-null non-empty Object[]"); 83 } 84 this.list = Arrays.asList(values); 85 this.index = 0; 86 } 87 88 89 94 public SpinnerListModel() { 95 this(new Object []{"empty"}); 96 } 97 98 99 105 public List<?> getList() { 106 return list; 107 } 108 109 110 123 public void setList(List<?> list) { 124 if ((list == null) || (list.size() == 0)) { 125 throw new IllegalArgumentException ("invalid list"); 126 } 127 if (!list.equals(this.list)) { 128 this.list = list; 129 index = 0; 130 fireStateChanged(); 131 } 132 } 133 134 135 142 public Object getValue() { 143 return list.get(index); 144 } 145 146 147 165 public void setValue(Object elt) { 166 int index = list.indexOf(elt); 167 if (index == -1) { 168 throw new IllegalArgumentException ("invalid sequence element"); 169 } 170 else if (index != this.index) { 171 this.index = index; 172 fireStateChanged(); 173 } 174 } 175 176 177 186 public Object getNextValue() { 187 return (index >= (list.size() - 1)) ? null : list.get(index + 1); 188 } 189 190 191 200 public Object getPreviousValue() { 201 return (index <= 0) ? null : list.get(index - 1); 202 } 203 204 205 211 Object findNextMatch(String substring) { 212 int max = list.size(); 213 214 if (max == 0) { 215 return null; 216 } 217 int counter = index; 218 219 do { 220 Object value = list.get(counter); 221 String 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 |