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: package javax.swing;
019:
020: import java.io.Serializable;
021: import javax.swing.event.ChangeEvent;
022: import javax.swing.event.ChangeListener;
023: import javax.swing.event.EventListenerList;
024:
025: import org.apache.harmony.x.swing.internal.nls.Messages;
026:
027: /**
028: * <p>
029: * <i>DefaultBoundedRangeModel</i>
030: * </p>
031: * <h3>Implementation Notes:</h3>
032: * <ul>
033: * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
034: * optimization, not as a guarantee of serialization compatibility.</li>
035: * </ul>
036: */
037: public class DefaultBoundedRangeModel implements BoundedRangeModel,
038: Serializable {
039: private static final long serialVersionUID = -3186121964369712936L;
040:
041: private static final int DEFAULT_MAX = 100;
042:
043: protected ChangeEvent changeEvent;
044:
045: protected EventListenerList listenerList;
046:
047: private int max;
048:
049: private int min;
050:
051: private int extent;
052:
053: private int value;
054:
055: private boolean valueIsAdjusting;
056:
057: public DefaultBoundedRangeModel() {
058: this (0, 0, 0, DEFAULT_MAX);
059: }
060:
061: public DefaultBoundedRangeModel(int value, int extent, int min,
062: int max) {
063: if (min > value || value > value + extent
064: || value + extent > max) {
065: throw new IllegalArgumentException(Messages
066: .getString("swing.07")); //$NON-NLS-1$
067: }
068: this .min = min;
069: this .max = max;
070: this .extent = extent;
071: this .value = value;
072: changeEvent = new ChangeEvent(this );
073: valueIsAdjusting = false;
074: listenerList = new EventListenerList();
075: }
076:
077: public void addChangeListener(ChangeListener listener) {
078: listenerList.add(ChangeListener.class, listener);
079: }
080:
081: protected void fireStateChanged() {
082: ChangeListener[] listeners = getChangeListeners();
083: for (int i = 0; i < listeners.length; i++) {
084: listeners[i].stateChanged(changeEvent);
085: }
086: }
087:
088: public ChangeListener[] getChangeListeners() {
089: return listenerList.getListeners(ChangeListener.class);
090: }
091:
092: public int getExtent() {
093: return extent;
094: }
095:
096: public <T extends java.util.EventListener> T[] getListeners(
097: Class<T> c) {
098: return listenerList.getListeners(c);
099: }
100:
101: public int getMaximum() {
102: return max;
103: }
104:
105: public int getMinimum() {
106: return min;
107: }
108:
109: public int getValue() {
110: return value;
111: }
112:
113: public boolean getValueIsAdjusting() {
114: return valueIsAdjusting;
115: }
116:
117: public void removeChangeListener(ChangeListener listener) {
118: listenerList.remove(ChangeListener.class, listener);
119: }
120:
121: public void setExtent(int n) {
122: int newExtent = Math.min(Math.max(n, 0), max - value);
123: if (newExtent != extent) {
124: extent = newExtent;
125: fireStateChanged();
126: }
127: }
128:
129: public void setMaximum(int n) {
130: if (max == n) {
131: return;
132: }
133: max = n;
134: min = Math.min(n, min);
135: extent = Math.min(max - min, extent);
136: value = Math.min(value, max - extent);
137: fireStateChanged();
138: }
139:
140: public void setMinimum(int n) {
141: if (n == min) {
142: return;
143: }
144: min = n;
145: max = Math.max(n, max);
146: extent = Math.min(max - min, extent);
147: value = Math.max(value, min);
148: fireStateChanged();
149: }
150:
151: public void setRangeProperties(int newValue, int newExtent,
152: int newMin, int newMax, boolean adjusting) {
153: if (newValue == value && newExtent == extent && newMin == min
154: && newMax == max && adjusting == valueIsAdjusting) {
155: return;
156: }
157: value = newValue;
158: min = Math.min(newMin, value);
159: max = Math.max(newMax, value);
160: extent = Math.min(newExtent, max - value);
161: valueIsAdjusting = adjusting;
162: fireStateChanged();
163: }
164:
165: public void setValue(int n) {
166: int newValue = Math.min(Math.max(n, min), max - extent);
167: if (newValue != value) {
168: value = newValue;
169: fireStateChanged();
170: }
171: }
172:
173: public void setValueIsAdjusting(boolean b) {
174: if (b != valueIsAdjusting) {
175: valueIsAdjusting = b;
176: fireStateChanged();
177: }
178: }
179:
180: /*
181: * The format of the string is based on 1.5 release behavior
182: * which can be revealed using the following code:
183: *
184: * Object obj = new DefaultBoundedRangeModel();
185: * System.out.println(obj.toString());
186: */
187: @Override
188: public String toString() {
189: return getClass().getName() + "[" + "value=" + value + ", "
190: + "extent=" + extent + ", " + "min=" + min + ", "
191: + "max=" + max + ", " + "adj=" + valueIsAdjusting + "]";
192: }
193: }
|