root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / NewMapControl.java @ 1062
History | View | Annotate | Download (11.9 KB)
1 |
package com.iver.cit.gvsig.fmap; |
---|---|
2 |
|
3 |
import java.awt.Dimension; |
4 |
import java.awt.Graphics; |
5 |
import java.awt.Graphics2D; |
6 |
import java.awt.event.ActionEvent; |
7 |
import java.awt.event.ActionListener; |
8 |
import java.awt.event.ComponentEvent; |
9 |
import java.awt.event.ComponentListener; |
10 |
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 |
import java.awt.image.BufferedImage; |
16 |
import java.util.HashMap; |
17 |
|
18 |
import javax.swing.JComponent; |
19 |
import javax.swing.Timer; |
20 |
|
21 |
import org.apache.log4j.Logger; |
22 |
import org.cresques.cts.IProjection; |
23 |
import org.cresques.cts.ProjectionPool; |
24 |
|
25 |
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 |
/**
|
35 |
* MapControl.
|
36 |
*
|
37 |
* @author Fernando Gonz?lez Cort?s
|
38 |
*/
|
39 |
public class NewMapControl extends JComponent implements ComponentListener { |
40 |
/** Cuando la vista est? actualizada. */
|
41 |
public static final int ACTUALIZADO = 0; |
42 |
|
43 |
/** Cuando la vista est? desactualizada. */
|
44 |
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 |
|
61 |
/**
|
62 |
* Crea un nuevo NewMapControl.
|
63 |
*/
|
64 |
public NewMapControl() {
|
65 |
setDoubleBuffered(true);
|
66 |
|
67 |
//Clase usada para cancelar el dibujado
|
68 |
canceldraw = new CancelDraw();
|
69 |
|
70 |
//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 |
|
77 |
//eventos
|
78 |
this.addComponentListener(this); |
79 |
this.addMouseListener(mapToolListener);
|
80 |
this.addMouseMotionListener(mapToolListener);
|
81 |
this.addMouseWheelListener(mapToolListener);
|
82 |
|
83 |
//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 |
|
92 |
/**
|
93 |
* Inserta el modelo.
|
94 |
*
|
95 |
* @param model FMap.
|
96 |
*/
|
97 |
public void setMapContext(FMap model) { |
98 |
if (mapContext != null) { |
99 |
mapContext.removeAtomicEventListener(mapContextListener); |
100 |
} |
101 |
|
102 |
mapContext = model; |
103 |
|
104 |
if (mapContext.getViewPort() == null) { |
105 |
mapContext.setViewPort(vp); |
106 |
} else {
|
107 |
vp = mapContext.getViewPort(); |
108 |
|
109 |
// vp.setImageSize(new Dimension(getWidth(), getHeight()));
|
110 |
//System.err.println("Viewport en setMapContext:" + vp);
|
111 |
} |
112 |
|
113 |
mapContext.addAtomicEventListener(mapContextListener); |
114 |
|
115 |
status = DESACTUALIZADO; |
116 |
} |
117 |
|
118 |
/**
|
119 |
* Devuelve la proyecci?n.
|
120 |
*
|
121 |
* @return Proyecci?n.
|
122 |
*/
|
123 |
public IProjection getProjection() {
|
124 |
return getMapContext().getProjection();
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* Inserta una proyecci?n.
|
129 |
*
|
130 |
* @param proj Proyecci?n.
|
131 |
*/
|
132 |
public void setProjection(IProjection proj) { |
133 |
getMapContext().setProjection(proj); |
134 |
} |
135 |
|
136 |
/**
|
137 |
* Devuelve el modelo.
|
138 |
*
|
139 |
* @return FMap.
|
140 |
*/
|
141 |
public FMap getMapContext() {
|
142 |
return mapContext;
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* Registra una herramienta (tool).
|
147 |
*
|
148 |
* @param name Nombre de la herramienta.
|
149 |
* @param tool Herramienta.
|
150 |
*/
|
151 |
public void addMapTool(String name, MapTool tool) { |
152 |
namesMapTools.put(name, tool); |
153 |
tool.setMapControl(this);
|
154 |
} |
155 |
|
156 |
public void addMapTool(String name, MapTool[] tools){ |
157 |
CompoundBehavior tool = new CompoundBehavior(tools);
|
158 |
addMapTool(name, tool); |
159 |
} |
160 |
|
161 |
/**
|
162 |
* DOCUMENT ME!
|
163 |
*
|
164 |
* @param toolName DOCUMENT ME!
|
165 |
*/
|
166 |
public void setTool(String toolName) { |
167 |
MapTool mapTool = (MapTool) namesMapTools.get(toolName); |
168 |
currentMapTool = mapTool; |
169 |
currentTool = toolName; |
170 |
this.setCursor(mapTool.getCursor());
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* Devuelve el nombre de la herramienta seleccionada.
|
175 |
*
|
176 |
* @return nombre.
|
177 |
*/
|
178 |
public String getTool() { |
179 |
return currentTool;
|
180 |
} |
181 |
|
182 |
/**
|
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 |
|
192 |
canceldraw.setCancel(true);
|
193 |
|
194 |
while (!isCancelled) {
|
195 |
} |
196 |
|
197 |
canceldraw.setCancel(false);
|
198 |
isCancelled = false;
|
199 |
} |
200 |
|
201 |
/**
|
202 |
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
|
203 |
*/
|
204 |
protected void paintComponent(Graphics g) { |
205 |
if (status == ACTUALIZADO) {
|
206 |
logger.debug("Dibujando la imagen obtenida");
|
207 |
|
208 |
/*
|
209 |
* Si hay un behaviour y la imagen es distinta de null se delega el dibujado
|
210 |
* en dicho behaviour
|
211 |
*/
|
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 |
|
218 |
//Se cancela el dibujado anterior
|
219 |
cancelDrawing(); |
220 |
|
221 |
//Se crea la imagen con el color de fonde deseado
|
222 |
image = new BufferedImage(this.getWidth(), this.getHeight(), |
223 |
BufferedImage.TYPE_INT_ARGB);
|
224 |
|
225 |
Graphics imgg = image.createGraphics();
|
226 |
|
227 |
//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 |
|
234 |
// repaint();
|
235 |
} |
236 |
} |
237 |
|
238 |
/**
|
239 |
* Devuelve la imagen de la vista.
|
240 |
*
|
241 |
* @return imagen.
|
242 |
*/
|
243 |
public BufferedImage getImage() { |
244 |
return image;
|
245 |
} |
246 |
|
247 |
/**
|
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 |
|
256 |
/**
|
257 |
* @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
|
258 |
*/
|
259 |
public void componentHidden(ComponentEvent e) { |
260 |
} |
261 |
|
262 |
/**
|
263 |
* @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
|
264 |
*/
|
265 |
public void componentMoved(ComponentEvent e) { |
266 |
} |
267 |
|
268 |
/**
|
269 |
* @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
|
270 |
*/
|
271 |
public void componentResized(ComponentEvent e) { |
272 |
drawMap(); |
273 |
} |
274 |
|
275 |
/**
|
276 |
* @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
|
277 |
*/
|
278 |
public void componentShown(ComponentEvent e) { |
279 |
} |
280 |
|
281 |
/**
|
282 |
* A?ade un listener de tipo ExceptionListener.
|
283 |
*
|
284 |
* @param o ExceptionListener.
|
285 |
*/
|
286 |
public void addExceptionListener(ExceptionListener o) { |
287 |
exceptionHandlingSupport.addExceptionListener(o); |
288 |
} |
289 |
|
290 |
/**
|
291 |
* Borra la ExceptioListener que se pasa como par?metro.
|
292 |
*
|
293 |
* @param o ExceptionListener.
|
294 |
*
|
295 |
* @return True si se borra correctamente.
|
296 |
*/
|
297 |
public boolean removeExceptionListener(ExceptionListener o) { |
298 |
return exceptionHandlingSupport.removeExceptionListener(o);
|
299 |
} |
300 |
|
301 |
/**
|
302 |
* Lanza una Excepci?n.
|
303 |
*
|
304 |
* @param t Excepci?n.
|
305 |
*/
|
306 |
private void throwException(Throwable t) { |
307 |
exceptionHandlingSupport.throwException(t); |
308 |
} |
309 |
|
310 |
/**
|
311 |
* Clase utilizada para dibujar las capas.
|
312 |
*
|
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 |
* @param image
|
325 |
* @param g
|
326 |
* @param cancel
|
327 |
*/
|
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 |
} catch (Throwable e) { |
347 |
throwException(e); |
348 |
} finally {
|
349 |
} |
350 |
} |
351 |
} |
352 |
|
353 |
/**
|
354 |
* Clase para cancelar el dibujado.
|
355 |
*
|
356 |
* @author Fernando Gonz?lez Cort?s
|
357 |
*/
|
358 |
public class CancelDraw implements Cancellable { |
359 |
private boolean cancel = false; |
360 |
|
361 |
/**
|
362 |
* Crea un nuevo CancelDraw.
|
363 |
*/
|
364 |
public CancelDraw() {
|
365 |
} |
366 |
|
367 |
/**
|
368 |
* Insertar si se debe cancelar el dibujado.
|
369 |
*
|
370 |
* @param b true si se debe cancelar el dibujado.
|
371 |
*/
|
372 |
public void setCancel(boolean b) { |
373 |
cancel = b; |
374 |
} |
375 |
|
376 |
/**
|
377 |
* @see com.iver.cit.gvsig.fmap.operations.Cancellable#isCanceled()
|
378 |
*/
|
379 |
public boolean isCanceled() { |
380 |
return cancel;
|
381 |
} |
382 |
} |
383 |
|
384 |
/**
|
385 |
* Listener del MapTool.
|
386 |
*
|
387 |
* @author Fernando Gonz?lez Cort?s
|
388 |
*/
|
389 |
public class MapToolListener implements MouseListener, MouseWheelListener, |
390 |
MouseMotionListener {
|
391 |
/**
|
392 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
393 |
*/
|
394 |
public void mouseClicked(MouseEvent e) { |
395 |
try {
|
396 |
currentMapTool.mouseClicked(e); |
397 |
} catch (BehaviorException t) {
|
398 |
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 |
try {
|
407 |
currentMapTool.mouseEntered(e); |
408 |
} catch (BehaviorException t) {
|
409 |
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 |
try {
|
418 |
currentMapTool.mouseExited(e); |
419 |
} catch (BehaviorException t) {
|
420 |
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 |
try {
|
429 |
currentMapTool.mousePressed(e); |
430 |
} catch (BehaviorException t) {
|
431 |
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 |
try {
|
440 |
currentMapTool.mouseReleased(e); |
441 |
} catch (BehaviorException t) {
|
442 |
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 |
try {
|
451 |
currentMapTool.mouseWheelMoved(e); |
452 |
} catch (BehaviorException t) {
|
453 |
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 |
try {
|
462 |
currentMapTool.mouseDragged(e); |
463 |
} catch (BehaviorException t) {
|
464 |
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 |
try {
|
473 |
currentMapTool.mouseMoved(e); |
474 |
} catch (BehaviorException t) {
|
475 |
throwException(t); |
476 |
} |
477 |
} |
478 |
} |
479 |
|
480 |
/**
|
481 |
* Listener sobre el MapContext.
|
482 |
*
|
483 |
* @author Fernando Gonz?lez Cort?s
|
484 |
*/
|
485 |
public class MapContextListener implements AtomicEventListener { |
486 |
/**
|
487 |
* @see com.iver.cit.gvsig.fmap.AtomicEventListener#atomicEvent(com.iver.cit.gvsig.fmap.AtomicEvent)
|
488 |
*/
|
489 |
public void atomicEvent(AtomicEvent e) { |
490 |
boolean redraw = false; |
491 |
LayerEvent[] layerEvents = e.getLayerEvents();
|
492 |
|
493 |
for (int i = 0; i < layerEvents.length; i++) { |
494 |
if (layerEvents[i].getProperty().equals("visible")) { |
495 |
redraw = true;
|
496 |
} |
497 |
} |
498 |
|
499 |
if (e.getColorEvents().length > 0) { |
500 |
redraw = true;
|
501 |
} |
502 |
|
503 |
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 |
NewMapControl.this.drawMap(); |
521 |
} |
522 |
} |
523 |
} |
524 |
public ViewPort getViewPort() {
|
525 |
return vp;
|
526 |
} |
527 |
} |