svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / MapControl.java @ 1223
History | View | Annotate | Download (14.2 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.fmap; |
42 |
|
43 |
import java.awt.Dimension; |
44 |
import java.awt.Graphics; |
45 |
import java.awt.Graphics2D; |
46 |
import java.awt.event.ActionEvent; |
47 |
import java.awt.event.ActionListener; |
48 |
import java.awt.event.ComponentEvent; |
49 |
import java.awt.event.ComponentListener; |
50 |
import java.awt.event.MouseEvent; |
51 |
import java.awt.event.MouseListener; |
52 |
import java.awt.event.MouseMotionListener; |
53 |
import java.awt.event.MouseWheelEvent; |
54 |
import java.awt.event.MouseWheelListener; |
55 |
import java.awt.image.BufferedImage; |
56 |
import java.util.HashMap; |
57 |
|
58 |
import javax.swing.JComponent; |
59 |
import javax.swing.Timer; |
60 |
|
61 |
import org.apache.log4j.Logger; |
62 |
import org.cresques.cts.IProjection; |
63 |
import org.cresques.cts.ProjectionPool; |
64 |
|
65 |
import com.iver.cit.gvsig.fmap.layers.LayerEvent; |
66 |
import com.iver.cit.gvsig.fmap.operations.Cancellable; |
67 |
import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
68 |
import com.iver.cit.gvsig.fmap.tools.CompoundBehavior; |
69 |
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior; |
70 |
import com.iver.utiles.exceptionHandling.ExceptionHandlingSupport; |
71 |
import com.iver.utiles.exceptionHandling.ExceptionListener; |
72 |
|
73 |
|
74 |
/**
|
75 |
* MapControl.
|
76 |
*
|
77 |
* @author Fernando Gonz?lez Cort?s
|
78 |
*/
|
79 |
public class MapControl extends JComponent implements ComponentListener { |
80 |
/** Cuando la vista est? actualizada. */
|
81 |
public static final int ACTUALIZADO = 0; |
82 |
|
83 |
/** Cuando la vista est? desactualizada. */
|
84 |
public static final int DESACTUALIZADO = 1; |
85 |
private static Logger logger = Logger.getLogger(MapControl.class.getName()); |
86 |
private FMap mapContext = null; |
87 |
private HashMap namesMapTools = new HashMap(); |
88 |
private Behavior currentMapTool = null; |
89 |
private int status = DESACTUALIZADO; |
90 |
private BufferedImage image = null; |
91 |
private String currentTool; |
92 |
private CancelDraw canceldraw;
|
93 |
private boolean isCancelled = true; |
94 |
private Timer timer; |
95 |
protected ViewPort vp;
|
96 |
private Drawer drawer;
|
97 |
private MapToolListener mapToolListener = new MapToolListener(); |
98 |
private MapContextListener mapContextListener = new MapContextListener(); |
99 |
private ExceptionHandlingSupport exceptionHandlingSupport = new ExceptionHandlingSupport(); |
100 |
|
101 |
/**
|
102 |
* Crea un nuevo NewMapControl.
|
103 |
*/
|
104 |
public MapControl() {
|
105 |
setDoubleBuffered(false);
|
106 |
setOpaque(true);
|
107 |
status = DESACTUALIZADO; |
108 |
|
109 |
//Clase usada para cancelar el dibujado
|
110 |
canceldraw = new CancelDraw();
|
111 |
|
112 |
//Modelo de datos y ventana del mismo
|
113 |
// TODO: Cuando creamos un mapControl, deber?amos asignar
|
114 |
// la projecci?n por defecto con la que vayamos a trabajar.
|
115 |
// 23030 es el c?digo EPSG del UTM30 elipsoide ED50
|
116 |
vp = new ViewPort(ProjectionPool.get("EPSG:23030")); |
117 |
setMapContext(new FMap(vp));
|
118 |
|
119 |
//eventos
|
120 |
this.addComponentListener(this); |
121 |
this.addMouseListener(mapToolListener);
|
122 |
this.addMouseMotionListener(mapToolListener);
|
123 |
this.addMouseWheelListener(mapToolListener);
|
124 |
|
125 |
//Timer para mostrar el redibujado mientras se dibuja
|
126 |
timer = new Timer(300, |
127 |
new ActionListener() { |
128 |
public void actionPerformed(ActionEvent e) { |
129 |
MapControl.this.repaint(); |
130 |
} |
131 |
}); |
132 |
} |
133 |
|
134 |
/**
|
135 |
* Inserta el modelo.
|
136 |
*
|
137 |
* @param model FMap.
|
138 |
*/
|
139 |
public void setMapContext(FMap model) { |
140 |
if (mapContext != null) { |
141 |
mapContext.removeAtomicEventListener(mapContextListener); |
142 |
} |
143 |
|
144 |
mapContext = model; |
145 |
|
146 |
if (mapContext.getViewPort() == null) { |
147 |
mapContext.setViewPort(vp); |
148 |
} else {
|
149 |
vp = mapContext.getViewPort(); |
150 |
|
151 |
// vp.setImageSize(new Dimension(getWidth(), getHeight()));
|
152 |
//System.err.println("Viewport en setMapContext:" + vp);
|
153 |
} |
154 |
|
155 |
mapContext.addAtomicEventListener(mapContextListener); |
156 |
|
157 |
status = DESACTUALIZADO; |
158 |
} |
159 |
|
160 |
/**
|
161 |
* Devuelve la proyecci?n.
|
162 |
*
|
163 |
* @return Proyecci?n.
|
164 |
*/
|
165 |
public IProjection getProjection() {
|
166 |
return getMapContext().getProjection();
|
167 |
} |
168 |
|
169 |
/**
|
170 |
* Inserta una proyecci?n.
|
171 |
*
|
172 |
* @param proj Proyecci?n.
|
173 |
*/
|
174 |
public void setProjection(IProjection proj) { |
175 |
getMapContext().setProjection(proj); |
176 |
} |
177 |
|
178 |
/**
|
179 |
* Devuelve el modelo.
|
180 |
*
|
181 |
* @return FMap.
|
182 |
*/
|
183 |
public FMap getMapContext() {
|
184 |
return mapContext;
|
185 |
} |
186 |
|
187 |
/**
|
188 |
* Registra una herramienta (tool).
|
189 |
*
|
190 |
* @param name Nombre de la herramienta.
|
191 |
* @param tool Herramienta.
|
192 |
*/
|
193 |
public void addMapTool(String name, Behavior tool) { |
194 |
namesMapTools.put(name, tool); |
195 |
tool.setMapControl(this);
|
196 |
} |
197 |
|
198 |
public void addMapTool(String name, Behavior[] tools){ |
199 |
CompoundBehavior tool = new CompoundBehavior(tools);
|
200 |
addMapTool(name, tool); |
201 |
} |
202 |
|
203 |
/**
|
204 |
* DOCUMENT ME!
|
205 |
*
|
206 |
* @param toolName DOCUMENT ME!
|
207 |
*/
|
208 |
public void setTool(String toolName) { |
209 |
Behavior mapTool = (Behavior) namesMapTools.get(toolName); |
210 |
currentMapTool = mapTool; |
211 |
currentTool = toolName; |
212 |
this.setCursor(mapTool.getCursor());
|
213 |
} |
214 |
|
215 |
/**
|
216 |
* Devuelve el nombre de la herramienta seleccionada.
|
217 |
*
|
218 |
* @return nombre.
|
219 |
*/
|
220 |
public String getTool() { |
221 |
return currentTool;
|
222 |
} |
223 |
|
224 |
/**
|
225 |
* Cancela el dibujado. Se espera a que la cancelaci?n surta efecto
|
226 |
*/
|
227 |
public void cancelDrawing() { |
228 |
if (drawer != null) { |
229 |
if (!drawer.isAlive()) {
|
230 |
return;
|
231 |
} |
232 |
} |
233 |
|
234 |
canceldraw.setCancel(true);
|
235 |
|
236 |
while (!isCancelled) {
|
237 |
} |
238 |
|
239 |
canceldraw.setCancel(false);
|
240 |
isCancelled = false;
|
241 |
} |
242 |
|
243 |
/**
|
244 |
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
|
245 |
*/
|
246 |
protected void paintComponent(Graphics g) { |
247 |
|
248 |
if (status == ACTUALIZADO) {
|
249 |
logger.debug("Dibujando la imagen obtenida");
|
250 |
|
251 |
/*
|
252 |
* Si hay un behaviour y la imagen es distinta de null se delega el dibujado
|
253 |
* en dicho behaviour
|
254 |
*/
|
255 |
if ((currentMapTool != null) && (image != null)) { |
256 |
currentMapTool.paintComponent(g); |
257 |
System.out.println("Pinto ACTUALIZADO"); |
258 |
} |
259 |
} else if (status == DESACTUALIZADO) { |
260 |
System.out.println("DESACTUALIZADO: Obteniendo la imagen de la cartograf?a"); |
261 |
/* if (isOpaque())
|
262 |
{
|
263 |
if (image==null)
|
264 |
{
|
265 |
g.setColor(vp.getBackColor());
|
266 |
g.fillRect(0,0,getWidth(), getHeight());
|
267 |
}
|
268 |
// else g.drawImage(image,0,0,null);
|
269 |
} */
|
270 |
|
271 |
//Se cancela el dibujado anterior
|
272 |
cancelDrawing(); |
273 |
|
274 |
//Se crea la imagen con el color de fonde deseado
|
275 |
if (image == null) |
276 |
{ |
277 |
image = new BufferedImage(this.getWidth(), this.getHeight(), |
278 |
BufferedImage.TYPE_INT_ARGB);
|
279 |
vp.setImageSize(new Dimension(getWidth(), getHeight())); |
280 |
Graphics gTemp = image.createGraphics();
|
281 |
gTemp.setColor(vp.getBackColor()); |
282 |
gTemp.fillRect(0,0,getWidth(), getHeight()); |
283 |
gTemp.dispose(); |
284 |
// g.drawImage(image,0,0,null);
|
285 |
System.out.println("Imagen con null en DESACTUALIZADO"); |
286 |
} |
287 |
g.drawImage(image,0,0,null); |
288 |
|
289 |
Graphics imgg = image.createGraphics();
|
290 |
|
291 |
//Se lanza el tread de dibujado
|
292 |
drawer = new Drawer(image, (Graphics2D) imgg, canceldraw); |
293 |
drawer.start(); |
294 |
timer.start(); |
295 |
status = ACTUALIZADO; |
296 |
|
297 |
// repaint();
|
298 |
} |
299 |
} |
300 |
|
301 |
/**
|
302 |
* Devuelve la imagen de la vista.
|
303 |
*
|
304 |
* @return imagen.
|
305 |
*/
|
306 |
public BufferedImage getImage() { |
307 |
return image;
|
308 |
} |
309 |
|
310 |
/**
|
311 |
* Marca el mapa para que en el pr?ximo redibujado se acceda a la
|
312 |
* cartograf?a para reobtener la imagen
|
313 |
* @param doClear TODO
|
314 |
*/
|
315 |
public void drawMap(boolean doClear) { |
316 |
status = DESACTUALIZADO; |
317 |
if (doClear) image = null; // Se usa para el PAN |
318 |
repaint(); |
319 |
} |
320 |
|
321 |
/**
|
322 |
* @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
|
323 |
*/
|
324 |
public void componentHidden(ComponentEvent e) { |
325 |
} |
326 |
|
327 |
/**
|
328 |
* @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
|
329 |
*/
|
330 |
public void componentMoved(ComponentEvent e) { |
331 |
} |
332 |
|
333 |
/**
|
334 |
* @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
|
335 |
*/
|
336 |
public void componentResized(ComponentEvent e) { |
337 |
/* image = new BufferedImage(this.getWidth(), this.getHeight(),
|
338 |
BufferedImage.TYPE_INT_ARGB);
|
339 |
Graphics gTemp = image.createGraphics();
|
340 |
gTemp.setColor(vp.getBackColor());
|
341 |
gTemp.fillRect(0,0,getWidth(), getHeight()); */
|
342 |
image = null;
|
343 |
vp.setImageSize(new Dimension(getWidth(), getHeight())); |
344 |
getMapContext().getViewPort().setScale(); |
345 |
drawMap(false);
|
346 |
} |
347 |
|
348 |
/**
|
349 |
* @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
|
350 |
*/
|
351 |
public void componentShown(ComponentEvent e) { |
352 |
} |
353 |
|
354 |
/**
|
355 |
* A?ade un listener de tipo ExceptionListener.
|
356 |
*
|
357 |
* @param o ExceptionListener.
|
358 |
*/
|
359 |
public void addExceptionListener(ExceptionListener o) { |
360 |
exceptionHandlingSupport.addExceptionListener(o); |
361 |
} |
362 |
|
363 |
/**
|
364 |
* Borra la ExceptioListener que se pasa como par?metro.
|
365 |
*
|
366 |
* @param o ExceptionListener.
|
367 |
*
|
368 |
* @return True si se borra correctamente.
|
369 |
*/
|
370 |
public boolean removeExceptionListener(ExceptionListener o) { |
371 |
return exceptionHandlingSupport.removeExceptionListener(o);
|
372 |
} |
373 |
|
374 |
/**
|
375 |
* Lanza una Excepci?n.
|
376 |
*
|
377 |
* @param t Excepci?n.
|
378 |
*/
|
379 |
private void throwException(Throwable t) { |
380 |
exceptionHandlingSupport.throwException(t); |
381 |
} |
382 |
|
383 |
/**
|
384 |
* Clase utilizada para dibujar las capas.
|
385 |
*
|
386 |
* @author Vicente Caballero Navarro
|
387 |
*/
|
388 |
public class Drawer extends Thread { |
389 |
private Graphics g; |
390 |
private BufferedImage image; |
391 |
private CancelDraw cancel;
|
392 |
private boolean threadCancel = false; |
393 |
|
394 |
/**
|
395 |
* Crea un nuevo Drawer.
|
396 |
*
|
397 |
* @param image
|
398 |
* @param g
|
399 |
* @param cancel
|
400 |
*/
|
401 |
public Drawer(BufferedImage image, Graphics g, CancelDraw cancel) { |
402 |
this.g = g;
|
403 |
this.image = image;
|
404 |
this.cancel = cancel;
|
405 |
} |
406 |
|
407 |
/**
|
408 |
* @see java.lang.Runnable#run()
|
409 |
*/
|
410 |
public void run() { |
411 |
try {
|
412 |
synchronized (Drawer.class) {
|
413 |
mapContext.draw(image, (Graphics2D) image.getGraphics(),
|
414 |
cancel); |
415 |
timer.stop(); |
416 |
isCancelled = true;
|
417 |
repaint(); |
418 |
} |
419 |
} catch (Throwable e) { |
420 |
throwException(e); |
421 |
} finally {
|
422 |
} |
423 |
} |
424 |
} |
425 |
|
426 |
/**
|
427 |
* Clase para cancelar el dibujado.
|
428 |
*
|
429 |
* @author Fernando Gonz?lez Cort?s
|
430 |
*/
|
431 |
public class CancelDraw implements Cancellable { |
432 |
private boolean cancel = false; |
433 |
|
434 |
/**
|
435 |
* Crea un nuevo CancelDraw.
|
436 |
*/
|
437 |
public CancelDraw() {
|
438 |
} |
439 |
|
440 |
/**
|
441 |
* Insertar si se debe cancelar el dibujado.
|
442 |
*
|
443 |
* @param b true si se debe cancelar el dibujado.
|
444 |
*/
|
445 |
public void setCancel(boolean b) { |
446 |
cancel = b; |
447 |
} |
448 |
|
449 |
/**
|
450 |
* @see com.iver.cit.gvsig.fmap.operations.Cancellable#isCanceled()
|
451 |
*/
|
452 |
public boolean isCanceled() { |
453 |
return cancel;
|
454 |
} |
455 |
} |
456 |
|
457 |
/**
|
458 |
* Listener del MapTool.
|
459 |
*
|
460 |
* @author Fernando Gonz?lez Cort?s
|
461 |
*/
|
462 |
public class MapToolListener implements MouseListener, MouseWheelListener, |
463 |
MouseMotionListener {
|
464 |
/**
|
465 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
466 |
*/
|
467 |
public void mouseClicked(MouseEvent e) { |
468 |
try {
|
469 |
currentMapTool.mouseClicked(e); |
470 |
} catch (BehaviorException t) {
|
471 |
throwException(t); |
472 |
} |
473 |
} |
474 |
|
475 |
/**
|
476 |
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
477 |
*/
|
478 |
public void mouseEntered(MouseEvent e) { |
479 |
try {
|
480 |
currentMapTool.mouseEntered(e); |
481 |
} catch (BehaviorException t) {
|
482 |
throwException(t); |
483 |
} |
484 |
} |
485 |
|
486 |
/**
|
487 |
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
488 |
*/
|
489 |
public void mouseExited(MouseEvent e) { |
490 |
try {
|
491 |
currentMapTool.mouseExited(e); |
492 |
} catch (BehaviorException t) {
|
493 |
throwException(t); |
494 |
} |
495 |
} |
496 |
|
497 |
/**
|
498 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
499 |
*/
|
500 |
public void mousePressed(MouseEvent e) { |
501 |
try {
|
502 |
currentMapTool.mousePressed(e); |
503 |
} catch (BehaviorException t) {
|
504 |
throwException(t); |
505 |
} |
506 |
} |
507 |
|
508 |
/**
|
509 |
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
510 |
*/
|
511 |
public void mouseReleased(MouseEvent e) { |
512 |
try {
|
513 |
currentMapTool.mouseReleased(e); |
514 |
} catch (BehaviorException t) {
|
515 |
throwException(t); |
516 |
} |
517 |
} |
518 |
|
519 |
/**
|
520 |
* @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
|
521 |
*/
|
522 |
public void mouseWheelMoved(MouseWheelEvent e) { |
523 |
try {
|
524 |
currentMapTool.mouseWheelMoved(e); |
525 |
} catch (BehaviorException t) {
|
526 |
throwException(t); |
527 |
} |
528 |
} |
529 |
|
530 |
/**
|
531 |
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
532 |
*/
|
533 |
public void mouseDragged(MouseEvent e) { |
534 |
try {
|
535 |
currentMapTool.mouseDragged(e); |
536 |
} catch (BehaviorException t) {
|
537 |
throwException(t); |
538 |
} |
539 |
} |
540 |
|
541 |
/**
|
542 |
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
543 |
*/
|
544 |
public void mouseMoved(MouseEvent e) { |
545 |
try {
|
546 |
currentMapTool.mouseMoved(e); |
547 |
} catch (BehaviorException t) {
|
548 |
throwException(t); |
549 |
} |
550 |
} |
551 |
} |
552 |
|
553 |
/**
|
554 |
* Listener sobre el MapContext.
|
555 |
*
|
556 |
* @author Fernando Gonz?lez Cort?s
|
557 |
*/
|
558 |
public class MapContextListener implements AtomicEventListener { |
559 |
/**
|
560 |
* @see com.iver.cit.gvsig.fmap.AtomicEventListener#atomicEvent(com.iver.cit.gvsig.fmap.AtomicEvent)
|
561 |
*/
|
562 |
public void atomicEvent(AtomicEvent e) { |
563 |
boolean redraw = false; |
564 |
LayerEvent[] layerEvents = e.getLayerEvents();
|
565 |
|
566 |
for (int i = 0; i < layerEvents.length; i++) { |
567 |
if (layerEvents[i].getProperty().equals("visible")) { |
568 |
redraw = true;
|
569 |
} |
570 |
} |
571 |
|
572 |
if (e.getColorEvents().length > 0) { |
573 |
redraw = true;
|
574 |
} |
575 |
|
576 |
if (e.getExtentEvents().length > 0) { |
577 |
redraw = true;
|
578 |
} |
579 |
|
580 |
if (e.getLayerCollectionEvents().length > 0) { |
581 |
redraw = true;
|
582 |
} |
583 |
|
584 |
if (e.getLegendEvents().length > 0) { |
585 |
redraw = true;
|
586 |
} |
587 |
|
588 |
if (e.getSelectionEvents().length > 0) { |
589 |
redraw = true;
|
590 |
} |
591 |
|
592 |
if (redraw) {
|
593 |
MapControl.this.drawMap(false);
|
594 |
} |
595 |
} |
596 |
} |
597 |
public ViewPort getViewPort() {
|
598 |
return vp;
|
599 |
} |
600 |
} |