Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / IncrementalNumberField.java @ 11404

History | View | Annotate | Download (6.87 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 com.iver.cit.gvsig.gui;
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.Timer;
55
import java.util.TimerTask;
56

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

    
63
import com.iver.andami.PluginServices;
64
import com.iver.cit.gvsig.gui.ValidatingTextField.Cleaner;
65
import com.iver.cit.gvsig.gui.ValidatingTextField.Validator;
66

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

    
76
        private ActionListener accum = new ActionListener() {
77
                public void actionPerformed(ActionEvent e) {
78
                        String command = e.getActionCommand();
79
                        if ("UP".equals(command)) {
80
                                if (acceptsDoubles) {
81
                                        setDouble(getDouble()+1);
82
                                } else {
83
                                        setInteger(getInteger()+1);
84
                                }
85
                        } else if ("DOWN".equals(command)) {
86
                                if (acceptsDoubles) {
87
                                        setDouble(getDouble()-1);
88
                                } else {
89
                                        setInteger(getInteger()-1);
90
                                }
91
                        }
92
                }
93
        };
94
        private JButton down;
95
        private JButton up;
96

    
97
        private class MousePressedTask extends TimerTask {
98
                private MouseEvent e;
99
                private ButtonMouseListener ml;
100

    
101
                private MousePressedTask(ButtonMouseListener ml, MouseEvent e){
102
                        super();
103
                        this.ml = ml;
104
                        this.e = e;
105
                }
106

    
107
                public void run() {
108
                        JButton b = (JButton) e.getComponent();
109

    
110
                        long time = System.currentTimeMillis();
111
                        long delay = 200;
112

    
113
                        while (ml.pressed) {
114
                                if (ml.in) {
115
                                        accum.actionPerformed(new ActionEvent(b, 12431, b.getActionCommand()));
116

    
117
                                        long currTime = System.currentTimeMillis();
118
                                        if (delay > 5 && ((currTime - time) > 2000)) {
119
                                                delay /= 2;
120
                                                time = currTime;
121
                                        }
122
                                } else time = System.currentTimeMillis();
123
                                try {
124
                                        Thread.sleep(delay);
125
                                } catch (InterruptedException e1) {
126
                                        e.consume();
127
                                }
128

    
129
                        }
130
                }
131

    
132

    
133

    
134
        }
135

    
136
        private class ButtonMouseListener implements MouseListener{
137
                boolean in = false;
138
                boolean pressed = false;
139

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

    
142
                public void mouseEntered(MouseEvent e) {
143
                        in = true;
144
                }
145

    
146
                public void mouseExited(MouseEvent e) {
147
                        in = false;
148
                }
149

    
150
                public void mousePressed(MouseEvent e) {
151
                        pressed = true;
152
                        Timer timer = new Timer();
153
                        MousePressedTask task = new MousePressedTask(this, e);
154
                        timer.schedule(task, 500);
155

    
156
                }
157

    
158
                public void mouseReleased(MouseEvent e) {
159
                        pressed = false;
160
                }
161

    
162
        };
163

    
164

    
165
        public IncrementalNumberField() {
166
                this("");
167
        }
168

    
169
        public IncrementalNumberField(String text) {
170
                this(text, 7);
171
        }
172

    
173
        public IncrementalNumberField(String text, int columns) {
174
                this(text, columns, ValidatingTextField.DOUBLE_VALIDATOR, ValidatingTextField.NUMBER_CLEANER);
175
        }
176

    
177
        public IncrementalNumberField(String text, int columns, Validator validator, Cleaner cleaner) {
178
                super();
179
                if (text == null) text = "";
180
                acceptsDoubles = validator.getClass().equals(ValidatingTextField.DOUBLE_VALIDATOR.getClass());
181

    
182
                JPanel lateralButtons = new JPanel();
183
                Icon upIcon = new Icon() {
184

    
185
                        public int getIconHeight() {
186
                                return 5;
187
                        }
188

    
189
                        public int getIconWidth() {
190
                                return 9;
191
                        }
192

    
193
                        public void paintIcon(Component c, Graphics g, int x, int y) {
194
                                g.setColor(Color.DARK_GRAY);
195
                                ((Graphics2D) g).setStroke(new BasicStroke(2));
196
                                g.drawLine(3, 6, 5, 3);
197
                                g.drawLine(5, 3, 8, 6);
198

    
199
                        }
200
                };
201
                Icon downIcon = new Icon() {
202
                        public int getIconHeight() {
203
                                return 5;
204
                        }
205

    
206
                        public int getIconWidth() {
207
                                return 9;
208
                        }
209

    
210
                        public void paintIcon(Component c, Graphics g, int x, int y) {
211
                                g.setColor(Color.DARK_GRAY);
212
                                ((Graphics2D) g).setStroke(new BasicStroke(2));
213
                                g.drawLine(3, 3, 5, 6);
214
                                g.drawLine(5, 6, 8, 3);
215

    
216

    
217
                        }
218
                };
219
                up = new JButton(upIcon);
220
                up.setActionCommand("UP");
221
                up.addActionListener(accum);
222
                up.addMouseListener(new ButtonMouseListener());
223
                up.setBounds(0, 0, 12, 11);
224

    
225
                down = new JButton(downIcon);
226
                down.setActionCommand("DOWN");
227
                down.addActionListener(accum);
228
                down.addMouseListener(new ButtonMouseListener());
229
                down.setBounds(0, 11, 12, 11);
230

    
231

    
232

    
233
                lateralButtons.setLayout(null);
234
                lateralButtons.setSize(12, 20);
235
                lateralButtons.add(up);
236
                lateralButtons.add(down);
237
                lateralButtons.setSize(new Dimension(11, 22));
238
                lateralButtons.setPreferredSize(new Dimension(11, 22));
239
                lateralButtons.setBorder(BorderFactory.createLineBorder(Color.GREEN));
240
                vtf = new ValidatingTextField(
241
                                text,
242
                                columns,
243
                                SwingConstants.RIGHT,
244
                                validator,
245
                                cleaner) ;
246
                setLayout(new BorderLayout(0, 0));
247
                add(vtf, BorderLayout.CENTER);
248
                add(lateralButtons, BorderLayout.EAST);
249
        }
250

    
251
        public int getInteger() {
252
                return vtf.getInteger();
253
        }
254

    
255
        public double getDouble() {
256
                if (!acceptsDoubles)
257
                        throw new Error(PluginServices.getText(this,
258
                                        "cannot_get_double_value_from_an_integer_number_field_use_getInteger()_instead"));
259
                return vtf.getDouble();
260
        }
261

    
262
        public void setDouble(double v) {
263
                if (!acceptsDoubles)
264
                        throw new Error(PluginServices.getText(this,
265
                                        "cannot_set_a_double_value_from_an_integer_number_field_use_setInteger(int)_instead"));
266
                vtf.setText(String.valueOf(v));
267
        }
268

    
269
        public void setInteger(int v) {
270
                vtf.setText(String.valueOf(v));
271
        }
272

    
273
        public void addActionListener(ActionListener l) {
274
                vtf.addActionListener(l);
275
                up.addActionListener(l);
276
                down.addActionListener(l);
277
        }
278
}