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