root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / swing / JIncrementalNumberField.java @ 13136
History | View | Annotate | Download (8.41 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
|
70 |
* and featuring 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 |
|
75 |
private static final long serialVersionUID = 5225633490545230468L; |
76 |
private boolean acceptsDoubles; |
77 |
private ValidatingTextField vtf;
|
78 |
private double step; |
79 |
private double maxValue; |
80 |
private double minValue; |
81 |
|
82 |
|
83 |
private ActionListener accum = new ActionListener() { |
84 |
public void actionPerformed(ActionEvent e) { |
85 |
String command = e.getActionCommand();
|
86 |
if ("UP".equals(command)) { |
87 |
if (acceptsDoubles) {
|
88 |
double v = getDouble() + step;
|
89 |
if (v>maxValue)
|
90 |
v = maxValue; |
91 |
setDouble(v); |
92 |
} else {
|
93 |
int v = getInteger() + (int) Math.round(step); |
94 |
if (v>maxValue)
|
95 |
v = (int) maxValue;
|
96 |
setInteger(v); |
97 |
} |
98 |
} else if ("DOWN".equals(command)) { |
99 |
if (acceptsDoubles) {
|
100 |
double v = getDouble() - step;
|
101 |
if (v<minValue)
|
102 |
v = minValue; |
103 |
setDouble(v); |
104 |
|
105 |
} else {
|
106 |
int v = getInteger() - (int) Math.round(step); |
107 |
if (v<minValue)
|
108 |
v = (int) minValue;
|
109 |
setInteger(v); |
110 |
} |
111 |
} |
112 |
fireActionPerformed(); |
113 |
} |
114 |
}; |
115 |
|
116 |
private JButton down; |
117 |
private JButton up; |
118 |
private ArrayList listeners = new ArrayList(); |
119 |
|
120 |
private class MousePressedTask extends TimerTask { |
121 |
private MouseEvent e; |
122 |
private ButtonMouseListener ml;
|
123 |
|
124 |
private MousePressedTask(ButtonMouseListener ml, MouseEvent e){ |
125 |
super();
|
126 |
this.ml = ml;
|
127 |
this.e = e;
|
128 |
} |
129 |
|
130 |
public void run() { |
131 |
JButton b = (JButton) e.getComponent(); |
132 |
|
133 |
long time = System.currentTimeMillis(); |
134 |
long delay = 200; |
135 |
|
136 |
while (ml.pressed) {
|
137 |
if (ml.in) {
|
138 |
accum.actionPerformed(new ActionEvent(b, 12431, b.getActionCommand())); |
139 |
|
140 |
long currTime = System.currentTimeMillis(); |
141 |
if (delay > 5 && ((currTime - time) > 1000)) { |
142 |
delay /= 2;
|
143 |
time = currTime; |
144 |
} |
145 |
} else time = System.currentTimeMillis(); |
146 |
try {
|
147 |
Thread.sleep(delay);
|
148 |
} catch (InterruptedException e1) { |
149 |
e.consume(); |
150 |
} |
151 |
|
152 |
} |
153 |
e.consume(); |
154 |
} |
155 |
} |
156 |
|
157 |
private class ButtonMouseListener implements MouseListener{ |
158 |
boolean in = false; |
159 |
boolean pressed = false; |
160 |
|
161 |
public void mouseClicked(MouseEvent e) { /* nothing (managed by the ActionListener) */ } |
162 |
|
163 |
public void mouseEntered(MouseEvent e) { |
164 |
in = true;
|
165 |
} |
166 |
|
167 |
public void mouseExited(MouseEvent e) { |
168 |
in = false;
|
169 |
} |
170 |
|
171 |
public void mousePressed(MouseEvent e) { |
172 |
MousePressedTask task; |
173 |
synchronized (this) { |
174 |
pressed = true;
|
175 |
Timer timer = new Timer(); |
176 |
task = new MousePressedTask(this, e); |
177 |
timer.schedule(task, 500);
|
178 |
|
179 |
} |
180 |
if (!pressed) {
|
181 |
task.cancel(); |
182 |
} |
183 |
|
184 |
} |
185 |
|
186 |
public void mouseReleased(MouseEvent e) { |
187 |
pressed = false;
|
188 |
} |
189 |
|
190 |
}; |
191 |
|
192 |
|
193 |
public JIncrementalNumberField() {
|
194 |
this(""); |
195 |
} |
196 |
|
197 |
public JIncrementalNumberField(String text) { |
198 |
this(text, 7); |
199 |
} |
200 |
|
201 |
public JIncrementalNumberField(String text, int columns) { |
202 |
this(text, columns, ValidatingTextField.DOUBLE_VALIDATOR, ValidatingTextField.NUMBER_CLEANER, -Double.MAX_VALUE, Double.MAX_VALUE, 1); |
203 |
} |
204 |
|
205 |
public JIncrementalNumberField(String text, int columns, double minValue, double maxValue, double step) { |
206 |
this(text, columns, ValidatingTextField.DOUBLE_VALIDATOR, ValidatingTextField.NUMBER_CLEANER, minValue, maxValue, step);
|
207 |
} |
208 |
|
209 |
public JIncrementalNumberField(String text, int columns, Validator validator, Cleaner cleaner, double minValue, double maxValue, double step) { |
210 |
super();
|
211 |
if (text == null) text = ""; |
212 |
|
213 |
this.minValue = minValue;
|
214 |
this.maxValue = maxValue;
|
215 |
this.step = step;
|
216 |
acceptsDoubles = validator.getClass().equals(ValidatingTextField.DOUBLE_VALIDATOR.getClass()); |
217 |
|
218 |
JPanel lateralButtons = new JPanel(); |
219 |
Icon upIcon = new Icon() { |
220 |
|
221 |
public int getIconHeight() { |
222 |
return 5; |
223 |
} |
224 |
|
225 |
public int getIconWidth() { |
226 |
return 9; |
227 |
} |
228 |
|
229 |
public void paintIcon(Component c, Graphics g, int x, int y) { |
230 |
g.setColor(Color.DARK_GRAY);
|
231 |
((Graphics2D) g).setStroke(new BasicStroke(2)); |
232 |
g.drawLine(3, 6, 5, 3); |
233 |
g.drawLine(5, 3, 8, 6); |
234 |
|
235 |
} |
236 |
}; |
237 |
Icon downIcon = new Icon() { |
238 |
public int getIconHeight() { |
239 |
return 5; |
240 |
} |
241 |
|
242 |
public int getIconWidth() { |
243 |
return 9; |
244 |
} |
245 |
|
246 |
public void paintIcon(Component c, Graphics g, int x, int y) { |
247 |
g.setColor(Color.DARK_GRAY);
|
248 |
((Graphics2D) g).setStroke(new BasicStroke(2)); |
249 |
g.drawLine(3, 3, 5, 6); |
250 |
g.drawLine(5, 6, 8, 3); |
251 |
|
252 |
|
253 |
} |
254 |
}; |
255 |
up = new JButton(upIcon); |
256 |
up.setActionCommand("UP");
|
257 |
up.addActionListener(accum); |
258 |
up.addMouseListener(new ButtonMouseListener());
|
259 |
up.setBounds(0, 0, 11, 11); |
260 |
|
261 |
down = new JButton(downIcon); |
262 |
down.setActionCommand("DOWN");
|
263 |
down.addActionListener(accum); |
264 |
down.addMouseListener(new ButtonMouseListener());
|
265 |
down.setBounds(0, 11, 11, 11); |
266 |
|
267 |
|
268 |
|
269 |
lateralButtons.setLayout(null);
|
270 |
lateralButtons.setSize(13, 20); |
271 |
lateralButtons.add(up); |
272 |
lateralButtons.add(down); |
273 |
lateralButtons.setSize(new Dimension(11, 22)); |
274 |
lateralButtons.setPreferredSize(new Dimension(11, 22)); |
275 |
lateralButtons.setBorder(BorderFactory.createLineBorder(Color.GREEN)); |
276 |
vtf = new ValidatingTextField(
|
277 |
text, |
278 |
columns, |
279 |
SwingConstants.RIGHT,
|
280 |
validator, |
281 |
cleaner) ; |
282 |
setLayout(new BorderLayout(0, 0)); |
283 |
add(vtf, BorderLayout.CENTER);
|
284 |
add(lateralButtons, BorderLayout.EAST);
|
285 |
} |
286 |
|
287 |
public int getInteger() { |
288 |
return vtf.getInteger();
|
289 |
} |
290 |
|
291 |
public double getDouble() { |
292 |
if (!acceptsDoubles)
|
293 |
throw new Error(Messages.getText( |
294 |
"cannot_get_double_value_from_an_integer_number_field_use_getInteger()_instead"));
|
295 |
return vtf.getDouble();
|
296 |
} |
297 |
|
298 |
public void setDouble(double v) { |
299 |
if (!acceptsDoubles)
|
300 |
throw new Error(Messages.getText( |
301 |
"cannot_set_a_double_value_from_an_integer_number_field_use_setInteger(int)_instead"));
|
302 |
vtf.setText(String.valueOf(v));
|
303 |
} |
304 |
|
305 |
public void setInteger(int v) { |
306 |
vtf.setText(String.valueOf(v));
|
307 |
} |
308 |
|
309 |
public void addActionListener(ActionListener l) { |
310 |
vtf.addActionListener(l); |
311 |
listeners .add(l); |
312 |
} |
313 |
|
314 |
private void fireActionPerformed() { |
315 |
ActionEvent evt = new ActionEvent(this, 0, null); |
316 |
for (int i = 0; i < listeners.size(); i++) { |
317 |
((ActionListener) listeners.get(i)).actionPerformed(evt);
|
318 |
} |
319 |
} |
320 |
|
321 |
public double getMaxValue() { |
322 |
return maxValue;
|
323 |
} |
324 |
|
325 |
public void setMaxValue(double maxValue) { |
326 |
this.maxValue = maxValue;
|
327 |
} |
328 |
|
329 |
public double getMinValue() { |
330 |
return minValue;
|
331 |
} |
332 |
|
333 |
public void setMinValue(double minValue) { |
334 |
this.minValue = minValue;
|
335 |
} |
336 |
|
337 |
public double getStep() { |
338 |
return step;
|
339 |
} |
340 |
|
341 |
public void setStep(double step) { |
342 |
this.step = step;
|
343 |
} |
344 |
} |