svn-gvsig-desktop / branches / v051 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / MapControl.java @ 3933
History | View | Annotate | Download (22.8 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 | import java.awt.event.ActionEvent; |
||
48 | import java.awt.event.ActionListener; |
||
49 | import java.awt.event.ComponentEvent; |
||
50 | import java.awt.event.ComponentListener; |
||
51 | import java.awt.event.MouseEvent; |
||
52 | import java.awt.event.MouseListener; |
||
53 | import java.awt.event.MouseMotionListener; |
||
54 | import java.awt.event.MouseWheelEvent; |
||
55 | import java.awt.event.MouseWheelListener; |
||
56 | import java.awt.image.BufferedImage; |
||
57 | import java.util.HashMap; |
||
58 | |||
59 | import javax.swing.JComponent; |
||
60 | import javax.swing.Timer; |
||
61 | |||
62 | import org.cresques.cts.IProjection; |
||
63 | import org.cresques.cts.ProjectionPool; |
||
64 | |||
65 | import com.iver.cit.gvsig.fmap.layers.LayerEvent; |
||
66 | import com.iver.cit.gvsig.fmap.operations.Cancellable; |
||
67 | import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
||
68 | import com.iver.cit.gvsig.fmap.tools.CompoundBehavior; |
||
69 | import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior; |
||
70 | import com.iver.utiles.exceptionHandling.ExceptionHandlingSupport; |
||
71 | import com.iver.utiles.exceptionHandling.ExceptionListener; |
||
72 | |||
73 | |||
74 | /**
|
||
75 | * MapControl.
|
||
76 | *
|
||
77 | * @author Fernando Gonz?lez Cort?s
|
||
78 | */
|
||
79 | public class MapControl extends JComponent implements ComponentListener { |
||
80 | /** Cuando la vista est? actualizada. */
|
||
81 | public static final int ACTUALIZADO = 0; |
||
82 | |||
83 | /** Cuando la vista est? desactualizada. */
|
||
84 | public static final int DESACTUALIZADO = 1; |
||
85 | 2901 | fjp | public static final int ONLY_GRAPHICS = 2; |
86 | 3346 | fjp | // public static final int FAST_PAINT = 3;
|
87 | 2531 | caballero | //private static Logger logger = Logger.getLogger(MapControl.class.getName());
|
88 | 1223 | fernando | private FMap mapContext = null; |
89 | 3331 | fjp | private boolean drawerAlive = false; |
90 | 1223 | fernando | private HashMap namesMapTools = new HashMap(); |
91 | private Behavior currentMapTool = null; |
||
92 | private int status = DESACTUALIZADO; |
||
93 | private BufferedImage image = null; |
||
94 | private String currentTool; |
||
95 | private CancelDraw canceldraw;
|
||
96 | private boolean isCancelled = true; |
||
97 | private Timer timer; |
||
98 | protected ViewPort vp;
|
||
99 | private Drawer drawer;
|
||
100 | 3368 | fjp | private Drawer2 drawer2;
|
101 | 3372 | fjp | // private boolean firstDraw = true;
|
102 | 1223 | fernando | private MapToolListener mapToolListener = new MapToolListener(); |
103 | private MapContextListener mapContextListener = new MapContextListener(); |
||
104 | private ExceptionHandlingSupport exceptionHandlingSupport = new ExceptionHandlingSupport(); |
||
105 | 3331 | fjp | /**
|
106 | * We need this to avoid not wanted refresh. REMEMBER TO SET TO TRUE!!
|
||
107 | */
|
||
108 | // private boolean paintEnabled = false;
|
||
109 | 1223 | fernando | |
110 | /**
|
||
111 | * Crea un nuevo NewMapControl.
|
||
112 | */
|
||
113 | public MapControl() {
|
||
114 | setDoubleBuffered(false);
|
||
115 | setOpaque(true);
|
||
116 | status = DESACTUALIZADO; |
||
117 | |||
118 | //Clase usada para cancelar el dibujado
|
||
119 | canceldraw = new CancelDraw();
|
||
120 | |||
121 | //Modelo de datos y ventana del mismo
|
||
122 | // TODO: Cuando creamos un mapControl, deber?amos asignar
|
||
123 | // la projecci?n por defecto con la que vayamos a trabajar.
|
||
124 | // 23030 es el c?digo EPSG del UTM30 elipsoide ED50
|
||
125 | vp = new ViewPort(ProjectionPool.get("EPSG:23030")); |
||
126 | setMapContext(new FMap(vp));
|
||
127 | 1243 | fjp | |
128 | 1223 | fernando | //eventos
|
129 | this.addComponentListener(this); |
||
130 | this.addMouseListener(mapToolListener);
|
||
131 | this.addMouseMotionListener(mapToolListener);
|
||
132 | this.addMouseWheelListener(mapToolListener);
|
||
133 | |||
134 | 3368 | fjp | this.drawer2 = new Drawer2(); |
135 | 1223 | fernando | //Timer para mostrar el redibujado mientras se dibuja
|
136 | timer = new Timer(300, |
||
137 | new ActionListener() { |
||
138 | public void actionPerformed(ActionEvent e) { |
||
139 | MapControl.this.repaint(); |
||
140 | } |
||
141 | }); |
||
142 | } |
||
143 | |||
144 | /**
|
||
145 | * Inserta el modelo.
|
||
146 | *
|
||
147 | * @param model FMap.
|
||
148 | */
|
||
149 | public void setMapContext(FMap model) { |
||
150 | if (mapContext != null) { |
||
151 | mapContext.removeAtomicEventListener(mapContextListener); |
||
152 | } |
||
153 | |||
154 | mapContext = model; |
||
155 | |||
156 | if (mapContext.getViewPort() == null) { |
||
157 | mapContext.setViewPort(vp); |
||
158 | } else {
|
||
159 | vp = mapContext.getViewPort(); |
||
160 | |||
161 | // vp.setImageSize(new Dimension(getWidth(), getHeight()));
|
||
162 | //System.err.println("Viewport en setMapContext:" + vp);
|
||
163 | } |
||
164 | |||
165 | mapContext.addAtomicEventListener(mapContextListener); |
||
166 | |||
167 | status = DESACTUALIZADO; |
||
168 | } |
||
169 | |||
170 | /**
|
||
171 | * Devuelve la proyecci?n.
|
||
172 | *
|
||
173 | * @return Proyecci?n.
|
||
174 | */
|
||
175 | public IProjection getProjection() {
|
||
176 | return getMapContext().getProjection();
|
||
177 | } |
||
178 | |||
179 | /**
|
||
180 | * Inserta una proyecci?n.
|
||
181 | *
|
||
182 | * @param proj Proyecci?n.
|
||
183 | */
|
||
184 | public void setProjection(IProjection proj) { |
||
185 | getMapContext().setProjection(proj); |
||
186 | } |
||
187 | |||
188 | /**
|
||
189 | * Devuelve el modelo.
|
||
190 | *
|
||
191 | * @return FMap.
|
||
192 | */
|
||
193 | public FMap getMapContext() {
|
||
194 | return mapContext;
|
||
195 | } |
||
196 | |||
197 | /**
|
||
198 | * Registra una herramienta (tool).
|
||
199 | *
|
||
200 | * @param name Nombre de la herramienta.
|
||
201 | * @param tool Herramienta.
|
||
202 | */
|
||
203 | public void addMapTool(String name, Behavior tool) { |
||
204 | namesMapTools.put(name, tool); |
||
205 | tool.setMapControl(this);
|
||
206 | } |
||
207 | |||
208 | public void addMapTool(String name, Behavior[] tools){ |
||
209 | CompoundBehavior tool = new CompoundBehavior(tools);
|
||
210 | addMapTool(name, tool); |
||
211 | } |
||
212 | |||
213 | /**
|
||
214 | * DOCUMENT ME!
|
||
215 | *
|
||
216 | * @param toolName DOCUMENT ME!
|
||
217 | */
|
||
218 | public void setTool(String toolName) { |
||
219 | Behavior mapTool = (Behavior) namesMapTools.get(toolName); |
||
220 | currentMapTool = mapTool; |
||
221 | currentTool = toolName; |
||
222 | this.setCursor(mapTool.getCursor());
|
||
223 | } |
||
224 | |||
225 | /**
|
||
226 | * Devuelve el nombre de la herramienta seleccionada.
|
||
227 | *
|
||
228 | * @return nombre.
|
||
229 | */
|
||
230 | public String getTool() { |
||
231 | return currentTool;
|
||
232 | } |
||
233 | |||
234 | /**
|
||
235 | * Cancela el dibujado. Se espera a que la cancelaci?n surta efecto
|
||
236 | */
|
||
237 | public void cancelDrawing() { |
||
238 | 3368 | fjp | /* if (drawer != null) {
|
239 | 1223 | fernando | if (!drawer.isAlive()) {
|
240 | return;
|
||
241 | }
|
||
242 | }
|
||
243 | 3368 | fjp | */
|
244 | 1223 | fernando | canceldraw.setCancel(true);
|
245 | |||
246 | 3368 | fjp | /* while (!isCancelled) {
|
247 | 1243 | fjp | if (!drawer.isAlive()) {
|
248 | 1327 | fjp | // Si hemos llegado aqu? con un thread vivo, seguramente
|
249 | // no estamos actualizados.
|
||
250 | |||
251 | break;
|
||
252 | 1243 | fjp | }
|
253 | |||
254 | 1223 | fernando | }
|
255 | canceldraw.setCancel(false);
|
||
256 | isCancelled = false;
|
||
257 | 3368 | fjp | drawerAlive = false; */
|
258 | 1223 | fernando | } |
259 | 3375 | fjp | |
260 | private boolean adaptToImageSize() |
||
261 | { |
||
262 | if ((image == null) || (vp.getImageWidth() != this.getWidth()) || (vp.getImageHeight() != this.getHeight())) |
||
263 | { |
||
264 | image = new BufferedImage(this.getWidth(), this.getHeight(), |
||
265 | BufferedImage.TYPE_INT_ARGB);
|
||
266 | vp.setImageSize(new Dimension(getWidth(), getHeight())); |
||
267 | getMapContext().getViewPort().setScale(); |
||
268 | |||
269 | |||
270 | Graphics gTemp = image.createGraphics();
|
||
271 | Color theBackColor = vp.getBackColor();
|
||
272 | if (theBackColor == null) |
||
273 | gTemp.setColor(Color.WHITE);
|
||
274 | else
|
||
275 | gTemp.setColor(theBackColor); |
||
276 | |||
277 | gTemp.fillRect(0,0,getWidth(), getHeight()); |
||
278 | gTemp.dispose(); |
||
279 | status = DESACTUALIZADO; |
||
280 | // g.drawImage(image,0,0,null);
|
||
281 | return true; |
||
282 | } |
||
283 | return false; |
||
284 | } |
||
285 | 1223 | fernando | |
286 | /**
|
||
287 | * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
|
||
288 | */
|
||
289 | protected void paintComponent(Graphics g) { |
||
290 | 3375 | fjp | adaptToImageSize(); |
291 | 3346 | fjp | /* if (status == FAST_PAINT) {
|
292 | 3331 | fjp | System.out.println("FAST_PAINT");
|
293 | g.drawImage(image,0,0,null);
|
||
294 | status = ACTUALIZADO;
|
||
295 | return;
|
||
296 | 3346 | fjp | } */
|
297 | 1223 | fernando | if (status == ACTUALIZADO) {
|
298 | 1680 | luisw | // LWS logger.debug("Dibujando la imagen obtenida");
|
299 | 1223 | fernando | |
300 | /*
|
||
301 | * Si hay un behaviour y la imagen es distinta de null se delega el dibujado
|
||
302 | * en dicho behaviour
|
||
303 | */
|
||
304 | 3372 | fjp | if (image != null) |
305 | { |
||
306 | if (currentMapTool != null) |
||
307 | currentMapTool.paintComponent(g); |
||
308 | else
|
||
309 | g.drawImage(image,0,0,null); |
||
310 | |||
311 | // System.out.println("Pinto ACTUALIZADO");
|
||
312 | 1223 | fernando | } |
313 | 2946 | fjp | } else if ((status == DESACTUALIZADO) |
314 | || (status == ONLY_GRAPHICS)) { |
||
315 | 1680 | luisw | // LWS System.out.println("DESACTUALIZADO: Obteniendo la imagen de la cartograf?a");
|
316 | 1223 | fernando | /* if (isOpaque())
|
317 | {
|
||
318 | if (image==null)
|
||
319 | {
|
||
320 | g.setColor(vp.getBackColor());
|
||
321 | g.fillRect(0,0,getWidth(), getHeight());
|
||
322 | }
|
||
323 | // else g.drawImage(image,0,0,null);
|
||
324 | } */
|
||
325 | 3368 | fjp | // cancelDrawing();
|
326 | 1223 | fernando | //Se crea la imagen con el color de fonde deseado
|
327 | 3375 | fjp | /* if (image == null)
|
328 | 1223 | fernando | {
|
329 | image = new BufferedImage(this.getWidth(), this.getHeight(),
|
||
330 | BufferedImage.TYPE_INT_ARGB);
|
||
331 | vp.setImageSize(new Dimension(getWidth(), getHeight()));
|
||
332 | Graphics gTemp = image.createGraphics();
|
||
333 | 1303 | fjp | Color theBackColor = vp.getBackColor();
|
334 | if (theBackColor == null)
|
||
335 | gTemp.setColor(Color.WHITE);
|
||
336 | else
|
||
337 | gTemp.setColor(theBackColor);
|
||
338 | |||
339 | 1223 | fernando | gTemp.fillRect(0,0,getWidth(), getHeight());
|
340 | gTemp.dispose();
|
||
341 | // g.drawImage(image,0,0,null);
|
||
342 | 3174 | ldiaz | System.out.println("Imagen con null en DESACTUALIZADO. Width = " + this.getWidth());
|
343 | 3375 | fjp | } */
|
344 | 3331 | fjp | // else
|
345 | // {
|
||
346 | 3372 | fjp | |
347 | |||
348 | // if (image != null)
|
||
349 | // {
|
||
350 | 3331 | fjp | g.drawImage(image,0,0,null); |
351 | 3372 | fjp | |
352 | drawer2.put(new PaintingRequest());
|
||
353 | timer.start(); |
||
354 | /* }
|
||
355 | else
|
||
356 | return; */
|
||
357 | 3331 | fjp | // }
|
358 | 3348 | fjp | |
359 | 3368 | fjp | /* if (drawerAlive == false)
|
360 | 3331 | fjp | {
|
361 | drawer = new Drawer(image, canceldraw);
|
||
362 | drawer.start();
|
||
363 | 3346 | fjp | //Se lanza el tread de dibujado
|
364 | 3368 | fjp | } */
|
365 | 1243 | fjp | |
366 | 2946 | fjp | // status = ACTUALIZADO;
|
367 | 1223 | fernando | } |
368 | } |
||
369 | |||
370 | /**
|
||
371 | * Devuelve la imagen de la vista.
|
||
372 | *
|
||
373 | * @return imagen.
|
||
374 | */
|
||
375 | public BufferedImage getImage() { |
||
376 | return image;
|
||
377 | } |
||
378 | |||
379 | /**
|
||
380 | * Marca el mapa para que en el pr?ximo redibujado se acceda a la
|
||
381 | * cartograf?a para reobtener la imagen
|
||
382 | 3422 | fjp | * @param doClear Solo deber?a ser true cuando se llama desde pan
|
383 | 1223 | fernando | */
|
384 | public void drawMap(boolean doClear) { |
||
385 | 3331 | fjp | System.out.println("drawMap con doClear=" + doClear); |
386 | 3346 | fjp | status = DESACTUALIZADO; |
387 | 3331 | fjp | if (doClear)
|
388 | { |
||
389 | 3372 | fjp | // image = null; // Se usa para el PAN
|
390 | if (image != null) |
||
391 | { |
||
392 | Graphics2D g = image.createGraphics();
|
||
393 | Color theBackColor = vp.getBackColor();
|
||
394 | if (theBackColor == null) |
||
395 | g.setColor(Color.WHITE);
|
||
396 | else
|
||
397 | g.setColor(theBackColor); |
||
398 | g.fillRect(0, 0, vp.getImageWidth(), vp.getImageHeight()); |
||
399 | g.dispose(); |
||
400 | 3422 | fjp | } |
401 | 3331 | fjp | } |
402 | 1223 | fernando | repaint(); |
403 | } |
||
404 | 2901 | fjp | public void drawGraphics() { |
405 | status = ONLY_GRAPHICS; |
||
406 | repaint(); |
||
407 | } |
||
408 | 1223 | fernando | |
409 | /**
|
||
410 | * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
|
||
411 | */
|
||
412 | public void componentHidden(ComponentEvent e) { |
||
413 | } |
||
414 | |||
415 | /**
|
||
416 | * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
|
||
417 | */
|
||
418 | public void componentMoved(ComponentEvent e) { |
||
419 | } |
||
420 | |||
421 | /**
|
||
422 | * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
|
||
423 | */
|
||
424 | public void componentResized(ComponentEvent e) { |
||
425 | 3375 | fjp | /* image = new BufferedImage(this.getWidth(), this.getHeight(),
|
426 | 1223 | fernando | BufferedImage.TYPE_INT_ARGB);
|
427 | Graphics gTemp = image.createGraphics();
|
||
428 | gTemp.setColor(vp.getBackColor());
|
||
429 | 3372 | fjp | gTemp.fillRect(0,0,getWidth(), getHeight());
|
430 | System.out.println("MapControl resized");
|
||
431 | // image = null;
|
||
432 | 1223 | fernando | vp.setImageSize(new Dimension(getWidth(), getHeight()));
|
433 | 3375 | fjp | getMapContext().getViewPort().setScale(); */
|
434 | 3372 | fjp | // drawMap(true);
|
435 | 1223 | fernando | } |
436 | |||
437 | /**
|
||
438 | * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
|
||
439 | */
|
||
440 | public void componentShown(ComponentEvent e) { |
||
441 | } |
||
442 | |||
443 | /**
|
||
444 | * A?ade un listener de tipo ExceptionListener.
|
||
445 | *
|
||
446 | * @param o ExceptionListener.
|
||
447 | */
|
||
448 | public void addExceptionListener(ExceptionListener o) { |
||
449 | exceptionHandlingSupport.addExceptionListener(o); |
||
450 | } |
||
451 | |||
452 | /**
|
||
453 | * Borra la ExceptioListener que se pasa como par?metro.
|
||
454 | *
|
||
455 | * @param o ExceptionListener.
|
||
456 | *
|
||
457 | * @return True si se borra correctamente.
|
||
458 | */
|
||
459 | public boolean removeExceptionListener(ExceptionListener o) { |
||
460 | return exceptionHandlingSupport.removeExceptionListener(o);
|
||
461 | } |
||
462 | |||
463 | /**
|
||
464 | * Lanza una Excepci?n.
|
||
465 | *
|
||
466 | * @param t Excepci?n.
|
||
467 | */
|
||
468 | private void throwException(Throwable t) { |
||
469 | exceptionHandlingSupport.throwException(t); |
||
470 | } |
||
471 | |||
472 | 3368 | fjp | |
473 | private class PaintingRequest |
||
474 | { |
||
475 | 3372 | fjp | |
476 | public PaintingRequest()
|
||
477 | 3368 | fjp | { |
478 | } |
||
479 | |||
480 | public void paint() |
||
481 | { |
||
482 | try
|
||
483 | { |
||
484 | 3372 | fjp | canceldraw.setCancel(false);
|
485 | /* if (image == null)
|
||
486 | {
|
||
487 | image = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(),
|
||
488 | BufferedImage.TYPE_INT_ARGB);
|
||
489 | Graphics gTemp = image.createGraphics();
|
||
490 | Color theBackColor = vp.getBackColor();
|
||
491 | if (theBackColor == null)
|
||
492 | gTemp.setColor(Color.WHITE);
|
||
493 | else
|
||
494 | gTemp.setColor(theBackColor);
|
||
495 | |||
496 | gTemp.fillRect(0,0,getWidth(), getHeight());
|
||
497 | gTemp.dispose();
|
||
498 | // g.drawImage(image,0,0,null);
|
||
499 | System.out.println("Imagen con null en DESACTUALIZADO. Width = " + this.getWidth());
|
||
500 | } */
|
||
501 | 3368 | fjp | Graphics2D g = image.createGraphics();
|
502 | |||
503 | ViewPort viewPort = mapContext.getViewPort(); |
||
504 | 3372 | fjp | |
505 | 3368 | fjp | if (status == DESACTUALIZADO)
|
506 | { |
||
507 | Color theBackColor = viewPort.getBackColor();
|
||
508 | if (theBackColor == null) |
||
509 | g.setColor(Color.WHITE);
|
||
510 | else
|
||
511 | g.setColor(theBackColor); |
||
512 | g.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight()); |
||
513 | status = ACTUALIZADO; |
||
514 | 3372 | fjp | mapContext.draw(image, g, canceldraw, mapContext.getScaleView()); |
515 | |||
516 | 3368 | fjp | } |
517 | else if (status == ONLY_GRAPHICS) |
||
518 | { |
||
519 | 3372 | fjp | status = ACTUALIZADO; |
520 | mapContext.drawGraphics(image, g, canceldraw,mapContext.getScaleView()); |
||
521 | |||
522 | 3368 | fjp | } |
523 | |||
524 | |||
525 | // status = FAST_PAINT;
|
||
526 | drawerAlive = false;
|
||
527 | 3372 | fjp | timer.stop(); |
528 | 3368 | fjp | repaint(); |
529 | |||
530 | 3372 | fjp | |
531 | 3368 | fjp | } catch (Throwable e) { |
532 | timer.stop(); |
||
533 | isCancelled = true;
|
||
534 | e.printStackTrace(); |
||
535 | throwException(e); |
||
536 | } finally {
|
||
537 | } |
||
538 | |||
539 | } |
||
540 | |||
541 | } |
||
542 | |||
543 | /**
|
||
544 | * @author fjp
|
||
545 | *
|
||
546 | * Basasdo en el patr?n WorkerThread
|
||
547 | *
|
||
548 | */
|
||
549 | public class Drawer2 |
||
550 | { |
||
551 | // Una mini cola de 2. No acumulamos peticiones de dibujado
|
||
552 | // dibujamos solo lo ?ltimo que nos han pedido.
|
||
553 | private PaintingRequest paintingRequest;
|
||
554 | private PaintingRequest waitingRequest;
|
||
555 | |||
556 | private boolean waiting; |
||
557 | private boolean shutdown; |
||
558 | |||
559 | public void setShutdown(boolean isShutdown) |
||
560 | { |
||
561 | shutdown = isShutdown; |
||
562 | } |
||
563 | |||
564 | public Drawer2()
|
||
565 | { |
||
566 | paintingRequest = null;
|
||
567 | waitingRequest = null;
|
||
568 | waiting = false;
|
||
569 | shutdown = false;
|
||
570 | new Thread(new Worker()).start(); |
||
571 | } |
||
572 | |||
573 | public void put(PaintingRequest newPaintRequest) |
||
574 | { |
||
575 | waitingRequest = newPaintRequest; |
||
576 | if (waiting)
|
||
577 | { |
||
578 | synchronized (this) { |
||
579 | notifyAll(); |
||
580 | } |
||
581 | } |
||
582 | } |
||
583 | |||
584 | public PaintingRequest take()
|
||
585 | { |
||
586 | if (waitingRequest == null) |
||
587 | { |
||
588 | synchronized (this) { |
||
589 | waiting = true;
|
||
590 | try {
|
||
591 | wait(); |
||
592 | } |
||
593 | catch (InterruptedException ie) |
||
594 | { |
||
595 | waiting = false;
|
||
596 | } |
||
597 | } |
||
598 | } |
||
599 | paintingRequest = waitingRequest; |
||
600 | waitingRequest = null;
|
||
601 | return paintingRequest;
|
||
602 | } |
||
603 | |||
604 | |||
605 | private class Worker implements Runnable |
||
606 | { |
||
607 | public void run() |
||
608 | { |
||
609 | while (!shutdown)
|
||
610 | { |
||
611 | PaintingRequest p = take(); |
||
612 | System.out.println("Pintando"); |
||
613 | 3372 | fjp | if (image != null) |
614 | p.paint(); |
||
615 | else
|
||
616 | status = DESACTUALIZADO; |
||
617 | 3368 | fjp | } |
618 | } |
||
619 | } |
||
620 | } |
||
621 | |||
622 | 1223 | fernando | /**
|
623 | * Clase utilizada para dibujar las capas.
|
||
624 | *
|
||
625 | * @author Vicente Caballero Navarro
|
||
626 | */
|
||
627 | public class Drawer extends Thread { |
||
628 | 2531 | caballero | //private Graphics g;
|
629 | 3331 | fjp | private BufferedImage image = null; |
630 | 1223 | fernando | private CancelDraw cancel;
|
631 | 2531 | caballero | //private boolean threadCancel = false;
|
632 | 1223 | fernando | |
633 | /**
|
||
634 | * Crea un nuevo Drawer.
|
||
635 | *
|
||
636 | */
|
||
637 | 3331 | fjp | public Drawer(BufferedImage image, CancelDraw cancel) |
638 | { |
||
639 | 1223 | fernando | this.image = image;
|
640 | this.cancel = cancel;
|
||
641 | 3331 | fjp | drawerAlive = true;
|
642 | 1223 | fernando | } |
643 | |||
644 | /**
|
||
645 | * @see java.lang.Runnable#run()
|
||
646 | */
|
||
647 | public void run() { |
||
648 | try {
|
||
649 | 2857 | jaume | // synchronized (Drawer.class) {
|
650 | 1282 | fjp | Graphics2D g = image.createGraphics();
|
651 | 3348 | fjp | |
652 | 1282 | fjp | ViewPort viewPort = mapContext.getViewPort(); |
653 | 2946 | fjp | if (status == DESACTUALIZADO)
|
654 | { |
||
655 | Color theBackColor = viewPort.getBackColor();
|
||
656 | if (theBackColor == null) |
||
657 | g.setColor(Color.WHITE);
|
||
658 | else
|
||
659 | g.setColor(theBackColor); |
||
660 | g.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight()); |
||
661 | status = ACTUALIZADO; |
||
662 | mapContext.draw(image, g, cancel,mapContext.getScaleView()); |
||
663 | } |
||
664 | else if (status == ONLY_GRAPHICS) |
||
665 | { |
||
666 | 2951 | fjp | status = ACTUALIZADO; |
667 | 2946 | fjp | mapContext.drawGraphics(image, g, cancel,mapContext.getScaleView()); |
668 | } |
||
669 | 3331 | fjp | |
670 | 1327 | fjp | timer.stop(); |
671 | 3346 | fjp | // status = FAST_PAINT;
|
672 | 3331 | fjp | drawerAlive = false;
|
673 | 1223 | fernando | repaint(); |
674 | 3331 | fjp | |
675 | |||
676 | 1327 | fjp | |
677 | 2857 | jaume | // }
|
678 | 1223 | fernando | } catch (Throwable e) { |
679 | 1266 | fernando | timer.stop(); |
680 | isCancelled = true;
|
||
681 | 2943 | fjp | e.printStackTrace(); |
682 | 1223 | fernando | throwException(e); |
683 | } finally {
|
||
684 | } |
||
685 | } |
||
686 | } |
||
687 | |||
688 | /**
|
||
689 | * Clase para cancelar el dibujado.
|
||
690 | *
|
||
691 | * @author Fernando Gonz?lez Cort?s
|
||
692 | */
|
||
693 | public class CancelDraw implements Cancellable { |
||
694 | private boolean cancel = false; |
||
695 | |||
696 | /**
|
||
697 | * Crea un nuevo CancelDraw.
|
||
698 | */
|
||
699 | public CancelDraw() {
|
||
700 | } |
||
701 | |||
702 | /**
|
||
703 | * Insertar si se debe cancelar el dibujado.
|
||
704 | *
|
||
705 | * @param b true si se debe cancelar el dibujado.
|
||
706 | */
|
||
707 | public void setCancel(boolean b) { |
||
708 | cancel = b; |
||
709 | } |
||
710 | |||
711 | /**
|
||
712 | * @see com.iver.cit.gvsig.fmap.operations.Cancellable#isCanceled()
|
||
713 | */
|
||
714 | public boolean isCanceled() { |
||
715 | return cancel;
|
||
716 | } |
||
717 | } |
||
718 | |||
719 | /**
|
||
720 | * Listener del MapTool.
|
||
721 | *
|
||
722 | * @author Fernando Gonz?lez Cort?s
|
||
723 | */
|
||
724 | public class MapToolListener implements MouseListener, MouseWheelListener, |
||
725 | MouseMotionListener {
|
||
726 | /**
|
||
727 | * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
||
728 | */
|
||
729 | public void mouseClicked(MouseEvent e) { |
||
730 | try {
|
||
731 | currentMapTool.mouseClicked(e); |
||
732 | } catch (BehaviorException t) {
|
||
733 | throwException(t); |
||
734 | } |
||
735 | } |
||
736 | |||
737 | /**
|
||
738 | * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
||
739 | */
|
||
740 | public void mouseEntered(MouseEvent e) { |
||
741 | try {
|
||
742 | currentMapTool.mouseEntered(e); |
||
743 | } catch (BehaviorException t) {
|
||
744 | throwException(t); |
||
745 | } |
||
746 | } |
||
747 | |||
748 | /**
|
||
749 | * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
||
750 | */
|
||
751 | public void mouseExited(MouseEvent e) { |
||
752 | try {
|
||
753 | currentMapTool.mouseExited(e); |
||
754 | } catch (BehaviorException t) {
|
||
755 | throwException(t); |
||
756 | } |
||
757 | } |
||
758 | |||
759 | /**
|
||
760 | * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
||
761 | */
|
||
762 | public void mousePressed(MouseEvent e) { |
||
763 | try {
|
||
764 | currentMapTool.mousePressed(e); |
||
765 | } catch (BehaviorException t) {
|
||
766 | throwException(t); |
||
767 | } |
||
768 | } |
||
769 | |||
770 | /**
|
||
771 | * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
||
772 | */
|
||
773 | public void mouseReleased(MouseEvent e) { |
||
774 | try {
|
||
775 | currentMapTool.mouseReleased(e); |
||
776 | } catch (BehaviorException t) {
|
||
777 | throwException(t); |
||
778 | } |
||
779 | } |
||
780 | |||
781 | /**
|
||
782 | * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
|
||
783 | */
|
||
784 | public void mouseWheelMoved(MouseWheelEvent e) { |
||
785 | try {
|
||
786 | currentMapTool.mouseWheelMoved(e); |
||
787 | } catch (BehaviorException t) {
|
||
788 | throwException(t); |
||
789 | } |
||
790 | } |
||
791 | |||
792 | /**
|
||
793 | * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
||
794 | */
|
||
795 | public void mouseDragged(MouseEvent e) { |
||
796 | try {
|
||
797 | currentMapTool.mouseDragged(e); |
||
798 | } catch (BehaviorException t) {
|
||
799 | throwException(t); |
||
800 | } |
||
801 | } |
||
802 | |||
803 | /**
|
||
804 | * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
||
805 | */
|
||
806 | public void mouseMoved(MouseEvent e) { |
||
807 | try {
|
||
808 | currentMapTool.mouseMoved(e); |
||
809 | } catch (BehaviorException t) {
|
||
810 | throwException(t); |
||
811 | } |
||
812 | } |
||
813 | } |
||
814 | |||
815 | /**
|
||
816 | * Listener sobre el MapContext.
|
||
817 | *
|
||
818 | * @author Fernando Gonz?lez Cort?s
|
||
819 | */
|
||
820 | public class MapContextListener implements AtomicEventListener { |
||
821 | /**
|
||
822 | * @see com.iver.cit.gvsig.fmap.AtomicEventListener#atomicEvent(com.iver.cit.gvsig.fmap.AtomicEvent)
|
||
823 | */
|
||
824 | public void atomicEvent(AtomicEvent e) { |
||
825 | boolean redraw = false; |
||
826 | LayerEvent[] layerEvents = e.getLayerEvents();
|
||
827 | |||
828 | for (int i = 0; i < layerEvents.length; i++) { |
||
829 | if (layerEvents[i].getProperty().equals("visible")) { |
||
830 | redraw = true;
|
||
831 | } |
||
832 | } |
||
833 | |||
834 | if (e.getColorEvents().length > 0) { |
||
835 | redraw = true;
|
||
836 | } |
||
837 | |||
838 | if (e.getExtentEvents().length > 0) { |
||
839 | redraw = true;
|
||
840 | } |
||
841 | |||
842 | if (e.getLayerCollectionEvents().length > 0) { |
||
843 | redraw = true;
|
||
844 | } |
||
845 | |||
846 | if (e.getLegendEvents().length > 0) { |
||
847 | redraw = true;
|
||
848 | } |
||
849 | |||
850 | if (e.getSelectionEvents().length > 0) { |
||
851 | redraw = true;
|
||
852 | } |
||
853 | |||
854 | if (redraw) {
|
||
855 | 3331 | fjp | System.out.println("MapContextListener redraw"); |
856 | 3422 | fjp | MapControl.this.drawMap(false);
|
857 | 1223 | fernando | } |
858 | } |
||
859 | } |
||
860 | public ViewPort getViewPort() {
|
||
861 | return vp;
|
||
862 | } |
||
863 | 2877 | caballero | |
864 | /**
|
||
865 | * Returns a HashMap with the tools that have been registered
|
||
866 | * in the Mapcontrol.
|
||
867 | */
|
||
868 | 2876 | caballero | public HashMap getNamesMapTools() { |
869 | return namesMapTools;
|
||
870 | } |
||
871 | 3331 | fjp | |
872 | 1223 | fernando | } |