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