Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / doubleslider / DoubleSlider.java @ 12127

History | View | Annotate | Download (11.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.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
        private boolean  bDoCallListeners = true;
62

    
63
        /**
64
         * Crea un DoubleSlider con las opciones por defecto.
65
         */
66
        public DoubleSlider() {
67
                this.setPreferredSize(new Dimension(100, 21));
68
        }
69

    
70
        /**
71
         * Establece el m?ximo valor que puede tomar el componente
72
         * @param value
73
         */
74
        public void setMaximum(int value) {
75
                maximum = value;
76
                validateValues();
77
                refreshImage();
78
        }
79

    
80
        /**
81
         * Establece el m?nimo valor que puede tomar el componente
82
         * @param value
83
         */
84
        public void setMinimum(int value) {
85
                minimum = value;
86
                validateValues();
87
                refreshImage();
88
        }
89

    
90
        /*
91
         * (non-Javadoc)
92
         * @see javax.swing.JComponent#addNotify()
93
         */
94
        public void addNotify() {
95
                super.addNotify();
96

    
97
                addMouseMotionListener(this);
98
                addMouseListener(this);
99
                addMouseWheelListener(this);
100

    
101
                refreshImage();
102
        }
103

    
104
        /**
105
         * Crea un graphics con las dimensiones del componente si no estaba creado y
106
         * lo devuelve para poder usarlo para dibujar en ?l.
107
         * @return Graphics
108
         */
109
        private Graphics getBufferGraphics() {
110
                int width2 = getBounds().width;
111
                int height2 = getBounds().height;
112
                if (width2 <= 0)
113
                        width2 = 1;
114
                if (height2 <= 0)
115
                        height2 = 1;
116

    
117
                if ((width!=width2) || (height!=height2)) {
118
                        bufferImage = createImage(width2, height2);
119
                        if (bufferImage == null) return null;
120
                        bufferGraphics = bufferImage.getGraphics();
121
                }
122

    
123
                width = width2;
124
                height = height2;
125

    
126
                return bufferGraphics;
127
        }
128

    
129
        public Color getColorPosition(int pos) {
130
                int r, g, b;
131

    
132
                Color color1 = this.color1;
133
                Color color2 = this.color2;
134
                if (!isEnabled()) {
135
                        r = Color.DARK_GRAY.getRed();
136
                        color1 = new Color(r, r, r);
137
                        r = Color.LIGHT_GRAY.getRed();
138
                        r = Color.WHITE.getRed();
139
                        color2 = new Color(r, r, r);
140
                }
141

    
142
                if ((width - 1) == 0)
143
                        return Color.BLACK;
144
                if (pos < 1)
145
                        pos = 1;
146
                if (pos > (width - 1))
147
                        pos = width - 1;
148
                r = (color1.getRed() + ((color2.getRed() - color1.getRed()) * pos) / (width - 1));
149
                g = (color1.getGreen() + ((color2.getGreen() - color1.getGreen()) * pos) / (width - 1));
150
                b = (color1.getBlue() + ((color2.getBlue() - color1.getBlue()) * pos) / (width - 1));
151
                return new Color(r, g, b);
152
        }
153
        /**
154
         * Redibujar el componente en el graphics temporal
155
         */
156
        public void redrawBuffer() {
157
                if (getBufferGraphics() == null) return;
158
                getBufferGraphics().setColor(this.getBackground());
159

    
160
                getBufferGraphics().fillRect(0,0,width,height);
161

    
162
                int top = (height - 11)/2;
163
                top -= 6;
164
                for (int i=1; i<=(width-1); i++) {
165
                        getBufferGraphics().setColor(getColorPosition(i));
166
                        getBufferGraphics().drawLine(i,top,i,top+10);
167
                }
168

    
169
                if (!isEnabled())
170
                        getBufferGraphics().setColor(Color.LIGHT_GRAY);
171
                else
172
                        getBufferGraphics().setColor(Color.BLACK);
173
                getBufferGraphics().drawRect(1, top, width - 2, 10);
174

    
175
                drawTriangle(valuetopixel(x1), top, getColorPosition(valuetopixel(x1)));
176
                if (twoSliders)
177
                        drawTriangle(valuetopixel(x2), top, getColorPosition(valuetopixel(x2)));
178
        }
179

    
180
        /**
181
         * Dibujar un triangulo, un triangulo es un deslizador del componente. Puedes
182
         * indicarle que color tendra y en que posici?n estar?.
183
         * @param x
184
         * @param color
185
         */
186
        private void drawTriangle(int x, int y, Color color) {
187
                if (isEnabled()) {
188
                        getBufferGraphics().setColor(color);
189
                        getBufferGraphics().drawLine(x, y + 12, x, y + 16);
190
                        getBufferGraphics().drawLine(x-1, y + 14, x-1, y + 16);
191
                        getBufferGraphics().drawLine(x+1, y + 14, x+1, y + 16);
192
                        getBufferGraphics().drawLine(x-2, y + 16, x-2, y + 16);
193
                        getBufferGraphics().drawLine(x+2, y + 16, x+2, y + 16);
194
                }
195

    
196
                if (isEnabled()) {
197
                        getBufferGraphics().setColor(Color.BLACK);
198
                } else {
199
                        getBufferGraphics().setColor(Color.GRAY);
200
                }
201
                getBufferGraphics().drawLine(x, y + 10, x-3, y + 17);
202
                getBufferGraphics().drawLine(x, y + 10, x+3, y + 17);
203
                getBufferGraphics().drawLine(x-3, y + 17, x+3, y + 17);
204
        }
205

    
206
        /**
207
         * Redibujar el componente en el graphics temporal y representarlo en el
208
         * componente
209
         */
210
        public void refreshImage() {
211
                redrawBuffer();
212
                if (bufferImage != null)
213
                        getGraphics().drawImage(bufferImage, 0, 0, this);
214
                super.paint(getGraphics());
215
        }
216

    
217
        /**
218
         * Convierte un valor a la coordenada pixel correspondiente
219
         * @param value
220
         * @return
221
         */
222
        private int valuetopixel(int value) {
223
                return ((value-minimum)*(width-3)/(maximum-minimum)) + 1;
224
        }
225

    
226
        /**
227
         * Convierte un pixel al valor que deber?a tener
228
         * @param value
229
         * @return
230
         */
231
        private int pixeltovalue(int value) {
232
                return (((value-1)*(maximum-minimum))/(width-3))+minimum;
233
        }
234

    
235
        /*
236
         * (non-Javadoc)
237
         * @see javax.swing.JComponent#paint(java.awt.Graphics)
238
         */
239
        public void paint(Graphics g) {
240
                redrawBuffer();
241
                g.drawImage(bufferImage, 0, 0, this);
242
                super.paint(g);
243
        }
244

    
245
        int valuePressed = 0;
246

    
247
        /*
248
         * (non-Javadoc)
249
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
250
         */
251
        public void mousePressed(MouseEvent e) {
252
                if (!isEnabled())
253
                        return;
254
                if (e.getButton() != MouseEvent.BUTTON1) return;
255

    
256
                int aux = pixeltovalue(e.getX()+1);
257
                int aux2 = aux - x1;
258
                int aux3 = x2 - aux;
259
                if (aux3 < aux2)
260
                        valuePressed = 2;
261
                else
262
                        valuePressed = 1;
263

    
264
                if (!twoSliders)
265
                        valuePressed = 1;
266

    
267
                changeValue(e.getX());
268
        }
269

    
270
        /*
271
         * (non-Javadoc)
272
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
273
         */
274
        public void mouseReleased(MouseEvent e) {
275
                valuePressed = 0;
276
        }
277

    
278
        /**
279
         * Establecer los l?mites de los valores en caso de que sean incorrectos
280
         */
281
        private void validateValues() {
282
                if (x1 < minimum) x1 = minimum;
283
                if (x1 > maximum) x1 = maximum;
284
                if (twoSliders) {
285
                        if (x2 < minimum) x2 = minimum;
286
                        if (x2 > maximum) x2 = maximum;
287
                }
288
        }
289

    
290
        /**
291
         * Establecer el valor del extremo izquierdo del slider
292
         * @param value
293
         */
294
        public void setX1(int value) {
295
                x1 = value;
296
                if (twoSliders)
297
                        if (x1 > x2)
298
                                x2 = x1;
299
                validateValues();
300
                refreshImage();
301
        }
302

    
303
        /**
304
         * Es lo mismo que setX1()
305
         * @param value
306
         */
307
        public void setValue(int value) {
308
                setX1(value);
309
        }
310

    
311
        /**
312
         * Establecer el valor del extremo derecho del slider
313
         * @param value
314
         */
315
        public void setX2(int value) {
316
                x2 = value;
317
                if (x2 < x1) x1 = x2;
318
                validateValues();
319
                refreshImage();
320
        }
321

    
322
        /**
323
         * Obtener el valor del extremo izquierdo del componente
324
         * @return
325
         */
326
        public int getX1() {
327
                return x1;
328
        }
329

    
330
        /**
331
         * Devuelve lo mismo que getX1()
332
         * @return
333
         */
334
        public int getValue() {
335
                return getX1();
336
        }
337

    
338
        /**
339
         * Obtener el valor del extremo derecho del componente
340
         * @return
341
         */
342
        public int getX2() {
343
                return x2;
344
        }
345

    
346
        /**
347
         * M?todo usado por los eventos del rat?n para establecer una nueva posici?n
348
         * del slider
349
         * @param pos
350
         */
351
        private void changeValue(int pos) {
352
                if (!isEnabled())
353
                        return;
354
                if (valuePressed == 0) return;
355

    
356
                int aux = pixeltovalue(pos + 1);
357

    
358
                if (valuePressed == 1) setX1(aux);
359
                if (valuePressed == 2) setX2(aux);
360
                callValueChangedListeners();
361
        }
362

    
363
        /*
364
         * (non-Javadoc)
365
         * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
366
         */
367
        public void mouseDragged(MouseEvent arg0) {
368
                if (!isEnabled())
369
                        return;
370
                changeValue(arg0.getX());
371
        }
372

    
373
        /*
374
         * (non-Javadoc)
375
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
376
         */
377
        public void mouseMoved(MouseEvent e) {
378
        }
379

    
380
        /*
381
         * (non-Javadoc)
382
         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
383
         */
384
        public void mouseClicked(MouseEvent e) {
385
        }
386

    
387
        /*
388
         * (non-Javadoc)
389
         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
390
         */
391
        public void mouseEntered(MouseEvent e) {
392
        }
393

    
394
        /*
395
         * (non-Javadoc)
396
         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
397
         */
398
        public void mouseExited(MouseEvent e) {
399
        }
400

    
401
        /**
402
         * A?adir un listener a la lista de eventos
403
         * @param listener
404
         */
405
        public void addValueChangedListener(DoubleSliderListener listener) {
406
                if (!actionCommandListeners.contains(listener))
407
                        actionCommandListeners.add(listener);
408
        }
409

    
410
        /**
411
         * Borrar un listener de la lista de eventos
412
         * @param listener
413
         */
414
        public void removeValueChangedListener(DoubleSliderListener listener) {
415
                actionCommandListeners.remove(listener);
416
        }
417

    
418
        /**
419
         * Invocar a los eventos asociados al componente
420
         */
421
        private void callValueChangedListeners() {
422
                if (!bDoCallListeners)
423
                        return;
424
                Iterator acIterator = actionCommandListeners.iterator();
425
                while (acIterator.hasNext()) {
426
                        DoubleSliderListener listener = (DoubleSliderListener) acIterator.next();
427
                        listener.actionValueChanged(new DoubleSliderEvent(this));
428
                }
429
        }
430

    
431
        /**
432
         * @return the twoSliders
433
         */
434
        public boolean isTwoSliders() {
435
                return twoSliders;
436
        }
437

    
438
        /**
439
         * @param twoSliders the twoSliders to set
440
         */
441
        public void setTwoSliders(boolean twoSliders) {
442
                this.twoSliders = twoSliders;
443
                refreshImage();
444
        }
445

    
446
        /**
447
         * @param color1 the color1 to set
448
         */
449
        public void setColor1(Color color1) {
450
                this.color1 = color1;
451
                refreshImage();
452
        }
453

    
454
        /**
455
         * @param color2 the color2 to set
456
         */
457
        public void setColor2(Color color2) {
458
                this.color2 = color2;
459
                refreshImage();
460
        }
461

    
462
        /* (non-Javadoc)
463
         * @see javax.swing.JComponent#setEnabled(boolean)
464
         */
465
        public void setEnabled(boolean enabled) {
466
                super.setEnabled(enabled);
467
                refreshImage();
468
        }
469

    
470
        public void mouseWheelMoved(MouseWheelEvent e) {
471
                if (!isEnabled())
472
                        return;
473

    
474
                int aux = pixeltovalue(e.getX()+1);
475
                int aux2 = aux - x1;
476
                int aux3 = x2 - aux;
477
                if (aux3 < aux2)
478
                        valuePressed = 2;
479
                else
480
                        valuePressed = 1;
481

    
482
                if (!twoSliders)
483
                        valuePressed = 1;
484

    
485
                if (valuePressed == 1)
486
                        setX1(getX1() - e.getWheelRotation());
487
                else
488
                        setX2(getX2() - e.getWheelRotation());
489
                valuePressed = 0;
490

    
491
                callValueChangedListeners();
492
        }
493
}