Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / imagenavigator / ImageNavigator.java @ 13355

History | View | Annotate | Download (20.6 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.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
        ImageIcon                     imageIconClose;
87
        ImageIcon                     imageIconHelp;
88

    
89
        /**
90
   * Crea un <code>ImageNavigator</code> especificandole quien pintara el
91
   * componente
92
   * @param iClient
93
   */
94
        public ImageNavigator(IClientImageNavigator iClient) {
95
                this.iClient = iClient;
96

    
97
                this.setFocusable(true);
98
                this.addKeyListener(this);
99
                this.addMouseMotionListener(this);
100
                this.addMouseListener(this);
101
                this.addMouseWheelListener(this);
102
        }
103

    
104
        double initX1 = 0.0;
105
        double initY1 = 0.0;
106
        double initX2 = 100.0;
107
        double initY2 = 100.0;
108
        double initZoom = 1.0;
109
        boolean autoAdjusted = true;
110

    
111
        /**
112
         * Actualiza las dimensiones para ajustar la imagen a los bordes especificados
113
         * con setViewDimensions.
114
         */
115
        private void updateDimensions() {
116
                double factor = this.getWidth() / (initX2 - initX1);
117
                if (factor > (this.getHeight() / (initY2 - initY1)))
118
                        factor = this.getHeight() / (initY2 - initY1);
119
                zoom = factor;
120
                imageCenter();
121
        }
122

    
123
        /**
124
         * Centra la imagen
125
         */
126
        public void imageCenter() {
127
                x1 = initX1;
128
                y1 = initY1;
129

    
130
                if (isXInverted())
131
                        x1 -= ((initX2 - initX1) - this.getWidth() / zoom) / 2.0;
132
                else
133
                        x1 += ((initX2 - initX1) - this.getWidth() / zoom) / 2.0;
134
                if (isYInverted())
135
                        y1 -= ((initY2 - initY1) - this.getHeight() / zoom) / 2.0;
136
                else
137
                        y1 += ((initY2 - initY1) - this.getHeight() / zoom) / 2.0;
138
        }
139

    
140
        /**
141
         * Especifica el rectangulo de la imagen a visualizar, pudiendo tener
142
         * cualquiera de los ejes X e Y invertidos
143
         *
144
         * @param x1 Coordenada izquierda
145
         * @param y1 Coordenada superior
146
         * @param x2 Coordenada derecha
147
         * @param y2 Coordenada inferior
148
         */
149
        public void setViewDimensions(double x1, double y1, double x2, double y2) {
150
                this.initX1 = x1;
151
                this.initX2 = x2;
152
                this.initY1 = y1;
153
                this.initY2 = y2;
154

    
155
                yInverted = (y2 < y1);
156
                if (yInverted) {
157
                        this.initY1 = y2;
158
                        this.initY2 = y1;
159
                }
160

    
161
                xInverted = (x2 < x1);
162
                if (xInverted) {
163
                        this.initX1 = x2;
164
                        this.initX2 = x1;
165
                }
166

    
167
                this.updateDimensions();
168
        }
169

    
170
        /**
171
         * Hace un forzado de pintado del buffer temporal de la imagen. Este m?todo
172
         * forzar? una llamada a la funci?n de pintado del cliente.
173
         */
174
        public void updateBuffer() {
175
                updateImageCache(true);
176
                refreshImage(0, 0);
177
        }
178

    
179
        /**
180
         * Especifica el zoom que usar? por defecto el componente.
181
         * @param zoom
182
         */
183
        public void setZoom(double zoom) {
184
                initZoom = zoom;
185
                this.zoom = initZoom;
186
                autoAdjusted = false;
187
                imageCenter();
188
        }
189

    
190
        /**
191
         * Especifica el zoom que usar? por defecto el componente.
192
         * @param zoom
193
         */
194
        public void setAutoAdjusted() {
195
                autoAdjusted = true;
196
                updateDimensions();
197
                updateImageCache(true);
198
                refreshImage(0, 0);
199
        }
200

    
201
        /*
202
         * (non-Javadoc)
203
         * @see javax.swing.JComponent#addNotify()
204
         */
205
        public void addNotify() {
206
                super.addNotify();
207

    
208
                updateImageCache(true);
209
                refreshImage(0, 0);
210
        }
211

    
212
        /**
213
         * Hace un zoom de aumento en las coordenadas especificadas
214
         * @param x
215
         * @param y
216
         */
217
        private void ZoomIn(double x, double y) {
218
                if ((int) (zoom * 100.0) >= (25600.0 / initZoom))
219
                        return;
220
                double xcent = (x / zoom);
221
                double ycent = (y / zoom);
222
                if (isXInverted())
223
                        x1 -= xcent;
224
                else
225
                        x1 += xcent;
226
                if (isYInverted())
227
                        y1 -= ycent;
228
                else
229
                        y1 += ycent;
230
                zoom = zoom * 2.0;
231
                xcent = (x / zoom);
232
                ycent = (y / zoom);
233
                if (isXInverted())
234
                        x1 += xcent;
235
                else
236
                        x1 -= xcent;
237
                if (isYInverted())
238
                        y1 += ycent;
239
                else
240
                        y1 -= ycent;
241
                updateImageCache(true);
242
                refreshImage(0, 0);
243
        }
244

    
245
        /**
246
         * Hace un zoom hacia afuera en las coordenadas especificadas
247
         * @param x
248
         * @param y
249
         */
250
        private void ZoomOut(double x, double y) {
251
                if ((int) ((1.0 / zoom) * 100.0) >= (25600.0 / initZoom))
252
                        return;
253

    
254
                double xcent = (x / zoom);
255
                double ycent = (y / zoom);
256
                if (isXInverted())
257
                        x1 -= xcent;
258
                else
259
                        x1 += xcent;
260
                if (isYInverted())
261
                        y1 -= ycent;
262
                else
263
                        y1 += ycent;
264
                zoom = zoom / 2.0;
265
                xcent = (x / zoom);
266
                ycent = (y / zoom);
267
                if (isXInverted())
268
                        x1 += xcent;
269
                else
270
                        x1 -= xcent;
271
                if (isYInverted())
272
                        y1 += ycent;
273
                else
274
                        y1 -= ycent;
275
                updateImageCache(true);
276
                refreshImage(0, 0);
277
        }
278

    
279
        /**
280
         * Mostrar o ocultar la ayuda
281
         *
282
         */
283
        private void callShowHelp() {
284
                showHelp = !showHelp;
285
                updateImageCache(true);
286
                refreshImage(0, 0);
287
        }
288

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

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

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

    
390
                double newY1 = 0.0;
391
                double newY2 = 0.0;
392
                double newX1 = 0.0;
393
                double newX2 = 0.0;
394
                if (isYInverted()) {
395
                        newY1 = y1 + this.getHeight() / zoom - ((y1 - initY1) * 2.0);
396
                        newY2 = newY1 - this.getHeight() / zoom;
397
                } else {
398
                        newY1 = y1;
399
                        newY2 = y1 + this.getHeight() / zoom;
400
                }
401
                if (isXInverted()) {
402
                        newX1 = x1 + this.getWidth() / zoom - ((x1 - initX1) * 2.0);
403
                        newX2 = newX1 - this.getWidth() / zoom;
404
                } else {
405
                        newX1 = x1;
406
                        newX2 = x1 + this.getWidth() / zoom;
407
                }
408
                iClient.drawImage(getCacheGraphics(), newX1, newY1, newX2, newY2, zoom, this.getWidth(), this.getHeight());
409
        }
410

    
411
        private Image getWidgetImage() {
412
                int width2 = getBounds().width;
413
                int height2 = getBounds().height;
414
                if (width2 <= 0)
415
                        width2 = 1;
416
                if (height2 <= 0)
417
                        height2=1;
418

    
419
                if ((width != width2) || (height != height2)) {
420
                        image = createImage(width2, height2);
421
                        imageCache = createImage(width2, height2);
422
                        if (image == null)
423
                                return null;
424
                        widgetGraphics = (Graphics2D) image.getGraphics();
425
                        cacheGraphics = (Graphics2D) imageCache.getGraphics();
426

    
427
                        RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
428
                        hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
429
                        cacheGraphics.setRenderingHints(hints);
430
                }
431

    
432
                width = width2;
433
                height = height2;
434
                return image;
435
        }
436

    
437
        private Graphics2D getWidgetGraphics() {
438
                getWidgetImage();
439
                return widgetGraphics;
440
        }
441

    
442
        private Graphics2D getCacheGraphics() {
443
                getWidgetImage();
444
                return cacheGraphics;
445
        }
446

    
447
        /**
448
         * Redibujar el componente en el graphics temporal
449
         */
450
        private void redrawBuffer(int x, int y) {
451
                if (showBackground) {
452
                        for (int i = -2; ((i - 2) * 4) <= width; i++) {
453
                                for (int j = -2; ((j - 2) * 4) <= height; j++) {
454
                                        if ((i + j) % 2 == 0)
455
                                                getWidgetGraphics().setColor(Color.white);
456
                                        else
457
                                                getWidgetGraphics().setColor(getBackgroundColor());
458
                                        getWidgetGraphics().fillRect((i * 4) + (x % 8), (j * 4) + (y % 8), 4, 4);
459
                                }
460
                        }
461
                } else {
462
                        getWidgetGraphics().setColor(Color.white);
463
                        getWidgetGraphics().fillRect(0, 0, width, height);
464
                }
465

    
466
                getWidgetGraphics().drawImage(imageCache, x, y, null);
467

    
468
                if (showHelp)
469
                        paintHelp((Graphics2D) getWidgetGraphics());
470
                else
471
                        getWidgetGraphics().drawImage(getIconHelp().getImage(), width - getIconHelp().getIconWidth() - 4, 3, null);
472
        }
473

    
474
        /*
475
         * (non-Javadoc)
476
         * @see javax.swing.JComponent#paint(java.awt.Graphics)
477
         */
478
        public void paint(Graphics g) {
479
                if (autoAdjusted) updateDimensions();
480
                Graphics2D g2d = (Graphics2D) g;
481
                RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
482
                hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
483
                g2d.setRenderingHints(hints);
484

    
485
                updateImageCache(false);
486

    
487
                redrawBuffer(0, 0);
488

    
489
                if (image != null) {
490
                        if (isEnabled()) {
491
                                g.drawImage(image, 0, 0, this);
492
                        } else {
493
                                // Dibujar en escala de grises y aclarado para cuando esta inactivo
494
                          BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
495

    
496
                                Graphics big = bi.createGraphics();
497
                                big.drawImage(image, 0, 0, this);
498

    
499
                                ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
500
                                colorConvert.filter(bi, bi);
501

    
502
                                big.setColor(new Color(255, 255, 255, 164));
503
                                big.fillRect(0, 0, width, height);
504

    
505
                                g.drawImage(bi, getVisibleRect().x, getVisibleRect().y, this);
506
                        }
507
                }
508
        }
509

    
510
        /**
511
         * Redibujar el componente en el graphics temporal y representarlo en el
512
         * componente
513
         */
514
        private void refreshImage(int x, int y) {
515
                Graphics2D g2d = (Graphics2D) getGraphics();
516
                if (g2d == null)
517
                        return;
518
                RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
519
                hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
520
                g2d.setRenderingHints(hints);
521
                redrawBuffer(x, y);
522

    
523
                if (image != null)
524
                        getGraphics().drawImage(image, 0, 0, this);
525
        }
526

    
527
        /**
528
         * Devuelve el icono de cerrar
529
         * @return
530
         */
531
        private ImageIcon getIconClose() {
532
                if (imageIconClose == null) {
533
                        imageIconClose = new ImageIcon(getClass().getResource("images/close.png"));
534
                }
535
                return imageIconClose;
536
        }
537

    
538
        /**
539
         * Devuelve el icono ayuda
540
         * @return
541
         */
542
        private ImageIcon getIconHelp() {
543
                if (imageIconHelp == null) {
544
                        imageIconHelp = new ImageIcon(getClass().getResource("images/help.png"));
545
                }
546
                return imageIconHelp;
547
        }
548

    
549
        private void paintHelp(Graphics2D g) {
550
                int sep = 13;
551
                int pos = sep + 1;
552

    
553
                int alto = sep * 8 + 6;
554

    
555
                Image image2 = createImage(width, alto);
556
                Graphics2D graphics2 = (Graphics2D) image2.getGraphics();
557

    
558
                RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
559
                hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
560
                graphics2.setRenderingHints(hints);
561

    
562
                alto--;
563

    
564
                Color color1 = new Color(255, 255, 178);
565
                Color color2 = new Color(255, 255, 74);
566
                graphics2.setPaint(new GradientPaint(0, 0, color1, 0, alto, color2, false));
567
                graphics2.fillRect(0, 0, width, alto);
568

    
569
                graphics2.setColor(new Color(0, 0, 0));
570

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

    
573
                graphics2.drawString(Messages.getText("teclas") + ":", 10, pos);
574

    
575
                graphics2.setFont(new java.awt.Font("Tahoma", 0, sep - 2));
576
                pos += sep;
577
                graphics2.drawString(Messages.getText("ayuda_c"), 20, pos);
578
                pos += sep;
579
                graphics2.drawString(Messages.getText("ayuda_0"), 20, pos);
580
                pos += sep;
581
                graphics2.drawString(Messages.getText("ayuda_1_5"), 20, pos);
582
                pos += sep;
583
                graphics2.drawString(Messages.getText("ayuda_more_less"), 20, pos);
584
                pos += sep;
585
                graphics2.drawString(Messages.getText("ayuda_wheel"), 20, pos);
586
                pos += sep;
587
                graphics2.drawString(Messages.getText("ayuda_background"), 20, pos);
588
                pos += sep;
589
                graphics2.drawString(Messages.getText("ayuda_h"), 20, pos);
590

    
591
                graphics2.setColor(new Color(185, 185, 185));
592
                graphics2.drawLine(0, alto, width, alto);
593

    
594
                graphics2.drawImage(getIconClose().getImage(), width - getIconClose().getIconWidth() - 4, 3, null);
595

    
596
                AlphaComposite myAlpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f);
597
                g.setComposite(myAlpha);
598

    
599
                g.drawImage(image2, 0, 0, this);
600

    
601
                myAlpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
602
                g.setComposite(myAlpha);
603
        }
604

    
605
        Point mouse = null;
606
        /*
607
         * (non-Javadoc)
608
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
609
         */
610
        public void mousePressed(MouseEvent e) {
611
                if (!isEnabled())
612
                        return;
613
                requestFocus();
614

    
615
                if ((e.getX() > (width - 20)) && (e.getY() < 20))
616
                        return;
617

    
618
                if ((e.getButton() != MouseEvent.BUTTON1) && (e.getButton() != MouseEvent.BUTTON2))
619
                        return;
620

    
621
                // Oscurece la imagen cuando se mueve
622
                Color gris = new Color(0, 0, 0, 16);
623
                getCacheGraphics().setColor(gris);
624
                getCacheGraphics().fillRect(0, 0, width-1, height-1);
625

    
626
                // Pone un borde a la imagen cuando se mueve
627
                getCacheGraphics().setColor(Color.gray);
628
                getCacheGraphics().drawRect(0, 0, width-1, height-1);
629

    
630
                mouse = new Point(e.getX(), e.getY());
631
                changePos(e.getX(), e.getY());
632
                autoAdjusted = false;
633
        }
634

    
635
        /*
636
         * (non-Javadoc)
637
         * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
638
         */
639
        public void mouseDragged(MouseEvent e) {
640
                if (!isEnabled())
641
                        return;
642
                changePos(e.getX(), e.getY());
643
        }
644

    
645
        /*
646
         * (non-Javadoc)
647
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
648
         */
649
        public void mouseReleased(MouseEvent e) {
650
                if (!isEnabled())
651
                        return;
652
                if (mouse != null) {
653
                        x1 = x1 - ((e.getX() - mouse.getX())/zoom);
654
                        y1 = y1 - ((e.getY() - mouse.getY())/zoom);
655
                        updateImageCache(true);
656
                        refreshImage(0, 0);
657
                }
658
                mouse = null;
659
        }
660

    
661
        private void changePos(int x, int y) {
662
                if (mouse != null)
663
                        refreshImage((int) (x - mouse.getX()), (int) (y - mouse.getY()));
664
        }
665

    
666
        /**
667
         * Evento de la rueda del rat?n para hacer Zoom In o Zoom Out en la posici?n
668
         * del puntero.
669
         */
670
        public void mouseWheelMoved(MouseWheelEvent e) {
671
                if (!isEnabled())
672
                        return;
673
                
674
                if (e.getWheelRotation() > 0) {
675
                        ZoomOut(isXInverted() ? this.getWidth() - e.getX() : e.getX(), isYInverted() ? this.getHeight() - e.getY() : e.getY());
676
                        autoAdjusted = false;
677
                }
678
                if (e.getWheelRotation() < 0) {
679
                        ZoomIn(isXInverted() ? this.getWidth() - e.getX() : e.getX(), isYInverted() ? this.getHeight() - e.getY() : e.getY());
680
                        autoAdjusted = false;
681
                }
682
        }
683

    
684
        /**
685
         * Obtener si el eje de las Y esta invertido
686
         * @return
687
         */
688
        private boolean isYInverted() {
689
                return yInverted;
690
        }
691

    
692
        /**
693
         * Obtener si el eje de las X esta invertido
694
         * @return
695
         */
696
        private boolean isXInverted() {
697
                return xInverted;
698
        }
699

    
700
        /*
701
         * (non-Javadoc)
702
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
703
         */
704
        public void mouseMoved(MouseEvent e) {
705
                if (!isEnabled())
706
                        return;
707
                if ((e.getX() > (width - 20)) && (e.getY() < 20)) {
708
                        setCursor(new Cursor(Cursor.HAND_CURSOR));
709
                        if (showHelp)
710
                                setToolTipText(Messages.getText("cerrar"));
711
                        else
712
                                setToolTipText(Messages.getText("ayuda"));
713
                } else {
714
                        setCursor(new Cursor(Cursor.MOVE_CURSOR));
715
                        setToolTipText(null);
716
                }
717
        }
718

    
719
        /*
720
         * (non-Javadoc)
721
         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
722
         */
723
        public void mouseClicked(MouseEvent e) {
724
                if (!isEnabled())
725
                        return;
726
                if ((e.getX() > (width - 20)) && (e.getY() < 20))
727
                        callShowHelp();
728
        }
729

    
730
        public void keyReleased(KeyEvent e) {}
731
        public void keyTyped(KeyEvent e) {}
732
        public void mouseEntered(MouseEvent e) {}
733
        public void mouseExited(MouseEvent e) {}
734

    
735
        /**
736
         * Define el color de los recuadros del fondo
737
         * 
738
   * @param backgroundColor the backgroundColor to set
739
   */
740
  public void setBackgroundColor(Color backgroundColor) {
741
          this.backgroundColor = backgroundColor;
742
  }
743

    
744
        /**
745
         * Devuelve el color de los recuadros del fondo
746
   * @return the backgroundColor
747
   */
748
  public Color getBackgroundColor() {
749
          return backgroundColor;
750
  }
751

    
752
        /**
753
         * Devuelve si se esta mostrando o no la cuadricula de fondo.
754
   * @return the showBackground
755
   */
756
  public boolean isShowBackground() {
757
          return showBackground;
758
  }
759

    
760
        /**
761
         * Define si se muestra o no la cuadricula de fondo. Util para imagenes
762
         * transparentes
763
   * @param showBackground the showBackground to set
764
   */
765
  public void setShowBackground(boolean showBackground) {
766
                this.showBackground = showBackground;
767
                updateImageCache(true);
768
                refreshImage(0, 0);
769
  }
770

    
771
        /* (non-Javadoc)
772
   * @see javax.swing.JComponent#setEnabled(boolean)
773
   */
774
  public void setEnabled(boolean enabled) {
775
          super.setEnabled(enabled);
776
                updateImageCache(true);
777
                refreshImage(0, 0);
778
  }
779
}