Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / MapControl.java @ 6164

History | View | Annotate | Download (24.6 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 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
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap;
42

    
43
import java.awt.Color;
44
import java.awt.Dimension;
45
import java.awt.Graphics;
46
import java.awt.Graphics2D;
47
import java.awt.event.ActionEvent;
48
import java.awt.event.ActionListener;
49
import java.awt.event.ComponentEvent;
50
import java.awt.event.ComponentListener;
51
import java.awt.event.MouseEvent;
52
import java.awt.event.MouseListener;
53
import java.awt.event.MouseMotionListener;
54
import java.awt.event.MouseWheelEvent;
55
import java.awt.event.MouseWheelListener;
56
import java.awt.geom.Point2D;
57
import java.awt.geom.Rectangle2D;
58
import java.awt.image.BufferedImage;
59
import java.util.HashMap;
60

    
61
import javax.swing.JComponent;
62
import javax.swing.Timer;
63

    
64
import org.cresques.cts.IProjection;
65

    
66
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
67
import com.iver.cit.gvsig.fmap.edition.commands.CommandListener;
68
import com.iver.cit.gvsig.fmap.layers.LayerEvent;
69
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
70
import com.iver.cit.gvsig.fmap.tools.CompoundBehavior;
71
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
72
import com.iver.utiles.exceptionHandling.ExceptionHandlingSupport;
73
import com.iver.utiles.exceptionHandling.ExceptionListener;
74
import com.iver.utiles.swing.threads.Cancellable;
75

    
76

    
77
/**
78
 * MapControl.
79
 *
80
 * @author Fernando Gonz?lez Cort?s
81
 */
82
public class MapControl extends JComponent implements ComponentListener,
83
                        CommandListener {
84
        /** Cuando la vista est? actualizada. */
85
        public static final int ACTUALIZADO = 0;
86

    
87
        /** Cuando la vista est? desactualizada. */
88
        public static final int DESACTUALIZADO = 1;
89
    public static final int ONLY_GRAPHICS = 2;
90
    // public static final int FAST_PAINT = 3;
91
        //private static Logger logger = Logger.getLogger(MapControl.class.getName());
92
        private FMap mapContext = null;
93
    //private boolean drawerAlive = false;
94
        private HashMap namesMapTools = new HashMap();
95
        private Behavior currentMapTool = null;
96
        private int status = DESACTUALIZADO;
97
        private BufferedImage image = null;
98
        private String currentTool;
99
        private CancelDraw canceldraw;
100
        //private boolean isCancelled = true;
101
        private Timer timer;
102
        protected ViewPort vp;
103
        //private Drawer drawer;
104
    private Drawer2 drawer2;
105
    // private boolean firstDraw = true;
106
        private MapToolListener mapToolListener = new MapToolListener();
107
        private MapContextListener mapContextListener = new MapContextListener();
108
        private ExceptionHandlingSupport exceptionHandlingSupport = new ExceptionHandlingSupport();
109

    
110
        private String prevTool;
111

    
112
        /**
113
     * We need this to avoid not wanted refresh. REMEMBER TO SET TO TRUE!!
114
     */
115
    // private boolean paintEnabled = false;
116

    
117
        /**
118
         * Crea un nuevo NewMapControl.
119
         */
120
        public MapControl() {
121
                this.setName("MapControl");
122
                setDoubleBuffered(false);
123
                setOpaque(true);
124
                status = DESACTUALIZADO;
125

    
126
                //Clase usada para cancelar el dibujado
127
                canceldraw = new CancelDraw();
128

    
129
                //Modelo de datos y ventana del mismo
130
                // TODO: Cuando creamos un mapControl, deber?amos asignar
131
                // la projecci?n por defecto con la que vayamos a trabajar.
132
                // 23030 es el c?digo EPSG del UTM30 elipsoide ED50
133
                vp = new ViewPort(CRSFactory.getCRS("EPSG:23030"));
134
                setMapContext(new FMap(vp));
135

    
136
                //eventos
137
                this.addComponentListener(this);
138
                this.addMouseListener(mapToolListener);
139
                this.addMouseMotionListener(mapToolListener);
140
                this.addMouseWheelListener(mapToolListener);
141

    
142
        this.drawer2 = new Drawer2();
143
                //Timer para mostrar el redibujado mientras se dibuja
144
                timer = new Timer(300,
145
                                new ActionListener() {
146
                                        public void actionPerformed(ActionEvent e) {
147
                                                MapControl.this.repaint();
148
                                        }
149
                                });
150
        }
151

    
152
        /**
153
         * Inserta el modelo.
154
         *
155
         * @param model FMap.
156
         */
157
        public void setMapContext(FMap model) {
158
                if (mapContext != null) {
159
                        mapContext.removeAtomicEventListener(mapContextListener);
160
                }
161

    
162
                mapContext = model;
163

    
164
                if (mapContext.getViewPort() == null) {
165
                        mapContext.setViewPort(vp);
166
                } else {
167
                        vp = mapContext.getViewPort();
168

    
169
                        // vp.setImageSize(new Dimension(getWidth(), getHeight()));
170
                        //System.err.println("Viewport en setMapContext:" + vp);
171
                }
172

    
173
                mapContext.addAtomicEventListener(mapContextListener);
174

    
175
                status = DESACTUALIZADO;
176
        }
177

    
178
        /**
179
         * Devuelve la proyecci?n.
180
         *
181
         * @return Proyecci?n.
182
         */
183
        public IProjection getProjection() {
184
                return getMapContext().getProjection();
185
        }
186

    
187
        /**
188
         * Inserta una proyecci?n.
189
         *
190
         * @param proj Proyecci?n.
191
         */
192
        public void setProjection(IProjection proj) {
193
                getMapContext().setProjection(proj);
194
        }
195

    
196
        /**
197
         * Devuelve el modelo.
198
         *
199
         * @return FMap.
200
         */
201
        public FMap getMapContext() {
202
                return mapContext;
203
        }
204

    
205
        /**
206
         * Registra una herramienta (tool).
207
         *
208
         * @param name Nombre de la herramienta.
209
         * @param tool Herramienta.
210
         */
211
        public void addMapTool(String name, Behavior tool) {
212
                namesMapTools.put(name, tool);
213
                tool.setMapControl(this);
214
        }
215

    
216
        public void addMapTool(String name, Behavior[] tools){
217
                CompoundBehavior tool = new CompoundBehavior(tools);
218
                addMapTool(name, tool);
219
        }
220

    
221
        /**
222
         * DOCUMENT ME!
223
         *
224
         * @param toolName DOCUMENT ME!
225
         */
226
        public void setTool(String toolName) {
227
                prevTool=getTool();
228
                Behavior mapTool = (Behavior) namesMapTools.get(toolName);
229
                currentMapTool = mapTool;
230
                currentTool = toolName;
231
                this.setCursor(mapTool.getCursor());
232
        }
233
        public Behavior getCurrentMapTool(){
234
                return currentMapTool;
235
        }
236
        /**
237
         * Devuelve el nombre de la herramienta seleccionada.
238
         *
239
         * @return nombre.
240
         */
241
        public String getTool() {
242
                return currentTool;
243
        }
244

    
245
        /**
246
         * Cancela el dibujado. Se espera a que la cancelaci?n surta efecto
247
         */
248
        public void cancelDrawing() {
249
                /* if (drawer != null) {
250
                        if (!drawer.isAlive()) {
251
                                return;
252
                        }
253
                }
254
                */
255
                canceldraw.setCanceled(true);
256

    
257
                /* while (!isCancelled) {
258
                        if (!drawer.isAlive()) {
259
                            // Si hemos llegado aqu? con un thread vivo, seguramente
260
                            // no estamos actualizados.
261

262
                                break;
263
                        }
264

265
                }
266
                canceldraw.setCancel(false);
267
                isCancelled = false;
268
        drawerAlive = false; */
269
        }
270

    
271
    private boolean adaptToImageSize()
272
    {
273
        if ((image == null) || (vp.getImageWidth() != this.getWidth()) || (vp.getImageHeight() != this.getHeight()))
274
        {
275
            image = new BufferedImage(this.getWidth(), this.getHeight(),
276
                    BufferedImage.TYPE_INT_ARGB);
277
            vp.setImageSize(new Dimension(getWidth(), getHeight()));
278
            getMapContext().getViewPort().setScale();
279

    
280

    
281
            Graphics gTemp = image.createGraphics();
282
            Color theBackColor = vp.getBackColor();
283
            if (theBackColor == null)
284
                gTemp.setColor(Color.WHITE);
285
            else
286
                gTemp.setColor(theBackColor);
287

    
288
            gTemp.fillRect(0,0,getWidth(), getHeight());
289
            gTemp.dispose();
290
            status = DESACTUALIZADO;
291
            // g.drawImage(image,0,0,null);
292
            return true;
293
        }
294
        return false;
295
    }
296

    
297
        /**
298
         * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
299
         */
300
        protected void paintComponent(Graphics g) {
301
        adaptToImageSize();
302
        /* if (status == FAST_PAINT) {
303
            System.out.println("FAST_PAINT");
304
            g.drawImage(image,0,0,null);
305
            status = ACTUALIZADO;
306
            return;
307
        } */
308
        // System.out.println("PINTANDO MAPCONTROL" + this);
309
                if (status == ACTUALIZADO) {
310
                        // LWS logger.debug("Dibujando la imagen obtenida");
311

    
312
                        /*
313
                         * Si hay un behaviour y la imagen es distinta de null se delega el dibujado
314
                         * en dicho behaviour
315
                         */
316
            if (image != null)
317
            {
318
                if (currentMapTool != null)
319
                    currentMapTool.paintComponent(g);
320
                else
321
                    g.drawImage(image,0,0,null);
322

    
323
                                // System.out.println("Pinto ACTUALIZADO");
324
                        }
325
                } else if ((status == DESACTUALIZADO)
326
                || (status == ONLY_GRAPHICS)) {
327
                        // LWS System.out.println("DESACTUALIZADO: Obteniendo la imagen de la cartograf?a");
328
                        /* if (isOpaque())
329
                        {
330
                            if (image==null)
331
                            {
332
                                g.setColor(vp.getBackColor());
333
                                g.fillRect(0,0,getWidth(), getHeight());
334
                            }
335
                            // else g.drawImage(image,0,0,null);
336
                        } */
337
            // cancelDrawing();
338
                        //Se crea la imagen con el color de fonde deseado
339
                        /* if (image == null)
340
                        {
341
                                image = new BufferedImage(this.getWidth(), this.getHeight(),
342
                                                BufferedImage.TYPE_INT_ARGB);
343
                                vp.setImageSize(new Dimension(getWidth(), getHeight()));
344
                                Graphics gTemp = image.createGraphics();
345
                            Color theBackColor = vp.getBackColor();
346
                            if (theBackColor == null)
347
                                gTemp.setColor(Color.WHITE);
348
                            else
349
                                gTemp.setColor(theBackColor);
350

351
                                gTemp.fillRect(0,0,getWidth(), getHeight());
352
                                gTemp.dispose();
353
                                // g.drawImage(image,0,0,null);
354
                                System.out.println("Imagen con null en DESACTUALIZADO. Width = " + this.getWidth());
355
                        } */
356
            // else
357
            // {
358

    
359

    
360
            // if (image != null)
361
            //  {
362
                g.drawImage(image,0,0,null);
363

    
364
                drawer2.put(new PaintingRequest());
365
                timer.start();
366
            /* }
367
            else
368
                return; */
369
            // }
370

    
371
            /* if (drawerAlive == false)
372
            {
373
                drawer = new Drawer(image, canceldraw);
374
                drawer.start();
375
                        //Se lanza el tread de dibujado
376
            } */
377

    
378
                        // status = ACTUALIZADO;
379
                }
380
        }
381

    
382
        /**
383
         * Devuelve la imagen de la vista.
384
         *
385
         * @return imagen.
386
         */
387
        public BufferedImage getImage() {
388
                return image;
389
        }
390

    
391
        /**
392
         * Marca el mapa para que en el pr?ximo redibujado se acceda a la
393
         * cartograf?a para reobtener la imagen
394
         * @param doClear Solo deber?a ser true cuando se llama desde pan
395
         */
396
        public void drawMap(boolean doClear) {
397
                cancelDrawing();
398
                System.out.println("drawMap con doClear=" + doClear);
399
        status = DESACTUALIZADO;
400
        getMapContext().getLayers().setDirty(true);
401
                if (doClear)
402
        {
403
            // image = null; // Se usa para el PAN
404
            if (image != null)
405
            {
406
                Graphics2D g = image.createGraphics();
407
                Color theBackColor = vp.getBackColor();
408
                if (theBackColor == null)
409
                    g.setColor(Color.WHITE);
410
                else
411
                    g.setColor(theBackColor);
412
                g.fillRect(0, 0, vp.getImageWidth(), vp.getImageHeight());
413
                g.dispose();
414
            }
415
        }
416
                repaint();
417
        }
418
        
419
        public void rePaintDirtyLayers()
420
        {
421
                cancelDrawing();
422
        status = DESACTUALIZADO;
423
        repaint();
424
        }
425
        
426
    public void drawGraphics() {
427
        status = ONLY_GRAPHICS;
428
        repaint();
429
    }
430

    
431
        /**
432
         * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
433
         */
434
        public void componentHidden(ComponentEvent e) {
435
        }
436

    
437
        /**
438
         * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
439
         */
440
        public void componentMoved(ComponentEvent e) {
441
        }
442

    
443
        /**
444
         * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
445
         */
446
        public void componentResized(ComponentEvent e) {
447
                /* image = new BufferedImage(this.getWidth(), this.getHeight(),
448
                                BufferedImage.TYPE_INT_ARGB);
449
                Graphics gTemp = image.createGraphics();
450
                gTemp.setColor(vp.getBackColor());
451
                gTemp.fillRect(0,0,getWidth(), getHeight());
452
        System.out.println("MapControl resized");
453
            // image = null;
454
            vp.setImageSize(new Dimension(getWidth(), getHeight()));
455
                getMapContext().getViewPort().setScale(); */
456
                // drawMap(true);
457
        }
458

    
459
        /**
460
         * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
461
         */
462
        public void componentShown(ComponentEvent e) {
463
        }
464

    
465
        /**
466
         * A?ade un listener de tipo ExceptionListener.
467
         *
468
         * @param o ExceptionListener.
469
         */
470
        public void addExceptionListener(ExceptionListener o) {
471
                exceptionHandlingSupport.addExceptionListener(o);
472
        }
473

    
474
        /**
475
         * Borra la ExceptioListener que se pasa como par?metro.
476
         *
477
         * @param o ExceptionListener.
478
         *
479
         * @return True si se borra correctamente.
480
         */
481
        public boolean removeExceptionListener(ExceptionListener o) {
482
                return exceptionHandlingSupport.removeExceptionListener(o);
483
        }
484

    
485
        /**
486
         * Lanza una Excepci?n.
487
         *
488
         * @param t Excepci?n.
489
         */
490
        private void throwException(Throwable t) {
491
                exceptionHandlingSupport.throwException(t);
492
        }
493

    
494

    
495
    private class PaintingRequest
496
    {
497

    
498
        public PaintingRequest()
499
        {
500
        }
501

    
502
        public void paint()
503
        {
504
            try
505
            {
506
                    canceldraw.setCanceled(false);
507
                /* if (image == null)
508
                {
509
                    image = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(),
510
                            BufferedImage.TYPE_INT_ARGB);
511
                    Graphics gTemp = image.createGraphics();
512
                    Color theBackColor = vp.getBackColor();
513
                    if (theBackColor == null)
514
                        gTemp.setColor(Color.WHITE);
515
                    else
516
                        gTemp.setColor(theBackColor);
517

518
                    gTemp.fillRect(0,0,getWidth(), getHeight());
519
                    gTemp.dispose();
520
                    // g.drawImage(image,0,0,null);
521
                    System.out.println("Imagen con null en DESACTUALIZADO. Width = " + this.getWidth());
522
                } */
523
                Graphics2D g = image.createGraphics();
524

    
525
                ViewPort viewPort = mapContext.getViewPort();
526

    
527
                if (status == DESACTUALIZADO)
528
                {
529
                        Graphics2D gTemp = image.createGraphics();
530
                    Color theBackColor = viewPort.getBackColor();
531
                    if (theBackColor == null)
532
                        gTemp.setColor(Color.WHITE);
533
                    else
534
                        gTemp.setColor(theBackColor);
535
                    gTemp.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight());
536
                    status = ACTUALIZADO;
537
                    mapContext.draw(image, g, canceldraw, mapContext.getScaleView());
538

    
539
                }
540
                else if (status == ONLY_GRAPHICS)
541
                {
542
                    status = ACTUALIZADO;
543
                    mapContext.drawGraphics(image, g, canceldraw,mapContext.getScaleView());
544

    
545
                }
546

    
547

    
548
                // status = FAST_PAINT;
549
              //  drawerAlive = false;
550
                timer.stop();
551
                repaint();
552

    
553

    
554
            } catch (Throwable e) {
555
                timer.stop();
556
              //  isCancelled = true;
557
                e.printStackTrace();
558
                throwException(e);
559
            } finally {
560
            }
561

    
562
        }
563

    
564
    }
565

    
566
    /**
567
     * @author fjp
568
     *
569
     * Basasdo en el patr?n WorkerThread
570
     *
571
     */
572
    public class Drawer2
573
    {
574
        // Una mini cola de 2. No acumulamos peticiones de dibujado
575
        // dibujamos solo lo ?ltimo que nos han pedido.
576
        private PaintingRequest paintingRequest;
577
        private PaintingRequest waitingRequest;
578

    
579
        private boolean waiting;
580
        private boolean shutdown;
581

    
582
        public void setShutdown(boolean isShutdown)
583
        {
584
            shutdown = isShutdown;
585
        }
586

    
587
        public Drawer2()
588
        {
589
            paintingRequest = null;
590
            waitingRequest = null;
591
            waiting = false;
592
            shutdown = false;
593
            new Thread(new Worker()).start();
594
        }
595

    
596
        public void put(PaintingRequest newPaintRequest)
597
        {
598
            waitingRequest = newPaintRequest;
599
            if (waiting)
600
            {
601
                synchronized (this) {
602
                    notifyAll();
603
                }
604
            }
605
        }
606

    
607
        public PaintingRequest take()
608
        {
609
            if (waitingRequest == null)
610
            {
611
                synchronized (this) {
612
                    waiting = true;
613
                    try {
614
                        wait();
615
                    }
616
                    catch (InterruptedException ie)
617
                    {
618
                        waiting = false;
619
                    }
620
                }
621
            }
622
            paintingRequest = waitingRequest;
623
            waitingRequest = null;
624
            return paintingRequest;
625
        }
626

    
627

    
628
        private class Worker implements Runnable
629
        {
630
            public void run()
631
            {
632
                while (!shutdown)
633
                {
634
                    PaintingRequest p = take();
635
                    System.out.println("Pintando");
636
                    if (image != null){
637
                            cancelDrawing();
638
                        p.paint();
639
                    } else
640
                        status = DESACTUALIZADO;
641
                }
642
            }
643
        }
644
    }
645

    
646
        /**
647
         * Clase utilizada para dibujar las capas.
648
         *
649
         * @author Vicente Caballero Navarro
650
         */
651
        public class Drawer extends Thread {
652
                //private Graphics g;
653
                private BufferedImage image = null;
654
                private CancelDraw cancel;
655
                //private boolean threadCancel = false;
656

    
657
                /**
658
                 * Crea un nuevo Drawer.
659
                 *
660
                 */
661
                public Drawer(BufferedImage image, CancelDraw cancel)
662
        {
663
                        this.image = image;
664
                        this.cancel = cancel;
665
         //   drawerAlive = true;
666
                }
667

    
668
                /**
669
                 * @see java.lang.Runnable#run()
670
                 */
671
                public void run() {
672
                        try {
673
                                // synchronized (Drawer.class) {
674
                                    Graphics2D g = image.createGraphics();
675

    
676
                                    ViewPort viewPort = mapContext.getViewPort();
677
                    if (status == DESACTUALIZADO)
678
                    {
679
                                        Color theBackColor = viewPort.getBackColor();
680
                                        if (theBackColor == null)
681
                                            g.setColor(Color.WHITE);
682
                                        else
683
                                            g.setColor(theBackColor);
684
                                            g.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight());
685
                        status = ACTUALIZADO;
686
                        mapContext.draw(image, g, cancel,mapContext.getScaleView());
687
                    }
688
                    else if (status == ONLY_GRAPHICS)
689
                    {
690
                        status = ACTUALIZADO;
691
                        mapContext.drawGraphics(image, g, cancel,mapContext.getScaleView());
692
                    }
693

    
694
                                        timer.stop();
695
                    // status = FAST_PAINT;
696
                  //  drawerAlive = false;
697
                                        repaint();
698

    
699

    
700

    
701
                                // }
702
                        } catch (Throwable e) {
703
                            timer.stop();
704
                                //isCancelled = true;
705
                e.printStackTrace();
706
                                throwException(e);
707
                        } finally {
708
                        }
709
                }
710
        }
711

    
712
        /**
713
         * Clase para cancelar el dibujado.
714
         *
715
         * @author Fernando Gonz?lez Cort?s
716
         */
717
        public class CancelDraw implements Cancellable {
718
                private boolean cancel = false;
719

    
720
                /**
721
                 * Crea un nuevo CancelDraw.
722
                 */
723
                public CancelDraw() {
724
                }
725

    
726
                /**
727
                 * Insertar si se debe cancelar el dibujado.
728
                 *
729
                 * @param b true si se debe cancelar el dibujado.
730
                 */
731
                public void setCanceled(boolean b) {
732
                        cancel = b;
733
                }
734

    
735
                /**
736
                 * @see com.iver.utiles.swing.threads.Cancellable#isCanceled()
737
                 */
738
                public boolean isCanceled() {
739
                        return cancel;
740
                }
741
        }
742
        
743

    
744
        /**
745
         * Listener del MapTool.
746
         *
747
         * @author Fernando Gonz?lez Cort?s
748
         */
749
        public class MapToolListener implements MouseListener, MouseWheelListener,
750
                MouseMotionListener {
751

    
752
                long t1;
753
                Point2D pReal;
754
                /**
755
                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
756
                 */
757
                public void mouseClicked(MouseEvent e) {
758
                        try {
759
                                currentMapTool.mouseClicked(e);
760
                        } catch (BehaviorException t) {
761
                                throwException(t);
762
                        }
763
                }
764

    
765
                /**
766
                 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
767
                 */
768
                public void mouseEntered(MouseEvent e) {
769
                        try {
770
                                currentMapTool.mouseEntered(e);
771
                        } catch (BehaviorException t) {
772
                                throwException(t);
773
                        }
774
                }
775

    
776
                /**
777
                 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
778
                 */
779
                public void mouseExited(MouseEvent e) {
780
                        try {
781
                                currentMapTool.mouseExited(e);
782
                        } catch (BehaviorException t) {
783
                                throwException(t);
784
                        }
785
                }
786

    
787
                /**
788
                 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
789
                 */
790
                public void mousePressed(MouseEvent e) {
791
                        try {
792
                                currentMapTool.mousePressed(e);
793
                        } catch (BehaviorException t) {
794
                                throwException(t);
795
                        }
796
                }
797

    
798
                /**
799
                 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
800
                 */
801
                public void mouseReleased(MouseEvent e) {
802
                        try {
803
                                currentMapTool.mouseReleased(e);
804
                        } catch (BehaviorException t) {
805
                                throwException(t);
806
                        }
807
                }
808

    
809
                /**
810
                 * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
811
                 */
812
                public void mouseWheelMoved(MouseWheelEvent e) {
813
                        try {
814
                                currentMapTool.mouseWheelMoved(e);
815
                                
816
                                // Si el tool actual no ha consumido el evento
817
                                // entendemos que quiere el comportamiento por defecto.
818
                                if (!e.isConsumed())
819
                                {
820
                                        // Para usar el primer punto sobre el que queremos centrar
821
                                        // el mapa, dejamos pasar un segundo para considerar el siguiente
822
                                        // punto como v?lido.
823
                                        if (t1 == 0)
824
                                        {
825
                                                t1= System.currentTimeMillis();
826
                                                pReal = vp.toMapPoint(e.getPoint());
827
                                        }
828
                                        else
829
                                        {
830
                                                long t2 = System.currentTimeMillis();
831
                                                if ((t2-t1) > 1000)
832
                                                        t1=0;
833
                                        }
834
                                        cancelDrawing();
835
                                        ViewPort vp = getViewPort();
836
                                        
837

    
838
                                        /* Point2D pReal = new Point2D.Double(vp.getAdjustedExtent().getCenterX(),
839
                                                        vp.getAdjustedExtent().getCenterY()); */
840
                                        int amount = e.getWheelRotation();
841
                                        double nuevoX;
842
                                        double nuevoY;
843
                                        double factor;
844

    
845
                                        if (amount > 0) // nos acercamos
846
                                        {
847
                                                factor = 0.9;
848
                                        } else // nos alejamos
849
                                        {
850
                                                factor = 1.2;
851
                                        }
852
                                        Rectangle2D.Double r = new Rectangle2D.Double();
853
                                        if (vp.getExtent() != null) {
854
                                                nuevoX = pReal.getX()
855
                                                                - ((vp.getExtent().getWidth() * factor) / 2.0);
856
                                                nuevoY = pReal.getY()
857
                                                                - ((vp.getExtent().getHeight() * factor) / 2.0);
858
                                                r.x = nuevoX;
859
                                                r.y = nuevoY;
860
                                                r.width = vp.getExtent().getWidth() * factor;
861
                                                r.height = vp.getExtent().getHeight() * factor;
862

    
863
                                                vp.setExtent(r);
864
                                        }
865
                                        
866
                                        
867
                                }
868
                        } catch (BehaviorException t) {
869
                                throwException(t);
870
                        }
871
                }
872

    
873
                /**
874
                 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
875
                 */
876
                public void mouseDragged(MouseEvent e) {
877
                        try {
878
                                currentMapTool.mouseDragged(e);
879
                        } catch (BehaviorException t) {
880
                                throwException(t);
881
                        }
882
                }
883

    
884
                /**
885
                 * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
886
                 */
887
                public void mouseMoved(MouseEvent e) {
888
                        try {
889
                                currentMapTool.mouseMoved(e);
890
                        } catch (BehaviorException t) {
891
                                throwException(t);
892
                        }
893
                }
894
        }
895

    
896
        /**
897
         * Listener sobre el MapContext.
898
         *
899
         * @author Fernando Gonz?lez Cort?s
900
         */
901
        public class MapContextListener implements AtomicEventListener {
902
                /**
903
                 * @see com.iver.cit.gvsig.fmap.AtomicEventListener#atomicEvent(com.iver.cit.gvsig.fmap.AtomicEvent)
904
                 */
905
                public void atomicEvent(AtomicEvent e) {
906
                        boolean redraw = false;
907
                        LayerEvent[] layerEvents = e.getLayerEvents();
908

    
909
                        for (int i = 0; i < layerEvents.length; i++) {
910
                                if (layerEvents[i].getProperty().equals("visible")) {
911
                                        redraw = true;
912
                                }
913
                        }
914

    
915
                        if (e.getColorEvents().length > 0) {
916
                                redraw = true;
917
                        }
918

    
919
                        if (e.getExtentEvents().length > 0) {
920
                                redraw = true;
921
                        }
922
                        
923
                        if (e.getProjectionEvents().length > 0) {
924
                                //redraw = true;
925
                        }
926

    
927
                        
928
                        if (e.getLayerCollectionEvents().length > 0) {
929
                                redraw = true;
930
                        }
931

    
932
                        if (e.getLegendEvents().length > 0) {
933
                                redraw = true;
934
                        }
935

    
936
                        if (e.getSelectionEvents().length > 0) {
937
                                redraw = true;
938
                        }
939

    
940
                        if (redraw) {
941
                System.out.println("MapContextListener redraw");
942
                                MapControl.this.drawMap(false);
943
                        }
944
                }
945
        }
946
        public ViewPort getViewPort() {
947
                return vp;
948
        }
949

    
950
        /**
951
         * Returns a HashMap with the tools that have been registered
952
         * in the Mapcontrol.
953
         */
954
        public HashMap getNamesMapTools() {
955
                return namesMapTools;
956
        }
957

    
958
        public void commandRepaint() {
959
                drawMap(false);
960
        }
961

    
962
        public void commandRefresh() {
963
                // TODO Auto-generated method stub
964
        }
965

    
966
        public void setPrevTool() {
967
                setTool(prevTool);
968
        }
969

    
970
}