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 | 13136 | evercher | /* 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 | 26897 | vcaballero | import java.awt.event.FocusEvent; |
53 | import java.awt.event.FocusListener; |
||
54 | 13136 | evercher | import java.awt.event.MouseEvent; |
55 | import java.awt.event.MouseListener; |
||
56 | import java.util.ArrayList; |
||
57 | 26897 | vcaballero | import java.util.Arrays; |
58 | import java.util.EventListener; |
||
59 | import java.util.Iterator; |
||
60 | 13136 | evercher | 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 | 26897 | vcaballero | import org.gvsig.gui.beans.Messages; |
70 | 13136 | evercher | 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 | 25736 | fdiaz | private ActionListener propage = new ActionListener() { |
88 | public void actionPerformed(ActionEvent e) { |
||
89 | 26897 | vcaballero | if (!isEnabled()) return; |
90 | 25736 | fdiaz | if (acceptsDoubles) {
|
91 | double v = getDouble();
|
||
92 | 26897 | vcaballero | if (v>maxValue)
|
93 | 25736 | fdiaz | v = maxValue; |
94 | 26897 | vcaballero | if (v<minValue)
|
95 | 25736 | fdiaz | v = minValue; |
96 | setDouble(v); |
||
97 | } else {
|
||
98 | int v = getInteger();
|
||
99 | 26897 | vcaballero | if (v>maxValue)
|
100 | 25736 | fdiaz | v = (int) maxValue;
|
101 | 26897 | vcaballero | if (v<minValue)
|
102 | 25736 | fdiaz | v = (int) minValue;
|
103 | setInteger(v); |
||
104 | } |
||
105 | fireActionPerformed(e); |
||
106 | } |
||
107 | }; |
||
108 | 13136 | evercher | |
109 | 26897 | vcaballero | 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 | 13136 | evercher | private ActionListener accum = new ActionListener() { |
146 | public void actionPerformed(ActionEvent e) { |
||
147 | 26897 | vcaballero | if (!isEnabled()) return; |
148 | 13136 | evercher | String command = e.getActionCommand();
|
149 | if ("UP".equals(command)) { |
||
150 | if (acceptsDoubles) {
|
||
151 | double v = getDouble() + step;
|
||
152 | 26897 | vcaballero | if (v>maxValue)
|
153 | 13136 | evercher | v = maxValue; |
154 | setDouble(v); |
||
155 | } else {
|
||
156 | int v = getInteger() + (int) Math.round(step); |
||
157 | 26897 | vcaballero | if (v>maxValue)
|
158 | 13136 | evercher | v = (int) maxValue;
|
159 | setInteger(v); |
||
160 | } |
||
161 | } else if ("DOWN".equals(command)) { |
||
162 | if (acceptsDoubles) {
|
||
163 | 26897 | vcaballero | double v = getDouble();// - step; |
164 | v = v - step; |
||
165 | if (v<minValue)
|
||
166 | 13136 | evercher | v = minValue; |
167 | setDouble(v); |
||
168 | |||
169 | } else {
|
||
170 | int v = getInteger() - (int) Math.round(step); |
||
171 | 26897 | vcaballero | if (v<minValue)
|
172 | 13136 | evercher | v = (int) minValue;
|
173 | setInteger(v); |
||
174 | } |
||
175 | } |
||
176 | fireActionPerformed(); |
||
177 | } |
||
178 | }; |
||
179 | |||
180 | private JButton down; |
||
181 | private JButton up; |
||
182 | 26897 | vcaballero | private ArrayList<EventListener> listeners = new ArrayList<EventListener>(); |
183 | 13136 | evercher | |
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 | 26897 | vcaballero | } else time = System.currentTimeMillis(); |
210 | 13136 | evercher | 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 | 17478 | jdominguez | this(text, columns, ValidatingTextField.DOUBLE_VALIDATOR, ValidatingTextField.NUMBER_CLEANER_2_DECIMALS, -Double.MAX_VALUE, Double.MAX_VALUE, 1); |
267 | 13136 | evercher | } |
268 | |||
269 | public JIncrementalNumberField(String text, int columns, double minValue, double maxValue, double step) { |
||
270 | 17478 | jdominguez | this(text, columns, ValidatingTextField.DOUBLE_VALIDATOR, ValidatingTextField.NUMBER_CLEANER_2_DECIMALS, minValue, maxValue, step);
|
271 | 13136 | evercher | } |
272 | |||
273 | public JIncrementalNumberField(String text, int columns, Validator validator, Cleaner cleaner, double minValue, double maxValue, double step) { |
||
274 | super();
|
||
275 | 26897 | vcaballero | if (text == null) text = ""; |
276 | 13136 | evercher | |
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 | 13960 | jvidal | g.setColor( isEnabled() ? Color.DARK_GRAY : Color.RED); |
295 | 13136 | evercher | ((Graphics2D) g).setStroke(new BasicStroke(2)); |
296 | 13960 | jvidal | g.drawLine(isEnabled() ? 3 : 1, |
297 | isEnabled() ? 6 : 4, |
||
298 | isEnabled() ? 5 : 3, |
||
299 | isEnabled() ? 3 : 1); |
||
300 | 13136 | evercher | |
301 | 13960 | jvidal | g.drawLine(isEnabled() ? 5 : 3, |
302 | isEnabled() ? 3 : 1, |
||
303 | isEnabled() ? 8 : 6, |
||
304 | isEnabled() ? 6 : 4); |
||
305 | |||
306 | 13136 | evercher | } |
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 | 13960 | jvidal | g.setColor(isEnabled() ? Color.DARK_GRAY : Color.RED); |
319 | 13136 | evercher | ((Graphics2D) g).setStroke(new BasicStroke(2)); |
320 | |||
321 | |||
322 | 13960 | jvidal | 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 | 13136 | evercher | } |
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 | 26897 | vcaballero | up.setFocusable(false);
|
340 | 13136 | evercher | |
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 | 26897 | vcaballero | down.setFocusable(false);
|
347 | 13136 | evercher | |
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 | 25736 | fdiaz | vtf.addActionListener(propage); |
365 | 26897 | vcaballero | vtf.addFocusListener(propageFocus); |
366 | 13136 | evercher | 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 | 26897 | vcaballero | if (!acceptsDoubles)
|
376 | throw new Error(Messages.getText( |
||
377 | "cannot_get_double_value_from_an_integer_number_field_use_getInteger()_instead"));
|
||
378 | 13136 | evercher | return vtf.getDouble();
|
379 | } |
||
380 | |||
381 | public void setDouble(double v) { |
||
382 | 26897 | vcaballero | if (!acceptsDoubles)
|
383 | throw new Error(Messages.getText( |
||
384 | "cannot_set_a_double_value_from_an_integer_number_field_use_setInteger(int)_instead"));
|
||
385 | 13136 | evercher | 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 | 25736 | fdiaz | // vtf.addActionListener(l);
|
394 | 17165 | jdominguez | listeners.add(l); |
395 | 13136 | evercher | } |
396 | |||
397 | 17165 | jdominguez | public void removeActionListener(ActionListener l) { |
398 | 25736 | fdiaz | // vtf.removeActionListener(l);
|
399 | 17165 | jdominguez | listeners.remove(l); |
400 | } |
||
401 | |||
402 | 13136 | evercher | private void fireActionPerformed() { |
403 | ActionEvent evt = new ActionEvent(this, 0, null); |
||
404 | for (int i = 0; i < listeners.size(); i++) { |
||
405 | 26897 | vcaballero | ((ActionListener) listeners.get(i)).actionPerformed(evt);
|
406 | 13136 | evercher | } |
407 | } |
||
408 | |||
409 | 25736 | fdiaz | private void fireActionPerformed(ActionEvent e) { |
410 | e.setSource(this);
|
||
411 | for (int i = 0; i < listeners.size(); i++) { |
||
412 | 26897 | vcaballero | ((ActionListener) listeners.get(i)).actionPerformed(e);
|
413 | 25736 | fdiaz | } |
414 | } |
||
415 | |||
416 | 13136 | evercher | 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 | 13960 | jvidal | |
440 | public void setEnabled(boolean enabled) { |
||
441 | super.setEnabled(enabled);
|
||
442 | up.setEnabled(enabled); |
||
443 | down.setEnabled(enabled); |
||
444 | vtf.setEnabled(enabled); |
||
445 | } |
||
446 | 13136 | evercher | } |