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.Component;
024: import java.util.Dictionary;
025: import java.util.Enumeration;
026: import java.util.Hashtable;
027:
028: import javax.accessibility.Accessible;
029: import javax.accessibility.AccessibleContext;
030: import javax.accessibility.AccessibleRole;
031: import javax.accessibility.AccessibleStateSet;
032: import javax.accessibility.AccessibleValue;
033: import javax.swing.event.ChangeEvent;
034: import javax.swing.event.ChangeListener;
035: import javax.swing.event.EventListenerList;
036: import javax.swing.plaf.SliderUI;
037: import javax.swing.plaf.UIResource;
038:
039: import org.apache.harmony.x.swing.internal.nls.Messages;
040:
041: public class JSlider extends JComponent implements SwingConstants,
042: Accessible {
043: protected class AccessibleJSlider extends AccessibleJComponent
044: implements AccessibleValue {
045: public AccessibleStateSet getAccessibleStateSet() {
046: throw new UnsupportedOperationException(Messages
047: .getString("swing.27")); //$NON-NLS-1$
048: }
049:
050: public AccessibleRole getAccessibleRole() {
051: throw new UnsupportedOperationException(Messages
052: .getString("swing.27")); //$NON-NLS-1$
053: }
054:
055: public AccessibleValue getAccessibleValue() {
056: throw new UnsupportedOperationException(Messages
057: .getString("swing.27")); //$NON-NLS-1$
058: }
059:
060: public Number getCurrentAccessibleValue() {
061: throw new UnsupportedOperationException(Messages
062: .getString("swing.27")); //$NON-NLS-1$
063: }
064:
065: public boolean setCurrentAccessibleValue(final Number n) {
066: throw new UnsupportedOperationException(Messages
067: .getString("swing.27")); //$NON-NLS-1$
068: }
069:
070: public Number getMinimumAccessibleValue() {
071: throw new UnsupportedOperationException(Messages
072: .getString("swing.27")); //$NON-NLS-1$
073: }
074:
075: public Number getMaximumAccessibleValue() {
076: throw new UnsupportedOperationException(Messages
077: .getString("swing.27")); //$NON-NLS-1$
078: }
079: }
080:
081: private class LabelUIResource extends JLabel implements UIResource {
082: LabelUIResource(final String text) {
083: super (text);
084: }
085: }
086:
087: protected BoundedRangeModel sliderModel;
088: protected int majorTickSpacing;
089: protected int minorTickSpacing;
090: protected boolean snapToTicks;
091: protected int orientation;
092: protected ChangeListener changeListener;
093: protected transient ChangeEvent changeEvent;
094:
095: private static final String SLIDER_UI_ID = "SliderUI";
096:
097: private static final String LABEL_TABLE = "labelTable";
098: private static final String INVERTED = "inverted";
099: private static final String MAJOR_TICK_SPACING = "majorTickSpacing";
100: private static final String MINOR_TICK_SPACING = "minorTickSpacing";
101: private static final String SNAP_TO_TICKS = "snapToTicks";
102: private static final String PAINT_TICKS = "paintTicks";
103: private static final String PAINT_TRACK = "paintTrack";
104: private static final String ORIENTATION_PROPERTY = "orientation";
105: private static final String MODEL_PROPERTY = "model";
106: private static final String PAINT_LABELS = "paintLabels";
107:
108: private EventListenerList eventListenerList;
109:
110: private boolean paintTrack = true;
111: private boolean inverted;
112: private Dictionary labels;
113: private boolean paintTicks;
114: private boolean paintLabels;
115: private ChangeListener modelChangeHandler;
116:
117: public JSlider() {
118: this (HORIZONTAL, 0, 100, 50);
119: }
120:
121: public JSlider(final int orientation) {
122: this (orientation, 0, 100, 50);
123: }
124:
125: public JSlider(final int min, final int max) {
126: this (HORIZONTAL, min, max, (min + max) / 2);
127: }
128:
129: public JSlider(final int min, final int max, final int value) {
130: this (HORIZONTAL, min, max, value);
131: }
132:
133: public JSlider(final int orientation, final int min, final int max,
134: final int value) {
135: this (checkOrientation(orientation),
136: new DefaultBoundedRangeModel(value, 0, min, max));
137: }
138:
139: public JSlider(final BoundedRangeModel brm) {
140: this (HORIZONTAL, brm);
141: }
142:
143: private JSlider(final int orientation, final BoundedRangeModel brm) {
144: this .orientation = orientation;
145: sliderModel = brm;
146:
147: modelChangeHandler = createChangeListener();
148: sliderModel.addChangeListener(modelChangeHandler);
149:
150: eventListenerList = new EventListenerList();
151:
152: updateUI();
153: }
154:
155: private static int checkOrientation(final int orientation) {
156: if (orientation != VERTICAL && orientation != HORIZONTAL) {
157: throw new IllegalArgumentException(Messages
158: .getString("swing.28")); //$NON-NLS-1$
159: }
160: return orientation;
161: }
162:
163: public SliderUI getUI() {
164: return (SliderUI) ui;
165: }
166:
167: public void setUI(final SliderUI ui) {
168: super .setUI(ui);
169: }
170:
171: public void updateUI() {
172: setUI((SliderUI) UIManager.getUI(this ));
173: }
174:
175: public String getUIClassID() {
176: return SLIDER_UI_ID;
177: }
178:
179: protected ChangeListener createChangeListener() {
180: return new ChangeListener() {
181: public void stateChanged(final ChangeEvent e) {
182: fireStateChanged();
183: }
184: };
185: }
186:
187: public void addChangeListener(final ChangeListener l) {
188: eventListenerList.add(ChangeListener.class, l);
189: }
190:
191: public void removeChangeListener(final ChangeListener l) {
192: eventListenerList.remove(ChangeListener.class, l);
193: }
194:
195: public ChangeListener[] getChangeListeners() {
196: return (ChangeListener[]) eventListenerList
197: .getListeners(ChangeListener.class);
198: }
199:
200: protected void fireStateChanged() {
201: ChangeListener[] changeListeners = getChangeListeners();
202: ChangeEvent changeEvent = new ChangeEvent(this );
203: for (int i = 0; i < changeListeners.length; i++) {
204: changeListeners[i].stateChanged(changeEvent);
205: }
206: }
207:
208: public BoundedRangeModel getModel() {
209: return sliderModel;
210: }
211:
212: public void setModel(final BoundedRangeModel newModel) {
213: if (newModel != sliderModel) {
214: BoundedRangeModel oldValue = sliderModel;
215: if (oldValue != null) {
216: oldValue.removeChangeListener(modelChangeHandler);
217: }
218:
219: sliderModel = newModel;
220: if (sliderModel != null) {
221: sliderModel.addChangeListener(modelChangeHandler);
222: }
223:
224: firePropertyChange(MODEL_PROPERTY, oldValue, newModel);
225: }
226: }
227:
228: public int getValue() {
229: return sliderModel.getValue();
230: }
231:
232: public void setValue(final int n) {
233: sliderModel.setValue(n);
234: }
235:
236: public int getMinimum() {
237: return sliderModel.getMinimum();
238: }
239:
240: public void setMinimum(final int minimum) {
241: sliderModel.setMinimum(minimum);
242: }
243:
244: public int getMaximum() {
245: return sliderModel.getMaximum();
246: }
247:
248: public void setMaximum(final int maximum) {
249: sliderModel.setMaximum(maximum);
250: }
251:
252: public boolean getValueIsAdjusting() {
253: return sliderModel.getValueIsAdjusting();
254: }
255:
256: public void setValueIsAdjusting(final boolean b) {
257: sliderModel.setValueIsAdjusting(b);
258: }
259:
260: public int getExtent() {
261: return sliderModel.getExtent();
262: }
263:
264: public void setExtent(final int extent) {
265: sliderModel.setExtent(extent);
266: }
267:
268: public int getOrientation() {
269: return orientation;
270: }
271:
272: public void setOrientation(final int orientation) {
273: if (orientation != VERTICAL && orientation != HORIZONTAL) {
274: throw new IllegalArgumentException(Messages
275: .getString("swing.29")); //$NON-NLS-1$
276: }
277: if (orientation != this .orientation) {
278: int oldValue = this .orientation;
279: this .orientation = orientation;
280: firePropertyChange(ORIENTATION_PROPERTY, oldValue,
281: orientation);
282: }
283: }
284:
285: public Dictionary getLabelTable() {
286: return labels;
287: }
288:
289: public void setLabelTable(final Dictionary labels) {
290: if (this .labels != labels) {
291: Dictionary oldValue = this .labels;
292: this .labels = labels;
293: firePropertyChange(LABEL_TABLE, oldValue, labels);
294:
295: Enumeration keys = this .labels.keys();
296: while (keys.hasMoreElements()) {
297: Object obj = this .labels.get(keys.nextElement());
298: if (obj instanceof Component) {
299: Component label = ((Component) obj);
300: label.setSize(label.getPreferredSize());
301: }
302: }
303: }
304: }
305:
306: protected void updateLabelUIs() {
307: if (labels == null) {
308: return;
309: }
310:
311: Enumeration en = labels.keys();
312: while (en.hasMoreElements()) {
313: JLabel l = (JLabel) en.nextElement();
314: l.updateUI();
315: }
316: }
317:
318: public Hashtable createStandardLabels(final int increment) {
319: return createStandardLabels(increment, sliderModel.getMinimum());
320: }
321:
322: public Hashtable createStandardLabels(final int increment,
323: final int start) {
324: if (increment <= 0) {
325: throw new IllegalArgumentException(Messages
326: .getString("swing.2A")); //$NON-NLS-1$
327: }
328: if (start < sliderModel.getMinimum()
329: || start > sliderModel.getMaximum()) {
330: throw new IllegalArgumentException(Messages
331: .getString("swing.2B")); //$NON-NLS-1$
332: }
333:
334: Hashtable result = new Hashtable();
335: for (int i = start; i <= sliderModel.getMaximum(); i += increment) {
336: JLabel label = new LabelUIResource(Integer.toString(i));
337: result.put(new Integer(i), label);
338: }
339:
340: return result;
341: }
342:
343: public boolean getInverted() {
344: return inverted;
345: }
346:
347: public void setInverted(final boolean b) {
348: if (inverted != b) {
349: boolean oldValue = inverted;
350: inverted = b;
351: firePropertyChange(INVERTED, oldValue, b);
352: }
353: }
354:
355: public int getMajorTickSpacing() {
356: return majorTickSpacing;
357: }
358:
359: public void setMajorTickSpacing(final int n) {
360: if (majorTickSpacing != n) {
361: int oldValue = majorTickSpacing;
362: majorTickSpacing = n;
363: firePropertyChange(MAJOR_TICK_SPACING, oldValue, n);
364:
365: if (labels == null && getPaintLabels()) {
366: setLabelTable(createStandardLabels(getMajorTickSpacing()));
367: }
368: }
369: }
370:
371: public int getMinorTickSpacing() {
372: return minorTickSpacing;
373: }
374:
375: public void setMinorTickSpacing(final int n) {
376: if (minorTickSpacing != n) {
377: int oldValue = minorTickSpacing;
378: minorTickSpacing = n;
379: firePropertyChange(MINOR_TICK_SPACING, oldValue, n);
380: }
381: }
382:
383: public boolean getSnapToTicks() {
384: return snapToTicks;
385: }
386:
387: public void setSnapToTicks(final boolean b) {
388: if (snapToTicks != b) {
389: boolean oldValue = snapToTicks;
390: snapToTicks = b;
391: firePropertyChange(SNAP_TO_TICKS, oldValue, b);
392: }
393: }
394:
395: public boolean getPaintTicks() {
396: return paintTicks;
397: }
398:
399: public void setPaintTicks(final boolean b) {
400: if (paintTicks != b) {
401: boolean oldValue = paintTicks;
402: paintTicks = b;
403: firePropertyChange(PAINT_TICKS, oldValue, b);
404: }
405: }
406:
407: public boolean getPaintTrack() {
408: return paintTrack;
409: }
410:
411: public void setPaintTrack(final boolean b) {
412: if (paintTrack != b) {
413: boolean oldValue = paintTrack;
414: paintTrack = b;
415: firePropertyChange(PAINT_TRACK, oldValue, b);
416: }
417: }
418:
419: public boolean getPaintLabels() {
420: return paintLabels;
421: }
422:
423: public void setPaintLabels(final boolean b) {
424: if (paintLabels != b) {
425: boolean oldValue = paintLabels;
426: paintLabels = b;
427: firePropertyChange(PAINT_LABELS, oldValue, b);
428:
429: if (labels == null && getMajorTickSpacing() != 0) {
430: setLabelTable(createStandardLabels(getMajorTickSpacing()));
431: }
432: }
433: }
434:
435: public AccessibleContext getAccessibleContext() {
436: if (accessibleContext == null) {
437: accessibleContext = new AccessibleJSlider();
438: }
439: return accessibleContext;
440: }
441: }
|