KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JSpinner


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: JSpinner.java,v $
11    Revision 1.3 2004/05/07 12:11:18 bobintetley
12    Default layout fixes and correct behaviour for null layout
13
14    Revision 1.2 2004/04/30 13:20:43 bobintetley
15    Fix to unrealised peer preferred sizes, forwarding window events to
16    content panes and fix for mouse drag events.
17
18    Revision 1.1 2004/04/18 14:21:50 bobintetley
19    JSpinner implementation
20
21
22 */

23
24 package swingwtx.swing;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import swingwt.awt.BorderLayout;
30 import swingwt.awt.Container;
31 import swingwt.awt.Dimension;
32 import swingwt.awt.event.AdjustmentEvent;
33 import swingwt.awt.event.AdjustmentListener;
34 import swingwt.awt.event.FocusListener;
35 import swingwt.awt.event.KeyAdapter;
36 import swingwt.awt.event.KeyEvent;
37 import swingwt.awt.event.KeyListener;
38 import swingwtx.swing.event.ChangeEvent;
39 import swingwtx.swing.event.ChangeListener;
40
41 /**
42  * Implementation of Swing's JSpinner class. We actually use
43  * a text box and a vertical scrollbar to get the same effect
44  * with native widgets.
45  *
46  * @author Robin Rawson-Tetley
47  */

48 public class JSpinner extends JPanel implements ChangeListener {
49     
50     /** Item events */
51     protected Vector JavaDoc changeListeners = new Vector JavaDoc();
52     /** Model */
53     protected SpinnerModel model = null;
54     /** Composite Components */
55     protected JTextField text = new JTextField();
56     protected JScrollBar spin = new JScrollBar(JScrollBar.VERTICAL);
57     
58     protected int lastValue = 300000;
59     
60     public JSpinner() { this(new SpinnerNumberModel()); }
61     
62     public JSpinner(SpinnerModel model) {
63         
64         super();
65         
66         setPreferredSize(new Dimension(100, 30));
67         setModel(model);
68         setLayout(new BorderLayout());
69         
70         // Watch for cursor keys
71
text.addKeyListener(new KeyAdapter() {
72             public void keyPressed(KeyEvent e) {
73                 if (e.getKeyCode() == 16777218) {
74                     nextItem(); // Go the next item with cursor down
75
e.consume();
76                 }
77                 else if (e.getKeyCode() == 16777217) {
78                     previousItem(); // Go to the previous item with cursor up
79
e.consume();
80                 }
81             }
82         });
83         
84         // Watch for changes to the scroller
85
spin.setMinimum(0);
86         spin.setMaximum(650000);
87         spin.setValue(30000);
88         spin.addAdjustmentListener(new AdjustmentListener() {
89             public void adjustmentValueChanged(AdjustmentEvent e) {
90                 if (e.getValue() > lastValue)
91                     previousItem();
92                 else
93                     nextItem();
94                 lastValue = e.getValue();
95             }
96         });
97         
98         // Add the components
99
add(text, BorderLayout.CENTER);
100         add(spin, BorderLayout.EAST);
101     }
102     
103     protected final static int CANCELED = 0;
104     protected final static int INVISIBLE = 1;
105     protected final static int VISIBLE = 2;
106     
107     public void setModel(SpinnerModel model) {
108         this.model = model;
109         model.addChangeListener(this);
110     }
111     
112     public SpinnerModel getModel() {
113         return model;
114     }
115     
116     public void addFocusListener(FocusListener l) {
117         text.addFocusListener(l);
118     }
119     public void removeFocusListener(FocusListener l) {
120         text.removeFocusListener(l);
121     }
122     public void addKeyListener(KeyListener l) {
123         text.addKeyListener(l);
124     }
125     public void removeKeyListener(KeyListener l) {
126         text.removeKeyListener(l);
127     }
128     
129     public void addChangeListener(ChangeListener l) {
130         changeListeners.add(l);
131     }
132     
133     public void removeItemListener(ChangeListener l) {
134         changeListeners.remove(l);
135     }
136     
137     public void processChangeEvent(ChangeEvent e) {
138         Iterator JavaDoc i = changeListeners.iterator();
139         while (i.hasNext()) {
140             ChangeListener l = (ChangeListener) i.next();
141             l.stateChanged(e);
142         }
143     }
144     
145     public void setValue(Object JavaDoc o) {
146         model.setValue(o);
147     }
148     
149     public Object JavaDoc getValue() {
150         return model.getValue();
151     }
152     
153     
154     public void setEnabled(boolean b) {
155         text.setEnabled(b);
156         spin.setEnabled(b);
157     }
158     
159     public boolean isEnabled() {
160         return spin.isEnabled();
161     }
162     
163     public String JavaDoc getToolTipText() {
164         return text.getToolTipText();
165     }
166     
167     public void setToolTipText(String JavaDoc tip) {
168         text.setToolTipText(tip);
169     }
170     
171     protected void previousItem() {
172         model.setValue( model.getPreviousValue() );
173     }
174     protected void nextItem() {
175         model.setValue( model.getNextValue() );
176     }
177
178     public void requestFocus() {
179         text.requestFocus();
180     }
181
182     public void grabFocus() {
183     text.grabFocus();
184     }
185
186     public JTextField getJTextField() {
187     return text;
188     }
189     
190     public void stateChanged(ChangeEvent e) {
191         this.text.setText( model.getValue().toString());
192     }
193     
194     /** Component displays oddly since we used a vertical scrollbar. This
195      * should lock it to no more than 30 pixels in height when LayoutManagers
196      * try to update it
197      */

198     public void setBounds(int x, int y, int width, int height) {
199         if (height > 30) height = 30;
200         super.setBounds(x, y, width, height);
201     }
202     
203     /** Overriden to calculate non-realised
204      * preferred size.
205      */

206     protected swingwt.awt.Dimension calculatePreferredSize() {
207         // Default 100x30
208
swingwt.awt.Dimension size = new swingwt.awt.Dimension(100, 30);
209         setSize(size);
210         return size;
211     }
212     
213     public void setSwingWTParent(Container parent) throws Exception JavaDoc {
214         super.setSwingWTParent(parent);
215         stateChanged(new ChangeEvent(model));
216     }
217     
218 }
219
Popular Tags