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