01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /**
18: * @author Dennis Ushakov
19: * @version $Revision$
20: */package javax.swing;
21:
22: import java.io.Serializable;
23: import java.util.Arrays;
24: import java.util.List;
25:
26: import org.apache.harmony.x.swing.internal.nls.Messages;
27:
28: public class SpinnerListModel extends AbstractSpinnerModel implements
29: Serializable {
30: private static final String[] DEFAULT_VALUES = { "empty" };
31: private List<?> values;
32: private int index = 0;
33:
34: public SpinnerListModel() {
35: this (Arrays.asList(DEFAULT_VALUES));
36: }
37:
38: public SpinnerListModel(final List<?> values) {
39: if (values == null || values.size() <= 0) {
40: throw new IllegalArgumentException(Messages
41: .getString("swing.5C")); //$NON-NLS-1$
42: }
43: this .values = values;
44: }
45:
46: public SpinnerListModel(final Object[] values) {
47: if (values == null || values.length <= 0) {
48: throw new IllegalArgumentException(Messages
49: .getString("swing.5D")); //$NON-NLS-1$
50: }
51: this .values = Arrays.asList(values);
52: }
53:
54: public List<?> getList() {
55: return values;
56: }
57:
58: public void setList(final List<?> list) {
59: if (list == null || list.size() <= 0) {
60: throw new IllegalArgumentException("");
61: }
62: values = list;
63: index = 0;
64: fireStateChanged();
65: }
66:
67: public Object getValue() {
68: return values.get(index);
69: }
70:
71: public void setValue(final Object obj) {
72: if (!values.contains(obj)) {
73: throw new IllegalArgumentException(Messages
74: .getString("swing.58")); //$NON-NLS-1$
75: }
76: index = values.indexOf(obj);
77: fireStateChanged();
78: }
79:
80: public Object getNextValue() {
81: return (index + 1 >= values.size()) ? null : values
82: .get(index + 1);
83: }
84:
85: public Object getPreviousValue() {
86: return (index == 0) ? null : values.get(index - 1);
87: }
88: }
|