Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / NewMapControl.java @ 984

History | View | Annotate | Download (12.4 KB)

1
package com.iver.cit.gvsig.fmap;
2

    
3
import com.iver.cit.gvsig.fmap.layers.LayerEvent;
4
import com.iver.cit.gvsig.fmap.operations.Cancellable;
5
import com.iver.cit.gvsig.fmap.tools.Behavior.MapTool;
6
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
7
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
8

    
9
import com.iver.utiles.exceptionHandling.ExceptionHandlingSupport;
10
import com.iver.utiles.exceptionHandling.ExceptionListener;
11

    
12
import org.apache.log4j.Logger;
13

    
14
import org.cresques.cts.IProjection;
15
import org.cresques.cts.ProjectionPool;
16

    
17
import java.awt.Color;
18
import java.awt.Dimension;
19
import java.awt.Graphics;
20
import java.awt.Graphics2D;
21
import java.awt.event.ActionEvent;
22
import java.awt.event.ActionListener;
23
import java.awt.event.ComponentEvent;
24
import java.awt.event.ComponentListener;
25
import java.awt.event.MouseEvent;
26
import java.awt.event.MouseListener;
27
import java.awt.event.MouseMotionListener;
28
import java.awt.event.MouseWheelEvent;
29
import java.awt.event.MouseWheelListener;
30
import java.awt.image.BufferedImage;
31

    
32
import java.util.HashMap;
33

    
34
import javax.swing.JComponent;
35
import javax.swing.Timer;
36

    
37

    
38
/**
39
 * DOCUMENT ME!
40
 *
41
 * @author Fernando Gonz?lez Cort?s
42
 */
43
public class NewMapControl extends JComponent implements ComponentListener {
44
        /** DOCUMENT ME! */
45
        public static final int ACTUALIZADO = 0;
46

    
47
        /** DOCUMENT ME! */
48
        public static final int DESACTUALIZADO = 1;
49
        private static Logger logger = Logger.getLogger(NewMapControl.class.getName());
50
        private FMap mapContext = null;
51
        private HashMap namesMapTools = new HashMap();
52
        private HashMap namesMapToolsNames = new HashMap();
53
        private HashMap nameListener = new HashMap();
54
        private MapTool currentMapTool = null;
55
        private int status = ACTUALIZADO;
56
        private BufferedImage image = null;
57
        private String currentTool;
58
        private CancelDraw canceldraw;
59
        private boolean isCancelled = true;
60
        private Timer timer;
61
        protected ViewPort vp;
62
        private Drawer drawer;
63
        private MapToolListener mapToolListener = new MapToolListener();
64
        private MapContextListener mapContextListener = new MapContextListener();
65
        private ExceptionHandlingSupport exceptionHandlingSupport = new ExceptionHandlingSupport();
66

    
67
        /**
68
         * Crea un nuevo NewMapControl.
69
         */
70
        public NewMapControl() {
71
                setDoubleBuffered(true);
72

    
73
                //Clase usada para cancelar el dibujado
74
                canceldraw = new CancelDraw();
75

    
76
                //Modelo de datos y ventana del mismo
77
                // TODO: Cuando creamos un mapControl, deber?amos asignar
78
                // la projecci?n por defecto con la que vayamos a trabajar.
79
                // 23030 es el c?digo EPSG del UTM30 elipsoide ED50
80
                vp = new ViewPort(ProjectionPool.get("23030"));
81
                setMapContext(new FMap(vp));
82

    
83
                //eventos
84
                this.addComponentListener(this);
85
                this.addMouseListener(mapToolListener);
86
                this.addMouseMotionListener(mapToolListener);
87
                this.addMouseWheelListener(mapToolListener);
88

    
89
                //Timer para mostrar el redibujado mientras se dibuja
90
                timer = new Timer(300,
91
                                new ActionListener() {
92
                                        public void actionPerformed(ActionEvent e) {
93
                                                NewMapControl.this.repaint();
94
                                        }
95
                                });
96
        }
97

    
98
        /**
99
         * DOCUMENT ME!
100
         *
101
         * @param model DOCUMENT ME!
102
         */
103
        public void setMapContext(FMap model) {
104
                if (mapContext != null) {
105
                        mapContext.removeAtomicEventListener(mapContextListener);
106
                }
107

    
108
                mapContext = model;
109

    
110
                if (mapContext.getViewPort() == null) {
111
                        mapContext.setViewPort(vp);
112
                } else {
113
                        vp = mapContext.getViewPort();
114

    
115
                        // vp.setImageSize(new Dimension(getWidth(), getHeight()));
116
                        // System.err.println("Viewport en setMapContext:" + vp);
117
                }
118

    
119
                mapContext.addAtomicEventListener(mapContextListener);
120

    
121
                status = DESACTUALIZADO;
122
        }
123

    
124
        /**
125
         * DOCUMENT ME!
126
         *
127
         * @return DOCUMENT ME!
128
         */
129
        public IProjection getProjection() {
130
                return getMapContext().getProjection();
131
        }
132

    
133
        /**
134
         * DOCUMENT ME!
135
         *
136
         * @param proj DOCUMENT ME!
137
         */
138
        public void setProjection(IProjection proj) {
139
                getMapContext().setProjection(proj);
140
        }
141

    
142
        /**
143
         * DOCUMENT ME!
144
         *
145
         * @return
146
         */
147
        public FMap getMapContext() {
148
                return mapContext;
149
        }
150

    
151
        /**
152
         * Registra una herramienta (tool).
153
         *
154
         * @param name DOCUMENT ME!
155
         * @param tool
156
         */
157
        public void addMapTool(String name, MapTool tool) {
158
                namesMapTools.put(name, tool);
159
                tool.setMapControl(this);
160
        }
161

    
162
        /**
163
         * DOCUMENT ME!
164
         *
165
         * @param toolName DOCUMENT ME!
166
         * @param mapToolName DOCUMENT ME!
167
         * @param tl DOCUMENT ME!
168
         */
169
        public void addTool(String toolName, String mapToolName, ToolListener tl) {
170
                namesMapToolsNames.put(toolName, mapToolName);
171
                nameListener.put(toolName, tl);
172

    
173
                MapTool mt = (MapTool) namesMapTools.get(mapToolName);
174
        }
175

    
176
        /**
177
         * DOCUMENT ME!
178
         *
179
         * @param toolName DOCUMENT ME!
180
         */
181
        public void setTool(String toolName) {
182
                /*        if (currentMapTool != null) {
183
                   this.removeMouseListener(currentMapTool);
184
                   this.removeMouseMotionListener(currentMapTool);
185
                   this.removeMouseWheelListener(currentMapTool);
186
                   }
187
                 */
188
                String mapToolName = (String) namesMapToolsNames.get(toolName);
189
                MapTool mapTool = (MapTool) namesMapTools.get(mapToolName);
190
                mapTool.setListener((ToolListener) nameListener.get(toolName));
191
                currentMapTool = mapTool;
192
                currentTool = toolName;
193

    
194
                /*
195
                   this.addMouseListener(currentMapTool);
196
                   this.addMouseMotionListener(currentMapTool);
197
                   this.addMouseWheelListener(currentMapTool);
198
                 */
199
                this.setCursor(mapTool.getListener().getCursor());
200
        }
201

    
202
        /**
203
         * DOCUMENT ME!
204
         *
205
         * @return DOCUMENT ME!
206
         */
207
        public String getTool() {
208
                return currentTool;
209
        }
210

    
211
        /**
212
         * Cancela el dibujado. Se espera a que la cancelaci?n surta efecto
213
         */
214
        public void cancelDrawing() {
215
                if (drawer != null) {
216
                        if (!drawer.isAlive()) {
217
                                return;
218
                        }
219
                }
220

    
221
                canceldraw.setCancel(true);
222

    
223
                while (!isCancelled) {
224
                }
225

    
226
                canceldraw.setCancel(false);
227
                isCancelled = false;
228
        }
229

    
230
        /**
231
         * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
232
         */
233
        protected void paintComponent(Graphics g) {
234
                if (status == ACTUALIZADO) {
235
                        logger.debug("Dibujando la imagen obtenida");
236

    
237
                        /* 
238
                         * Si hay un behaviour y la imagen es distinta de null se delega el dibujado
239
                         * en dicho behaviour 
240
                         */
241
                        if ((currentMapTool != null) && (image != null)) {
242
                                currentMapTool.paintComponent(g);
243
                        }
244

    
245
                } else if (status == DESACTUALIZADO) {
246
                        logger.debug("Obteniendo la imagen de la cartograf?a");
247

    
248
                        //Se cancela el dibujado anterior
249
                        cancelDrawing();
250
                        
251
                        //Se crea la imagen con el color de fonde deseado
252
                        image = new BufferedImage(this.getWidth(), this.getHeight(),
253
                                        BufferedImage.TYPE_INT_ARGB);
254
                        Graphics imgg = image.createGraphics();
255
                        imgg.setColor(vp.getBackColor());
256
                        imgg.fillRect(0, 0, getWidth(), getHeight());
257

    
258
                        //Se actualizan los datos del viewPort y se lanza el tread de dibujado
259
                        vp.setImageSize(new Dimension(getWidth(), getHeight()));
260
                        drawer = new Drawer(image, (Graphics2D) imgg, canceldraw);
261
                        drawer.start();
262
                        timer.start();
263
                        status = ACTUALIZADO;
264
                        repaint();
265
                }
266
        }
267

    
268
        /**
269
         * DOCUMENT ME!
270
         *
271
         * @return
272
         */
273
        public BufferedImage getImage() {
274
                return image;
275
        }
276

    
277
        /**
278
         * Marca el mapa para que en el pr?ximo redibujado se acceda a la
279
         * cartograf?a para reobtener la imagen
280
         */
281
        public void drawMap() {
282
                status = DESACTUALIZADO;
283
                repaint();
284
        }
285

    
286
        /**
287
         * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
288
         */
289
        public void componentHidden(ComponentEvent e) {
290
        }
291

    
292
        /**
293
         * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
294
         */
295
        public void componentMoved(ComponentEvent e) {
296
        }
297

    
298
        /**
299
         * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
300
         */
301
        public void componentResized(ComponentEvent e) {
302
                drawMap();
303
        }
304

    
305
        /**
306
         * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
307
         */
308
        public void componentShown(ComponentEvent e) {
309
        }
310

    
311
        /**
312
         * DOCUMENT ME!
313
         *
314
         * @param o DOCUMENT ME!
315
         */
316
        public void addExceptionListener(ExceptionListener o) {
317
                exceptionHandlingSupport.addExceptionListener(o);
318
        }
319

    
320
        /**
321
         * DOCUMENT ME!
322
         *
323
         * @param o DOCUMENT ME!
324
         *
325
         * @return DOCUMENT ME!
326
         */
327
        public boolean removeExceptionListener(ExceptionListener o) {
328
                return exceptionHandlingSupport.removeExceptionListener(o);
329
        }
330

    
331
        /**
332
         * DOCUMENT ME!
333
         *
334
         * @param t DOCUMENT ME!
335
         */
336
        private void throwException(Throwable t) {
337
                exceptionHandlingSupport.throwException(t);
338
        }
339

    
340
        /**
341
         * DOCUMENT ME!
342
         *
343
         * @author Vicente Caballero Navarro
344
         */
345
        public class Drawer extends Thread {
346
                private Graphics g;
347
                private BufferedImage image;
348
                private CancelDraw cancel;
349
                private boolean threadCancel = false;
350

    
351
                /**
352
                 * Crea un nuevo Drawer.
353
                 *
354
                 * @param image DOCUMENT ME!
355
                 * @param g DOCUMENT ME!
356
                 * @param cancel DOCUMENT ME!
357
                 */
358
                public Drawer(BufferedImage image, Graphics g, CancelDraw cancel) {
359
                        this.g = g;
360
                        this.image = image;
361
                        this.cancel = cancel;
362
                }
363

    
364
                /**
365
                 * @see java.lang.Runnable#run()
366
                 */
367
                public void run() {
368
                        try {
369
                                synchronized (Drawer.class) {
370
                                        mapContext.draw(image, (Graphics2D) image.getGraphics(),
371
                                                cancel);
372
                                        timer.stop();
373
                                        isCancelled = true;
374
                                        repaint();
375
                                }
376
                        } catch (Throwable e) {
377
                                throwException(e);
378
                        } finally {
379
                        }
380
                }
381
        }
382

    
383
        /**
384
         * DOCUMENT ME!
385
         *
386
         * @author Fernando Gonz?lez Cort?s
387
         */
388
        public class CancelDraw implements Cancellable {
389
                private boolean cancel = false;
390

    
391
                /**
392
                 * Crea un nuevo CancelDraw.
393
                 */
394
                public CancelDraw() {
395
                }
396

    
397
                /**
398
                 * DOCUMENT ME!
399
                 *
400
                 * @param b DOCUMENT ME!
401
                 */
402
                public void setCancel(boolean b) {
403
                        cancel = b;
404
                }
405

    
406
                /**
407
                 * @see com.iver.cit.gvsig.fmap.operations.Cancellable#isCanceled()
408
                 */
409
                public boolean isCanceled() {
410
                        return cancel;
411
                }
412
        }
413

    
414
        /**
415
         * DOCUMENT ME!
416
         *
417
         * @author Fernando Gonz?lez Cort?s
418
         */
419
        public class MapToolListener implements MouseListener, MouseWheelListener,
420
                MouseMotionListener {
421
                /**
422
                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
423
                 */
424
                public void mouseClicked(MouseEvent e) {
425
                        try {
426
                                currentMapTool.mouseClicked(e);
427
                        } catch (BehaviorException t) {
428
                                throwException(t);
429
                        }
430
                }
431

    
432
                /**
433
                 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
434
                 */
435
                public void mouseEntered(MouseEvent e) {
436
                        try {
437
                                currentMapTool.mouseEntered(e);
438
                        } catch (BehaviorException t) {
439
                                throwException(t);
440
                        }
441
                }
442

    
443
                /**
444
                 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
445
                 */
446
                public void mouseExited(MouseEvent e) {
447
                        try {
448
                                currentMapTool.mouseExited(e);
449
                        } catch (BehaviorException t) {
450
                                throwException(t);
451
                        }
452
                }
453

    
454
                /**
455
                 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
456
                 */
457
                public void mousePressed(MouseEvent e) {
458
                        try {
459
                                currentMapTool.mousePressed(e);
460
                        } catch (BehaviorException t) {
461
                                throwException(t);
462
                        }
463
                }
464

    
465
                /**
466
                 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
467
                 */
468
                public void mouseReleased(MouseEvent e) {
469
                        try {
470
                                currentMapTool.mouseReleased(e);
471
                        } catch (BehaviorException t) {
472
                                throwException(t);
473
                        }
474
                }
475

    
476
                /**
477
                 * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
478
                 */
479
                public void mouseWheelMoved(MouseWheelEvent e) {
480
                        try {
481
                                currentMapTool.mouseWheelMoved(e);
482
                        } catch (BehaviorException t) {
483
                                throwException(t);
484
                        }
485
                }
486

    
487
                /**
488
                 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
489
                 */
490
                public void mouseDragged(MouseEvent e) {
491
                        try {
492
                                currentMapTool.mouseDragged(e);
493
                        } catch (BehaviorException t) {
494
                                throwException(t);
495
                        }
496
                }
497

    
498
                /**
499
                 * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
500
                 */
501
                public void mouseMoved(MouseEvent e) {
502
                        try {
503
                                currentMapTool.mouseMoved(e);
504
                        } catch (BehaviorException t) {
505
                                throwException(t);
506
                        }
507
                }
508
        }
509

    
510
        /**
511
         * DOCUMENT ME!
512
         *
513
         * @author Fernando Gonz?lez Cort?s
514
         */
515
        public class MapContextListener implements AtomicEventListener {
516
                /**
517
                 * @see com.iver.cit.gvsig.fmap.AtomicEventListener#atomicEvent(com.iver.cit.gvsig.fmap.AtomicEvent)
518
                 */
519
                public void atomicEvent(AtomicEvent e) {
520
                        boolean redraw = false;
521
                        LayerEvent[] layerEvents = e.getLayerEvents();
522

    
523
                        for (int i = 0; i < layerEvents.length; i++) {
524
                                if (layerEvents[i].getProperty().equals("visible")) {
525
                                        redraw = true;
526
                                }
527
                        }
528

    
529
                        if (e.getColorEvents().length > 0) {
530
                                redraw = true;
531
                        }
532

    
533
                        if (e.getExtentEvents().length > 0) {
534
                                redraw = true;
535
                        }
536

    
537
                        if (e.getLayerCollectionEvents().length > 0) {
538
                                redraw = true;
539
                        }
540

    
541
                        if (e.getLegendEvents().length > 0) {
542
                                redraw = true;
543
                        }
544

    
545
                        if (e.getSelectionEvents().length > 0) {
546
                                redraw = true;
547
                        }
548

    
549
                        if (redraw) {
550
                                NewMapControl.this.drawMap();
551
                        }
552
                }
553
        }
554
}