Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libUIComponent / src / org / gvsig / gui / beans / swing / JIncrementalNumberField.java @ 33683

History | View | Annotate | Download (11.1 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.FocusEvent;
53
import java.awt.event.FocusListener;
54
import java.awt.event.MouseEvent;
55
import java.awt.event.MouseListener;
56
import java.util.ArrayList;
57
import java.util.Arrays;
58
import java.util.EventListener;
59
import java.util.Iterator;
60
import java.util.Timer;
61
import java.util.TimerTask;
62

    
63
import javax.swing.BorderFactory;
64
import javax.swing.Icon;
65
import javax.swing.JButton;
66
import javax.swing.JPanel;
67
import javax.swing.SwingConstants;
68

    
69
import org.gvsig.gui.beans.Messages;
70
import org.gvsig.gui.beans.swing.ValidatingTextField.Cleaner;
71
import org.gvsig.gui.beans.swing.ValidatingTextField.Validator;
72

    
73
/**
74
 * This class represents a JTextField-like component that allows to input numbers
75
 * and featuring a built-in increment or decrease of the number using the mouse.
76
 * @author jaume dominguez faus - jaume.dominguez@iver.es
77
 */
78
public class JIncrementalNumberField extends JPanel {
79

    
80
        private static final long serialVersionUID = 5225633490545230468L;
81
        private boolean acceptsDoubles;
82
        private ValidatingTextField vtf;
83
        private double step;
84
        private double maxValue;
85
        private double minValue;
86

    
87
        private ActionListener propage = new ActionListener() {
88
                public void actionPerformed(ActionEvent e) {
89
                        if (!isEnabled()) return;
90
                        if (acceptsDoubles) {
91
                                double v = getDouble();
92
                                if (v>maxValue)
93
                                        v = maxValue;
94
                                if (v<minValue)
95
                                        v = minValue;
96
                                setDouble(v);
97
                        } else {
98
                                int v = getInteger();
99
                                if (v>maxValue)
100
                                        v = (int) maxValue;
101
                                if (v<minValue)
102
                                        v = (int) minValue;
103
                                setInteger(v);
104
                        }
105
                        fireActionPerformed(e);
106
                }
107
        };
108

    
109
        private FocusListener propageFocus = new FocusListener() {
110
                public void focusGained(FocusEvent e) {
111
                        if (!isEnabled()) return;
112
                        Iterator<FocusListener> iter = Arrays.asList(JIncrementalNumberField.this.getFocusListeners()).iterator();
113
                        e.setSource(JIncrementalNumberField.this);
114
                        while (iter.hasNext()){
115
                                iter.next().focusGained(e);
116
                        }
117
                }
118

    
119
                public void focusLost(FocusEvent e) {
120
                        if (!isEnabled()) return;
121
                        if (acceptsDoubles) {
122
                                double v = getDouble();
123
                                if (v>maxValue)
124
                                        v = maxValue;
125
                                if (v<minValue)
126
                                        v = minValue;
127
                                setDouble(v);
128
                        } else {
129
                                int v = getInteger();
130
                                if (v>maxValue)
131
                                        v = (int) maxValue;
132
                                if (v<minValue)
133
                                        v = (int) minValue;
134
                                setInteger(v);
135
                        }
136
                        Iterator<FocusListener> iter = Arrays.asList(JIncrementalNumberField.this.getFocusListeners()).iterator();
137
                        e.setSource(JIncrementalNumberField.this);
138
                        while (iter.hasNext()){
139
                                iter.next().focusLost(e);
140
                        }
141
                }
142
        };
143

    
144

    
145
        private ActionListener accum = new ActionListener() {
146
                public void actionPerformed(ActionEvent e) {
147
                        if (!isEnabled()) return;
148
                        String command = e.getActionCommand();
149
                        if ("UP".equals(command)) {
150
                                if (acceptsDoubles) {
151
                                        double v = getDouble() + step;
152
                                        if (v>maxValue)
153
                                                v = maxValue;
154
                                        setDouble(v);
155
                                } else {
156
                                        int v = getInteger() + (int) Math.round(step);
157
                                        if (v>maxValue)
158
                                                v = (int) maxValue;
159
                                        setInteger(v);
160
                                }
161
                        } else if ("DOWN".equals(command)) {
162
                                if (acceptsDoubles) {
163
                                        double v = getDouble();// - step;
164
                                        v = v - step;
165
                                        if (v<minValue)
166
                                                v = minValue;
167
                                        setDouble(v);
168

    
169
                                } else {
170
                                        int v = getInteger() - (int) Math.round(step);
171
                                        if (v<minValue)
172
                                                v = (int) minValue;
173
                                        setInteger(v);
174
                                }
175
                        }
176
                        fireActionPerformed();
177
                }
178
        };
179

    
180
        private JButton down;
181
        private JButton up;
182
        private ArrayList<EventListener> listeners = new ArrayList<EventListener>();
183

    
184
        private class MousePressedTask extends TimerTask {
185
                private MouseEvent e;
186
                private ButtonMouseListener ml;
187

    
188
                private MousePressedTask(ButtonMouseListener ml, MouseEvent e){
189
                        super();
190
                        this.ml = ml;
191
                        this.e = e;
192
                }
193

    
194
                public void run() {
195
                        JButton b = (JButton) e.getComponent();
196

    
197
                        long time = System.currentTimeMillis();
198
                        long delay = 200;
199

    
200
                        while (ml.pressed) {
201
                                if (ml.in) {
202
                                        accum.actionPerformed(new ActionEvent(b, 12431, b.getActionCommand()));
203

    
204
                                        long currTime = System.currentTimeMillis();
205
                                        if (delay > 5 && ((currTime - time) > 1000)) {
206
                                                delay /= 2;
207
                                                time = currTime;
208
                                        }
209
                                } else time = System.currentTimeMillis();
210
                                try {
211
                                        Thread.sleep(delay);
212
                                } catch (InterruptedException e1) {
213
                                        e.consume();
214
                                }
215

    
216
                        }
217
                        e.consume();
218
                }
219
        }
220

    
221
        private class ButtonMouseListener implements MouseListener{
222
                boolean in = false;
223
                boolean pressed = false;
224

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

    
227
                public void mouseEntered(MouseEvent e) {
228
                        in = true;
229
                }
230

    
231
                public void mouseExited(MouseEvent e) {
232
                        in = false;
233
                }
234

    
235
                public void mousePressed(MouseEvent e) {
236
                        MousePressedTask task;
237
                        synchronized (this) {
238
                                pressed = true;
239
                                Timer timer = new Timer();
240
                                task = new MousePressedTask(this, e);
241
                                timer.schedule(task, 500);
242

    
243
                        }
244
                        if (!pressed) {
245
                                task.cancel();
246
                        }
247

    
248
                }
249

    
250
                public void mouseReleased(MouseEvent e) {
251
                        pressed = false;
252
                }
253

    
254
        };
255

    
256

    
257
        public JIncrementalNumberField() {
258
                this("");
259
        }
260

    
261
        public JIncrementalNumberField(String text) {
262
                this(text, 7);
263
        }
264

    
265
        public JIncrementalNumberField(String text, int columns) {
266
                this(text, columns, ValidatingTextField.DOUBLE_VALIDATOR, ValidatingTextField.NUMBER_CLEANER_2_DECIMALS, -Double.MAX_VALUE, Double.MAX_VALUE, 1);
267
        }
268

    
269
        public JIncrementalNumberField(String text, int columns, double minValue, double maxValue, double step) {
270
                this(text, columns, ValidatingTextField.DOUBLE_VALIDATOR, ValidatingTextField.NUMBER_CLEANER_2_DECIMALS, minValue, maxValue, step);
271
        }
272

    
273
        public JIncrementalNumberField(String text, int columns, Validator validator, Cleaner cleaner, double minValue, double maxValue, double step) {
274
                super();
275
                if (text == null) text = "";
276

    
277
                this.minValue = minValue;
278
                this.maxValue = maxValue;
279
                this.step = step;
280
                acceptsDoubles = validator.getClass().equals(ValidatingTextField.DOUBLE_VALIDATOR.getClass());
281

    
282
                JPanel lateralButtons = new JPanel();
283
                Icon upIcon = new Icon() {
284

    
285
                        public int getIconHeight() {
286
                                return 5;
287
                        }
288

    
289
                        public int getIconWidth() {
290
                                return 9;
291
                        }
292

    
293
                        public void paintIcon(Component c, Graphics g, int x, int y) {
294
                                g.setColor( isEnabled() ? Color.DARK_GRAY : Color.RED);
295
                                ((Graphics2D) g).setStroke(new BasicStroke(2));
296
                                g.drawLine(isEnabled() ? 3 : 1,
297
                                                   isEnabled() ? 6 : 4,
298
                                                   isEnabled() ? 5 : 3,
299
                                                   isEnabled() ? 3 : 1);
300

    
301
                                g.drawLine(isEnabled() ? 5 : 3,
302
                                                   isEnabled() ? 3 : 1,
303
                                                   isEnabled() ? 8 : 6,
304
                                                   isEnabled() ? 6 : 4);
305

    
306
                        }
307
                };
308
                Icon downIcon = new Icon() {
309
                        public int getIconHeight() {
310
                                return 5;
311
                        }
312

    
313
                        public int getIconWidth() {
314
                                return 9;
315
                        }
316

    
317
                        public void paintIcon(Component c, Graphics g, int x, int y) {
318
                                g.setColor(isEnabled() ? Color.DARK_GRAY : Color.RED);
319
                                ((Graphics2D) g).setStroke(new BasicStroke(2));
320

    
321

    
322
                                g.drawLine(isEnabled() ? 3 : 1,
323
                                                   isEnabled() ? 3 : 1,
324
                                                   isEnabled() ? 5 : 3,
325
                                                   isEnabled() ? 6 : 4);
326

    
327
                                g.drawLine(isEnabled() ? 5 : 3,
328
                                                   isEnabled() ? 6 : 4,
329
                                                   isEnabled() ? 8 : 6,
330
                                                   isEnabled() ? 3 : 1);
331

    
332
                        }
333
                };
334
                up = new JButton(upIcon);
335
                up.setActionCommand("UP");
336
                up.addActionListener(accum);
337
                up.addMouseListener(new ButtonMouseListener());
338
                up.setBounds(0, 0, 11, 11);
339
                up.setFocusable(false);
340

    
341
                down = new JButton(downIcon);
342
                down.setActionCommand("DOWN");
343
                down.addActionListener(accum);
344
                down.addMouseListener(new ButtonMouseListener());
345
                down.setBounds(0, 11, 11, 11);
346
                down.setFocusable(false);
347

    
348

    
349

    
350
                lateralButtons.setLayout(null);
351
                lateralButtons.setSize(13, 20);
352
                lateralButtons.add(up);
353
                lateralButtons.add(down);
354
                lateralButtons.setSize(new Dimension(11, 22));
355
                lateralButtons.setPreferredSize(new Dimension(11, 22));
356
                lateralButtons.setBorder(BorderFactory.createLineBorder(Color.GREEN));
357
                vtf = new ValidatingTextField(
358
                                text,
359
                                columns,
360
                                SwingConstants.RIGHT,
361
                                validator,
362
                                cleaner) ;
363
                setLayout(new BorderLayout(0, 0));
364
                vtf.addActionListener(propage);
365
                vtf.addFocusListener(propageFocus);
366
                add(vtf, BorderLayout.CENTER);
367
                add(lateralButtons, BorderLayout.EAST);
368
        }
369

    
370
        public int getInteger() {
371
                return vtf.getInteger();
372
        }
373

    
374
        public double getDouble() {
375
                if (!acceptsDoubles)
376
                        throw new Error(Messages.getText(
377
                                        "cannot_get_double_value_from_an_integer_number_field_use_getInteger()_instead"));
378
                return vtf.getDouble();
379
        }
380

    
381
        public void setDouble(double v) {
382
                if (!acceptsDoubles)
383
                        throw new Error(Messages.getText(
384
                                        "cannot_set_a_double_value_from_an_integer_number_field_use_setInteger(int)_instead"));
385
                vtf.setText(String.valueOf(v));
386
        }
387

    
388
        public void setInteger(int v) {
389
                vtf.setText(String.valueOf(v));
390
        }
391

    
392
        public void addActionListener(ActionListener l) {
393
//                vtf.addActionListener(l);
394
                listeners.add(l);
395
        }
396

    
397
        public void removeActionListener(ActionListener l) {
398
//                vtf.removeActionListener(l);
399
                listeners.remove(l);
400
        }
401

    
402
        private void fireActionPerformed() {
403
                ActionEvent evt = new ActionEvent(this, 0, null);
404
                for (int i = 0; i < listeners.size(); i++) {
405
                        ((ActionListener) listeners.get(i)).actionPerformed(evt);
406
                }
407
        }
408

    
409
        private void fireActionPerformed(ActionEvent e) {
410
                e.setSource(this);
411
                for (int i = 0; i < listeners.size(); i++) {
412
                        ((ActionListener) listeners.get(i)).actionPerformed(e);
413
                }
414
        }
415

    
416
        public double getMaxValue() {
417
                return maxValue;
418
        }
419

    
420
        public void setMaxValue(double maxValue) {
421
                this.maxValue = maxValue;
422
        }
423

    
424
        public double getMinValue() {
425
                return minValue;
426
        }
427

    
428
        public void setMinValue(double minValue) {
429
                this.minValue = minValue;
430
        }
431

    
432
        public double getStep() {
433
                return step;
434
        }
435

    
436
        public void setStep(double step) {
437
                this.step = step;
438
        }
439

    
440
        public void setEnabled(boolean enabled) {
441
                super.setEnabled(enabled);
442
                up.setEnabled(enabled);
443
                down.setEnabled(enabled);
444
                vtf.setEnabled(enabled);
445
        }
446
}