Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / imagenavigator / ImageNavigator.java @ 21224

History | View | Annotate | Download (20.9 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.imagenavigator;
20

    
21
import java.awt.AlphaComposite;
22
import java.awt.Color;
23
import java.awt.Cursor;
24
import java.awt.GradientPaint;
25
import java.awt.Graphics;
26
import java.awt.Graphics2D;
27
import java.awt.Image;
28
import java.awt.Point;
29
import java.awt.RenderingHints;
30
import java.awt.color.ColorSpace;
31
import java.awt.event.KeyEvent;
32
import java.awt.event.KeyListener;
33
import java.awt.event.MouseEvent;
34
import java.awt.event.MouseListener;
35
import java.awt.event.MouseMotionListener;
36
import java.awt.event.MouseWheelEvent;
37
import java.awt.event.MouseWheelListener;
38
import java.awt.image.BufferedImage;
39
import java.awt.image.ColorConvertOp;
40

    
41
import javax.swing.ImageIcon;
42
import javax.swing.JComponent;
43

    
44
import org.gvsig.gui.beans.Messages;
45
/**
46
 * <code>ImageNavigator</code> es un componente que representa un manejador
47
 * de im?genes. En ?l se puede desplazar, hacer un zoom out o un zoom in a una
48
 * imagen virtual. El componente no trata la imagen en si, solo lanza los
49
 * eventos indicando la nueva posici?n y zoom de la imagen, luego es el usuario
50
 * el que se encargar? de dibujar esa imagen en la posici?n correspondiente.
51
 *
52
 * El modo de uso es el siguiente:
53
 * - Se puede desplazar una imagen con el bot?n izquierdo del rat?n.
54
 * - Se puede hacer zoom in/out con las teclas +/- del teclado.
55
 * - Se puede hacer zoom in/out con la rueda del rat?n teniendo en cuenta la
56
 * posici?n del mismo.
57
 * - Se puede resetear los valores con las teclas 'Espacio' o 0;
58
 * - Las teclas 1, 2, 3, 4 y 5 equivalen a zoom 1, 2, 4, 8 y 16 respectivamente.
59
 * - La tecla C sirve para centrar la imagen.
60
 * - La tecla B sirve para mostrar u ocultar los cuadros del fondo. ?til para
61
 *   mostrar imagenes con transparencia.
62
 * - La tecla H muestra la ayuda.
63
 *
64
 * @version 04/05/2007
65
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
66
 */
67
public class ImageNavigator extends JComponent implements KeyListener, MouseMotionListener, MouseListener, MouseWheelListener {
68
        private static final long serialVersionUID = 1164788214432359272L;
69
        private IClientImageNavigator iClient         = null;
70

    
71
        private Image                 image           = null;
72
        private Graphics2D            widgetGraphics  = null;
73
        private Image                 imageCache      = null;
74
        private Graphics2D            cacheGraphics   = null;
75

    
76
        private double                zoom            = 1.0;
77
        private double                x1              = 0.0;
78
        private double                y1              = 0.0;
79
        private boolean               yInverted       = false;
80
        private boolean               xInverted       = false;
81
        private int                   width           = 0;
82
        private int                   height          = 0;
83
        private boolean               showHelp        = false;
84
        private boolean               showBackground  = false;
85
        private Color                 backgroundColor = new Color(224, 224, 224);
86
        private ImageIcon             imageIconClose;
87
        private ImageIcon             imageIconHelp;
88

    
89
        private double                initX1          = 0.0;
90
        private double                initY1          = 0.0;
91
        private double                initX2          = 100.0;
92
        private double                initY2          = 100.0;
93
        private double                initZoom        = 1.0;
94
        private boolean               autoAdjusted    = true;
95

    
96
        /**
97
         * Crea un <code>ImageNavigator</code>
98
         */
99
        public ImageNavigator() {
100
                this.setFocusable(true);
101
                this.addKeyListener(this);
102
                this.addMouseMotionListener(this);
103
                this.addMouseListener(this);
104
                this.addMouseWheelListener(this);
105
        }
106
        
107
        /**
108
         * Crea un <code>ImageNavigator</code> especificandole quien pintara el
109
         * componente
110
         * @param iClient
111
         */
112
        public ImageNavigator(IClientImageNavigator iClient) {
113
                this();
114
                setClientImageNavigator(iClient);
115
        }
116
        
117
        public void setClientImageNavigator(IClientImageNavigator iClient) {
118
                this.iClient = iClient;
119
        }
120

    
121
        /**
122
         * Actualiza las dimensiones para ajustar la imagen a los bordes especificados
123
         * con setViewDimensions.
124
         */
125
        private void updateDimensions() {
126
                double factor = this.getWidth() / (initX2 - initX1);
127
                if (factor > (this.getHeight() / (initY2 - initY1)))
128
                        factor = this.getHeight() / (initY2 - initY1);
129
                zoom = factor;
130
                imageCenter();
131
        }
132

    
133
        /**
134
         * Centra la imagen
135
         */
136
        public void imageCenter() {
137
                x1 = initX1;
138
                y1 = initY1;
139

    
140
                if (isXInverted())
141
                        x1 -= ((initX2 - initX1) - this.getWidth() / zoom) / 2.0;
142
                else
143
                        x1 += ((initX2 - initX1) - this.getWidth() / zoom) / 2.0;
144
                if (isYInverted())
145
                        y1 -= ((initY2 - initY1) - this.getHeight() / zoom) / 2.0;
146
                else
147
                        y1 += ((initY2 - initY1) - this.getHeight() / zoom) / 2.0;
148
        }
149

    
150
        /**
151
         * Especifica el rectangulo de la imagen a visualizar, pudiendo tener
152
         * cualquiera de los ejes X e Y invertidos
153
         *
154
         * @param x1 Coordenada izquierda
155
         * @param y1 Coordenada superior
156
         * @param x2 Coordenada derecha
157
         * @param y2 Coordenada inferior
158
         */
159
        public void setViewDimensions(double x1, double y1, double x2, double y2) {
160
                this.initX1 = x1;
161
                this.initX2 = x2;
162
                this.initY1 = y1;
163
                this.initY2 = y2;
164

    
165
                yInverted = (y2 < y1);
166
                if (yInverted) {
167
                        this.initY1 = y2;
168
                        this.initY2 = y1;
169
                }
170

    
171
                xInverted = (x2 < x1);
172
                if (xInverted) {
173
                        this.initX1 = x2;
174
                        this.initX2 = x1;
175
                }
176

    
177
                this.updateDimensions();
178
        }
179

    
180
        /**
181
         * Hace un forzado de pintado del buffer temporal de la imagen. Este m?todo
182
         * forzar? una llamada a la funci?n de pintado del cliente.
183
         */
184
        public void updateBuffer() {
185
                updateImageCache(true);
186
                refreshImage(0, 0);
187
        }
188

    
189
        /**
190
         * Especifica el zoom que usar? por defecto el componente.
191
         * @param zoom
192
         */
193
        public void setZoom(double zoom) {
194
                initZoom = zoom;
195
                this.zoom = initZoom;
196
                autoAdjusted = false;
197
                imageCenter();
198
        }
199

    
200
        /**
201
         * Especifica el zoom que usar? por defecto el componente.
202
         * @param zoom
203
         */
204
        public void setAutoAdjusted() {
205
                autoAdjusted = true;
206
                updateDimensions();
207
                updateImageCache(true);
208
                refreshImage(0, 0);
209
        }
210

    
211
        /*
212
         * (non-Javadoc)
213
         * @see javax.swing.JComponent#addNotify()
214
         */
215
        public void addNotify() {
216
                super.addNotify();
217

    
218
                updateImageCache(true);
219
                refreshImage(0, 0);
220
        }
221

    
222
        /**
223
         * Hace un zoom de aumento en las coordenadas especificadas
224
         * @param x
225
         * @param y
226
         */
227
        private void ZoomIn(double x, double y) {
228
                double xcent = (x / zoom);
229
                double ycent = (y / zoom);
230
                if (isXInverted())
231
                        x1 -= xcent;
232
                else
233
                        x1 += xcent;
234
                if (isYInverted())
235
                        y1 -= ycent;
236
                else
237
                        y1 += ycent;
238
                zoom = zoom * 2.0;
239
                xcent = (x / zoom);
240
                ycent = (y / zoom);
241
                if (isXInverted())
242
                        x1 += xcent;
243
                else
244
                        x1 -= xcent;
245
                if (isYInverted())
246
                        y1 += ycent;
247
                else
248
                        y1 -= ycent;
249
                updateImageCache(true);
250
                refreshImage(0, 0);
251
        }
252

    
253
        /**
254
         * Hace un zoom hacia afuera en las coordenadas especificadas
255
         * @param x
256
         * @param y
257
         */
258
        private void ZoomOut(double x, double y) {
259
                double xcent = (x / zoom);
260
                double ycent = (y / zoom);
261
                if (isXInverted())
262
                        x1 -= xcent;
263
                else
264
                        x1 += xcent;
265
                if (isYInverted())
266
                        y1 -= ycent;
267
                else
268
                        y1 += ycent;
269
                zoom = zoom / 2.0;
270
                xcent = (x / zoom);
271
                ycent = (y / zoom);
272
                if (isXInverted())
273
                        x1 += xcent;
274
                else
275
                        x1 -= xcent;
276
                if (isYInverted())
277
                        y1 += ycent;
278
                else
279
                        y1 -= ycent;
280
                updateImageCache(true);
281
                refreshImage(0, 0);
282
        }
283

    
284
        /**
285
         * Mostrar o ocultar la ayuda
286
         *
287
         */
288
        private void callShowHelp() {
289
                showHelp = !showHelp;
290
                updateImageCache(true);
291
                refreshImage(0, 0);
292
        }
293

    
294
        /*
295
         * (non-Javadoc)
296
         * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
297
         */
298
        public void keyPressed(KeyEvent e) {
299
                switch (e.getKeyChar()) {
300
                        case 'h':
301
                        case 'H':
302
                                callShowHelp();
303
                                break;
304
                        case '+':
305
                                ZoomIn(width / 2.0, height / 2.0);
306
                                autoAdjusted = false;
307
                                break;
308
                        case '-':
309
                                ZoomOut(width / 2.0, height / 2.0);
310
                                autoAdjusted = false;
311
                                break;
312
                        case '1':
313
                                autoAdjusted = false;
314
                                this.zoom = initZoom;
315
                                imageCenter();
316
                                updateImageCache(true);
317
                                refreshImage(0, 0);
318
                                break;
319
                        case '2':
320
                                autoAdjusted = false;
321
                                this.zoom = initZoom * 2.0;
322
                                imageCenter();
323
                                updateImageCache(true);
324
                                refreshImage(0, 0);
325
                                break;
326
                        case '3':
327
                                autoAdjusted = false;
328
                                this.zoom = initZoom * 4.0;
329
                                imageCenter();
330
                                updateImageCache(true);
331
                                refreshImage(0, 0);
332
                                break;
333
                        case '4':
334
                                autoAdjusted = false;
335
                                this.zoom = initZoom * 8.0;
336
                                imageCenter();
337
                                updateImageCache(true);
338
                                refreshImage(0, 0);
339
                                break;
340
                        case '5':
341
                                autoAdjusted = false;
342
                                this.zoom = initZoom * 16.0;
343
                                imageCenter();
344
                                updateImageCache(true);
345
                                refreshImage(0, 0);
346
                                break;
347
                        case 'c':
348
                        case 'C':
349
                                imageCenter();
350
                                updateImageCache(true);
351
                                refreshImage(0, 0);
352
                                break;
353
                        case 'b':
354
                        case 'B':
355
                                setShowBackground(!isShowBackground());
356
                                break;
357
                        case '0':
358
                        case ' ':
359
                                setAutoAdjusted();
360
                                break;
361
                }
362
        }
363

    
364
        double updateWidth = 0;
365
        double updateHeight = 0;
366
        /**
367
         * M?todo que hara la invocaci?n al cliente del pintado del trozo de imagen a
368
         * visualizar
369
         * @param forceUpdate
370
         */
371
        private void updateImageCache(boolean forceUpdate) {
372
                if (getWidgetImage() == null ||
373
                                (updateWidth == getWidgetImage().getWidth(this) &&
374
                                updateHeight == getWidgetImage().getHeight(this) &&
375
                                !forceUpdate))
376
                        return;
377
                updateWidth = getWidgetImage().getWidth(this);
378
                updateHeight = getWidgetImage().getHeight(this);
379

    
380
                if (showBackground) {
381
                        for (int i = 0; (i * 4) <= width; i++) {
382
                                for (int j = 0; (j * 4) <= height; j++) {
383
                                        if ((i + j) % 2 == 0)
384
                                                getCacheGraphics().setColor(Color.white);
385
                                        else
386
                                                getCacheGraphics().setColor(getBackgroundColor());
387
                                        getCacheGraphics().fillRect(i * 4, j * 4, 4, 4);
388
                                }
389
                        }
390
                } else {
391
                        getCacheGraphics().setColor(Color.white);
392
                        getCacheGraphics().fillRect(0, 0, width, height);
393
                }
394

    
395
                double newY1 = 0.0;
396
                double newY2 = 0.0;
397
                double newX1 = 0.0;
398
                double newX2 = 0.0;
399

    
400
                if (isYInverted()) {
401
                        newY1 = y1 + this.getHeight() / zoom - ((y1 - initY1) * 2.0);
402
                        newY2 = newY1 - this.getHeight() / zoom;
403
                } else {
404
                        newY1 = y1;
405
                        newY2 = y1 + this.getHeight() / zoom;
406
                }
407

    
408
                if (isXInverted()) {
409
                        newX1 = x1 + this.getWidth() / zoom - ((x1 - initX1) * 2.0);
410
                        newX2 = newX1 - this.getWidth() / zoom;
411
                } else {
412
                        newX1 = x1;
413
                        newX2 = x1 + this.getWidth() / zoom;
414
                }
415

    
416
                if ((Double.isNaN(newX1)) || (Double.isNaN(newY1)))
417
                        return;
418

    
419
                if (iClient != null)
420
                        iClient.drawImage(getCacheGraphics(), newX1, newY1, newX2, newY2, zoom, this.getWidth(), this.getHeight());
421
        }
422

    
423
        private Image getWidgetImage() {
424
                int width2 = getBounds().width;
425
                int height2 = getBounds().height;
426
                if (width2 <= 0)
427
                        width2 = 1;
428
                if (height2 <= 0)
429
                        height2=1;
430

    
431
                if ((width != width2) || (height != height2)) {
432
                        image = createImage(width2, height2);
433
                        imageCache = createImage(width2, height2);
434
                        if (image == null)
435
                                return null;
436
                        widgetGraphics = (Graphics2D) image.getGraphics();
437
                        cacheGraphics = (Graphics2D) imageCache.getGraphics();
438

    
439
                        RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
440
                        hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
441
                        cacheGraphics.setRenderingHints(hints);
442
                }
443

    
444
                width = width2;
445
                height = height2;
446
                return image;
447
        }
448

    
449
        private Graphics2D getWidgetGraphics() {
450
                getWidgetImage();
451
                return widgetGraphics;
452
        }
453

    
454
        private Graphics2D getCacheGraphics() {
455
                getWidgetImage();
456
                return cacheGraphics;
457
        }
458

    
459
        /**
460
         * Redibujar el componente en el graphics temporal
461
         */
462
        private void redrawBuffer(int x, int y) {
463
                if (showBackground) {
464
                        for (int i = -2; ((i - 2) * 4) <= width; i++) {
465
                                for (int j = -2; ((j - 2) * 4) <= height; j++) {
466
                                        if ((i + j) % 2 == 0)
467
                                                getWidgetGraphics().setColor(Color.white);
468
                                        else
469
                                                getWidgetGraphics().setColor(getBackgroundColor());
470
                                        getWidgetGraphics().fillRect((i * 4) + (x % 8), (j * 4) + (y % 8), 4, 4);
471
                                }
472
                        }
473
                } else {
474
                        getWidgetGraphics().setColor(Color.white);
475
                        getWidgetGraphics().fillRect(0, 0, width, height);
476
                }
477

    
478
                getWidgetGraphics().drawImage(imageCache, x, y, null);
479

    
480
                if (showHelp)
481
                        paintHelp((Graphics2D) getWidgetGraphics());
482
                else
483
                        getWidgetGraphics().drawImage(getIconHelp().getImage(), width - getIconHelp().getIconWidth() - 4, 3, null);
484
        }
485

    
486
        /*
487
         * (non-Javadoc)
488
         * @see javax.swing.JComponent#paint(java.awt.Graphics)
489
         */
490
        public void paint(Graphics g) {
491
                if (autoAdjusted) updateDimensions();
492
                Graphics2D g2d = (Graphics2D) g;
493
                RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
494
                hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
495
                g2d.setRenderingHints(hints);
496

    
497
                updateImageCache(false);
498

    
499
                redrawBuffer(0, 0);
500

    
501
                if (image != null) {
502
                        if (isEnabled()) {
503
                                g.drawImage(image, 0, 0, this);
504
                        } else {
505
                                // Dibujar en escala de grises y aclarado para cuando esta inactivo
506
                                BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
507

    
508
                                Graphics big = bi.createGraphics();
509
                                big.drawImage(image, 0, 0, this);
510

    
511
                                ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
512
                                colorConvert.filter(bi, bi);
513

    
514
                                big.setColor(new Color(255, 255, 255, 164));
515
                                big.fillRect(0, 0, width, height);
516

    
517
                                g.drawImage(bi, getVisibleRect().x, getVisibleRect().y, this);
518
                        }
519
                }
520
        }
521

    
522
        /**
523
         * Redibujar el componente en el graphics temporal y representarlo en el
524
         * componente
525
         */
526
        private void refreshImage(int x, int y) {
527
                Graphics2D g2d = (Graphics2D) getGraphics();
528
                if (g2d == null)
529
                        return;
530
                RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
531
                hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
532
                g2d.setRenderingHints(hints);
533
                redrawBuffer(x, y);
534

    
535
                if (image != null)
536
                        getGraphics().drawImage(image, 0, 0, this);
537
        }
538

    
539
        /**
540
         * Devuelve el icono de cerrar
541
         * @return
542
         */
543
        private ImageIcon getIconClose() {
544
                if (imageIconClose == null) {
545
                        imageIconClose = new ImageIcon(ImageNavigator.class.getResource("images/close.png"));
546
                }
547
                return imageIconClose;
548
        }
549

    
550
        /**
551
         * Devuelve el icono ayuda
552
         * @return
553
         */
554
        private ImageIcon getIconHelp() {
555
                if (imageIconHelp == null) {
556
                        imageIconHelp = new ImageIcon(ImageNavigator.class.getResource("images/help.png"));
557
                }
558
                return imageIconHelp;
559
        }
560

    
561
        private void paintHelp(Graphics2D g) {
562
                int sep = 13;
563
                int pos = sep + 1;
564

    
565
                int alto = sep * 8 + 6;
566

    
567
                Image image2 = createImage(width, alto);
568
                Graphics2D graphics2 = (Graphics2D) image2.getGraphics();
569

    
570
                RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
571
                hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
572
                graphics2.setRenderingHints(hints);
573

    
574
                alto--;
575

    
576
                Color color1 = new Color(255, 255, 178);
577
                Color color2 = new Color(255, 255, 74);
578
                graphics2.setPaint(new GradientPaint(0, 0, color1, 0, alto, color2, false));
579
                graphics2.fillRect(0, 0, width, alto);
580

    
581
                graphics2.setColor(new Color(0, 0, 0));
582

    
583
                graphics2.setFont(new java.awt.Font("Tahoma", 1, sep - 2));
584

    
585
                graphics2.drawString(Messages.getText("teclas") + ":", 10, pos);
586

    
587
                graphics2.setFont(new java.awt.Font("Tahoma", 0, sep - 2));
588
                pos += sep;
589
                graphics2.drawString(Messages.getText("ayuda_c"), 20, pos);
590
                pos += sep;
591
                graphics2.drawString(Messages.getText("ayuda_0"), 20, pos);
592
                pos += sep;
593
                graphics2.drawString(Messages.getText("ayuda_1_5"), 20, pos);
594
                pos += sep;
595
                graphics2.drawString(Messages.getText("ayuda_more_less"), 20, pos);
596
                pos += sep;
597
                graphics2.drawString(Messages.getText("ayuda_wheel"), 20, pos);
598
                pos += sep;
599
                graphics2.drawString(Messages.getText("ayuda_background"), 20, pos);
600
                pos += sep;
601
                graphics2.drawString(Messages.getText("ayuda_h"), 20, pos);
602

    
603
                graphics2.setColor(new Color(185, 185, 185));
604
                graphics2.drawLine(0, alto, width, alto);
605

    
606
                graphics2.drawImage(getIconClose().getImage(), width - getIconClose().getIconWidth() - 4, 3, null);
607

    
608
                AlphaComposite myAlpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f);
609
                g.setComposite(myAlpha);
610

    
611
                g.drawImage(image2, 0, 0, this);
612

    
613
                myAlpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
614
                g.setComposite(myAlpha);
615
        }
616

    
617
        Point mouse = null;
618
        /*
619
         * (non-Javadoc)
620
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
621
         */
622
        public void mousePressed(MouseEvent e) {
623
                if (!isEnabled())
624
                        return;
625
                requestFocus();
626

    
627
                if ((e.getX() > (width - 20)) && (e.getY() < 20))
628
                        return;
629

    
630
                if ((e.getButton() != MouseEvent.BUTTON1) && (e.getButton() != MouseEvent.BUTTON2))
631
                        return;
632

    
633
                // Oscurece la imagen cuando se mueve
634
                Color gris = new Color(0, 0, 0, 16);
635
                getCacheGraphics().setColor(gris);
636
                getCacheGraphics().fillRect(0, 0, width-1, height-1);
637

    
638
                // Pone un borde a la imagen cuando se mueve
639
                getCacheGraphics().setColor(Color.gray);
640
                getCacheGraphics().drawRect(0, 0, width-1, height-1);
641

    
642
                mouse = new Point(e.getX(), e.getY());
643
                changePos(e.getX(), e.getY());
644
                autoAdjusted = false;
645
        }
646

    
647
        /*
648
         * (non-Javadoc)
649
         * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
650
         */
651
        public void mouseDragged(MouseEvent e) {
652
                if (!isEnabled())
653
                        return;
654
                changePos(e.getX(), e.getY());
655
        }
656

    
657
        /*
658
         * (non-Javadoc)
659
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
660
         */
661
        public void mouseReleased(MouseEvent e) {
662
                if (!isEnabled())
663
                        return;
664
                if (mouse != null) {
665
                        x1 = x1 - ((e.getX() - mouse.getX())/zoom);
666
                        y1 = y1 - ((e.getY() - mouse.getY())/zoom);
667
                        updateImageCache(true);
668
                        refreshImage(0, 0);
669
                }
670
                mouse = null;
671
        }
672

    
673
        private void changePos(int x, int y) {
674
                if (mouse != null)
675
                        refreshImage((int) (x - mouse.getX()), (int) (y - mouse.getY()));
676
        }
677

    
678
        /**
679
         * Evento de la rueda del rat?n para hacer Zoom In o Zoom Out en la posici?n
680
         * del puntero.
681
         */
682
        public void mouseWheelMoved(MouseWheelEvent e) {
683
                if (!isEnabled())
684
                        return;
685

    
686
                if (e.getWheelRotation() > 0) {
687
                        ZoomOut(isXInverted() ? this.getWidth() - e.getX() : e.getX(), isYInverted() ? this.getHeight() - e.getY() : e.getY());
688
                        autoAdjusted = false;
689
                }
690
                if (e.getWheelRotation() < 0) {
691
                        ZoomIn(isXInverted() ? this.getWidth() - e.getX() : e.getX(), isYInverted() ? this.getHeight() - e.getY() : e.getY());
692
                        autoAdjusted = false;
693
                }
694
        }
695

    
696
        /**
697
         * Obtener si el eje de las Y esta invertido
698
         * @return
699
         */
700
        private boolean isYInverted() {
701
                return yInverted;
702
        }
703

    
704
        /**
705
         * Obtener si el eje de las X esta invertido
706
         * @return
707
         */
708
        private boolean isXInverted() {
709
                return xInverted;
710
        }
711

    
712
        /*
713
         * (non-Javadoc)
714
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
715
         */
716
        public void mouseMoved(MouseEvent e) {
717
                if (!isEnabled())
718
                        return;
719
                if ((e.getX() > (width - 20)) && (e.getY() < 20)) {
720
                        setCursor(new Cursor(Cursor.HAND_CURSOR));
721
                        if (showHelp)
722
                                setToolTipText(Messages.getText("cerrar"));
723
                        else
724
                                setToolTipText(Messages.getText("ayuda"));
725
                } else {
726
                        setCursor(new Cursor(Cursor.MOVE_CURSOR));
727
                        setToolTipText(null);
728
                }
729
        }
730

    
731
        /*
732
         * (non-Javadoc)
733
         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
734
         */
735
        public void mouseClicked(MouseEvent e) {
736
                if (!isEnabled())
737
                        return;
738
                if ((e.getX() > (width - 20)) && (e.getY() < 20))
739
                        callShowHelp();
740
        }
741

    
742
        public void keyReleased(KeyEvent e) {}
743
        public void keyTyped(KeyEvent e) {}
744
        public void mouseEntered(MouseEvent e) {}
745
        public void mouseExited(MouseEvent e) {}
746

    
747
        /**
748
         * Define el color de los recuadros del fondo
749
         *
750
         * @param backgroundColor the backgroundColor to set
751
         */
752
        public void setBackgroundColor(Color backgroundColor) {
753
                this.backgroundColor = backgroundColor;
754
        }
755

    
756
        /**
757
         * Devuelve el color de los recuadros del fondo
758
         * @return the backgroundColor
759
         */
760
        public Color getBackgroundColor() {
761
                return backgroundColor;
762
        }
763

    
764
        /**
765
         * Devuelve si se esta mostrando o no la cuadricula de fondo.
766
         * @return the showBackground
767
         */
768
        public boolean isShowBackground() {
769
                return showBackground;
770
        }
771

    
772
        /**
773
         * Define si se muestra o no la cuadricula de fondo. Util para imagenes
774
         * transparentes
775
         * @param showBackground the showBackground to set
776
         */
777
        public void setShowBackground(boolean showBackground) {
778
                this.showBackground = showBackground;
779
                updateImageCache(true);
780
                refreshImage(0, 0);
781
        }
782

    
783
        /* (non-Javadoc)
784
         * @see javax.swing.JComponent#setEnabled(boolean)
785
         */
786
        public void setEnabled(boolean enabled) {
787
                super.setEnabled(enabled);
788
                updateImageCache(true);
789
                refreshImage(0, 0);
790
        }
791
}