Statistics
| Revision:

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

History | View | Annotate | Download (8.66 KB)

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

    
3
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
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.Listeners.ToolListener;
7

    
8
import java.awt.Color;
9
import java.awt.Dimension;
10
import java.awt.Graphics;
11
import java.awt.Graphics2D;
12
import java.awt.event.ActionEvent;
13
import java.awt.event.ActionListener;
14
import java.awt.event.ComponentEvent;
15
import java.awt.event.ComponentListener;
16
import java.awt.image.BufferedImage;
17

    
18
import java.util.HashMap;
19

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

    
23
import org.apache.log4j.Logger;
24

    
25

    
26
/**
27
 * DOCUMENT ME!
28
 *
29
 * @author Fernando Gonz?lez Cort?s
30
 */
31
public class NewMapControl extends JComponent implements ComponentListener {
32
    /** DOCUMENT ME! */
33
    public static final int ACTUALIZADO = 0;
34

    
35
    /** DOCUMENT ME! */
36
    public static final int DESACTUALIZADO = 1;
37
    private FMap mapContext = null;
38
    private HashMap namesMapTools = new HashMap();
39
    private HashMap namesMapToolsNames = new HashMap();
40
    private MapTool currentMapTool = null;
41
    private int status = ACTUALIZADO;
42
    private BufferedImage image = null;
43
    private String currentTool;
44
    private CancelDraw canceldraw;
45
    private boolean isCancelled = true;
46
    private Timer timer;
47
    private ViewPort vp;
48
    private Color backColor = Color.WHITE;
49
    private Drawer drawer;
50
    private static Logger logger = Logger.getLogger(NewMapControl.class.getName());
51
    /**
52
     * Crea un nuevo NewMapControl.
53
     */
54
    public NewMapControl() {
55
        setDoubleBuffered(true);
56
        
57
        //Clase usada para cancelar el dibujado
58
        canceldraw = new CancelDraw();
59

    
60
        //Modelo de datos y ventana del mismo
61
        vp = new ViewPort();
62
        mapContext = new FMap(vp);
63

    
64
        //eventos
65
        this.addComponentListener(this);
66
        
67
        //Timer para mostrar el redibujado mientras se dibuja
68
        timer = new Timer(300, new ActionListener() {
69
                        public void actionPerformed(ActionEvent e) {
70
                                NewMapControl.this.repaint();
71
                        }
72
                });
73
    }
74

    
75
    /**
76
     * DOCUMENT ME!
77
     *
78
     * @param model DOCUMENT ME!
79
     */
80
    public void setMapContext(FMap model) {
81
        mapContext = model;
82
        if (mapContext.getViewPort()==null){
83
                mapContext.setViewPort(vp);
84
        }else{
85
        vp = mapContext.getViewPort();
86
        }
87
    //        mapContext.setViewPort(vp);
88
    }
89

    
90
    /**
91
     * DOCUMENT ME!
92
     *
93
     * @return
94
     */
95
    public FMap getMapContext() {
96
        return mapContext;
97
    }
98

    
99
    /**
100
     * Registra una herramienta (tool).
101
     *
102
     * @param name DOCUMENT ME!
103
     * @param tool
104
     */
105
    public void addMapTool(String name, MapTool tool) {
106
        namesMapTools.put(name, tool);
107
        tool.setMapControl(this);
108
    }
109

    
110
    /**
111
     * DOCUMENT ME!
112
     *
113
     * @param toolName DOCUMENT ME!
114
     * @param mapToolName DOCUMENT ME!
115
     * @param tl DOCUMENT ME!
116
     */
117
    public void addTool(String toolName, String mapToolName, ToolListener tl) {
118
        namesMapToolsNames.put(toolName, mapToolName);
119

    
120
        MapTool mt = (MapTool) namesMapTools.get(mapToolName);
121
        mt.setListener(tl);
122
    }
123

    
124
    /**
125
     * DOCUMENT ME!
126
     *
127
     * @param toolName DOCUMENT ME!
128
     */
129
    public void setTool(String toolName) {
130
        if (currentMapTool != null) {
131
            this.removeMouseListener(currentMapTool);
132
            this.removeMouseMotionListener(currentMapTool);
133
            this.removeMouseWheelListener(currentMapTool);
134
        }
135

    
136
        String mapToolName = (String) namesMapToolsNames.get(toolName);
137
        MapTool mapTool = (MapTool) namesMapTools.get(mapToolName);
138
        currentMapTool = mapTool;
139
        currentTool = toolName;
140

    
141
        this.addMouseListener(currentMapTool);
142
        this.addMouseMotionListener(currentMapTool);
143
        this.addMouseWheelListener(currentMapTool);
144
        
145
        this.setCursor(mapTool.getListener().getCursor());
146
    }
147

    
148
    /**
149
     * DOCUMENT ME!
150
     *
151
     * @return DOCUMENT ME!
152
     */
153
    public String getTool() {
154
        return currentTool;
155
    }
156

    
157
    /**
158
     * Cancela el dibujado. Se espera a que la cancelaci?n surta efecto
159
     */
160
    public void cancelDrawing() {
161
        if (drawer != null) {
162
            if (!drawer.isAlive()) {
163
                return;
164
            }
165
        }
166

    
167
        canceldraw.setCancel(true);
168

    
169
        while (!isCancelled) {
170
        }
171

    
172
        canceldraw.setCancel(false);
173
        isCancelled = false;
174
    }
175

    
176
    /**
177
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
178
     */
179
    protected void paintComponent(Graphics g) {
180
        if (status == ACTUALIZADO) {
181
            if (image != null) 
182
                g.drawImage(image, 0, 0, this);
183
            else
184
            {
185
                image = new BufferedImage(this.getWidth(), this.getHeight(),
186
                        BufferedImage.TYPE_INT_ARGB);
187
            }
188
                    
189
                
190
            if (currentMapTool != null) {
191
                currentMapTool.paintComponent(g);
192
            }
193
            
194
        } else if (status == DESACTUALIZADO) {
195
                logger.debug("Pintando MapControl");
196
            image = new BufferedImage(this.getWidth(), this.getHeight(),
197
                    BufferedImage.TYPE_INT_ARGB);
198
            image.getGraphics().setColor(backColor);
199
            image.getGraphics().fillRect(0, 0, image.getWidth(),
200
                image.getHeight());
201

    
202
            
203
            cancelDrawing();
204

    
205
            drawer = new Drawer(image, (Graphics2D) image.getGraphics(),
206
                    canceldraw);
207
            drawer.start();
208
            timer.start();
209
            status = ACTUALIZADO;
210
        }
211
    }
212

    
213
    /**
214
     * DOCUMENT ME!
215
     *
216
     * @return
217
     */
218
    public BufferedImage getImage() {
219
        return image;
220
    }
221

    
222
    /**
223
     * DOCUMENT ME!
224
     */
225
    public void drawMap() {
226
        status = DESACTUALIZADO;
227
        vp.setImageSize(new Dimension(getWidth(),
228
                getHeight()));
229

    
230
        repaint();
231
    }
232

    
233
    /**
234
     * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
235
     */
236
    public void componentHidden(ComponentEvent e) {
237
    }
238

    
239
    /**
240
     * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
241
     */
242
    public void componentMoved(ComponentEvent e) {
243
    }
244

    
245
    /**
246
     * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
247
     */
248
    public void componentResized(ComponentEvent e) {
249
        drawMap();
250
    }
251

    
252
    /**
253
     * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
254
     */
255
    public void componentShown(ComponentEvent e) {
256
    }
257

    
258
    /**
259
     * DOCUMENT ME!
260
     *
261
     * @return DOCUMENT ME!
262
     */
263
    public Color getBackColor() {
264
        return backColor;
265
    }
266

    
267
    /**
268
     * DOCUMENT ME!
269
     *
270
     * @param backColor DOCUMENT ME!
271
     */
272
    public void setBackColor(Color backColor) {
273
        this.backColor = backColor;
274
    }
275

    
276
    /**
277
     * DOCUMENT ME!
278
     *
279
     * @author Vicente Caballero Navarro
280
     */
281
    public class Drawer extends Thread {
282
        private Graphics g;
283
        private BufferedImage image;
284
        private CancelDraw cancel;
285
        private boolean threadCancel = false;
286

    
287
        /**
288
         * Crea un nuevo Drawer.
289
         *
290
         * @param image DOCUMENT ME!
291
         * @param g DOCUMENT ME!
292
         * @param cancel DOCUMENT ME!
293
         */
294
        public Drawer(BufferedImage image, Graphics g, CancelDraw cancel) {
295
            this.g = g;
296
            this.image = image;
297
            this.cancel = cancel;
298
        }
299

    
300
        /**
301
         * @see java.lang.Runnable#run()
302
         */
303
        public void run() {
304
            try {
305
                synchronized (Drawer.class) {
306
                    mapContext.draw(image, (Graphics2D) image.getGraphics(),
307
                        cancel);
308
                    timer.stop();
309
                    isCancelled = true;
310
                    System.err.println("Cancelado");
311
                    repaint();
312
                }
313
            } catch (DriverIOException e) {
314
            } finally {
315
            }
316
        }
317
    }
318

    
319
    /**
320
     * DOCUMENT ME!
321
     *
322
     * @author Fernando Gonz?lez Cort?s
323
     */
324
    public class CancelDraw implements Cancellable {
325
        private boolean cancel = false;
326

    
327
        /**
328
         * Crea un nuevo CancelDraw.
329
         */
330
        public CancelDraw() {
331
        }
332

    
333
        /**
334
         * DOCUMENT ME!
335
         *
336
         * @param b DOCUMENT ME!
337
         */
338
        public void setCancel(boolean b) {
339
            cancel = b;
340
        }
341

    
342
        /**
343
         * @see com.iver.cit.gvsig.fmap.operations.Cancellable#isCanceled()
344
         */
345
        public boolean isCanceled() {
346
            return cancel;
347
        }
348
    }
349
}