~ubuntu-branches/ubuntu/hardy/libjcalendar-java/hardy

« back to all changes in this revision

Viewing changes to src/com/toedter/components/JSpinField.java

  • Committer: Bazaar Package Importer
  • Author(s): Hilko Bengen
  • Date: 2005-08-24 12:23:48 UTC
  • Revision ID: james.westby@ubuntu.com-20050824122348-sb0i5x1k5rt91njk
Tags: upstream-1.2.2
ImportĀ upstreamĀ versionĀ 1.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  JSpinField.java  - A spin field using a JSpinner (JDK 1.4)
 
3
 *  Copyright (C) 2004 Kai Toedter
 
4
 *  kai@toedter.com
 
5
 *  www.toedter.com
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or
 
8
 *  modify it under the terms of the GNU Lesser General Public License
 
9
 *  as published by the Free Software Foundation; either version 2
 
10
 *  of the License, or (at your option) any later version.
 
11
 *
 
12
 *  This program is distributed in the hope that it will be useful,
 
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *  GNU Lesser General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU Lesser General Public License
 
18
 *  along with this program; if not, write to the Free Software
 
19
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
20
 */
 
21
package com.toedter.components;
 
22
 
 
23
import java.awt.BorderLayout;
 
24
import java.awt.Color;
 
25
import java.awt.Component;
 
26
import java.awt.Dimension;
 
27
import java.awt.Font;
 
28
import java.awt.event.ActionEvent;
 
29
import java.awt.event.ActionListener;
 
30
 
 
31
import javax.swing.BorderFactory;
 
32
import javax.swing.JFrame;
 
33
import javax.swing.JPanel;
 
34
import javax.swing.JSpinner;
 
35
import javax.swing.JTextField;
 
36
import javax.swing.SpinnerNumberModel;
 
37
import javax.swing.SwingConstants;
 
38
import javax.swing.event.CaretEvent;
 
39
import javax.swing.event.CaretListener;
 
40
import javax.swing.event.ChangeEvent;
 
41
import javax.swing.event.ChangeListener;
 
42
 
 
43
/**
 
44
 * JSpinField2 is a numeric field with 2 spin buttons to increase or decrease
 
45
 * the value. It has the same interface as the "old" JSpinField but uses a JSpinner
 
46
 * internally (since J2SE SDK 1.4) rather than a scrollbar for emulating the spin buttons.
 
47
 * 
 
48
 * @author Kai Toedter
 
49
 * @version 1.2
 
50
 */
 
51
public class JSpinField extends JPanel implements ChangeListener, CaretListener, ActionListener {
 
52
        protected JSpinner spinner;
 
53
 
 
54
        /** the text (number) field */
 
55
        protected JTextField textField;
 
56
        protected int min;
 
57
        protected int max;
 
58
        protected int value;
 
59
        protected Color darkGreen;
 
60
 
 
61
        /**
 
62
         * Default JSpinField constructor.
 
63
         */
 
64
        public JSpinField() {
 
65
            this(0,Integer.MAX_VALUE);
 
66
        }
 
67
        
 
68
        /**
 
69
         * JSpinField constructor with given minimum and maximum vaues..
 
70
         */
 
71
        public JSpinField(int min, int max) {
 
72
                super();
 
73
                this.min = min;
 
74
                if(max < min)
 
75
                    max = min;
 
76
                this.max = max;
 
77
                value = 0;
 
78
                if(value < min)
 
79
                    value = min;
 
80
                if(value > max)
 
81
                    value = max;
 
82
                
 
83
                darkGreen = new Color(0, 150, 0);
 
84
                setLayout(new BorderLayout());
 
85
                textField = new JTextField();
 
86
                textField.addCaretListener(this);
 
87
                textField.addActionListener(this);
 
88
                textField.setHorizontalAlignment(SwingConstants.RIGHT);
 
89
                textField.setBorder(BorderFactory.createEmptyBorder());
 
90
                textField.setText(Integer.toString(value));
 
91
                spinner = new JSpinner();
 
92
                spinner.setEditor(textField);
 
93
                spinner.addChangeListener(this);
 
94
                add(spinner, BorderLayout.CENTER);
 
95
        }
 
96
 
 
97
        public void adjustWidthToMaximumValue() {
 
98
                JTextField testTextField = new JTextField(Integer.toString(max));
 
99
                int width = testTextField.getPreferredSize().width;
 
100
                int height = testTextField.getPreferredSize().height;
 
101
                textField.setPreferredSize(new Dimension(width,height));
 
102
                textField.revalidate();
 
103
        }
 
104
        
 
105
        /**
 
106
         * Is invoked when the spinner model changes
 
107
         * 
 
108
         * @param e
 
109
         *            the ChangeEvent
 
110
         */
 
111
        public void stateChanged(ChangeEvent e) {
 
112
                SpinnerNumberModel model = (SpinnerNumberModel) spinner.getModel();
 
113
                int value = model.getNumber().intValue();
 
114
                setValue(value);
 
115
        }
 
116
 
 
117
        /**
 
118
         * Sets the value attribute of the JSpinField object.
 
119
         * 
 
120
         * @param newValue
 
121
         *            The new value
 
122
         * @param updateTextField
 
123
         *            true if text field should be updated
 
124
         */
 
125
        protected void setValue(int newValue, boolean updateTextField, boolean firePropertyChange) {
 
126
                int oldValue = value;
 
127
 
 
128
                if (newValue < min) {
 
129
                        value = min;
 
130
                } else if (newValue > max) {
 
131
                        value = max;
 
132
                } else {
 
133
                        value = newValue;
 
134
                }
 
135
 
 
136
                if (updateTextField) {
 
137
                        textField.setText(Integer.toString(value));
 
138
                        textField.setForeground(Color.black);
 
139
                }
 
140
 
 
141
                if(firePropertyChange) {
 
142
                        firePropertyChange("value", oldValue, value);
 
143
                }
 
144
        }
 
145
 
 
146
        /**
 
147
         * Sets the value. This is a bound property.
 
148
         * 
 
149
         * @param newValue
 
150
         *            the new value
 
151
         * 
 
152
         * @see #getValue
 
153
         */
 
154
        public void setValue(int newValue) {
 
155
                setValue(newValue, true, true);
 
156
                spinner.setValue(new Integer(value));
 
157
        }
 
158
 
 
159
        /**
 
160
         * Returns the value.
 
161
         * 
 
162
         * @return the value value
 
163
         */
 
164
        public int getValue() {
 
165
                return value;
 
166
        }
 
167
 
 
168
        /**
 
169
         * Sets the minimum value.
 
170
         * 
 
171
         * @param newMinimum
 
172
         *            the new minimum value
 
173
         * 
 
174
         * @see #getMinimum
 
175
         */
 
176
        public void setMinimum(int newMinimum) {
 
177
                min = newMinimum;
 
178
        }
 
179
 
 
180
        /**
 
181
         * Returns the minimum value.
 
182
         * 
 
183
         * @return the minimum value
 
184
         */
 
185
        public int getMinimum() {
 
186
                return min;
 
187
        }
 
188
 
 
189
        /**
 
190
         * Sets the maximum value and adjusts the preferred width.
 
191
         * 
 
192
         * @param newMaximum
 
193
         *            the new maximum value
 
194
         * 
 
195
         * @see #getMaximum
 
196
         */
 
197
        public void setMaximum(int newMaximum) {
 
198
                max = newMaximum;
 
199
        }
 
200
 
 
201
        /**
 
202
         * Sets the horizontal alignment of the displayed value.
 
203
         * 
 
204
         * @param alignment
 
205
         *            the horizontal alignment
 
206
         */
 
207
        public void setHorizontalAlignment(int alignment) {
 
208
                textField.setHorizontalAlignment(alignment);
 
209
        }
 
210
 
 
211
        /**
 
212
         * Returns the maximum value.
 
213
         * 
 
214
         * @return the maximum value
 
215
         */
 
216
        public int getMaximum() {
 
217
                return max;
 
218
        }
 
219
 
 
220
        /**
 
221
         * Sets the font property.
 
222
         * 
 
223
         * @param font
 
224
         *            the new font
 
225
         */
 
226
        public void setFont(Font font) {
 
227
                if (textField != null) {
 
228
                        textField.setFont(font);
 
229
                }
 
230
        }
 
231
 
 
232
        /**
 
233
         * DOCUMENT ME!
 
234
         * 
 
235
         * @param fg
 
236
         *            DOCUMENT ME!
 
237
         */
 
238
        public void setForeground(Color fg) {
 
239
                if (textField != null) {
 
240
                        textField.setForeground(fg);
 
241
                }
 
242
        }
 
243
 
 
244
        /**
 
245
         * After any user input, the value of the textfield is proofed. Depending on
 
246
         * being an integer, the value is colored green or red.
 
247
         * 
 
248
         * @param e
 
249
         *            Description of the Parameter
 
250
         */
 
251
        public void caretUpdate(CaretEvent e) {
 
252
                try {
 
253
                        int testValue = Integer.valueOf(textField.getText()).intValue();
 
254
 
 
255
                        if ((testValue >= min) && (testValue <= max)) {
 
256
                                textField.setForeground(darkGreen);
 
257
                                setValue(testValue, false, true);
 
258
                        } else {
 
259
                                textField.setForeground(Color.red);
 
260
                        }
 
261
                } catch (Exception ex) {
 
262
                        if (ex instanceof NumberFormatException) {
 
263
                                textField.setForeground(Color.red);
 
264
                        }
 
265
 
 
266
                        // Ignore all other exceptions, e.g. illegal state exception
 
267
                }
 
268
 
 
269
                textField.repaint();
 
270
        }
 
271
 
 
272
        /**
 
273
         * After any user input, the value of the textfield is proofed. Depending on
 
274
         * being an integer, the value is colored green or red. If the textfield is
 
275
         * green, the enter key is accepted and the new value is set.
 
276
         * 
 
277
         * @param e
 
278
         *            Description of the Parameter
 
279
         */
 
280
        public void actionPerformed(ActionEvent e) {
 
281
                if (textField.getForeground().equals(darkGreen)) {
 
282
                        setValue(Integer.valueOf(textField.getText()).intValue());
 
283
                }
 
284
        }
 
285
 
 
286
        /**
 
287
         * Enable or disable the JSpinField.
 
288
         * 
 
289
         * @param enabled
 
290
         *            The new enabled value
 
291
         */
 
292
        public void setEnabled(boolean enabled) {
 
293
                super.setEnabled(enabled);
 
294
                textField.setEnabled(enabled);
 
295
                spinner.setEnabled(enabled);
 
296
        }
 
297
 
 
298
        /**
 
299
         * Returns the year chooser's spinner (which allow the focus to be set to
 
300
         * it).
 
301
         * 
 
302
         * @return Component the spinner or null, if the month chooser has no
 
303
         *         spinner
 
304
         */
 
305
        public Component getSpinner() {
 
306
                return spinner;
 
307
        }
 
308
 
 
309
    /**
 
310
     * Returns "JSpinField".
 
311
     *
 
312
     * @return the name value
 
313
     */
 
314
    public String getName() {
 
315
        return "JSpinField";
 
316
    }
 
317
 
 
318
        /**
 
319
         * Creates a JFrame with a JSpinField inside and can be used for testing.
 
320
         * 
 
321
         * @param s
 
322
         *            The command line arguments
 
323
         */
 
324
        public static void main(String[] s) {
 
325
                JFrame frame = new JFrame("JSpinField2");
 
326
                frame.getContentPane().add(new JSpinField());
 
327
                frame.pack();
 
328
                frame.setVisible(true);
 
329
        }
 
330
}
 
 
b'\\ No newline at end of file'