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