root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / doubleslider / DoubleSlider.java @ 12153
History | View | Annotate | Download (11.8 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 |
package org.gvsig.gui.beans.doubleslider; |
20 |
|
21 |
import java.awt.Color; |
22 |
import java.awt.Dimension; |
23 |
import java.awt.Graphics; |
24 |
import java.awt.Image; |
25 |
import java.awt.event.MouseEvent; |
26 |
import java.awt.event.MouseListener; |
27 |
import java.awt.event.MouseMotionListener; |
28 |
import java.awt.event.MouseWheelEvent; |
29 |
import java.awt.event.MouseWheelListener; |
30 |
import java.util.ArrayList; |
31 |
import java.util.Iterator; |
32 |
|
33 |
import javax.swing.JComponent; |
34 |
/**
|
35 |
* <code>DoubleSlider</code> representa un componente que tiene dos
|
36 |
* deslizadores. Se puede definir un m?ximo y un m?nimo.
|
37 |
*
|
38 |
* @version 04/05/2007
|
39 |
* @author Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
40 |
*/
|
41 |
public class DoubleSlider extends JComponent implements MouseMotionListener, MouseListener, MouseWheelListener { |
42 |
private static final long serialVersionUID = 663355422780987493L; |
43 |
|
44 |
private ArrayList actionCommandListeners = new ArrayList(); |
45 |
|
46 |
private Image bufferImage = null; |
47 |
private int width = 0; |
48 |
private int height = 0; |
49 |
private Graphics bufferGraphics; |
50 |
private int x1 = 0; |
51 |
private int x2 = 100; |
52 |
|
53 |
private Color color1 = Color.BLACK; |
54 |
private Color color2 = Color.WHITE; |
55 |
|
56 |
private int minimum = 0; |
57 |
private int maximum = 100; |
58 |
|
59 |
private boolean twoSliders = true; |
60 |
|
61 |
/**
|
62 |
* Crea un DoubleSlider con las opciones por defecto.
|
63 |
*/
|
64 |
public DoubleSlider() {
|
65 |
this.setPreferredSize(new Dimension(100, 21)); |
66 |
addMouseMotionListener(this);
|
67 |
addMouseListener(this);
|
68 |
addMouseWheelListener(this);
|
69 |
} |
70 |
|
71 |
/**
|
72 |
* Establece el m?ximo valor que puede tomar el componente
|
73 |
* @param value
|
74 |
*/
|
75 |
public void setMaximum(int value) { |
76 |
maximum = value; |
77 |
validateValues(); |
78 |
refreshImage(); |
79 |
} |
80 |
|
81 |
/**
|
82 |
* Establece el m?nimo valor que puede tomar el componente
|
83 |
* @param value
|
84 |
*/
|
85 |
public void setMinimum(int value) { |
86 |
minimum = value; |
87 |
validateValues(); |
88 |
refreshImage(); |
89 |
} |
90 |
|
91 |
/*
|
92 |
* (non-Javadoc)
|
93 |
* @see javax.swing.JComponent#addNotify()
|
94 |
*/
|
95 |
public void addNotify() { |
96 |
super.addNotify();
|
97 |
|
98 |
refreshImage(); |
99 |
} |
100 |
|
101 |
/**
|
102 |
* Crea un graphics con las dimensiones del componente si no estaba creado y
|
103 |
* lo devuelve para poder usarlo para dibujar en ?l.
|
104 |
* @return Graphics
|
105 |
*/
|
106 |
private Graphics getBufferGraphics() { |
107 |
int width2 = getBounds().width;
|
108 |
int height2 = getBounds().height;
|
109 |
if (width2 <= 0) |
110 |
width2 = 1;
|
111 |
if (height2 <= 0) |
112 |
height2 = 1;
|
113 |
|
114 |
if ((width!=width2) || (height!=height2)) {
|
115 |
bufferImage = createImage(width2, height2); |
116 |
if (bufferImage == null) return null; |
117 |
bufferGraphics = bufferImage.getGraphics(); |
118 |
} |
119 |
|
120 |
width = width2; |
121 |
height = height2; |
122 |
|
123 |
return bufferGraphics;
|
124 |
} |
125 |
|
126 |
public Color getColorPosition(int pos) { |
127 |
int r, g, b;
|
128 |
|
129 |
Color color1 = this.color1; |
130 |
Color color2 = this.color2; |
131 |
if (!isEnabled()) {
|
132 |
r = Color.DARK_GRAY.getRed();
|
133 |
color1 = new Color(r, r, r); |
134 |
r = Color.LIGHT_GRAY.getRed();
|
135 |
r = Color.WHITE.getRed();
|
136 |
color2 = new Color(r, r, r); |
137 |
} |
138 |
|
139 |
if ((width - 1) == 0) |
140 |
return Color.BLACK; |
141 |
if (pos < 1) |
142 |
pos = 1;
|
143 |
if (pos > (width - 1)) |
144 |
pos = width - 1;
|
145 |
r = (color1.getRed() + ((color2.getRed() - color1.getRed()) * pos) / (width - 1));
|
146 |
g = (color1.getGreen() + ((color2.getGreen() - color1.getGreen()) * pos) / (width - 1));
|
147 |
b = (color1.getBlue() + ((color2.getBlue() - color1.getBlue()) * pos) / (width - 1));
|
148 |
return new Color(r, g, b); |
149 |
} |
150 |
/**
|
151 |
* Redibujar el componente en el graphics temporal
|
152 |
*/
|
153 |
public void redrawBuffer() { |
154 |
if (getBufferGraphics() == null) return; |
155 |
getBufferGraphics().setColor(this.getBackground());
|
156 |
|
157 |
getBufferGraphics().fillRect(0,0,width,height); |
158 |
|
159 |
int top = (height - 11)/2; |
160 |
top -= 6;
|
161 |
for (int i=1; i<=(width-1); i++) { |
162 |
getBufferGraphics().setColor(getColorPosition(i)); |
163 |
getBufferGraphics().drawLine(i,top,i,top+10);
|
164 |
} |
165 |
|
166 |
if (!isEnabled())
|
167 |
getBufferGraphics().setColor(Color.LIGHT_GRAY);
|
168 |
else
|
169 |
getBufferGraphics().setColor(Color.BLACK);
|
170 |
getBufferGraphics().drawRect(1, top, width - 2, 10); |
171 |
|
172 |
drawTriangle(valuetopixel(x1), top, getColorPosition(valuetopixel(x1))); |
173 |
if (twoSliders)
|
174 |
drawTriangle(valuetopixel(x2), top, getColorPosition(valuetopixel(x2))); |
175 |
} |
176 |
|
177 |
/**
|
178 |
* Dibujar un triangulo, un triangulo es un deslizador del componente. Puedes
|
179 |
* indicarle que color tendra y en que posici?n estar?.
|
180 |
* @param x
|
181 |
* @param color
|
182 |
*/
|
183 |
private void drawTriangle(int x, int y, Color color) { |
184 |
if (isEnabled()) {
|
185 |
getBufferGraphics().setColor(color); |
186 |
getBufferGraphics().drawLine(x, y + 12, x, y + 16); |
187 |
getBufferGraphics().drawLine(x-1, y + 14, x-1, y + 16); |
188 |
getBufferGraphics().drawLine(x+1, y + 14, x+1, y + 16); |
189 |
getBufferGraphics().drawLine(x-2, y + 16, x-2, y + 16); |
190 |
getBufferGraphics().drawLine(x+2, y + 16, x+2, y + 16); |
191 |
} |
192 |
|
193 |
if (isEnabled()) {
|
194 |
getBufferGraphics().setColor(Color.BLACK);
|
195 |
} else {
|
196 |
getBufferGraphics().setColor(Color.GRAY);
|
197 |
} |
198 |
getBufferGraphics().drawLine(x, y + 10, x-3, y + 17); |
199 |
getBufferGraphics().drawLine(x, y + 10, x+3, y + 17); |
200 |
getBufferGraphics().drawLine(x-3, y + 17, x+3, y + 17); |
201 |
} |
202 |
|
203 |
/**
|
204 |
* Redibujar el componente en el graphics temporal y representarlo en el
|
205 |
* componente
|
206 |
*/
|
207 |
public void refreshImage() { |
208 |
redrawBuffer(); |
209 |
if (bufferImage != null) |
210 |
getGraphics().drawImage(bufferImage, 0, 0, this); |
211 |
super.paint(getGraphics());
|
212 |
} |
213 |
|
214 |
/**
|
215 |
* Convierte un valor a la coordenada pixel correspondiente
|
216 |
* @param value
|
217 |
* @return
|
218 |
*/
|
219 |
private int valuetopixel(int value) { |
220 |
return ((value-minimum)*(width-3)/(maximum-minimum)) + 1; |
221 |
} |
222 |
|
223 |
/**
|
224 |
* Convierte un pixel al valor que deber?a tener
|
225 |
* @param value
|
226 |
* @return
|
227 |
*/
|
228 |
private int pixeltovalue(int value) { |
229 |
return (((value-1)*(maximum-minimum))/(width-3))+minimum; |
230 |
} |
231 |
|
232 |
/*
|
233 |
* (non-Javadoc)
|
234 |
* @see javax.swing.JComponent#paint(java.awt.Graphics)
|
235 |
*/
|
236 |
public void paint(Graphics g) { |
237 |
redrawBuffer(); |
238 |
g.drawImage(bufferImage, 0, 0, this); |
239 |
super.paint(g);
|
240 |
} |
241 |
|
242 |
int valuePressed = 0; |
243 |
|
244 |
/*
|
245 |
* (non-Javadoc)
|
246 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
247 |
*/
|
248 |
public void mousePressed(MouseEvent e) { |
249 |
if (!isEnabled())
|
250 |
return;
|
251 |
if (e.getButton() != MouseEvent.BUTTON1) return; |
252 |
|
253 |
int aux = pixeltovalue(e.getX()+1); |
254 |
int aux2 = aux - x1;
|
255 |
int aux3 = x2 - aux;
|
256 |
if (aux3 < aux2)
|
257 |
valuePressed = 2;
|
258 |
else
|
259 |
valuePressed = 1;
|
260 |
|
261 |
if (!twoSliders)
|
262 |
valuePressed = 1;
|
263 |
|
264 |
changeValue(e.getX()); |
265 |
} |
266 |
|
267 |
/*
|
268 |
* (non-Javadoc)
|
269 |
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
270 |
*/
|
271 |
public void mouseReleased(MouseEvent e) { |
272 |
valuePressed = 0;
|
273 |
callValueChangedListeners(); |
274 |
} |
275 |
|
276 |
/**
|
277 |
* Establecer los l?mites de los valores en caso de que sean incorrectos
|
278 |
*/
|
279 |
private void validateValues() { |
280 |
if (x1 < minimum) x1 = minimum;
|
281 |
if (x1 > maximum) x1 = maximum;
|
282 |
if (twoSliders) {
|
283 |
if (x2 < minimum) x2 = minimum;
|
284 |
if (x2 > maximum) x2 = maximum;
|
285 |
} |
286 |
} |
287 |
|
288 |
/**
|
289 |
* Establecer el valor del extremo izquierdo del slider
|
290 |
* @param value
|
291 |
*/
|
292 |
public void setX1(int value) { |
293 |
x1 = value; |
294 |
if (twoSliders)
|
295 |
if (x1 > x2)
|
296 |
x2 = x1; |
297 |
validateValues(); |
298 |
refreshImage(); |
299 |
} |
300 |
|
301 |
/**
|
302 |
* Es lo mismo que setX1()
|
303 |
* @param value
|
304 |
*/
|
305 |
public void setValue(int value) { |
306 |
setX1(value); |
307 |
} |
308 |
|
309 |
/**
|
310 |
* Establecer el valor del extremo derecho del slider
|
311 |
* @param value
|
312 |
*/
|
313 |
public void setX2(int value) { |
314 |
x2 = value; |
315 |
if (x2 < x1) x1 = x2;
|
316 |
validateValues(); |
317 |
refreshImage(); |
318 |
} |
319 |
|
320 |
/**
|
321 |
* Obtener el valor del extremo izquierdo del componente
|
322 |
* @return
|
323 |
*/
|
324 |
public int getX1() { |
325 |
return x1;
|
326 |
} |
327 |
|
328 |
/**
|
329 |
* Devuelve lo mismo que getX1()
|
330 |
* @return
|
331 |
*/
|
332 |
public int getValue() { |
333 |
return getX1();
|
334 |
} |
335 |
|
336 |
/**
|
337 |
* Obtener el valor del extremo derecho del componente
|
338 |
* @return
|
339 |
*/
|
340 |
public int getX2() { |
341 |
return x2;
|
342 |
} |
343 |
|
344 |
/**
|
345 |
* M?todo usado por los eventos del rat?n para establecer una nueva posici?n
|
346 |
* del slider
|
347 |
* @param pos
|
348 |
*/
|
349 |
private void changeValue(int pos) { |
350 |
if (!isEnabled())
|
351 |
return;
|
352 |
if (valuePressed == 0) return; |
353 |
|
354 |
int aux = pixeltovalue(pos + 1); |
355 |
|
356 |
if (valuePressed == 1) setX1(aux); |
357 |
if (valuePressed == 2) setX2(aux); |
358 |
} |
359 |
|
360 |
/*
|
361 |
* (non-Javadoc)
|
362 |
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
363 |
*/
|
364 |
public void mouseDragged(MouseEvent arg0) { |
365 |
if (!isEnabled())
|
366 |
return;
|
367 |
changeValue(arg0.getX()); |
368 |
callValueDraggedListeners(); |
369 |
} |
370 |
|
371 |
/*
|
372 |
* (non-Javadoc)
|
373 |
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
374 |
*/
|
375 |
public void mouseMoved(MouseEvent e) { |
376 |
} |
377 |
|
378 |
/*
|
379 |
* (non-Javadoc)
|
380 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
381 |
*/
|
382 |
public void mouseClicked(MouseEvent e) { |
383 |
} |
384 |
|
385 |
/*
|
386 |
* (non-Javadoc)
|
387 |
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
388 |
*/
|
389 |
public void mouseEntered(MouseEvent e) { |
390 |
} |
391 |
|
392 |
/*
|
393 |
* (non-Javadoc)
|
394 |
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
395 |
*/
|
396 |
public void mouseExited(MouseEvent e) { |
397 |
} |
398 |
|
399 |
/**
|
400 |
* A?adir un listener a la lista de eventos
|
401 |
* @param listener
|
402 |
*/
|
403 |
public void addValueChangedListener(DoubleSliderListener listener) { |
404 |
if (!actionCommandListeners.contains(listener))
|
405 |
actionCommandListeners.add(listener); |
406 |
} |
407 |
|
408 |
/**
|
409 |
* Borrar un listener de la lista de eventos
|
410 |
* @param listener
|
411 |
*/
|
412 |
public void removeValueChangedListener(DoubleSliderListener listener) { |
413 |
actionCommandListeners.remove(listener); |
414 |
} |
415 |
|
416 |
/**
|
417 |
* Invocar a los eventos asociados al componente
|
418 |
*/
|
419 |
private void callValueChangedListeners() { |
420 |
Iterator acIterator = actionCommandListeners.iterator();
|
421 |
while (acIterator.hasNext()) {
|
422 |
DoubleSliderListener listener = (DoubleSliderListener) acIterator.next(); |
423 |
listener.actionValueChanged(new DoubleSliderEvent(this)); |
424 |
} |
425 |
} |
426 |
|
427 |
/**
|
428 |
* Invocar a los eventos asociados al componente
|
429 |
*/
|
430 |
private void callValueDraggedListeners() { |
431 |
Iterator acIterator = actionCommandListeners.iterator();
|
432 |
while (acIterator.hasNext()) {
|
433 |
DoubleSliderListener listener = (DoubleSliderListener) acIterator.next(); |
434 |
listener.actionValueDragged(new DoubleSliderEvent(this)); |
435 |
} |
436 |
} |
437 |
|
438 |
/**
|
439 |
* @return the twoSliders
|
440 |
*/
|
441 |
public boolean isTwoSliders() { |
442 |
return twoSliders;
|
443 |
} |
444 |
|
445 |
/**
|
446 |
* @param twoSliders the twoSliders to set
|
447 |
*/
|
448 |
public void setTwoSliders(boolean twoSliders) { |
449 |
this.twoSliders = twoSliders;
|
450 |
refreshImage(); |
451 |
} |
452 |
|
453 |
/**
|
454 |
* @param color1 the color1 to set
|
455 |
*/
|
456 |
public void setColor1(Color color1) { |
457 |
this.color1 = color1;
|
458 |
refreshImage(); |
459 |
} |
460 |
|
461 |
/**
|
462 |
* @param color2 the color2 to set
|
463 |
*/
|
464 |
public void setColor2(Color color2) { |
465 |
this.color2 = color2;
|
466 |
refreshImage(); |
467 |
} |
468 |
|
469 |
/* (non-Javadoc)
|
470 |
* @see javax.swing.JComponent#setEnabled(boolean)
|
471 |
*/
|
472 |
public void setEnabled(boolean enabled) { |
473 |
super.setEnabled(enabled);
|
474 |
refreshImage(); |
475 |
} |
476 |
|
477 |
public void mouseWheelMoved(MouseWheelEvent e) { |
478 |
if (!isEnabled())
|
479 |
return;
|
480 |
|
481 |
int aux = pixeltovalue(e.getX()+1); |
482 |
int aux2 = aux - x1;
|
483 |
int aux3 = x2 - aux;
|
484 |
if (aux3 < aux2)
|
485 |
valuePressed = 2;
|
486 |
else
|
487 |
valuePressed = 1;
|
488 |
|
489 |
if (!twoSliders)
|
490 |
valuePressed = 1;
|
491 |
|
492 |
if (valuePressed == 1) |
493 |
setX1(getX1() - e.getWheelRotation()); |
494 |
else
|
495 |
setX2(getX2() - e.getWheelRotation()); |
496 |
valuePressed = 0;
|
497 |
|
498 |
callValueChangedListeners(); |
499 |
} |
500 |
} |