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