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