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