001: package example;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.beans.*;
006:
007: import javax.swing.*;
008:
009: /**
010: * Pick a number, any number. Well, maybe not any number.
011: */
012:
013: public class NumberChooser extends JPanel implements ActionListener {
014: protected ArrowButton up, down;
015: protected JTextField field;
016:
017: public NumberChooser() {
018: setLayout(new BorderLayout());
019: JPanel buttons = new JPanel(new GridLayout(0, 1));
020: down = new ArrowButton(ArrowButton.DOWN);
021: down.addActionListener(this );
022: add(field = new JTextField(4), BorderLayout.CENTER);
023: field.addActionListener(this );
024: up = new ArrowButton(ArrowButton.UP);
025: up.addActionListener(this );
026: buttons.add(up);
027: buttons.add(down);
028: add(buttons, BorderLayout.EAST);
029: }
030:
031: public NumberChooser(int min, int max, int value) {
032: this ();
033: setMinimum(min);
034: setMaximum(max);
035: setValue(value);
036: }
037:
038: public void requestFocus() {
039: field.requestFocus();
040: }
041:
042: public void setColumns(int c) {
043: field.setColumns(c);
044: }
045:
046: public int getColumns() {
047: return field.getColumns();
048: }
049:
050: public synchronized void setValue(int v) {
051: field.setText(String.valueOf(v));
052: fireValueChange(getValue());
053: }
054:
055: public int getValue() {
056: try {
057: return clamp(Integer.parseInt(field.getText()));
058: } catch (NumberFormatException ex) {
059: return clamp(0);
060: }
061: }
062:
063: protected int minimum = Integer.MIN_VALUE;
064:
065: public synchronized void setMinimum(int m) {
066: minimum = m;
067: }
068:
069: public int getMinimum() {
070: return minimum;
071: }
072:
073: protected int maximum = Integer.MAX_VALUE;
074:
075: public synchronized void setMaximum(int m) {
076: maximum = m;
077: }
078:
079: public int getMaximum() {
080: return maximum;
081: }
082:
083: protected int clamp(int v) {
084: return Math.max(minimum, Math.min(maximum, v));
085: }
086:
087: protected int step = 1;
088:
089: public synchronized void setStep(int s) {
090: if (s <= 0)
091: throw new IllegalArgumentException("Step too small (" + s
092: + ").");
093: step = s;
094: }
095:
096: public int getStep() {
097: return step;
098: }
099:
100: public synchronized void actionPerformed(ActionEvent e) {
101: int value = getValue();
102: if (e.getSource() == down) {
103: if (value > minimum) {
104: value = (value - step > value) ? minimum : clamp(value
105: - step);
106: setValue(value);
107: fireValueChange(value);
108: }
109: } else if (e.getSource() == up) {
110: if (value < maximum) {
111: value = (value + step < value) ? maximum : clamp(value
112: + step);
113: setValue(value);
114: fireValueChange(value);
115: }
116: } else if (e.getSource() == field) {
117: try {
118: int v = Integer.parseInt(e.getActionCommand());
119: if ((v < minimum) || (v > maximum))
120: getToolkit().beep();
121: else
122: fireValueChange(v);
123: } catch (NumberFormatException ex) {
124: getToolkit().beep();
125: }
126: }
127: }
128:
129: protected PropertyChangeSupport listeners = new PropertyChangeSupport(
130: this );
131:
132: public void addPropertyChangeListener(PropertyChangeListener l) {
133: listeners.addPropertyChangeListener(l);
134: }
135:
136: public void removePropertyChangeListener(PropertyChangeListener l) {
137: listeners.removePropertyChangeListener(l);
138: }
139:
140: Integer oValue = new Integer(0);
141:
142: protected void fireValueChange(int v) {
143: listeners.firePropertyChange("value", oValue,
144: oValue = new Integer(v));
145: }
146:
147: public static void main(String[] args) {
148: try {
149: final JFrame frame = new JFrame("NumberChooser unit test");
150: frame.addWindowListener(new WindowAdapter() {
151: public void windowClosing(WindowEvent e) {
152: Window w = e.getWindow();
153: w.setVisible(false);
154: w.dispose();
155: System.exit(0);
156: }
157: });
158: JPanel panel = new JPanel(new GridLayout(0, 1));
159: final JLabel label = new JLabel("Enter a value");
160: NumberChooser chooser = new NumberChooser();
161: panel.add(label);
162: panel.add(chooser);
163: chooser
164: .addPropertyChangeListener(new PropertyChangeListener() {
165: public void propertyChange(
166: PropertyChangeEvent ev) {
167: System.out.println(((Integer) ev
168: .getNewValue()).intValue());
169: }
170: });
171: frame.getContentPane().add(panel);
172: frame.pack();
173: frame.setVisible(true);
174: } catch (Exception exc) {
175: System.out.println("Exception: " + exc.getMessage());
176: exc.printStackTrace();
177: System.exit(1);
178: }
179: }
180: }
|