01: package com.xoetrope.swing;
02:
03: import java.awt.Dimension;
04: import javax.swing.JSlider;
05: import javax.swing.event.ChangeEvent;
06: import javax.swing.event.ChangeListener;
07:
08: /**
09: * Wraps JSlider and adds some convenience methods and configuration
10: *
11: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
12: * the GNU Public License (GPL), please see license.txt for more details. If
13: * you make commercial use of this software you must purchase a commercial
14: * license from Xoetrope.</p>
15: * <p> $Revision: 1.9 $</p>
16: */
17: public class XSlider extends JSlider implements ChangeListener {
18: private final static int DEFAULT_WIDTH = 100;
19: private final static int DEFAULT_HEIGHT = 15;
20:
21: private final static int DEFAULT_MIN = 0;
22: private final static int DEFAULT_MAX = 100;
23:
24: /**
25: * Create a new slider
26: */
27: public XSlider() {
28: initialize();
29: }
30:
31: private void initialize() {
32: setPreferredSize(new Dimension(100, 25));
33:
34: setMinimum(DEFAULT_MIN);
35: setMaximum(DEFAULT_MAX);
36:
37: setPaintTicks(true);
38: setPaintLabels(true);
39: setMajorTickSpacing(50);
40: setMinorTickSpacing(10);
41:
42: addChangeListener(this );
43: }
44:
45: /**
46: * Set one or more attributes of the component.
47: * <OL>
48: * <LI>min, value=the minimum field value</LI>
49: * <LI>max, value=the maximum field value</LI>
50: * <LI>step, value=the field step size</LI>
51: * </OL>
52: * @param attribName the attribute name
53: * @param attribValue the attribute value
54: */
55: public void setAttribute(String attribName, Object attribValue) {
56: String attribNameLwr = attribName.toLowerCase();
57: String attribValueLwr = ((String) attribValue).toLowerCase();
58: if (attribNameLwr.equals("minimum"))
59: setMinimum(Integer.parseInt(attribValueLwr));
60: else if (attribNameLwr.equals("maximum"))
61: setMaximum(Integer.parseInt(attribValueLwr));
62: else if (attribNameLwr.equals("increment"))
63: setIncrement(Integer.parseInt(attribValueLwr));
64: }
65:
66: /**
67: * The state has changed
68: * @param e the state change event
69: */
70: public void stateChanged(ChangeEvent e) {
71: }
72:
73: /**
74: * Determines if the object can accept the input focus in response to user tab
75: * key presses.
76: * @return false.
77: */
78: public boolean isFocusable() {
79: return true;
80: }
81:
82: /**
83: * Sets the controls page size.
84: * @param mi Set the slider increment
85: */
86: public void setIncrement(int mi) {
87: setMajorTickSpacing(mi);
88: setSnapToTicks(true);
89: }
90:
91: /**
92: * Gets the control's page size.
93: * @return the slider increment
94: */
95: public int getIncrement() {
96: return getMajorTickSpacing();
97: }
98: }
|