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 Sergey Burlak
020: * @version $Revision$
021: */package javax.swing;
022:
023: import java.awt.Adjustable;
024: import java.awt.Dimension;
025: import java.awt.event.AdjustmentEvent;
026: import java.awt.event.AdjustmentListener;
027:
028: import javax.accessibility.Accessible;
029: import javax.accessibility.AccessibleContext;
030: import javax.accessibility.AccessibleRole;
031: import javax.accessibility.AccessibleState;
032: import javax.accessibility.AccessibleStateSet;
033: import javax.accessibility.AccessibleValue;
034: import javax.swing.event.ChangeEvent;
035: import javax.swing.event.ChangeListener;
036: import javax.swing.event.EventListenerList;
037: import javax.swing.plaf.ScrollBarUI;
038:
039: import org.apache.harmony.x.swing.internal.nls.Messages;
040:
041: public class JScrollBar extends JComponent implements Adjustable,
042: Accessible {
043:
044: protected class AccessibleJScrollBar extends
045: JComponent.AccessibleJComponent implements AccessibleValue {
046: public AccessibleStateSet getAccessibleStateSet() {
047: AccessibleStateSet result = new AccessibleStateSet();
048:
049: if (isEnabled()) {
050: result.add(AccessibleState.ENABLED);
051: }
052: if (isFocusable()) {
053: result.add(AccessibleState.FOCUSABLE);
054: }
055: if (isVisible()) {
056: result.add(AccessibleState.VISIBLE);
057: }
058: if (isOpaque()) {
059: result.add(AccessibleState.OPAQUE);
060: }
061: if (getOrientation() == HORIZONTAL) {
062: result.add(AccessibleState.HORIZONTAL);
063: } else {
064: result.add(AccessibleState.VERTICAL);
065: }
066:
067: return result;
068: }
069:
070: public AccessibleRole getAccessibleRole() {
071: return AccessibleRole.SCROLL_BAR;
072: }
073:
074: public AccessibleValue getAccessibleValue() {
075: return this ;
076: }
077:
078: public Number getCurrentAccessibleValue() {
079: return new Integer(getValue());
080: }
081:
082: public boolean setCurrentAccessibleValue(final Number n) {
083: boolean result;
084: try {
085: setValue(n.intValue());
086: result = true;
087: } catch (final Exception e) {
088: result = false;
089: }
090: return result;
091: }
092:
093: public Number getMinimumAccessibleValue() {
094: return new Integer(getMinimum());
095: }
096:
097: public Number getMaximumAccessibleValue() {
098: return new Integer(getMaximum() - getVisibleAmount());
099: }
100: }
101:
102: protected BoundedRangeModel model;
103: protected int blockIncrement = 10;
104: protected int unitIncrement = 1;
105: protected int orientation = VERTICAL;
106: private static final String classUIID = "ScrollBarUI";
107:
108: private ChangeListener modelChangeHandler;
109: private static final String BLOCK_INCREMENT = "blockIncrement";
110: private static final String UNIT_INCREMENT = "unitIncrement";
111: private static final String ORIENTATION_PROPERTY = "orientation";
112: private static final String MODEL_PROPERTY = "model";
113:
114: public JScrollBar() {
115: this (VERTICAL, 0, 10, 0, 100);
116: }
117:
118: public JScrollBar(final int orientation) {
119: this (orientation, 0, 10, 0, 100);
120: }
121:
122: public JScrollBar(final int orientation, final int value,
123: final int extent, final int min, final int max) {
124: model = new DefaultBoundedRangeModel(value, extent, min, max);
125:
126: if (orientation != HORIZONTAL && orientation != VERTICAL) {
127: throw new IllegalArgumentException(Messages
128: .getString("swing.28")); //$NON-NLS-1$
129: }
130:
131: this .orientation = orientation;
132: blockIncrement = extent;
133:
134: modelChangeHandler = new ChangeListener() {
135: public void stateChanged(final ChangeEvent e) {
136: fireAdjustmentValueChanged(
137: AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
138: AdjustmentEvent.TRACK, getValue());
139: }
140: };
141:
142: model.addChangeListener(modelChangeHandler);
143: listenerList = new EventListenerList();
144:
145: updateUI();
146: }
147:
148: public void setUI(final ScrollBarUI ui) {
149: super .setUI(ui);
150: }
151:
152: public ScrollBarUI getUI() {
153: return (ScrollBarUI) ui;
154: }
155:
156: public void updateUI() {
157: setUI((ScrollBarUI) UIManager.getUI(this ));
158: }
159:
160: public String getUIClassID() {
161: return classUIID;
162: }
163:
164: public int getOrientation() {
165: return orientation;
166: }
167:
168: public void setOrientation(final int orientation) {
169: if (orientation != VERTICAL && orientation != HORIZONTAL) {
170: throw new IllegalArgumentException(Messages
171: .getString("swing.28")); //$NON-NLS-1$
172: }
173: if (orientation != this .orientation) {
174: int oldValue = this .orientation;
175: this .orientation = orientation;
176: firePropertyChange(ORIENTATION_PROPERTY, oldValue,
177: orientation);
178: }
179: }
180:
181: public BoundedRangeModel getModel() {
182: return model;
183: }
184:
185: public void setModel(final BoundedRangeModel model) {
186: if (model != this .model) {
187: BoundedRangeModel oldValue = this .model;
188: if (oldValue != null) {
189: oldValue.removeChangeListener(modelChangeHandler);
190: }
191:
192: this .model = model;
193: if (model != null) {
194: model.addChangeListener(modelChangeHandler);
195: }
196:
197: firePropertyChange(MODEL_PROPERTY, oldValue, model);
198: }
199: }
200:
201: public int getUnitIncrement(final int direction) {
202: return unitIncrement;
203: }
204:
205: public void setUnitIncrement(final int unitIncrement) {
206: if (this .unitIncrement != unitIncrement) {
207: int oldValue = this .unitIncrement;
208: this .unitIncrement = unitIncrement;
209: firePropertyChange(UNIT_INCREMENT, oldValue, unitIncrement);
210: }
211: }
212:
213: public int getBlockIncrement(final int direction) {
214: return blockIncrement;
215: }
216:
217: public void setBlockIncrement(final int blockIncrement) {
218: if (this .blockIncrement != blockIncrement) {
219: int oldValue = this .blockIncrement;
220: this .blockIncrement = blockIncrement;
221: firePropertyChange(BLOCK_INCREMENT, oldValue,
222: blockIncrement);
223: }
224: }
225:
226: public int getUnitIncrement() {
227: return unitIncrement;
228: }
229:
230: public int getBlockIncrement() {
231: return blockIncrement;
232: }
233:
234: public int getValue() {
235: return getModel().getValue();
236: }
237:
238: public void setValue(final int value) {
239: getModel().setValue(value);
240: }
241:
242: public int getVisibleAmount() {
243: return getModel().getExtent();
244: }
245:
246: public void setVisibleAmount(final int extent) {
247: getModel().setExtent(extent);
248: }
249:
250: public int getMinimum() {
251: return getModel().getMinimum();
252: }
253:
254: public void setMinimum(final int minimum) {
255: getModel().setMinimum(minimum);
256: }
257:
258: public int getMaximum() {
259: return getModel().getMaximum();
260: }
261:
262: public void setMaximum(final int maximum) {
263: getModel().setMaximum(maximum);
264: }
265:
266: public boolean getValueIsAdjusting() {
267: return getModel().getValueIsAdjusting();
268: }
269:
270: public void setValueIsAdjusting(final boolean b) {
271: getModel().setValueIsAdjusting(b);
272: }
273:
274: public void setValues(final int newValue, final int newExtent,
275: final int newMin, final int newMax) {
276: getModel().setRangeProperties(newValue, newExtent, newMin,
277: newMax, getModel().getValueIsAdjusting());
278: }
279:
280: public void addAdjustmentListener(final AdjustmentListener l) {
281: listenerList.add(AdjustmentListener.class, l);
282: }
283:
284: public void removeAdjustmentListener(final AdjustmentListener l) {
285: listenerList.remove(AdjustmentListener.class, l);
286: }
287:
288: public AdjustmentListener[] getAdjustmentListeners() {
289: return (AdjustmentListener[]) listenerList
290: .getListeners(AdjustmentListener.class);
291: }
292:
293: protected void fireAdjustmentValueChanged(final int id,
294: final int type, final int value) {
295: AdjustmentListener[] listeners = getAdjustmentListeners();
296: for (int i = 0; i < listeners.length; i++) {
297: listeners[i].adjustmentValueChanged(new AdjustmentEvent(
298: this , id, type, value));
299: }
300: }
301:
302: public void setEnabled(final boolean b) {
303: super .setEnabled(b);
304: }
305:
306: protected String paramString() {
307: return super .paramString();
308: }
309:
310: public AccessibleContext getAccessibleContext() {
311: if (accessibleContext == null) {
312: accessibleContext = new AccessibleJScrollBar();
313: }
314: return accessibleContext;
315: }
316: }
|