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