001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Dennis Ushakov
020: * @version $Revision$
021: */package javax.swing;
022:
023: import java.io.Serializable;
024:
025: import org.apache.harmony.x.swing.internal.nls.Messages;
026:
027: public class SpinnerNumberModel extends AbstractSpinnerModel implements
028: Serializable {
029:
030: private static final Number DEFAULT_VALUE = new Integer(0);
031: private static final Number DEFAULT_STEP = new Integer(1);
032:
033: private Number value;
034: private Comparable minimum;
035: private Comparable maximum;
036: private Number stepSize;
037:
038: public SpinnerNumberModel(final Number value,
039: final Comparable minimum, final Comparable maximum,
040: final Number stepSize) {
041: if (value == null || stepSize == null) {
042: throw new IllegalArgumentException(Messages
043: .getString("swing.5E")); //$NON-NLS-1$
044: }
045: if (minimum != null && minimum.compareTo(value) > 0) {
046: throw new IllegalArgumentException(Messages
047: .getString("swing.5F")); //$NON-NLS-1$
048: }
049: if (maximum != null && maximum.compareTo(value) < 0) {
050: throw new IllegalArgumentException(Messages
051: .getString("swing.5F")); //$NON-NLS-1$
052: }
053: this .value = value;
054: this .minimum = minimum;
055: this .maximum = maximum;
056: this .stepSize = stepSize;
057: }
058:
059: public SpinnerNumberModel(final int value, final int minimum,
060: final int maximum, final int stepSize) {
061: this (new Integer(value), new Integer(minimum), new Integer(
062: maximum), new Integer(stepSize));
063: }
064:
065: public SpinnerNumberModel(final double value, final double minimum,
066: final double maximum, final double stepSize) {
067: this (new Double(value), new Double(minimum),
068: new Double(maximum), new Double(stepSize));
069: }
070:
071: public SpinnerNumberModel() {
072: this (DEFAULT_VALUE, null, null, DEFAULT_STEP);
073: }
074:
075: public void setMinimum(final Comparable minimum) {
076: if (this .minimum != minimum) {
077: this .minimum = minimum;
078: fireStateChanged();
079: }
080: }
081:
082: public Comparable getMinimum() {
083: return minimum;
084: }
085:
086: public void setMaximum(final Comparable maximum) {
087: if (this .maximum != maximum) {
088: this .maximum = maximum;
089: fireStateChanged();
090: }
091: }
092:
093: public Comparable getMaximum() {
094: return maximum;
095: }
096:
097: public void setStepSize(final Number stepSize) {
098: if (stepSize == null || !(stepSize instanceof Number)) {
099: throw new IllegalArgumentException(Messages
100: .getString("swing.60")); //$NON-NLS-1$
101: }
102: if (this .stepSize != stepSize) {
103: this .stepSize = stepSize;
104: fireStateChanged();
105: }
106: }
107:
108: public Number getStepSize() {
109: return stepSize;
110: }
111:
112: public Object getNextValue() {
113: Number result = inc(value, stepSize, 1);
114: return (maximum == null) ? result
115: : (maximum.compareTo(result) < 0) ? null : result;
116: }
117:
118: public Object getPreviousValue() {
119: Number result = inc(value, stepSize, -1);
120: return (minimum == null) ? result
121: : (minimum.compareTo(result) > 0) ? null : result;
122: }
123:
124: public Number getNumber() {
125: return value;
126: }
127:
128: public Object getValue() {
129: return getNumber();
130: }
131:
132: public void setValue(final Object value) {
133: if (!(value instanceof Number)) {
134: throw new IllegalArgumentException(Messages
135: .getString("swing.5B")); //$NON-NLS-1$
136: }
137: if (this .value != value) {
138: this .value = (Number) value;
139: fireStateChanged();
140: }
141: }
142:
143: private Number inc(final Number val, final Number step,
144: final int sgn) {
145: if (val instanceof Integer) {
146: return new Integer(val.intValue() + step.intValue() * sgn);
147: }
148: if (val instanceof Long) {
149: return new Long(val.longValue() + step.longValue() * sgn);
150: }
151: if (val instanceof Float) {
152: return new Float(val.floatValue() + step.floatValue() * sgn);
153: }
154: if (val instanceof Double) {
155: return new Double(val.doubleValue() + step.doubleValue()
156: * sgn);
157: }
158: if (val instanceof Short) {
159: return new Short((short) (val.shortValue() + step
160: .shortValue()
161: * sgn));
162: }
163: return new Byte((byte) (val.byteValue() + step.byteValue()
164: * sgn));
165: }
166:
167: }
|