Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUI / src / org / gvsig / gui / beans / swing / JIncrementalNumberField.java @ 11920

History | View | Annotate | Download (7.93 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.gui.beans.swing;
42

    
43
import java.awt.BasicStroke;
44
import java.awt.BorderLayout;
45
import java.awt.Color;
46
import java.awt.Component;
47
import java.awt.Dimension;
48
import java.awt.Graphics;
49
import java.awt.Graphics2D;
50
import java.awt.event.ActionEvent;
51
import java.awt.event.ActionListener;
52
import java.awt.event.MouseEvent;
53
import java.awt.event.MouseListener;
54
import java.util.ArrayList;
55
import java.util.Timer;
56
import java.util.TimerTask;
57

    
58
import javax.swing.BorderFactory;
59
import javax.swing.Icon;
60
import javax.swing.JButton;
61
import javax.swing.JPanel;
62
import javax.swing.SwingConstants;
63

    
64
import org.gvsig.gui.beans.Messages;
65
import org.gvsig.gui.beans.swing.ValidatingTextField.Cleaner;
66
import org.gvsig.gui.beans.swing.ValidatingTextField.Validator;
67

    
68
/**
69
 * This class represents a JTextField-like component that allows to input numbers and it
70
 * features a built-in increment or decrease of the number using the mouse
71
 * @author jaume dominguez faus - jaume.dominguez@iver.es
72
 */
73
public class JIncrementalNumberField extends JPanel {
74
        private boolean acceptsDoubles;
75
        private ValidatingTextField vtf;
76
        private double step;
77
        private double max;
78
        private double min;
79

    
80

    
81
        private ActionListener accum = new ActionListener() {
82
                public void actionPerformed(ActionEvent e) {
83
                        String command = e.getActionCommand();
84
                        if ("UP".equals(command)) {
85
                                if (acceptsDoubles) {
86
                                        double v = getDouble() + step;
87
                                        if (v>max)
88
                                                v = max;
89
                                        setDouble(v);
90
                                } else {
91
                                        int v = getInteger() + (int) Math.round(step);
92
                                        if (v>max)
93
                                                v = (int) max;
94
                                        setInteger(v);
95
                                }
96
                        } else if ("DOWN".equals(command)) {
97
                                if (acceptsDoubles) {
98
                                        double v = getDouble() - step;
99
                                        if (v<min)
100
                                                v = min;
101
                                        setDouble(v);
102

    
103
                                } else {
104
                                        int v = getInteger() - (int) Math.round(step);
105
                                        if (v<min)
106
                                                v = (int) min;
107
                                        setInteger(v);
108
                                }
109
                        }
110
                        fireActionPerformed();
111
                }
112
        };
113

    
114
        private JButton down;
115
        private JButton up;
116
        private ArrayList listeners = new ArrayList();
117

    
118
        private class MousePressedTask extends TimerTask {
119
                private MouseEvent e;
120
                private ButtonMouseListener ml;
121

    
122
                private MousePressedTask(ButtonMouseListener ml, MouseEvent e){
123
                        super();
124
                        this.ml = ml;
125
                        this.e = e;
126
                }
127

    
128
                public void run() {
129
                        JButton b = (JButton) e.getComponent();
130

    
131
                        long time = System.currentTimeMillis();
132
                        long delay = 200;
133

    
134
                        while (ml.pressed) {
135
                                if (ml.in) {
136
                                        accum.actionPerformed(new ActionEvent(b, 12431, b.getActionCommand()));
137

    
138
                                        long currTime = System.currentTimeMillis();
139
                                        if (delay > 5 && ((currTime - time) > 1000)) {
140
                                                delay /= 2;
141
                                                time = currTime;
142
                                        }
143
                                } else time = System.currentTimeMillis();
144
                                try {
145
                                        Thread.sleep(delay);
146
                                } catch (InterruptedException e1) {
147
                                        e.consume();
148
                                }
149

    
150
                        }
151
                        e.consume();
152
                }
153

    
154

    
155

    
156
        }
157

    
158
        private class ButtonMouseListener implements MouseListener{
159
                boolean in = false;
160
                boolean pressed = false;
161

    
162
                public void mouseClicked(MouseEvent e) { /* nothing (managed by the ActionListener) */ }
163

    
164
                public void mouseEntered(MouseEvent e) {
165
                        in = true;
166
                }
167

    
168
                public void mouseExited(MouseEvent e) {
169
                        in = false;
170
                }
171

    
172
                public void mousePressed(MouseEvent e) {
173
                        MousePressedTask task;
174
                        synchronized (this) {
175
                                pressed = true;
176
                                Timer timer = new Timer();
177
                                task = new MousePressedTask(this, e);
178
                                timer.schedule(task, 500);
179

    
180
                        }
181
                        if (!pressed) {
182
                                task.cancel();
183
                        }
184

    
185
                }
186

    
187
                public void mouseReleased(MouseEvent e) {
188
                        pressed = false;
189
                }
190

    
191
        };
192

    
193

    
194
        public JIncrementalNumberField() {
195
                this("");
196
        }
197

    
198
        public JIncrementalNumberField(String text) {
199
                this(text, 7);
200
        }
201

    
202
        public JIncrementalNumberField(String text, int columns) {
203
                this(text, columns, ValidatingTextField.DOUBLE_VALIDATOR, ValidatingTextField.NUMBER_CLEANER, -Double.MAX_VALUE, Double.MAX_VALUE, 1);
204
        }
205

    
206
        public JIncrementalNumberField(String text, int columns, double minValue, double maxValue, double step) {
207
                this(text, columns, ValidatingTextField.DOUBLE_VALIDATOR, ValidatingTextField.NUMBER_CLEANER, minValue, maxValue, step);
208
        }
209

    
210
        public JIncrementalNumberField(String text, int columns, Validator validator, Cleaner cleaner, double minValue, double maxValue, double step) {
211
                super();
212
                if (text == null) text = "";
213

    
214
                this.min = minValue;
215
                this.max = maxValue;
216
                this.step = step;
217
                acceptsDoubles = validator.getClass().equals(ValidatingTextField.DOUBLE_VALIDATOR.getClass());
218

    
219
                JPanel lateralButtons = new JPanel();
220
                Icon upIcon = new Icon() {
221

    
222
                        public int getIconHeight() {
223
                                return 5;
224
                        }
225

    
226
                        public int getIconWidth() {
227
                                return 9;
228
                        }
229

    
230
                        public void paintIcon(Component c, Graphics g, int x, int y) {
231
                                g.setColor(Color.DARK_GRAY);
232
                                ((Graphics2D) g).setStroke(new BasicStroke(2));
233
                                g.drawLine(3, 6, 5, 3);
234
                                g.drawLine(5, 3, 8, 6);
235

    
236
                        }
237
                };
238
                Icon downIcon = new Icon() {
239
                        public int getIconHeight() {
240
                                return 5;
241
                        }
242

    
243
                        public int getIconWidth() {
244
                                return 9;
245
                        }
246

    
247
                        public void paintIcon(Component c, Graphics g, int x, int y) {
248
                                g.setColor(Color.DARK_GRAY);
249
                                ((Graphics2D) g).setStroke(new BasicStroke(2));
250
                                g.drawLine(3, 3, 5, 6);
251
                                g.drawLine(5, 6, 8, 3);
252

    
253

    
254
                        }
255
                };
256
                up = new JButton(upIcon);
257
                up.setActionCommand("UP");
258
                up.addActionListener(accum);
259
                up.addMouseListener(new ButtonMouseListener());
260
                up.setBounds(0, 0, 12, 11);
261

    
262
                down = new JButton(downIcon);
263
                down.setActionCommand("DOWN");
264
                down.addActionListener(accum);
265
                down.addMouseListener(new ButtonMouseListener());
266
                down.setBounds(0, 11, 12, 11);
267

    
268

    
269

    
270
                lateralButtons.setLayout(null);
271
                lateralButtons.setSize(12, 20);
272
                lateralButtons.add(up);
273
                lateralButtons.add(down);
274
                lateralButtons.setSize(new Dimension(11, 22));
275
                lateralButtons.setPreferredSize(new Dimension(11, 22));
276
                lateralButtons.setBorder(BorderFactory.createLineBorder(Color.GREEN));
277
                vtf = new ValidatingTextField(
278
                                text,
279
                                columns,
280
                                SwingConstants.RIGHT,
281
                                validator,
282
                                cleaner) ;
283
                setLayout(new BorderLayout(0, 0));
284
                add(vtf, BorderLayout.CENTER);
285
                add(lateralButtons, BorderLayout.EAST);
286
        }
287

    
288
        public int getInteger() {
289
                return vtf.getInteger();
290
        }
291

    
292
        public double getDouble() {
293
                if (!acceptsDoubles)
294
                        throw new Error(Messages.getText(
295
                                        "cannot_get_double_value_from_an_integer_number_field_use_getInteger()_instead"));
296
                return vtf.getDouble();
297
        }
298

    
299
        public void setDouble(double v) {
300
                if (!acceptsDoubles)
301
                        throw new Error(Messages.getText(
302
                                        "cannot_set_a_double_value_from_an_integer_number_field_use_setInteger(int)_instead"));
303
                vtf.setText(String.valueOf(v));
304
        }
305

    
306
        public void setInteger(int v) {
307
                vtf.setText(String.valueOf(v));
308
        }
309

    
310
        public void addActionListener(ActionListener l) {
311
                vtf.addActionListener(l);
312
                listeners .add(l);
313
        }
314

    
315
        private void fireActionPerformed() {
316
                ActionEvent evt = new ActionEvent(this, 0, null);
317
                for (int i = 0; i < listeners.size(); i++) {
318
                        ((ActionListener) listeners.get(i)).actionPerformed(evt);
319
                }
320
        }
321
}