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