root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / NewMapControl.java @ 1062
History | View | Annotate | Download (11.9 KB)
1 | 230 | fernando | package com.iver.cit.gvsig.fmap; |
---|---|---|---|
2 | |||
3 | import java.awt.Dimension; |
||
4 | import java.awt.Graphics; |
||
5 | import java.awt.Graphics2D; |
||
6 | 336 | vcaballero | import java.awt.event.ActionEvent; |
7 | import java.awt.event.ActionListener; |
||
8 | 230 | fernando | import java.awt.event.ComponentEvent; |
9 | import java.awt.event.ComponentListener; |
||
10 | 453 | fernando | import java.awt.event.MouseEvent; |
11 | import java.awt.event.MouseListener; |
||
12 | import java.awt.event.MouseMotionListener; |
||
13 | import java.awt.event.MouseWheelEvent; |
||
14 | import java.awt.event.MouseWheelListener; |
||
15 | 230 | fernando | import java.awt.image.BufferedImage; |
16 | import java.util.HashMap; |
||
17 | |||
18 | import javax.swing.JComponent; |
||
19 | 338 | fernando | import javax.swing.Timer; |
20 | 230 | fernando | |
21 | 1062 | fernando | import org.apache.log4j.Logger; |
22 | import org.cresques.cts.IProjection; |
||
23 | import org.cresques.cts.ProjectionPool; |
||
24 | 230 | fernando | |
25 | 1062 | fernando | import com.iver.cit.gvsig.fmap.layers.LayerEvent; |
26 | import com.iver.cit.gvsig.fmap.operations.Cancellable; |
||
27 | import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
||
28 | import com.iver.cit.gvsig.fmap.tools.CompoundBehavior; |
||
29 | import com.iver.cit.gvsig.fmap.tools.Behavior.MapTool; |
||
30 | import com.iver.utiles.exceptionHandling.ExceptionHandlingSupport; |
||
31 | import com.iver.utiles.exceptionHandling.ExceptionListener; |
||
32 | |||
33 | |||
34 | 230 | fernando | /**
|
35 | 1051 | vcaballero | * MapControl.
|
36 | 230 | fernando | *
|
37 | * @author Fernando Gonz?lez Cort?s
|
||
38 | */
|
||
39 | public class NewMapControl extends JComponent implements ComponentListener { |
||
40 | 1051 | vcaballero | /** Cuando la vista est? actualizada. */
|
41 | 984 | fernando | public static final int ACTUALIZADO = 0; |
42 | 230 | fernando | |
43 | 1051 | vcaballero | /** Cuando la vista est? desactualizada. */
|
44 | 984 | fernando | public static final int DESACTUALIZADO = 1; |
45 | private static Logger logger = Logger.getLogger(NewMapControl.class.getName()); |
||
46 | private FMap mapContext = null; |
||
47 | private HashMap namesMapTools = new HashMap(); |
||
48 | private MapTool currentMapTool = null; |
||
49 | private int status = ACTUALIZADO; |
||
50 | private BufferedImage image = null; |
||
51 | private String currentTool; |
||
52 | private CancelDraw canceldraw;
|
||
53 | private boolean isCancelled = true; |
||
54 | private Timer timer; |
||
55 | protected ViewPort vp;
|
||
56 | private Drawer drawer;
|
||
57 | private MapToolListener mapToolListener = new MapToolListener(); |
||
58 | private MapContextListener mapContextListener = new MapContextListener(); |
||
59 | private ExceptionHandlingSupport exceptionHandlingSupport = new ExceptionHandlingSupport(); |
||
60 | 490 | fernando | |
61 | 984 | fernando | /**
|
62 | * Crea un nuevo NewMapControl.
|
||
63 | */
|
||
64 | public NewMapControl() {
|
||
65 | setDoubleBuffered(true);
|
||
66 | 453 | fernando | |
67 | 984 | fernando | //Clase usada para cancelar el dibujado
|
68 | canceldraw = new CancelDraw();
|
||
69 | 338 | fernando | |
70 | 984 | fernando | //Modelo de datos y ventana del mismo
|
71 | // TODO: Cuando creamos un mapControl, deber?amos asignar
|
||
72 | // la projecci?n por defecto con la que vayamos a trabajar.
|
||
73 | // 23030 es el c?digo EPSG del UTM30 elipsoide ED50
|
||
74 | vp = new ViewPort(ProjectionPool.get("23030")); |
||
75 | setMapContext(new FMap(vp));
|
||
76 | 230 | fernando | |
77 | 984 | fernando | //eventos
|
78 | this.addComponentListener(this); |
||
79 | this.addMouseListener(mapToolListener);
|
||
80 | this.addMouseMotionListener(mapToolListener);
|
||
81 | this.addMouseWheelListener(mapToolListener);
|
||
82 | 453 | fernando | |
83 | 984 | fernando | //Timer para mostrar el redibujado mientras se dibuja
|
84 | timer = new Timer(300, |
||
85 | new ActionListener() { |
||
86 | public void actionPerformed(ActionEvent e) { |
||
87 | NewMapControl.this.repaint(); |
||
88 | } |
||
89 | }); |
||
90 | } |
||
91 | 230 | fernando | |
92 | 984 | fernando | /**
|
93 | 1051 | vcaballero | * Inserta el modelo.
|
94 | 984 | fernando | *
|
95 | 1051 | vcaballero | * @param model FMap.
|
96 | 984 | fernando | */
|
97 | public void setMapContext(FMap model) { |
||
98 | if (mapContext != null) { |
||
99 | mapContext.removeAtomicEventListener(mapContextListener); |
||
100 | } |
||
101 | 490 | fernando | |
102 | 984 | fernando | mapContext = model; |
103 | 490 | fernando | |
104 | 984 | fernando | if (mapContext.getViewPort() == null) { |
105 | mapContext.setViewPort(vp); |
||
106 | } else {
|
||
107 | vp = mapContext.getViewPort(); |
||
108 | |||
109 | // vp.setImageSize(new Dimension(getWidth(), getHeight()));
|
||
110 | 1003 | vcaballero | //System.err.println("Viewport en setMapContext:" + vp);
|
111 | 984 | fernando | } |
112 | |||
113 | mapContext.addAtomicEventListener(mapContextListener); |
||
114 | |||
115 | status = DESACTUALIZADO; |
||
116 | } |
||
117 | |||
118 | /**
|
||
119 | 1051 | vcaballero | * Devuelve la proyecci?n.
|
120 | 984 | fernando | *
|
121 | 1051 | vcaballero | * @return Proyecci?n.
|
122 | 984 | fernando | */
|
123 | 885 | fjp | public IProjection getProjection() {
|
124 | return getMapContext().getProjection();
|
||
125 | } |
||
126 | 984 | fernando | |
127 | /**
|
||
128 | 1051 | vcaballero | * Inserta una proyecci?n.
|
129 | 984 | fernando | *
|
130 | 1051 | vcaballero | * @param proj Proyecci?n.
|
131 | 984 | fernando | */
|
132 | public void setProjection(IProjection proj) { |
||
133 | 885 | fjp | getMapContext().setProjection(proj); |
134 | } |
||
135 | 230 | fernando | |
136 | 984 | fernando | /**
|
137 | 1051 | vcaballero | * Devuelve el modelo.
|
138 | 984 | fernando | *
|
139 | 1051 | vcaballero | * @return FMap.
|
140 | 984 | fernando | */
|
141 | public FMap getMapContext() {
|
||
142 | return mapContext;
|
||
143 | } |
||
144 | 230 | fernando | |
145 | 984 | fernando | /**
|
146 | * Registra una herramienta (tool).
|
||
147 | *
|
||
148 | 1051 | vcaballero | * @param name Nombre de la herramienta.
|
149 | * @param tool Herramienta.
|
||
150 | 984 | fernando | */
|
151 | public void addMapTool(String name, MapTool tool) { |
||
152 | namesMapTools.put(name, tool); |
||
153 | tool.setMapControl(this);
|
||
154 | } |
||
155 | 230 | fernando | |
156 | 1062 | fernando | public void addMapTool(String name, MapTool[] tools){ |
157 | CompoundBehavior tool = new CompoundBehavior(tools);
|
||
158 | addMapTool(name, tool); |
||
159 | 984 | fernando | } |
160 | 1062 | fernando | |
161 | 984 | fernando | /**
|
162 | 1062 | fernando | * DOCUMENT ME!
|
163 | 984 | fernando | *
|
164 | 1062 | fernando | * @param toolName DOCUMENT ME!
|
165 | 1003 | vcaballero | */
|
166 | 984 | fernando | public void setTool(String toolName) { |
167 | 1062 | fernando | MapTool mapTool = (MapTool) namesMapTools.get(toolName); |
168 | 984 | fernando | currentMapTool = mapTool; |
169 | currentTool = toolName; |
||
170 | 1062 | fernando | this.setCursor(mapTool.getCursor());
|
171 | 984 | fernando | } |
172 | 330 | vcaballero | |
173 | 984 | fernando | /**
|
174 | 1051 | vcaballero | * Devuelve el nombre de la herramienta seleccionada.
|
175 | 984 | fernando | *
|
176 | 1051 | vcaballero | * @return nombre.
|
177 | 984 | fernando | */
|
178 | public String getTool() { |
||
179 | return currentTool;
|
||
180 | } |
||
181 | 358 | fernando | |
182 | 984 | fernando | /**
|
183 | * Cancela el dibujado. Se espera a que la cancelaci?n surta efecto
|
||
184 | */
|
||
185 | public void cancelDrawing() { |
||
186 | if (drawer != null) { |
||
187 | if (!drawer.isAlive()) {
|
||
188 | return;
|
||
189 | } |
||
190 | } |
||
191 | 358 | fernando | |
192 | 984 | fernando | canceldraw.setCancel(true);
|
193 | 358 | fernando | |
194 | 984 | fernando | while (!isCancelled) {
|
195 | } |
||
196 | 330 | vcaballero | |
197 | 984 | fernando | canceldraw.setCancel(false);
|
198 | isCancelled = false;
|
||
199 | } |
||
200 | 453 | fernando | |
201 | 984 | fernando | /**
|
202 | * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
|
||
203 | */
|
||
204 | protected void paintComponent(Graphics g) { |
||
205 | 1051 | vcaballero | if (status == ACTUALIZADO) {
|
206 | 984 | fernando | logger.debug("Dibujando la imagen obtenida");
|
207 | 330 | vcaballero | |
208 | 1051 | vcaballero | /*
|
209 | 984 | fernando | * Si hay un behaviour y la imagen es distinta de null se delega el dibujado
|
210 | 1051 | vcaballero | * en dicho behaviour
|
211 | 984 | fernando | */
|
212 | if ((currentMapTool != null) && (image != null)) { |
||
213 | currentMapTool.paintComponent(g); |
||
214 | } |
||
215 | } else if (status == DESACTUALIZADO) { |
||
216 | logger.debug("Obteniendo la imagen de la cartograf?a");
|
||
217 | 330 | vcaballero | |
218 | 984 | fernando | //Se cancela el dibujado anterior
|
219 | cancelDrawing(); |
||
220 | 1051 | vcaballero | |
221 | 984 | fernando | //Se crea la imagen con el color de fonde deseado
|
222 | image = new BufferedImage(this.getWidth(), this.getHeight(), |
||
223 | BufferedImage.TYPE_INT_ARGB);
|
||
224 | 1051 | vcaballero | |
225 | 984 | fernando | Graphics imgg = image.createGraphics();
|
226 | 330 | vcaballero | |
227 | 984 | fernando | //Se actualizan los datos del viewPort y se lanza el tread de dibujado
|
228 | vp.setImageSize(new Dimension(getWidth(), getHeight())); |
||
229 | drawer = new Drawer(image, (Graphics2D) imgg, canceldraw); |
||
230 | drawer.start(); |
||
231 | timer.start(); |
||
232 | status = ACTUALIZADO; |
||
233 | 1051 | vcaballero | |
234 | 990 | fjp | // repaint();
|
235 | 984 | fernando | } |
236 | } |
||
237 | 330 | vcaballero | |
238 | 984 | fernando | /**
|
239 | 1051 | vcaballero | * Devuelve la imagen de la vista.
|
240 | 984 | fernando | *
|
241 | 1051 | vcaballero | * @return imagen.
|
242 | 984 | fernando | */
|
243 | public BufferedImage getImage() { |
||
244 | return image;
|
||
245 | } |
||
246 | 980 | fjp | |
247 | 984 | fernando | /**
|
248 | * Marca el mapa para que en el pr?ximo redibujado se acceda a la
|
||
249 | * cartograf?a para reobtener la imagen
|
||
250 | */
|
||
251 | public void drawMap() { |
||
252 | status = DESACTUALIZADO; |
||
253 | repaint(); |
||
254 | } |
||
255 | 330 | vcaballero | |
256 | 984 | fernando | /**
|
257 | * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
|
||
258 | */
|
||
259 | public void componentHidden(ComponentEvent e) { |
||
260 | } |
||
261 | 330 | vcaballero | |
262 | 984 | fernando | /**
|
263 | * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
|
||
264 | */
|
||
265 | public void componentMoved(ComponentEvent e) { |
||
266 | } |
||
267 | 338 | fernando | |
268 | 984 | fernando | /**
|
269 | * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
|
||
270 | */
|
||
271 | public void componentResized(ComponentEvent e) { |
||
272 | drawMap(); |
||
273 | } |
||
274 | 980 | fjp | |
275 | 984 | fernando | /**
|
276 | * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
|
||
277 | */
|
||
278 | public void componentShown(ComponentEvent e) { |
||
279 | } |
||
280 | 330 | vcaballero | |
281 | 984 | fernando | /**
|
282 | 1051 | vcaballero | * A?ade un listener de tipo ExceptionListener.
|
283 | 984 | fernando | *
|
284 | 1051 | vcaballero | * @param o ExceptionListener.
|
285 | 984 | fernando | */
|
286 | public void addExceptionListener(ExceptionListener o) { |
||
287 | exceptionHandlingSupport.addExceptionListener(o); |
||
288 | } |
||
289 | |||
290 | /**
|
||
291 | 1051 | vcaballero | * Borra la ExceptioListener que se pasa como par?metro.
|
292 | 984 | fernando | *
|
293 | 1051 | vcaballero | * @param o ExceptionListener.
|
294 | 984 | fernando | *
|
295 | 1051 | vcaballero | * @return True si se borra correctamente.
|
296 | 984 | fernando | */
|
297 | public boolean removeExceptionListener(ExceptionListener o) { |
||
298 | return exceptionHandlingSupport.removeExceptionListener(o);
|
||
299 | } |
||
300 | |||
301 | /**
|
||
302 | 1051 | vcaballero | * Lanza una Excepci?n.
|
303 | 984 | fernando | *
|
304 | 1051 | vcaballero | * @param t Excepci?n.
|
305 | 984 | fernando | */
|
306 | private void throwException(Throwable t) { |
||
307 | exceptionHandlingSupport.throwException(t); |
||
308 | } |
||
309 | |||
310 | /**
|
||
311 | 1051 | vcaballero | * Clase utilizada para dibujar las capas.
|
312 | 984 | fernando | *
|
313 | * @author Vicente Caballero Navarro
|
||
314 | */
|
||
315 | public class Drawer extends Thread { |
||
316 | private Graphics g; |
||
317 | private BufferedImage image; |
||
318 | private CancelDraw cancel;
|
||
319 | private boolean threadCancel = false; |
||
320 | |||
321 | /**
|
||
322 | * Crea un nuevo Drawer.
|
||
323 | *
|
||
324 | 1051 | vcaballero | * @param image
|
325 | * @param g
|
||
326 | * @param cancel
|
||
327 | 984 | fernando | */
|
328 | public Drawer(BufferedImage image, Graphics g, CancelDraw cancel) { |
||
329 | this.g = g;
|
||
330 | this.image = image;
|
||
331 | this.cancel = cancel;
|
||
332 | } |
||
333 | |||
334 | /**
|
||
335 | * @see java.lang.Runnable#run()
|
||
336 | */
|
||
337 | public void run() { |
||
338 | try {
|
||
339 | synchronized (Drawer.class) {
|
||
340 | mapContext.draw(image, (Graphics2D) image.getGraphics(),
|
||
341 | cancel); |
||
342 | timer.stop(); |
||
343 | isCancelled = true;
|
||
344 | repaint(); |
||
345 | } |
||
346 | 762 | fernando | } catch (Throwable e) { |
347 | 750 | fernando | throwException(e); |
348 | 470 | fjp | } finally {
|
349 | 984 | fernando | } |
350 | } |
||
351 | } |
||
352 | 330 | vcaballero | |
353 | 984 | fernando | /**
|
354 | 1051 | vcaballero | * Clase para cancelar el dibujado.
|
355 | 984 | fernando | *
|
356 | * @author Fernando Gonz?lez Cort?s
|
||
357 | */
|
||
358 | public class CancelDraw implements Cancellable { |
||
359 | private boolean cancel = false; |
||
360 | 338 | fernando | |
361 | 984 | fernando | /**
|
362 | * Crea un nuevo CancelDraw.
|
||
363 | */
|
||
364 | public CancelDraw() {
|
||
365 | } |
||
366 | 336 | vcaballero | |
367 | 984 | fernando | /**
|
368 | 1051 | vcaballero | * Insertar si se debe cancelar el dibujado.
|
369 | 984 | fernando | *
|
370 | 1051 | vcaballero | * @param b true si se debe cancelar el dibujado.
|
371 | 984 | fernando | */
|
372 | public void setCancel(boolean b) { |
||
373 | cancel = b; |
||
374 | } |
||
375 | 338 | fernando | |
376 | 984 | fernando | /**
|
377 | * @see com.iver.cit.gvsig.fmap.operations.Cancellable#isCanceled()
|
||
378 | */
|
||
379 | public boolean isCanceled() { |
||
380 | return cancel;
|
||
381 | } |
||
382 | } |
||
383 | 453 | fernando | |
384 | 984 | fernando | /**
|
385 | 1051 | vcaballero | * Listener del MapTool.
|
386 | 984 | fernando | *
|
387 | * @author Fernando Gonz?lez Cort?s
|
||
388 | */
|
||
389 | public class MapToolListener implements MouseListener, MouseWheelListener, |
||
390 | MouseMotionListener {
|
||
391 | 453 | fernando | /**
|
392 | * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
||
393 | */
|
||
394 | public void mouseClicked(MouseEvent e) { |
||
395 | 984 | fernando | try {
|
396 | 453 | fernando | currentMapTool.mouseClicked(e); |
397 | 984 | fernando | } catch (BehaviorException t) {
|
398 | 453 | fernando | throwException(t); |
399 | } |
||
400 | } |
||
401 | |||
402 | /**
|
||
403 | * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
||
404 | */
|
||
405 | public void mouseEntered(MouseEvent e) { |
||
406 | 984 | fernando | try {
|
407 | 453 | fernando | currentMapTool.mouseEntered(e); |
408 | 984 | fernando | } catch (BehaviorException t) {
|
409 | 453 | fernando | throwException(t); |
410 | } |
||
411 | } |
||
412 | |||
413 | /**
|
||
414 | * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
||
415 | */
|
||
416 | public void mouseExited(MouseEvent e) { |
||
417 | 984 | fernando | try {
|
418 | 453 | fernando | currentMapTool.mouseExited(e); |
419 | 984 | fernando | } catch (BehaviorException t) {
|
420 | 453 | fernando | throwException(t); |
421 | } |
||
422 | } |
||
423 | |||
424 | /**
|
||
425 | * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
||
426 | */
|
||
427 | public void mousePressed(MouseEvent e) { |
||
428 | 984 | fernando | try {
|
429 | 453 | fernando | currentMapTool.mousePressed(e); |
430 | 984 | fernando | } catch (BehaviorException t) {
|
431 | 453 | fernando | throwException(t); |
432 | } |
||
433 | } |
||
434 | |||
435 | /**
|
||
436 | * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
||
437 | */
|
||
438 | public void mouseReleased(MouseEvent e) { |
||
439 | 984 | fernando | try {
|
440 | 453 | fernando | currentMapTool.mouseReleased(e); |
441 | 984 | fernando | } catch (BehaviorException t) {
|
442 | 453 | fernando | throwException(t); |
443 | } |
||
444 | } |
||
445 | |||
446 | /**
|
||
447 | * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
|
||
448 | */
|
||
449 | public void mouseWheelMoved(MouseWheelEvent e) { |
||
450 | 984 | fernando | try {
|
451 | 453 | fernando | currentMapTool.mouseWheelMoved(e); |
452 | 984 | fernando | } catch (BehaviorException t) {
|
453 | 453 | fernando | throwException(t); |
454 | } |
||
455 | } |
||
456 | |||
457 | /**
|
||
458 | * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
||
459 | */
|
||
460 | public void mouseDragged(MouseEvent e) { |
||
461 | 984 | fernando | try {
|
462 | 453 | fernando | currentMapTool.mouseDragged(e); |
463 | 984 | fernando | } catch (BehaviorException t) {
|
464 | 453 | fernando | throwException(t); |
465 | } |
||
466 | } |
||
467 | |||
468 | /**
|
||
469 | * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
||
470 | */
|
||
471 | public void mouseMoved(MouseEvent e) { |
||
472 | 984 | fernando | try {
|
473 | 453 | fernando | currentMapTool.mouseMoved(e); |
474 | 984 | fernando | } catch (BehaviorException t) {
|
475 | 453 | fernando | throwException(t); |
476 | } |
||
477 | } |
||
478 | 984 | fernando | } |
479 | |||
480 | /**
|
||
481 | 1051 | vcaballero | * Listener sobre el MapContext.
|
482 | 984 | fernando | *
|
483 | * @author Fernando Gonz?lez Cort?s
|
||
484 | */
|
||
485 | public class MapContextListener implements AtomicEventListener { |
||
486 | /**
|
||
487 | 681 | fernando | * @see com.iver.cit.gvsig.fmap.AtomicEventListener#atomicEvent(com.iver.cit.gvsig.fmap.AtomicEvent)
|
488 | 490 | fernando | */
|
489 | 681 | fernando | public void atomicEvent(AtomicEvent e) { |
490 | 764 | fernando | boolean redraw = false; |
491 | LayerEvent[] layerEvents = e.getLayerEvents();
|
||
492 | 984 | fernando | |
493 | 764 | fernando | for (int i = 0; i < layerEvents.length; i++) { |
494 | 984 | fernando | if (layerEvents[i].getProperty().equals("visible")) { |
495 | 764 | fernando | redraw = true;
|
496 | } |
||
497 | } |
||
498 | 1051 | vcaballero | |
499 | 984 | fernando | if (e.getColorEvents().length > 0) { |
500 | redraw = true;
|
||
501 | } |
||
502 | 1051 | vcaballero | |
503 | 984 | fernando | if (e.getExtentEvents().length > 0) { |
504 | redraw = true;
|
||
505 | } |
||
506 | |||
507 | if (e.getLayerCollectionEvents().length > 0) { |
||
508 | redraw = true;
|
||
509 | } |
||
510 | |||
511 | if (e.getLegendEvents().length > 0) { |
||
512 | redraw = true;
|
||
513 | } |
||
514 | |||
515 | if (e.getSelectionEvents().length > 0) { |
||
516 | redraw = true;
|
||
517 | } |
||
518 | |||
519 | if (redraw) {
|
||
520 | 764 | fernando | NewMapControl.this.drawMap(); |
521 | } |
||
522 | 490 | fernando | } |
523 | 849 | fernando | } |
524 | 1062 | fernando | public ViewPort getViewPort() {
|
525 | return vp;
|
||
526 | } |
||
527 | 230 | fernando | } |