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