Statistics
| Revision:

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

History | View | Annotate | Download (24.9 KB)

1 1223 fernando
/* 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 1282 fjp
import java.awt.Color;
44 1223 fernando
import java.awt.Dimension;
45
import java.awt.Graphics;
46
import java.awt.Graphics2D;
47 6212 fjp
import java.awt.GraphicsEnvironment;
48 1223 fernando
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50
import java.awt.event.ComponentEvent;
51
import java.awt.event.ComponentListener;
52
import java.awt.event.MouseEvent;
53
import java.awt.event.MouseListener;
54
import java.awt.event.MouseMotionListener;
55
import java.awt.event.MouseWheelEvent;
56
import java.awt.event.MouseWheelListener;
57 5874 fjp
import java.awt.geom.Point2D;
58
import java.awt.geom.Rectangle2D;
59 1223 fernando
import java.awt.image.BufferedImage;
60
import java.util.HashMap;
61
62
import javax.swing.JComponent;
63
import javax.swing.Timer;
64
65
import org.cresques.cts.IProjection;
66
67 6164 luisw2
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
68 4120 caballero
import com.iver.cit.gvsig.fmap.edition.commands.CommandListener;
69 1223 fernando
import com.iver.cit.gvsig.fmap.layers.LayerEvent;
70
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
71
import com.iver.cit.gvsig.fmap.tools.CompoundBehavior;
72
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
73
import com.iver.utiles.exceptionHandling.ExceptionHandlingSupport;
74
import com.iver.utiles.exceptionHandling.ExceptionListener;
75 5317 fjp
import com.iver.utiles.swing.threads.Cancellable;
76 1223 fernando
77
78
/**
79
 * MapControl.
80
 *
81
 * @author Fernando Gonz?lez Cort?s
82
 */
83 5874 fjp
public class MapControl extends JComponent implements ComponentListener,
84
                        CommandListener {
85 1223 fernando
        /** Cuando la vista est? actualizada. */
86
        public static final int ACTUALIZADO = 0;
87
88
        /** Cuando la vista est? desactualizada. */
89
        public static final int DESACTUALIZADO = 1;
90 2901 fjp
    public static final int ONLY_GRAPHICS = 2;
91 3346 fjp
    // public static final int FAST_PAINT = 3;
92 2531 caballero
        //private static Logger logger = Logger.getLogger(MapControl.class.getName());
93 1223 fernando
        private FMap mapContext = null;
94 3719 caballero
    //private boolean drawerAlive = false;
95 1223 fernando
        private HashMap namesMapTools = new HashMap();
96
        private Behavior currentMapTool = null;
97
        private int status = DESACTUALIZADO;
98
        private BufferedImage image = null;
99
        private String currentTool;
100
        private CancelDraw canceldraw;
101 3719 caballero
        //private boolean isCancelled = true;
102 1223 fernando
        private Timer timer;
103
        protected ViewPort vp;
104 3719 caballero
        //private Drawer drawer;
105 3368 fjp
    private Drawer2 drawer2;
106 3372 fjp
    // private boolean firstDraw = true;
107 1223 fernando
        private MapToolListener mapToolListener = new MapToolListener();
108
        private MapContextListener mapContextListener = new MapContextListener();
109
        private ExceptionHandlingSupport exceptionHandlingSupport = new ExceptionHandlingSupport();
110 4619 caballero
111 4888 caballero
        private String prevTool;
112
113 3719 caballero
        /**
114 3331 fjp
     * We need this to avoid not wanted refresh. REMEMBER TO SET TO TRUE!!
115
     */
116
    // private boolean paintEnabled = false;
117 1223 fernando
118
        /**
119
         * Crea un nuevo NewMapControl.
120
         */
121
        public MapControl() {
122 5787 jaume
                this.setName("MapControl");
123 1223 fernando
                setDoubleBuffered(false);
124
                setOpaque(true);
125
                status = DESACTUALIZADO;
126
127
                //Clase usada para cancelar el dibujado
128
                canceldraw = new CancelDraw();
129
130
                //Modelo de datos y ventana del mismo
131
                // TODO: Cuando creamos un mapControl, deber?amos asignar
132
                // la projecci?n por defecto con la que vayamos a trabajar.
133
                // 23030 es el c?digo EPSG del UTM30 elipsoide ED50
134 6164 luisw2
                vp = new ViewPort(CRSFactory.getCRS("EPSG:23030"));
135 1223 fernando
                setMapContext(new FMap(vp));
136 3719 caballero
137 1223 fernando
                //eventos
138
                this.addComponentListener(this);
139
                this.addMouseListener(mapToolListener);
140
                this.addMouseMotionListener(mapToolListener);
141
                this.addMouseWheelListener(mapToolListener);
142
143 3368 fjp
        this.drawer2 = new Drawer2();
144 1223 fernando
                //Timer para mostrar el redibujado mientras se dibuja
145
                timer = new Timer(300,
146
                                new ActionListener() {
147
                                        public void actionPerformed(ActionEvent e) {
148
                                                MapControl.this.repaint();
149
                                        }
150
                                });
151
        }
152
153
        /**
154
         * Inserta el modelo.
155
         *
156
         * @param model FMap.
157
         */
158
        public void setMapContext(FMap model) {
159
                if (mapContext != null) {
160
                        mapContext.removeAtomicEventListener(mapContextListener);
161
                }
162
163
                mapContext = model;
164
165
                if (mapContext.getViewPort() == null) {
166
                        mapContext.setViewPort(vp);
167
                } else {
168
                        vp = mapContext.getViewPort();
169
170
                        // vp.setImageSize(new Dimension(getWidth(), getHeight()));
171
                        //System.err.println("Viewport en setMapContext:" + vp);
172
                }
173
174
                mapContext.addAtomicEventListener(mapContextListener);
175
176
                status = DESACTUALIZADO;
177
        }
178
179
        /**
180
         * Devuelve la proyecci?n.
181
         *
182
         * @return Proyecci?n.
183
         */
184
        public IProjection getProjection() {
185
                return getMapContext().getProjection();
186
        }
187
188
        /**
189
         * Inserta una proyecci?n.
190
         *
191
         * @param proj Proyecci?n.
192
         */
193
        public void setProjection(IProjection proj) {
194
                getMapContext().setProjection(proj);
195
        }
196
197
        /**
198
         * Devuelve el modelo.
199
         *
200
         * @return FMap.
201
         */
202
        public FMap getMapContext() {
203
                return mapContext;
204
        }
205
206
        /**
207
         * Registra una herramienta (tool).
208
         *
209
         * @param name Nombre de la herramienta.
210
         * @param tool Herramienta.
211
         */
212
        public void addMapTool(String name, Behavior tool) {
213
                namesMapTools.put(name, tool);
214
                tool.setMapControl(this);
215
        }
216
217
        public void addMapTool(String name, Behavior[] tools){
218
                CompoundBehavior tool = new CompoundBehavior(tools);
219
                addMapTool(name, tool);
220
        }
221 3719 caballero
222 1223 fernando
        /**
223
         * DOCUMENT ME!
224
         *
225
         * @param toolName DOCUMENT ME!
226
         */
227
        public void setTool(String toolName) {
228 4888 caballero
                prevTool=getTool();
229 1223 fernando
                Behavior mapTool = (Behavior) namesMapTools.get(toolName);
230
                currentMapTool = mapTool;
231
                currentTool = toolName;
232
                this.setCursor(mapTool.getCursor());
233
        }
234 3719 caballero
        public Behavior getCurrentMapTool(){
235
                return currentMapTool;
236
        }
237 1223 fernando
        /**
238
         * Devuelve el nombre de la herramienta seleccionada.
239
         *
240
         * @return nombre.
241
         */
242
        public String getTool() {
243
                return currentTool;
244
        }
245
246
        /**
247
         * Cancela el dibujado. Se espera a que la cancelaci?n surta efecto
248
         */
249
        public void cancelDrawing() {
250 3368 fjp
                /* if (drawer != null) {
251 1223 fernando
                        if (!drawer.isAlive()) {
252
                                return;
253
                        }
254
                }
255 3368 fjp
                */
256 4832 fjp
                canceldraw.setCanceled(true);
257 1223 fernando
258 3368 fjp
                /* while (!isCancelled) {
259 1243 fjp
                        if (!drawer.isAlive()) {
260 1327 fjp
                            // Si hemos llegado aqu? con un thread vivo, seguramente
261
                            // no estamos actualizados.
262

263
                                break;
264 1243 fjp
                        }
265 3719 caballero

266 1223 fernando
                }
267
                canceldraw.setCancel(false);
268
                isCancelled = false;
269 3368 fjp
        drawerAlive = false; */
270 1223 fernando
        }
271 3719 caballero
272 3375 fjp
    private boolean adaptToImageSize()
273
    {
274
        if ((image == null) || (vp.getImageWidth() != this.getWidth()) || (vp.getImageHeight() != this.getHeight()))
275
        {
276
            image = new BufferedImage(this.getWidth(), this.getHeight(),
277
                    BufferedImage.TYPE_INT_ARGB);
278 6212 fjp
            // ESTILO MAC
279
//                image = GraphicsEnvironment.getLocalGraphicsEnvironment()
280
//                                .getDefaultScreenDevice().getDefaultConfiguration()
281
//                                .createCompatibleImage(this.getWidth(), this.getHeight());
282 3375 fjp
            vp.setImageSize(new Dimension(getWidth(), getHeight()));
283
            getMapContext().getViewPort().setScale();
284 3719 caballero
285
286 3375 fjp
            Graphics gTemp = image.createGraphics();
287
            Color theBackColor = vp.getBackColor();
288
            if (theBackColor == null)
289
                gTemp.setColor(Color.WHITE);
290
            else
291
                gTemp.setColor(theBackColor);
292 3719 caballero
293 3375 fjp
            gTemp.fillRect(0,0,getWidth(), getHeight());
294
            gTemp.dispose();
295
            status = DESACTUALIZADO;
296
            // g.drawImage(image,0,0,null);
297
            return true;
298
        }
299
        return false;
300
    }
301 1223 fernando
302
        /**
303
         * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
304
         */
305
        protected void paintComponent(Graphics g) {
306 3375 fjp
        adaptToImageSize();
307 3346 fjp
        /* if (status == FAST_PAINT) {
308 3331 fjp
            System.out.println("FAST_PAINT");
309
            g.drawImage(image,0,0,null);
310
            status = ACTUALIZADO;
311
            return;
312 3346 fjp
        } */
313 4568 fjp
        // System.out.println("PINTANDO MAPCONTROL" + this);
314 1223 fernando
                if (status == ACTUALIZADO) {
315 1680 luisw
                        // LWS logger.debug("Dibujando la imagen obtenida");
316 1223 fernando
317
                        /*
318
                         * Si hay un behaviour y la imagen es distinta de null se delega el dibujado
319
                         * en dicho behaviour
320
                         */
321 3372 fjp
            if (image != null)
322
            {
323
                if (currentMapTool != null)
324
                    currentMapTool.paintComponent(g);
325
                else
326
                    g.drawImage(image,0,0,null);
327 3719 caballero
328 3372 fjp
                                // System.out.println("Pinto ACTUALIZADO");
329 1223 fernando
                        }
330 3719 caballero
                } else if ((status == DESACTUALIZADO)
331 2946 fjp
                || (status == ONLY_GRAPHICS)) {
332 1680 luisw
                        // LWS System.out.println("DESACTUALIZADO: Obteniendo la imagen de la cartograf?a");
333 1223 fernando
                        /* if (isOpaque())
334
                        {
335
                            if (image==null)
336
                            {
337
                                g.setColor(vp.getBackColor());
338 3719 caballero
                                g.fillRect(0,0,getWidth(), getHeight());
339
                            }
340 1223 fernando
                            // else g.drawImage(image,0,0,null);
341
                        } */
342 3368 fjp
            // cancelDrawing();
343 1223 fernando
                        //Se crea la imagen con el color de fonde deseado
344 3375 fjp
                        /* if (image == null)
345 1223 fernando
                        {
346
                                image = new BufferedImage(this.getWidth(), this.getHeight(),
347
                                                BufferedImage.TYPE_INT_ARGB);
348
                                vp.setImageSize(new Dimension(getWidth(), getHeight()));
349
                                Graphics gTemp = image.createGraphics();
350 1303 fjp
                            Color theBackColor = vp.getBackColor();
351
                            if (theBackColor == null)
352
                                gTemp.setColor(Color.WHITE);
353
                            else
354
                                gTemp.setColor(theBackColor);
355 3719 caballero

356 1223 fernando
                                gTemp.fillRect(0,0,getWidth(), getHeight());
357
                                gTemp.dispose();
358
                                // g.drawImage(image,0,0,null);
359 3174 ldiaz
                                System.out.println("Imagen con null en DESACTUALIZADO. Width = " + this.getWidth());
360 3375 fjp
                        } */
361 3331 fjp
            // else
362
            // {
363 3719 caballero
364
365 3372 fjp
            // if (image != null)
366
            //  {
367 3331 fjp
                g.drawImage(image,0,0,null);
368 3719 caballero
369 3372 fjp
                drawer2.put(new PaintingRequest());
370
                timer.start();
371
            /* }
372
            else
373
                return; */
374 3331 fjp
            // }
375 3719 caballero
376 3368 fjp
            /* if (drawerAlive == false)
377 3331 fjp
            {
378
                drawer = new Drawer(image, canceldraw);
379
                drawer.start();
380 3719 caballero
                        //Se lanza el tread de dibujado
381 3368 fjp
            } */
382 3719 caballero
383 2946 fjp
                        // status = ACTUALIZADO;
384 1223 fernando
                }
385
        }
386
387
        /**
388
         * Devuelve la imagen de la vista.
389
         *
390
         * @return imagen.
391
         */
392
        public BufferedImage getImage() {
393
                return image;
394
        }
395
396
        /**
397
         * Marca el mapa para que en el pr?ximo redibujado se acceda a la
398
         * cartograf?a para reobtener la imagen
399 3422 fjp
         * @param doClear Solo deber?a ser true cuando se llama desde pan
400 1223 fernando
         */
401
        public void drawMap(boolean doClear) {
402 3979 caballero
                cancelDrawing();
403
                System.out.println("drawMap con doClear=" + doClear);
404 3346 fjp
        status = DESACTUALIZADO;
405 5923 fjp
        getMapContext().getLayers().setDirty(true);
406 3331 fjp
                if (doClear)
407
        {
408 3372 fjp
            // image = null; // Se usa para el PAN
409
            if (image != null)
410
            {
411
                Graphics2D g = image.createGraphics();
412
                Color theBackColor = vp.getBackColor();
413
                if (theBackColor == null)
414
                    g.setColor(Color.WHITE);
415
                else
416
                    g.setColor(theBackColor);
417
                g.fillRect(0, 0, vp.getImageWidth(), vp.getImageHeight());
418
                g.dispose();
419 3719 caballero
            }
420 3331 fjp
        }
421 1223 fernando
                repaint();
422
        }
423 5923 fjp
424
        public void rePaintDirtyLayers()
425
        {
426
                cancelDrawing();
427
        status = DESACTUALIZADO;
428
        repaint();
429
        }
430
431 2901 fjp
    public void drawGraphics() {
432
        status = ONLY_GRAPHICS;
433
        repaint();
434
    }
435 1223 fernando
436
        /**
437
         * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
438
         */
439
        public void componentHidden(ComponentEvent e) {
440
        }
441
442
        /**
443
         * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
444
         */
445
        public void componentMoved(ComponentEvent e) {
446
        }
447
448
        /**
449
         * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
450
         */
451
        public void componentResized(ComponentEvent e) {
452 3375 fjp
                /* image = new BufferedImage(this.getWidth(), this.getHeight(),
453 3719 caballero
                                BufferedImage.TYPE_INT_ARGB);
454 1223 fernando
                Graphics gTemp = image.createGraphics();
455
                gTemp.setColor(vp.getBackColor());
456 3372 fjp
                gTemp.fillRect(0,0,getWidth(), getHeight());
457
        System.out.println("MapControl resized");
458
            // image = null;
459 1223 fernando
            vp.setImageSize(new Dimension(getWidth(), getHeight()));
460 3375 fjp
                getMapContext().getViewPort().setScale(); */
461 3372 fjp
                // drawMap(true);
462 1223 fernando
        }
463
464
        /**
465
         * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
466
         */
467
        public void componentShown(ComponentEvent e) {
468
        }
469
470
        /**
471
         * A?ade un listener de tipo ExceptionListener.
472
         *
473
         * @param o ExceptionListener.
474
         */
475
        public void addExceptionListener(ExceptionListener o) {
476
                exceptionHandlingSupport.addExceptionListener(o);
477
        }
478
479
        /**
480
         * Borra la ExceptioListener que se pasa como par?metro.
481
         *
482
         * @param o ExceptionListener.
483
         *
484
         * @return True si se borra correctamente.
485
         */
486
        public boolean removeExceptionListener(ExceptionListener o) {
487
                return exceptionHandlingSupport.removeExceptionListener(o);
488
        }
489
490
        /**
491
         * Lanza una Excepci?n.
492
         *
493
         * @param t Excepci?n.
494
         */
495
        private void throwException(Throwable t) {
496
                exceptionHandlingSupport.throwException(t);
497
        }
498
499 3719 caballero
500 3368 fjp
    private class PaintingRequest
501
    {
502 3719 caballero
503 3372 fjp
        public PaintingRequest()
504 3368 fjp
        {
505
        }
506 3719 caballero
507 3368 fjp
        public void paint()
508
        {
509 3719 caballero
            try
510
            {
511 4832 fjp
                    canceldraw.setCanceled(false);
512 3372 fjp
                /* if (image == null)
513
                {
514
                    image = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(),
515
                            BufferedImage.TYPE_INT_ARGB);
516
                    Graphics gTemp = image.createGraphics();
517
                    Color theBackColor = vp.getBackColor();
518
                    if (theBackColor == null)
519
                        gTemp.setColor(Color.WHITE);
520
                    else
521
                        gTemp.setColor(theBackColor);
522 3719 caballero

523 3372 fjp
                    gTemp.fillRect(0,0,getWidth(), getHeight());
524
                    gTemp.dispose();
525
                    // g.drawImage(image,0,0,null);
526
                    System.out.println("Imagen con null en DESACTUALIZADO. Width = " + this.getWidth());
527
                } */
528 3368 fjp
                Graphics2D g = image.createGraphics();
529 3719 caballero
530 3368 fjp
                ViewPort viewPort = mapContext.getViewPort();
531 3719 caballero
532 3368 fjp
                if (status == DESACTUALIZADO)
533
                {
534 5874 fjp
                        Graphics2D gTemp = image.createGraphics();
535 3368 fjp
                    Color theBackColor = viewPort.getBackColor();
536
                    if (theBackColor == null)
537 5874 fjp
                        gTemp.setColor(Color.WHITE);
538 3368 fjp
                    else
539 5874 fjp
                        gTemp.setColor(theBackColor);
540
                    gTemp.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight());
541 3368 fjp
                    status = ACTUALIZADO;
542 3372 fjp
                    mapContext.draw(image, g, canceldraw, mapContext.getScaleView());
543 3719 caballero
544 3368 fjp
                }
545
                else if (status == ONLY_GRAPHICS)
546
                {
547 3719 caballero
                    status = ACTUALIZADO;
548 3372 fjp
                    mapContext.drawGraphics(image, g, canceldraw,mapContext.getScaleView());
549 3719 caballero
550 3368 fjp
                }
551 3719 caballero
552
553 3368 fjp
                // status = FAST_PAINT;
554 3719 caballero
              //  drawerAlive = false;
555 3372 fjp
                timer.stop();
556 3368 fjp
                repaint();
557 3719 caballero
558
559 3368 fjp
            } catch (Throwable e) {
560
                timer.stop();
561 3719 caballero
              //  isCancelled = true;
562 3368 fjp
                e.printStackTrace();
563
                throwException(e);
564
            } finally {
565
            }
566 3719 caballero
567 3368 fjp
        }
568 3719 caballero
569 3368 fjp
    }
570 3719 caballero
571 3368 fjp
    /**
572
     * @author fjp
573 3719 caballero
     *
574 3368 fjp
     * Basasdo en el patr?n WorkerThread
575
     *
576
     */
577
    public class Drawer2
578
    {
579
        // Una mini cola de 2. No acumulamos peticiones de dibujado
580
        // dibujamos solo lo ?ltimo que nos han pedido.
581
        private PaintingRequest paintingRequest;
582
        private PaintingRequest waitingRequest;
583 3719 caballero
584 3368 fjp
        private boolean waiting;
585
        private boolean shutdown;
586 3719 caballero
587 3368 fjp
        public void setShutdown(boolean isShutdown)
588
        {
589
            shutdown = isShutdown;
590
        }
591 3719 caballero
592
        public Drawer2()
593 3368 fjp
        {
594
            paintingRequest = null;
595
            waitingRequest = null;
596
            waiting = false;
597
            shutdown = false;
598
            new Thread(new Worker()).start();
599
        }
600 3719 caballero
601 3368 fjp
        public void put(PaintingRequest newPaintRequest)
602
        {
603
            waitingRequest = newPaintRequest;
604
            if (waiting)
605
            {
606
                synchronized (this) {
607
                    notifyAll();
608
                }
609
            }
610
        }
611 3719 caballero
612 3368 fjp
        public PaintingRequest take()
613
        {
614
            if (waitingRequest == null)
615
            {
616
                synchronized (this) {
617
                    waiting = true;
618
                    try {
619
                        wait();
620
                    }
621
                    catch (InterruptedException ie)
622
                    {
623
                        waiting = false;
624
                    }
625
                }
626
            }
627
            paintingRequest = waitingRequest;
628
            waitingRequest = null;
629
            return paintingRequest;
630
        }
631 3719 caballero
632
633 3368 fjp
        private class Worker implements Runnable
634
        {
635
            public void run()
636
            {
637
                while (!shutdown)
638
                {
639
                    PaintingRequest p = take();
640
                    System.out.println("Pintando");
641 4120 caballero
                    if (image != null){
642
                            cancelDrawing();
643 3372 fjp
                        p.paint();
644 4120 caballero
                    } else
645 3372 fjp
                        status = DESACTUALIZADO;
646 3368 fjp
                }
647
            }
648
        }
649
    }
650 3719 caballero
651 1223 fernando
        /**
652
         * Clase utilizada para dibujar las capas.
653
         *
654
         * @author Vicente Caballero Navarro
655
         */
656
        public class Drawer extends Thread {
657 2531 caballero
                //private Graphics g;
658 3331 fjp
                private BufferedImage image = null;
659 1223 fernando
                private CancelDraw cancel;
660 2531 caballero
                //private boolean threadCancel = false;
661 1223 fernando
662
                /**
663
                 * Crea un nuevo Drawer.
664
                 *
665
                 */
666 3331 fjp
                public Drawer(BufferedImage image, CancelDraw cancel)
667
        {
668 1223 fernando
                        this.image = image;
669
                        this.cancel = cancel;
670 3719 caballero
         //   drawerAlive = true;
671 1223 fernando
                }
672
673
                /**
674
                 * @see java.lang.Runnable#run()
675
                 */
676
                public void run() {
677
                        try {
678 2857 jaume
                                // synchronized (Drawer.class) {
679 1282 fjp
                                    Graphics2D g = image.createGraphics();
680 3719 caballero
681 1282 fjp
                                    ViewPort viewPort = mapContext.getViewPort();
682 2946 fjp
                    if (status == DESACTUALIZADO)
683
                    {
684
                                        Color theBackColor = viewPort.getBackColor();
685
                                        if (theBackColor == null)
686
                                            g.setColor(Color.WHITE);
687
                                        else
688
                                            g.setColor(theBackColor);
689
                                            g.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight());
690
                        status = ACTUALIZADO;
691
                        mapContext.draw(image, g, cancel,mapContext.getScaleView());
692
                    }
693
                    else if (status == ONLY_GRAPHICS)
694
                    {
695 2951 fjp
                        status = ACTUALIZADO;
696 2946 fjp
                        mapContext.drawGraphics(image, g, cancel,mapContext.getScaleView());
697
                    }
698 3719 caballero
699 1327 fjp
                                        timer.stop();
700 3346 fjp
                    // status = FAST_PAINT;
701 3719 caballero
                  //  drawerAlive = false;
702 1223 fernando
                                        repaint();
703 3719 caballero
704
705
706 2857 jaume
                                // }
707 1223 fernando
                        } catch (Throwable e) {
708 1266 fernando
                            timer.stop();
709 3719 caballero
                                //isCancelled = true;
710 2943 fjp
                e.printStackTrace();
711 1223 fernando
                                throwException(e);
712
                        } finally {
713
                        }
714
                }
715
        }
716
717
        /**
718
         * Clase para cancelar el dibujado.
719
         *
720
         * @author Fernando Gonz?lez Cort?s
721
         */
722
        public class CancelDraw implements Cancellable {
723
                private boolean cancel = false;
724
725
                /**
726
                 * Crea un nuevo CancelDraw.
727
                 */
728
                public CancelDraw() {
729
                }
730
731
                /**
732
                 * Insertar si se debe cancelar el dibujado.
733
                 *
734
                 * @param b true si se debe cancelar el dibujado.
735
                 */
736 4832 fjp
                public void setCanceled(boolean b) {
737 1223 fernando
                        cancel = b;
738
                }
739
740
                /**
741 5317 fjp
                 * @see com.iver.utiles.swing.threads.Cancellable#isCanceled()
742 1223 fernando
                 */
743
                public boolean isCanceled() {
744
                        return cancel;
745
                }
746
        }
747 5874 fjp
748 1223 fernando
749
        /**
750
         * Listener del MapTool.
751
         *
752
         * @author Fernando Gonz?lez Cort?s
753
         */
754
        public class MapToolListener implements MouseListener, MouseWheelListener,
755
                MouseMotionListener {
756 5881 fjp
757
                long t1;
758
                Point2D pReal;
759 1223 fernando
                /**
760
                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
761
                 */
762
                public void mouseClicked(MouseEvent e) {
763
                        try {
764
                                currentMapTool.mouseClicked(e);
765
                        } catch (BehaviorException t) {
766
                                throwException(t);
767
                        }
768
                }
769
770
                /**
771
                 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
772
                 */
773
                public void mouseEntered(MouseEvent e) {
774
                        try {
775
                                currentMapTool.mouseEntered(e);
776
                        } catch (BehaviorException t) {
777
                                throwException(t);
778
                        }
779
                }
780
781
                /**
782
                 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
783
                 */
784
                public void mouseExited(MouseEvent e) {
785
                        try {
786
                                currentMapTool.mouseExited(e);
787
                        } catch (BehaviorException t) {
788
                                throwException(t);
789
                        }
790
                }
791
792
                /**
793
                 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
794
                 */
795
                public void mousePressed(MouseEvent e) {
796
                        try {
797
                                currentMapTool.mousePressed(e);
798
                        } catch (BehaviorException t) {
799
                                throwException(t);
800
                        }
801
                }
802
803
                /**
804
                 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
805
                 */
806
                public void mouseReleased(MouseEvent e) {
807
                        try {
808
                                currentMapTool.mouseReleased(e);
809
                        } catch (BehaviorException t) {
810
                                throwException(t);
811
                        }
812
                }
813
814
                /**
815
                 * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
816
                 */
817
                public void mouseWheelMoved(MouseWheelEvent e) {
818
                        try {
819
                                currentMapTool.mouseWheelMoved(e);
820 5874 fjp
821
                                // Si el tool actual no ha consumido el evento
822
                                // entendemos que quiere el comportamiento por defecto.
823
                                if (!e.isConsumed())
824
                                {
825 5881 fjp
                                        // Para usar el primer punto sobre el que queremos centrar
826
                                        // el mapa, dejamos pasar un segundo para considerar el siguiente
827
                                        // punto como v?lido.
828
                                        if (t1 == 0)
829
                                        {
830
                                                t1= System.currentTimeMillis();
831
                                                pReal = vp.toMapPoint(e.getPoint());
832
                                        }
833
                                        else
834
                                        {
835
                                                long t2 = System.currentTimeMillis();
836
                                                if ((t2-t1) > 1000)
837
                                                        t1=0;
838
                                        }
839 5874 fjp
                                        cancelDrawing();
840
                                        ViewPort vp = getViewPort();
841 5881 fjp
842 5874 fjp
843
                                        /* Point2D pReal = new Point2D.Double(vp.getAdjustedExtent().getCenterX(),
844
                                                        vp.getAdjustedExtent().getCenterY()); */
845
                                        int amount = e.getWheelRotation();
846
                                        double nuevoX;
847
                                        double nuevoY;
848
                                        double factor;
849
850
                                        if (amount > 0) // nos acercamos
851
                                        {
852
                                                factor = 0.9;
853
                                        } else // nos alejamos
854
                                        {
855
                                                factor = 1.2;
856
                                        }
857
                                        Rectangle2D.Double r = new Rectangle2D.Double();
858
                                        if (vp.getExtent() != null) {
859
                                                nuevoX = pReal.getX()
860
                                                                - ((vp.getExtent().getWidth() * factor) / 2.0);
861
                                                nuevoY = pReal.getY()
862
                                                                - ((vp.getExtent().getHeight() * factor) / 2.0);
863
                                                r.x = nuevoX;
864
                                                r.y = nuevoY;
865
                                                r.width = vp.getExtent().getWidth() * factor;
866
                                                r.height = vp.getExtent().getHeight() * factor;
867
868
                                                vp.setExtent(r);
869
                                        }
870
871
872
                                }
873 1223 fernando
                        } catch (BehaviorException t) {
874
                                throwException(t);
875
                        }
876
                }
877
878
                /**
879
                 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
880
                 */
881
                public void mouseDragged(MouseEvent e) {
882
                        try {
883
                                currentMapTool.mouseDragged(e);
884
                        } catch (BehaviorException t) {
885
                                throwException(t);
886
                        }
887
                }
888
889
                /**
890
                 * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
891
                 */
892
                public void mouseMoved(MouseEvent e) {
893
                        try {
894
                                currentMapTool.mouseMoved(e);
895
                        } catch (BehaviorException t) {
896
                                throwException(t);
897
                        }
898
                }
899
        }
900
901
        /**
902
         * Listener sobre el MapContext.
903
         *
904
         * @author Fernando Gonz?lez Cort?s
905
         */
906
        public class MapContextListener implements AtomicEventListener {
907
                /**
908
                 * @see com.iver.cit.gvsig.fmap.AtomicEventListener#atomicEvent(com.iver.cit.gvsig.fmap.AtomicEvent)
909
                 */
910
                public void atomicEvent(AtomicEvent e) {
911
                        boolean redraw = false;
912
                        LayerEvent[] layerEvents = e.getLayerEvents();
913
914
                        for (int i = 0; i < layerEvents.length; i++) {
915
                                if (layerEvents[i].getProperty().equals("visible")) {
916
                                        redraw = true;
917
                                }
918
                        }
919
920
                        if (e.getColorEvents().length > 0) {
921
                                redraw = true;
922
                        }
923
924
                        if (e.getExtentEvents().length > 0) {
925
                                redraw = true;
926
                        }
927 5941 jmvivo
928
                        if (e.getProjectionEvents().length > 0) {
929
                                //redraw = true;
930
                        }
931 1223 fernando
932 5941 jmvivo
933 1223 fernando
                        if (e.getLayerCollectionEvents().length > 0) {
934
                                redraw = true;
935
                        }
936
937
                        if (e.getLegendEvents().length > 0) {
938
                                redraw = true;
939
                        }
940
941
                        if (e.getSelectionEvents().length > 0) {
942
                                redraw = true;
943
                        }
944
945
                        if (redraw) {
946 3331 fjp
                System.out.println("MapContextListener redraw");
947 3422 fjp
                                MapControl.this.drawMap(false);
948 1223 fernando
                        }
949
                }
950
        }
951
        public ViewPort getViewPort() {
952
                return vp;
953
        }
954 3719 caballero
955 2877 caballero
        /**
956 3719 caballero
         * Returns a HashMap with the tools that have been registered
957 2877 caballero
         * in the Mapcontrol.
958
         */
959 2876 caballero
        public HashMap getNamesMapTools() {
960
                return namesMapTools;
961
        }
962 4120 caballero
963 4142 caballero
        public void commandRepaint() {
964 4127 caballero
                drawMap(false);
965 4120 caballero
        }
966 4127 caballero
967
        public void commandRefresh() {
968
                // TODO Auto-generated method stub
969
        }
970 4888 caballero
971
        public void setPrevTool() {
972
                setTool(prevTool);
973
        }
974 5874 fjp
975 1223 fernando
}