Statistics
| Revision:

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

History | View | Annotate | Download (13.5 KB)

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

    
3
import java.awt.Color;
4
import java.awt.Dimension;
5
import java.awt.Graphics;
6
import java.awt.Graphics2D;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
import java.awt.event.ComponentEvent;
10
import java.awt.event.ComponentListener;
11
import java.awt.event.MouseEvent;
12
import java.awt.event.MouseListener;
13
import java.awt.event.MouseMotionListener;
14
import java.awt.event.MouseWheelEvent;
15
import java.awt.event.MouseWheelListener;
16
import java.awt.image.BufferedImage;
17
import java.util.HashMap;
18

    
19
import javax.swing.JComponent;
20
import javax.swing.Timer;
21

    
22
import org.apache.log4j.Logger;
23
import org.cresques.cts.IProjection;
24
import org.cresques.cts.ProjectionPool;
25

    
26
import com.iver.cit.gvsig.fmap.layers.LayerEvent;
27
import com.iver.cit.gvsig.fmap.operations.Cancellable;
28
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
29
import com.iver.cit.gvsig.fmap.tools.Behavior.MapTool;
30
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
31
import com.iver.utiles.exceptionHandling.ExceptionHandlingSupport;
32
import com.iver.utiles.exceptionHandling.ExceptionListener;
33

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

    
43
    /** DOCUMENT ME! */
44
    public static final int DESACTUALIZADO = 1;
45
    private FMap mapContext = null;
46
    private HashMap namesMapTools = new HashMap();
47
    private HashMap namesMapToolsNames = new HashMap();
48
        private HashMap nameListener = new HashMap();
49
    private MapTool currentMapTool = null;
50
    private int status = ACTUALIZADO;
51
    private BufferedImage image = null;
52
    private String currentTool;
53
    private CancelDraw canceldraw;
54
    private boolean isCancelled = true;
55
    private Timer timer;
56
    private ViewPort vp;
57
    private Color backColor = Color.WHITE;
58
    private Drawer drawer;
59

    
60
    private MapToolListener mapToolListener = new MapToolListener();
61
    
62
    private MapContextListener mapContextListener = new MapContextListener();
63
    
64
    private ExceptionHandlingSupport exceptionHandlingSupport = new ExceptionHandlingSupport();
65
    
66
    private static Logger logger = Logger.getLogger(NewMapControl.class.getName());
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
        if (mapContext.getViewPort()==null){
110
                mapContext.setViewPort(vp);
111
        }else{
112
                vp = mapContext.getViewPort();
113
                // vp.setImageSize(new Dimension(getWidth(), getHeight()));
114
                System.err.println("Viewport en setMapContext:" + vp);
115
        }
116

    
117
        mapContext.addAtomicEventListener(mapContextListener);
118
        
119
        status = DESACTUALIZADO;
120
    }
121
        public IProjection getProjection() {
122
                return getMapContext().getProjection();
123
        }
124
        public void setProjection(IProjection proj)
125
        {
126
                getMapContext().setProjection(proj);
127
        }
128
    
129

    
130
    /**
131
     * DOCUMENT ME!
132
     *
133
     * @return
134
     */
135
    public FMap getMapContext() {
136
        return mapContext;
137
    }
138

    
139
    /**
140
     * Registra una herramienta (tool).
141
     *
142
     * @param name DOCUMENT ME!
143
     * @param tool
144
     */
145
    public void addMapTool(String name, MapTool tool) {
146
        namesMapTools.put(name, tool);
147
        tool.setMapControl(this);
148
    }
149

    
150
    /**
151
     * DOCUMENT ME!
152
     *
153
     * @param toolName DOCUMENT ME!
154
     * @param mapToolName DOCUMENT ME!
155
     * @param tl DOCUMENT ME!
156
     */
157
    public void addTool(String toolName, String mapToolName, ToolListener tl) {
158
        namesMapToolsNames.put(toolName, mapToolName);
159
        nameListener.put(toolName, tl);
160

    
161
        MapTool mt = (MapTool) namesMapTools.get(mapToolName);
162
    }
163

    
164
    /**
165
     * DOCUMENT ME!
166
     *
167
     * @param toolName DOCUMENT ME!
168
     */
169
    public void setTool(String toolName) {
170
/*        if (currentMapTool != null) {
171
            this.removeMouseListener(currentMapTool);
172
            this.removeMouseMotionListener(currentMapTool);
173
            this.removeMouseWheelListener(currentMapTool);
174
        }
175
*/
176
        String mapToolName = (String) namesMapToolsNames.get(toolName);
177
        MapTool mapTool = (MapTool) namesMapTools.get(mapToolName);
178
        mapTool.setListener((ToolListener) nameListener.get(toolName));
179
        currentMapTool = mapTool;
180
        currentTool = toolName;
181
/*
182
        this.addMouseListener(currentMapTool);
183
        this.addMouseMotionListener(currentMapTool);
184
        this.addMouseWheelListener(currentMapTool);
185
*/
186
        this.setCursor(mapTool.getListener().getCursor());
187
    }
188

    
189
    /**
190
     * DOCUMENT ME!
191
     *
192
     * @return DOCUMENT ME!
193
     */
194
    public String getTool() {
195
        return currentTool;
196
    }
197

    
198
    /**
199
     * Cancela el dibujado. Se espera a que la cancelaci?n surta efecto
200
     */
201
    public void cancelDrawing() {
202
        if (drawer != null) {
203
            if (!drawer.isAlive()) {
204
                return;
205
            }
206
        }
207

    
208
        canceldraw.setCancel(true);
209

    
210
        while (!isCancelled) {
211
        }
212

    
213
        canceldraw.setCancel(false);
214
        isCancelled = false;
215
    }
216

    
217
    /**
218
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
219
     */
220
    protected void paintComponent(Graphics g) {
221
        if (status == ACTUALIZADO) {
222
                logger.debug("Dibujando la imagen obtenida");
223
            if (image != null) {
224
                g.drawImage(image, 0, 0, this);
225
            } else {
226
                image = new BufferedImage(this.getWidth(), this.getHeight(),
227
                        BufferedImage.TYPE_INT_ARGB);
228
            }
229

    
230
            if (currentMapTool != null) {
231
                currentMapTool.paintComponent(g);
232
            }
233
        } else if (status == DESACTUALIZADO) {
234
                logger.debug("Obteniendo la imagen de la cartograf?a");
235
            image = new BufferedImage(this.getWidth(), this.getHeight(),
236
                    BufferedImage.TYPE_INT_ARGB);
237
            image.getGraphics().setColor(backColor);
238
            image.getGraphics().fillRect(0, 0, image.getWidth(),
239
                image.getHeight());
240

    
241
            cancelDrawing();
242

    
243
            drawer = new Drawer(image, (Graphics2D) image.getGraphics(),
244
                    canceldraw);
245
            drawer.start();
246
            timer.start();
247
            status = ACTUALIZADO;
248
        }
249
    }
250

    
251
    /**
252
     * DOCUMENT ME!
253
     *
254
     * @return
255
     */
256
    public BufferedImage getImage() {
257
        return image;
258
    }
259

    
260
    /**
261
     * DOCUMENT ME!
262
     */
263
    public void drawMap() {
264
        status = DESACTUALIZADO;
265
        vp.setImageSize(new Dimension(getWidth(), getHeight()));
266

    
267
        repaint();
268
    }
269

    
270
    /**
271
     * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
272
     */
273
    public void componentHidden(ComponentEvent e) {
274
    }
275

    
276
    /**
277
     * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
278
     */
279
    public void componentMoved(ComponentEvent e) {
280
    }
281

    
282
    /**
283
     * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
284
     */
285
    public void componentResized(ComponentEvent e) {
286
        drawMap();
287
    }
288

    
289
    /**
290
     * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
291
     */
292
    public void componentShown(ComponentEvent e) {
293
    }
294

    
295
    /**
296
     * DOCUMENT ME!
297
     *
298
     * @return DOCUMENT ME!
299
     */
300
    public Color getBackColor() {
301
        return backColor;
302
    }
303

    
304
    /**
305
     * DOCUMENT ME!
306
     *
307
     * @param backColor DOCUMENT ME!
308
     */
309
    public void setBackColor(Color backColor) {
310
        this.backColor = backColor;
311
    }
312

    
313
    /**
314
     * DOCUMENT ME!
315
     *
316
     * @author Vicente Caballero Navarro
317
     */
318
    public class Drawer extends Thread {
319
        private Graphics g;
320
        private BufferedImage image;
321
        private CancelDraw cancel;
322
        private boolean threadCancel = false;
323

    
324
        /**
325
         * Crea un nuevo Drawer.
326
         *
327
         * @param image DOCUMENT ME!
328
         * @param g DOCUMENT ME!
329
         * @param cancel DOCUMENT ME!
330
         */
331
        public Drawer(BufferedImage image, Graphics g, CancelDraw cancel) {
332
            this.g = g;
333
            this.image = image;
334
            this.cancel = cancel;
335
        }
336

    
337
        /**
338
         * @see java.lang.Runnable#run()
339
         */
340
        public void run() {
341
            try {
342
                synchronized (Drawer.class) {
343
                    mapContext.draw(image, (Graphics2D) image.getGraphics(),
344
                        cancel);
345
                    timer.stop();
346
                    isCancelled = true;
347
                    System.err.println("Cancelado");
348
                    image.setRGB(5, 5, Color.BLACK.getRGB());
349
                    repaint();
350
                }
351
                        } catch (Throwable e) {
352
                                throwException(e);
353
                        } finally {
354
            }
355
        }
356
    }
357

    
358
    /**
359
     * DOCUMENT ME!
360
     *
361
     * @author Fernando Gonz?lez Cort?s
362
     */
363
    public class CancelDraw implements Cancellable {
364
        private boolean cancel = false;
365

    
366
        /**
367
         * Crea un nuevo CancelDraw.
368
         */
369
        public CancelDraw() {
370
        }
371

    
372
        /**
373
         * DOCUMENT ME!
374
         *
375
         * @param b DOCUMENT ME!
376
         */
377
        public void setCancel(boolean b) {
378
            cancel = b;
379
        }
380

    
381
        /**
382
         * @see com.iver.cit.gvsig.fmap.operations.Cancellable#isCanceled()
383
         */
384
        public boolean isCanceled() {
385
            return cancel;
386
        }
387
    }
388

    
389
    public class MapToolListener implements MouseListener, MouseWheelListener, MouseMotionListener{
390

    
391
                /**
392
                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
393
                 */
394
                public void mouseClicked(MouseEvent e) {
395
                        try{
396
                                currentMapTool.mouseClicked(e);
397
                        }catch(BehaviorException t){
398
                                throwException(t);
399
                        }
400
                }
401

    
402
                /**
403
                 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
404
                 */
405
                public void mouseEntered(MouseEvent e) {
406
                        try{
407
                                currentMapTool.mouseEntered(e);
408
                        }catch(BehaviorException t){
409
                                throwException(t);
410
                        }
411
                }
412

    
413
                /**
414
                 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
415
                 */
416
                public void mouseExited(MouseEvent e) {
417
                        try{
418
                                currentMapTool.mouseExited(e);
419
                        }catch(BehaviorException t){
420
                                throwException(t);
421
                        }
422
                }
423

    
424
                /**
425
                 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
426
                 */
427
                public void mousePressed(MouseEvent e) {
428
                        try{
429
                                currentMapTool.mousePressed(e);
430
                        }catch(BehaviorException t){
431
                                throwException(t);
432
                        }
433
                }
434

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

    
446
                /**
447
                 * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
448
                 */
449
                public void mouseWheelMoved(MouseWheelEvent e) {
450
                        try{
451
                                currentMapTool.mouseWheelMoved(e);
452
                        }catch(BehaviorException t){
453
                                throwException(t);
454
                        }
455
                }
456

    
457
                /**
458
                 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
459
                 */
460
                public void mouseDragged(MouseEvent e) {
461
                        try{
462
                                currentMapTool.mouseDragged(e);
463
                        }catch(BehaviorException t){
464
                                throwException(t);
465
                        }
466
                }
467

    
468
                /**
469
                 * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
470
                 */
471
                public void mouseMoved(MouseEvent e) {
472
                        try{
473
                                currentMapTool.mouseMoved(e);
474
                        }catch(BehaviorException t){
475
                                throwException(t);
476
                        }
477
                }
478
    }
479
    
480
    public class MapContextListener implements AtomicEventListener{
481
                /**
482
                 * @see com.iver.cit.gvsig.fmap.AtomicEventListener#atomicEvent(com.iver.cit.gvsig.fmap.AtomicEvent)
483
                 */
484
                public void atomicEvent(AtomicEvent e) {
485
                        boolean redraw = false;
486
                        LayerEvent[] layerEvents = e.getLayerEvents();
487
                        for (int i = 0; i < layerEvents.length; i++) {
488
                                if (layerEvents[i].getProperty().equals("visible")){
489
                                        redraw = true;
490
                                }
491
                        }
492
                        if (e.getExtentEvents().length > 0) redraw = true;
493
                        if (e.getLayerCollectionEvents().length > 0) redraw = true;
494
                        if (e.getLegendEvents().length > 0) redraw = true;
495
                        if (e.getSelectionEvents().length > 0) redraw = true;
496
                        
497
                        if (redraw){
498
                                NewMapControl.this.drawMap();
499
                        }
500
                }
501
    }
502
        public void addExceptionListener(ExceptionListener o) {
503
                exceptionHandlingSupport.addExceptionListener(o);
504
        }
505
        public boolean removeExceptionListener(ExceptionListener o) {
506
                return exceptionHandlingSupport.removeExceptionListener(o);
507
        }
508
        private void throwException(Throwable t) {
509
                exceptionHandlingSupport.throwException(t);
510
        }
511
}