Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_1_RELEASE / libraries / libFMap / src / com / iver / cit / gvsig / fmap / MapControl.java @ 9531

History | View | Annotate | Download (27.3 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.Component;
45
import java.awt.Dimension;
46
import java.awt.Graphics;
47
import java.awt.Graphics2D;
48
import java.awt.GraphicsEnvironment;
49
import java.awt.event.ActionEvent;
50
import java.awt.event.ActionListener;
51
import java.awt.event.ComponentEvent;
52
import java.awt.event.ComponentListener;
53
import java.awt.event.MouseEvent;
54
import java.awt.event.MouseListener;
55
import java.awt.event.MouseMotionListener;
56
import java.awt.event.MouseWheelEvent;
57
import java.awt.event.MouseWheelListener;
58
import java.awt.geom.Point2D;
59
import java.awt.geom.Rectangle2D;
60
import java.awt.image.BufferedImage;
61
import java.util.HashMap;
62
import java.util.Set;
63

    
64
import javax.swing.JComponent;
65
import javax.swing.Timer;
66

    
67
import org.cresques.cts.IProjection;
68

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

    
79

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

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

    
113
        private String prevTool;
114

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

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

    
129
                //Clase usada para cancelar el dibujado
130
                canceldraw = new CancelDraw();
131

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

    
139
                //eventos
140
                this.addComponentListener(this);
141
                this.addMouseListener(mapToolListener);
142
                this.addMouseMotionListener(mapToolListener);
143
                this.addMouseWheelListener(mapToolListener);
144

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

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

    
165
                mapContext = model;
166

    
167
                if (mapContext.getViewPort() == null) {
168
                        mapContext.setViewPort(vp);
169
                } else {
170
                        vp = mapContext.getViewPort();
171

    
172
                        // vp.setImageSize(new Dimension(getWidth(), getHeight()));
173
                        //System.err.println("Viewport en setMapContext:" + vp);
174
                }
175

    
176
                mapContext.addAtomicEventListener(mapContextListener);
177

    
178
                status = DESACTUALIZADO;
179
        }
180

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

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

    
199
        /**
200
         * Devuelve el modelo.
201
         *
202
         * @return FMap.
203
         */
204
        public MapContext getMapContext() {
205
                return mapContext;
206
        }
207

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

    
219
        public void addMapTool(String name, Behavior[] tools){
220
                CompoundBehavior tool = new CompoundBehavior(tools);
221
                addMapTool(name, tool);
222
        }
223
        
224
        /**
225
         * Devuelve una herramienta (tool) registrada.
226
         *
227
         * @param name Nombre de la herramienta.
228
         * @return tool Herramienta.
229
         */
230
        public Behavior getMapTool(String name) {
231
                return (Behavior)namesMapTools.get(name);
232
        }
233

    
234
        /**
235
         * Devuelve los nombres de herramientas (tool) registradas.
236
         * 
237
         * @return Set (String) nombres de las herramientas.
238
         */
239
        public Set getMapToolsKeySet() {
240
                return namesMapTools.keySet();
241
        }
242

    
243
        
244
        /**
245
         * Returns true if this map control contains a tool named with the value
246
         * passed in the toolName parameter. If you have added two tools with the
247
         * same name, the last tool will overwrite the previous ones.
248
         * @param toolName
249
         * @return true if the map control contains a tool with the same name than
250
         *                 toolName. False, otherwise.
251
         */
252
        public boolean hasTool(String toolName) {
253
                return namesMapTools.containsKey(toolName);
254
        }
255
        /**
256
         * DOCUMENT ME!
257
         *
258
         * @param toolName DOCUMENT ME!
259
         */
260
        public void setTool(String toolName) {
261
                prevTool=getCurrentTool();
262
                Behavior mapTool = (Behavior) namesMapTools.get(toolName);
263
                currentMapTool = mapTool;
264
                currentTool = toolName;
265
                this.setCursor(mapTool.getCursor());
266
        }
267
        public Behavior getCurrentMapTool(){
268
                return currentMapTool;
269
        }
270
        /**
271
         * Returns the name of the current selected tool on this MapControl
272
         *
273
         * @return A tool name.
274
         */
275
        public String getCurrentTool() {
276
                return currentTool;
277
        }
278
        
279

    
280
        /**
281
         * Cancela el dibujado. Se espera a que la cancelaci?n surta efecto
282
         */
283
        public void cancelDrawing() {
284
                /* if (drawer != null) {
285
                        if (!drawer.isAlive()) {
286
                                return;
287
                        }
288
                }
289
                */
290
                canceldraw.setCanceled(true);
291

    
292
                /* while (!isCancelled) {
293
                        if (!drawer.isAlive()) {
294
                            // Si hemos llegado aqu? con un thread vivo, seguramente
295
                            // no estamos actualizados.
296

297
                                break;
298
                        }
299

300
                }
301
                canceldraw.setCancel(false);
302
                isCancelled = false;
303
        drawerAlive = false; */
304
        }
305

    
306
    private boolean adaptToImageSize()
307
    {
308
        if ((image == null) || (vp.getImageWidth() != this.getWidth()) || (vp.getImageHeight() != this.getHeight()))
309
        {
310
            image = new BufferedImage(this.getWidth(), this.getHeight(),
311
                    BufferedImage.TYPE_INT_ARGB);
312
            // ESTILO MAC
313
//                image = GraphicsEnvironment.getLocalGraphicsEnvironment()
314
//                                .getDefaultScreenDevice().getDefaultConfiguration()
315
//                                .createCompatibleImage(this.getWidth(), this.getHeight());
316
            vp.setImageSize(new Dimension(getWidth(), getHeight()));
317
            getMapContext().getViewPort().refreshExtent();
318

    
319

    
320
            Graphics gTemp = image.createGraphics();
321
            Color theBackColor = vp.getBackColor();
322
            if (theBackColor == null)
323
                gTemp.setColor(Color.WHITE);
324
            else
325
                gTemp.setColor(theBackColor);
326

    
327
            gTemp.fillRect(0,0,getWidth(), getHeight());
328
            gTemp.dispose();
329
            status = DESACTUALIZADO;
330
            // g.drawImage(image,0,0,null);
331
            return true;
332
        }
333
        return false;
334
    }
335

    
336
        /**
337
         * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
338
         */
339
        protected void paintComponent(Graphics g) {
340
        adaptToImageSize();
341
        /* if (status == FAST_PAINT) {
342
            System.out.println("FAST_PAINT");
343
            g.drawImage(image,0,0,null);
344
            status = ACTUALIZADO;
345
            return;
346
        } */
347
        // System.out.println("PINTANDO MAPCONTROL" + this);
348
                if (status == ACTUALIZADO) {
349
                        // LWS logger.debug("Dibujando la imagen obtenida");
350

    
351
                        /*
352
                         * Si hay un behaviour y la imagen es distinta de null se delega el dibujado
353
                         * en dicho behaviour
354
                         */
355
            if (image != null)
356
            {
357
                if (currentMapTool != null)
358
                    currentMapTool.paintComponent(g);
359
                else
360
                    g.drawImage(image,0,0,null);
361

    
362
                                // System.out.println("Pinto ACTUALIZADO");
363
                        }
364
                } else if ((status == DESACTUALIZADO)
365
                || (status == ONLY_GRAPHICS)) {
366
                        // LWS System.out.println("DESACTUALIZADO: Obteniendo la imagen de la cartograf?a");
367
                        /* if (isOpaque())
368
                        {
369
                            if (image==null)
370
                            {
371
                                g.setColor(vp.getBackColor());
372
                                g.fillRect(0,0,getWidth(), getHeight());
373
                            }
374
                            // else g.drawImage(image,0,0,null);
375
                        } */
376
            // cancelDrawing();
377
                        //Se crea la imagen con el color de fonde deseado
378
                        /* if (image == null)
379
                        {
380
                                image = new BufferedImage(this.getWidth(), this.getHeight(),
381
                                                BufferedImage.TYPE_INT_ARGB);
382
                                vp.setImageSize(new Dimension(getWidth(), getHeight()));
383
                                Graphics gTemp = image.createGraphics();
384
                            Color theBackColor = vp.getBackColor();
385
                            if (theBackColor == null)
386
                                gTemp.setColor(Color.WHITE);
387
                            else
388
                                gTemp.setColor(theBackColor);
389

390
                                gTemp.fillRect(0,0,getWidth(), getHeight());
391
                                gTemp.dispose();
392
                                // g.drawImage(image,0,0,null);
393
                                System.out.println("Imagen con null en DESACTUALIZADO. Width = " + this.getWidth());
394
                        } */
395
            // else
396
            // {
397

    
398

    
399
            // if (image != null)
400
            //  {
401
                g.drawImage(image,0,0,null);
402

    
403
                drawer2.put(new PaintingRequest());
404
                timer.start();
405
            /* }
406
            else
407
                return; */
408
            // }
409

    
410
            /* if (drawerAlive == false)
411
            {
412
                drawer = new Drawer(image, canceldraw);
413
                drawer.start();
414
                        //Se lanza el tread de dibujado
415
            } */
416

    
417
                        // status = ACTUALIZADO;
418
                }
419
        }
420

    
421
        /**
422
         * Devuelve la imagen de la vista.
423
         *
424
         * @return imagen.
425
         */
426
        public BufferedImage getImage() {
427
                return image;
428
        }
429

    
430
        /**
431
         * Marca el mapa para que en el pr?ximo redibujado se acceda a la
432
         * cartograf?a para reobtener la imagen
433
         * @param doClear Solo deber?a ser true cuando se llama desde pan
434
         */
435
        public void drawMap(boolean doClear) {
436
                cancelDrawing();
437
                System.out.println("drawMap con doClear=" + doClear);
438
        status = DESACTUALIZADO;
439
        getMapContext().getLayers().setDirty(true);
440
                if (doClear)
441
        {
442
            // image = null; // Se usa para el PAN
443
            if (image != null)
444
            {
445
                Graphics2D g = image.createGraphics();
446
                Color theBackColor = vp.getBackColor();
447
                if (theBackColor == null)
448
                    g.setColor(Color.WHITE);
449
                else
450
                    g.setColor(theBackColor);
451
                g.fillRect(0, 0, vp.getImageWidth(), vp.getImageHeight());
452
                g.dispose();
453
            }
454
        }
455
                repaint();
456
        }
457

    
458
        public void rePaintDirtyLayers()
459
        {
460
                cancelDrawing();
461
        status = DESACTUALIZADO;
462
        repaint();
463
        }
464

    
465
    public void drawGraphics() {
466
        status = ONLY_GRAPHICS;
467
        repaint();
468
    }
469

    
470
        /**
471
         * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
472
         */
473
        public void componentHidden(ComponentEvent e) {
474
        }
475

    
476
        /**
477
         * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
478
         */
479
        public void componentMoved(ComponentEvent e) {
480
        }
481

    
482
        /**
483
         * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
484
         */
485
        public void componentResized(ComponentEvent e) {
486
                /* image = new BufferedImage(this.getWidth(), this.getHeight(),
487
                                BufferedImage.TYPE_INT_ARGB);
488
                Graphics gTemp = image.createGraphics();
489
                gTemp.setColor(vp.getBackColor());
490
                gTemp.fillRect(0,0,getWidth(), getHeight());
491
        System.out.println("MapControl resized");
492
            // image = null;
493
            vp.setImageSize(new Dimension(getWidth(), getHeight()));
494
                getMapContext().getViewPort().setScale(); */
495
                // drawMap(true);
496
        }
497

    
498
        /**
499
         * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
500
         */
501
        public void componentShown(ComponentEvent e) {
502
        }
503

    
504
        /**
505
         * A?ade un listener de tipo ExceptionListener.
506
         *
507
         * @param o ExceptionListener.
508
         */
509
        public void addExceptionListener(ExceptionListener o) {
510
                exceptionHandlingSupport.addExceptionListener(o);
511
        }
512

    
513
        /**
514
         * Borra la ExceptioListener que se pasa como par?metro.
515
         *
516
         * @param o ExceptionListener.
517
         *
518
         * @return True si se borra correctamente.
519
         */
520
        public boolean removeExceptionListener(ExceptionListener o) {
521
                return exceptionHandlingSupport.removeExceptionListener(o);
522
        }
523

    
524
        /**
525
         * Lanza una Excepci?n.
526
         *
527
         * @param t Excepci?n.
528
         */
529
        private void throwException(Throwable t) {
530
                exceptionHandlingSupport.throwException(t);
531
        }
532

    
533

    
534
    private class PaintingRequest
535
    {
536

    
537
        public PaintingRequest()
538
        {
539
        }
540

    
541
        public void paint()
542
        {
543
            try
544
            {
545
                    canceldraw.setCanceled(false);
546
                /* if (image == null)
547
                {
548
                    image = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(),
549
                            BufferedImage.TYPE_INT_ARGB);
550
                    Graphics gTemp = image.createGraphics();
551
                    Color theBackColor = vp.getBackColor();
552
                    if (theBackColor == null)
553
                        gTemp.setColor(Color.WHITE);
554
                    else
555
                        gTemp.setColor(theBackColor);
556

557
                    gTemp.fillRect(0,0,getWidth(), getHeight());
558
                    gTemp.dispose();
559
                    // g.drawImage(image,0,0,null);
560
                    System.out.println("Imagen con null en DESACTUALIZADO. Width = " + this.getWidth());
561
                } */
562
                Graphics2D g = image.createGraphics();
563

    
564
                ViewPort viewPort = mapContext.getViewPort();
565

    
566
                if (status == DESACTUALIZADO)
567
                {
568
                        Graphics2D gTemp = image.createGraphics();
569
                    Color theBackColor = viewPort.getBackColor();
570
                    if (theBackColor == null)
571
                        gTemp.setColor(Color.WHITE);
572
                    else
573
                        gTemp.setColor(theBackColor);
574
                    gTemp.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight());
575
                    status = ACTUALIZADO;
576
                    // ESTILO MAC
577
//                    BufferedImage imgMac = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(),
578
//                            BufferedImage.TYPE_INT_ARGB);
579
//
580
//                    mapContext.draw(imgMac, g, canceldraw, mapContext.getScaleView());
581
//                    g.drawImage(imgMac, 0, 0, null);
582
                    // FIN ESTILO MAC
583
                    // SIN MAC:
584
                    mapContext.draw(image, g, canceldraw, mapContext.getScaleView());
585

    
586
                }
587
                else if (status == ONLY_GRAPHICS)
588
                {
589
                    status = ACTUALIZADO;
590
                    mapContext.drawGraphics(image, g, canceldraw,mapContext.getScaleView());
591

    
592
                }
593

    
594

    
595
                // status = FAST_PAINT;
596
              //  drawerAlive = false;
597
                timer.stop();
598
                repaint();
599

    
600

    
601
            } catch (Throwable e) {
602
                timer.stop();
603
              //  isCancelled = true;
604
                e.printStackTrace();
605
                throwException(e);
606
            } finally {
607
            }
608

    
609
        }
610

    
611
    }
612

    
613
    /**
614
     * @author fjp
615
     *
616
     * Basasdo en el patr?n WorkerThread
617
     *
618
     */
619
    public class Drawer2
620
    {
621
        // Una mini cola de 2. No acumulamos peticiones de dibujado
622
        // dibujamos solo lo ?ltimo que nos han pedido.
623
        private PaintingRequest paintingRequest;
624
        private PaintingRequest waitingRequest;
625

    
626
        private boolean waiting;
627
        private boolean shutdown;
628

    
629
        public void setShutdown(boolean isShutdown)
630
        {
631
            shutdown = isShutdown;
632
        }
633

    
634
        public Drawer2()
635
        {
636
            paintingRequest = null;
637
            waitingRequest = null;
638
            waiting = false;
639
            shutdown = false;
640
            new Thread(new Worker()).start();
641
        }
642

    
643
        public void put(PaintingRequest newPaintRequest)
644
        {
645
            waitingRequest = newPaintRequest;
646
            if (waiting)
647
            {
648
                synchronized (this) {
649
                    notifyAll();
650
                }
651
            }
652
        }
653

    
654
        public PaintingRequest take()
655
        {
656
            if (waitingRequest == null)
657
            {
658
                synchronized (this) {
659
                    waiting = true;
660
                    try {
661
                        wait();
662
                    }
663
                    catch (InterruptedException ie)
664
                    {
665
                        waiting = false;
666
                    }
667
                }
668
            }
669
            paintingRequest = waitingRequest;
670
            waitingRequest = null;
671
            return paintingRequest;
672
        }
673

    
674

    
675
        private class Worker implements Runnable
676
        {
677
            public void run()
678
            {
679
                while (!shutdown)
680
                {
681
                    PaintingRequest p = take();
682
                    System.out.println("Pintando");
683
                    if (image != null){
684
                            cancelDrawing();
685
                        p.paint();
686
                    } else
687
                        status = DESACTUALIZADO;
688
                }
689
            }
690
        }
691
    }
692

    
693
        /**
694
         * Clase utilizada para dibujar las capas.
695
         *
696
         * @author Vicente Caballero Navarro
697
         */
698
        public class Drawer extends Thread {
699
                //private Graphics g;
700
                private BufferedImage image = null;
701
                private CancelDraw cancel;
702
                //private boolean threadCancel = false;
703

    
704
                /**
705
                 * Crea un nuevo Drawer.
706
                 *
707
                 */
708
                public Drawer(BufferedImage image, CancelDraw cancel)
709
        {
710
                        this.image = image;
711
                        this.cancel = cancel;
712
         //   drawerAlive = true;
713
                }
714

    
715
                /**
716
                 * @see java.lang.Runnable#run()
717
                 */
718
                public void run() {
719
                        try {
720
                                // synchronized (Drawer.class) {
721
                                    Graphics2D g = image.createGraphics();
722

    
723
                                    ViewPort viewPort = mapContext.getViewPort();
724
                    if (status == DESACTUALIZADO)
725
                    {
726
                                        Color theBackColor = viewPort.getBackColor();
727
                                        if (theBackColor == null)
728
                                            g.setColor(Color.WHITE);
729
                                        else
730
                                            g.setColor(theBackColor);
731
                                            g.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight());
732
                        status = ACTUALIZADO;
733
                        mapContext.draw(image, g, cancel,mapContext.getScaleView());
734
                    }
735
                    else if (status == ONLY_GRAPHICS)
736
                    {
737
                        status = ACTUALIZADO;
738
                        mapContext.drawGraphics(image, g, cancel,mapContext.getScaleView());
739
                    }
740

    
741
                                        timer.stop();
742
                    // status = FAST_PAINT;
743
                  //  drawerAlive = false;
744
                                        repaint();
745

    
746

    
747

    
748
                                // }
749
                        } catch (Throwable e) {
750
                            timer.stop();
751
                                //isCancelled = true;
752
                e.printStackTrace();
753
                                throwException(e);
754
                        } finally {
755
                        }
756
                }
757
        }
758

    
759
        /**
760
         * Clase para cancelar el dibujado.
761
         *
762
         * @author Fernando Gonz?lez Cort?s
763
         */
764
        public class CancelDraw implements Cancellable {
765
                private boolean cancel = false;
766

    
767
                /**
768
                 * Crea un nuevo CancelDraw.
769
                 */
770
                public CancelDraw() {
771
                }
772

    
773
                /**
774
                 * Insertar si se debe cancelar el dibujado.
775
                 *
776
                 * @param b true si se debe cancelar el dibujado.
777
                 */
778
                public void setCanceled(boolean b) {
779
                        cancel = b;
780
                }
781

    
782
                /**
783
                 * @see com.iver.utiles.swing.threads.Cancellable#isCanceled()
784
                 */
785
                public boolean isCanceled() {
786
                        return cancel;
787
                }
788
        }
789

    
790

    
791
        /**
792
         * Listener del MapTool.
793
         *
794
         * @author Fernando Gonz?lez Cort?s
795
         */
796
        public class MapToolListener implements MouseListener, MouseWheelListener,
797
                MouseMotionListener {
798

    
799
                long t1;
800
                Point2D pReal;
801
                /**
802
                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
803
                 */
804
                public void mouseClicked(MouseEvent e) {
805
                        try {
806
                                currentMapTool.mouseClicked(e);
807
                        } catch (BehaviorException t) {
808
                                throwException(t);
809
                        }
810
                }
811

    
812
                /**
813
                 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
814
                 */
815
                public void mouseEntered(MouseEvent e) {
816
                        try {
817
                                currentMapTool.mouseEntered(e);
818
                        } catch (BehaviorException t) {
819
                                throwException(t);
820
                        }
821
                }
822

    
823
                /**
824
                 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
825
                 */
826
                public void mouseExited(MouseEvent e) {
827
                        try {
828
                                currentMapTool.mouseExited(e);
829
                        } catch (BehaviorException t) {
830
                                throwException(t);
831
                        }
832
                }
833

    
834
                /**
835
                 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
836
                 */
837
                public void mousePressed(MouseEvent e) {
838
                        try {
839
                                currentMapTool.mousePressed(e);
840
                        } catch (BehaviorException t) {
841
                                throwException(t);
842
                        }
843
                }
844

    
845
                /**
846
                 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
847
                 */
848
                public void mouseReleased(MouseEvent e) {
849
                        try {
850
                                currentMapTool.mouseReleased(e);
851
                        } catch (BehaviorException t) {
852
                                throwException(t);
853
                        }
854
                }
855

    
856
                /**
857
                 * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
858
                 */
859
                public void mouseWheelMoved(MouseWheelEvent e) {
860
                        try {
861
                                currentMapTool.mouseWheelMoved(e);
862

    
863
                                // Si el tool actual no ha consumido el evento
864
                                // entendemos que quiere el comportamiento por defecto.
865
                                if (!e.isConsumed())
866
                                {
867
                                        // Para usar el primer punto sobre el que queremos centrar
868
                                        // el mapa, dejamos pasar un segundo para considerar el siguiente
869
                                        // punto como v?lido.
870
                                        if (t1 == 0)
871
                                        {
872
                                                t1= System.currentTimeMillis();
873
                                                pReal = vp.toMapPoint(e.getPoint());
874
                                        }
875
                                        else
876
                                        {
877
                                                long t2 = System.currentTimeMillis();
878
                                                if ((t2-t1) > 1000)
879
                                                        t1=0;
880
                                        }
881
                                        cancelDrawing();
882
                                        ViewPort vp = getViewPort();
883

    
884

    
885
                                        /* Point2D pReal = new Point2D.Double(vp.getAdjustedExtent().getCenterX(),
886
                                                        vp.getAdjustedExtent().getCenterY()); */
887
                                        int amount = e.getWheelRotation();
888
                                        double nuevoX;
889
                                        double nuevoY;
890
                                        double factor;
891

    
892
                                        if (amount < 0) // nos acercamos
893
                                        {
894
                                                factor = 0.9;
895
                                        } else // nos alejamos
896
                                        {
897
                                                factor = 1.2;
898
                                        }
899
                                        Rectangle2D.Double r = new Rectangle2D.Double();
900
                                        if (vp.getExtent() != null) {
901
                                                nuevoX = pReal.getX()
902
                                                                - ((vp.getExtent().getWidth() * factor) / 2.0);
903
                                                nuevoY = pReal.getY()
904
                                                                - ((vp.getExtent().getHeight() * factor) / 2.0);
905
                                                r.x = nuevoX;
906
                                                r.y = nuevoY;
907
                                                r.width = vp.getExtent().getWidth() * factor;
908
                                                r.height = vp.getExtent().getHeight() * factor;
909

    
910
                                                vp.setExtent(r);
911
                                        }
912

    
913

    
914
                                }
915
                        } catch (BehaviorException t) {
916
                                throwException(t);
917
                        }
918
                }
919

    
920
                /**
921
                 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
922
                 */
923
                public void mouseDragged(MouseEvent e) {
924
                        try {
925
                                currentMapTool.mouseDragged(e);
926
                        } catch (BehaviorException t) {
927
                                throwException(t);
928
                        }
929
                }
930

    
931
                /**
932
                 * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
933
                 */
934
                public void mouseMoved(MouseEvent e) {
935
                        try {
936
                                currentMapTool.mouseMoved(e);
937
                        } catch (BehaviorException t) {
938
                                throwException(t);
939
                        }
940
                }
941
        }
942

    
943
        /**
944
         * Listener sobre el MapContext.
945
         *
946
         * @author Fernando Gonz?lez Cort?s
947
         */
948
        public class MapContextListener implements AtomicEventListener {
949
                /**
950
                 * @see com.iver.cit.gvsig.fmap.AtomicEventListener#atomicEvent(com.iver.cit.gvsig.fmap.AtomicEvent)
951
                 */
952
                public void atomicEvent(AtomicEvent e) {
953
                        boolean redraw = false;
954
                        LayerEvent[] layerEvents = e.getLayerEvents();
955

    
956
                        for (int i = 0; i < layerEvents.length; i++) {
957
                                if (layerEvents[i].getProperty().equals("visible")) {
958
                                        redraw = true;
959
                                }
960
                        }
961

    
962
                        if (e.getColorEvents().length > 0) {
963
                                redraw = true;
964
                        }
965

    
966
                        if (e.getExtentEvents().length > 0) {
967
                                redraw = true;
968
                        }
969

    
970
                        if (e.getProjectionEvents().length > 0) {
971
                                //redraw = true;
972
                        }
973

    
974

    
975
                        if (e.getLayerCollectionEvents().length > 0) {
976
                                redraw = true;
977
                        }
978

    
979
                        if (e.getLegendEvents().length > 0) {
980
                                redraw = true;
981
                        }
982

    
983
                        if (e.getSelectionEvents().length > 0) {
984
                                redraw = true;
985
                        }
986

    
987
                        if (redraw) {
988
                System.out.println("MapContextListener redraw");
989
                                MapControl.this.drawMap(false);
990
                        }
991
                }
992
        }
993
        public ViewPort getViewPort() {
994
                return vp;
995
        }
996

    
997
        /**
998
         * Returns a HashMap with the tools that have been registered
999
         * in the Mapcontrol.
1000
         */
1001
        public HashMap getNamesMapTools() {
1002
                return namesMapTools;
1003
        }
1004

    
1005
        public void commandRepaint() {
1006
                drawMap(false);
1007
        }
1008

    
1009
        public void commandRefresh() {
1010
                // TODO Auto-generated method stub
1011
        }
1012

    
1013
        public void setPrevTool() {
1014
                setTool(prevTool);
1015
        }
1016

    
1017
                public void zoomIn() {
1018
                Behavior mapTool = (Behavior) namesMapTools.get("zoomIn");
1019
                ViewPort vp=getViewPort();
1020
                Rectangle2D r=getViewPort().getAdjustedExtent();
1021
                Point2D pCenter=vp.fromMapPoint(r.getCenterX(),r.getCenterY());
1022
                MouseEvent e=new MouseEvent((Component)this,MouseEvent.MOUSE_RELEASED,MouseEvent.ACTION_EVENT_MASK,MouseEvent.BUTTON1,(int)pCenter.getX(),(int)pCenter.getY(),1,true,MouseEvent.BUTTON1);
1023
                try {
1024
                        mapTool.mousePressed(e);
1025
                        mapTool.mouseReleased(e);
1026
                } catch (BehaviorException t) {
1027
                        throwException(t);
1028
                }
1029
        }
1030
        public void zoomOut() {
1031
                Behavior mapTool = (Behavior) namesMapTools.get("zoomOut");
1032
                ViewPort vp=getViewPort();
1033
                Rectangle2D r=getViewPort().getAdjustedExtent();
1034
                Point2D pCenter=vp.fromMapPoint(r.getCenterX(),r.getCenterY());
1035
                MouseEvent e=new MouseEvent((Component)this,MouseEvent.MOUSE_RELEASED,MouseEvent.ACTION_EVENT_MASK,MouseEvent.BUTTON1,(int)pCenter.getX(),(int)pCenter.getY(),1,true,MouseEvent.BUTTON1);
1036
                try {
1037
                        mapTool.mousePressed(e);
1038
                        mapTool.mouseReleased(e);
1039
                } catch (BehaviorException t) {
1040
                        throwException(t);
1041
                }
1042
        }
1043

    
1044

    
1045
}