svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / NewMapControl.java @ 652
History | View | Annotate | Download (12.8 KB)
1 |
package com.iver.cit.gvsig.fmap; |
---|---|
2 |
|
3 |
import com.hardcode.driverManager.DriverLoadException; |
4 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
5 |
import com.iver.cit.gvsig.fmap.operations.Cancellable; |
6 |
import com.iver.cit.gvsig.fmap.tools.Behavior.MapTool; |
7 |
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener; |
8 |
|
9 |
import java.awt.Color; |
10 |
import java.awt.Dimension; |
11 |
import java.awt.Graphics; |
12 |
import java.awt.Graphics2D; |
13 |
import java.awt.event.ActionEvent; |
14 |
import java.awt.event.ActionListener; |
15 |
import java.awt.event.ComponentEvent; |
16 |
import java.awt.event.ComponentListener; |
17 |
import java.awt.event.MouseEvent; |
18 |
import java.awt.event.MouseListener; |
19 |
import java.awt.event.MouseMotionListener; |
20 |
import java.awt.event.MouseWheelEvent; |
21 |
import java.awt.event.MouseWheelListener; |
22 |
import java.awt.image.BufferedImage; |
23 |
|
24 |
import java.util.ArrayList; |
25 |
import java.util.HashMap; |
26 |
import java.util.Iterator; |
27 |
|
28 |
import javax.swing.JComponent; |
29 |
import javax.swing.Timer; |
30 |
|
31 |
import org.apache.log4j.Logger; |
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 ArrayList exceptionListeners = new ArrayList(); |
45 |
private FMap mapContext = null; |
46 |
private HashMap namesMapTools = new HashMap(); |
47 |
private HashMap namesMapToolsNames = new HashMap(); |
48 |
private HashMap nameListener = new HashMap(); |
49 |
private MapTool currentMapTool = null; |
50 |
private int status = ACTUALIZADO; |
51 |
private BufferedImage image = null; |
52 |
private String currentTool; |
53 |
private CancelDraw canceldraw;
|
54 |
private boolean isCancelled = true; |
55 |
private Timer timer; |
56 |
private ViewPort vp;
|
57 |
private Color backColor = Color.WHITE; |
58 |
private Drawer drawer;
|
59 |
|
60 |
private MapToolListener mapToolListener = new MapToolListener(); |
61 |
|
62 |
private MapContextListener mapContextListener = new MapContextListener(); |
63 |
|
64 |
private static Logger logger = Logger.getLogger(NewMapControl.class.getName()); |
65 |
/**
|
66 |
* Crea un nuevo NewMapControl.
|
67 |
*/
|
68 |
public NewMapControl() {
|
69 |
setDoubleBuffered(true);
|
70 |
|
71 |
//Clase usada para cancelar el dibujado
|
72 |
canceldraw = new CancelDraw();
|
73 |
|
74 |
//Modelo de datos y ventana del mismo
|
75 |
vp = new ViewPort();
|
76 |
setMapContext(new FMap(vp));
|
77 |
|
78 |
//eventos
|
79 |
this.addComponentListener(this); |
80 |
this.addMouseListener(mapToolListener);
|
81 |
this.addMouseMotionListener(mapToolListener);
|
82 |
this.addMouseWheelListener(mapToolListener);
|
83 |
|
84 |
//Timer para mostrar el redibujado mientras se dibuja
|
85 |
timer = new Timer(300, |
86 |
new ActionListener() { |
87 |
public void actionPerformed(ActionEvent e) { |
88 |
NewMapControl.this.repaint(); |
89 |
} |
90 |
}); |
91 |
} |
92 |
|
93 |
/**
|
94 |
* A?ade un listener de las excepciones que se producen en los behavior
|
95 |
*
|
96 |
* @param o Listener de las excepciones
|
97 |
*/
|
98 |
public void addExceptionListener(ExceptionListener o) { |
99 |
exceptionListeners.add(o); |
100 |
} |
101 |
|
102 |
/**
|
103 |
* Elimina un listener de las excepciones que se producen en los behaviours
|
104 |
*
|
105 |
* @param o listener que se quiere eliminar
|
106 |
*
|
107 |
* @return true si el elemento era listener de las excepciones
|
108 |
*/
|
109 |
public boolean removeExceptionListener(ExceptionListener o) { |
110 |
return exceptionListeners.remove(o);
|
111 |
} |
112 |
|
113 |
/**
|
114 |
* Notifica a los listeners de excepciones con la excepci?n o error
|
115 |
* que se pasa como par?metro
|
116 |
*
|
117 |
* @param t DOCUMENT ME!
|
118 |
*/
|
119 |
private void throwException(Throwable t) { |
120 |
for (Iterator iter = exceptionListeners.iterator(); iter.hasNext();) { |
121 |
ExceptionListener listener = (ExceptionListener) iter.next(); |
122 |
listener.exceptionThrown(t); |
123 |
} |
124 |
} |
125 |
|
126 |
/**
|
127 |
* DOCUMENT ME!
|
128 |
*
|
129 |
* @param model DOCUMENT ME!
|
130 |
*/
|
131 |
public void setMapContext(FMap model) { |
132 |
if (mapContext != null){ |
133 |
mapContext.removeInvalidationListener(mapContextListener); |
134 |
} |
135 |
|
136 |
mapContext = model; |
137 |
if (mapContext.getViewPort()==null){ |
138 |
mapContext.setViewPort(vp); |
139 |
}else{
|
140 |
vp = mapContext.getViewPort(); |
141 |
} |
142 |
|
143 |
mapContext.addInvalidationListener(mapContextListener); |
144 |
|
145 |
} |
146 |
|
147 |
/**
|
148 |
* DOCUMENT ME!
|
149 |
*
|
150 |
* @return
|
151 |
*/
|
152 |
public FMap getMapContext() {
|
153 |
return mapContext;
|
154 |
} |
155 |
|
156 |
/**
|
157 |
* Registra una herramienta (tool).
|
158 |
*
|
159 |
* @param name DOCUMENT ME!
|
160 |
* @param tool
|
161 |
*/
|
162 |
public void addMapTool(String name, MapTool tool) { |
163 |
namesMapTools.put(name, tool); |
164 |
tool.setMapControl(this);
|
165 |
} |
166 |
|
167 |
/**
|
168 |
* DOCUMENT ME!
|
169 |
*
|
170 |
* @param toolName DOCUMENT ME!
|
171 |
* @param mapToolName DOCUMENT ME!
|
172 |
* @param tl DOCUMENT ME!
|
173 |
*/
|
174 |
public void addTool(String toolName, String mapToolName, ToolListener tl) { |
175 |
namesMapToolsNames.put(toolName, mapToolName); |
176 |
nameListener.put(toolName, tl); |
177 |
|
178 |
MapTool mt = (MapTool) namesMapTools.get(mapToolName); |
179 |
} |
180 |
|
181 |
/**
|
182 |
* DOCUMENT ME!
|
183 |
*
|
184 |
* @param toolName DOCUMENT ME!
|
185 |
*/
|
186 |
public void setTool(String toolName) { |
187 |
/* if (currentMapTool != null) {
|
188 |
this.removeMouseListener(currentMapTool);
|
189 |
this.removeMouseMotionListener(currentMapTool);
|
190 |
this.removeMouseWheelListener(currentMapTool);
|
191 |
}
|
192 |
*/
|
193 |
String mapToolName = (String) namesMapToolsNames.get(toolName); |
194 |
MapTool mapTool = (MapTool) namesMapTools.get(mapToolName); |
195 |
mapTool.setListener((ToolListener) nameListener.get(toolName)); |
196 |
currentMapTool = mapTool; |
197 |
currentTool = toolName; |
198 |
/*
|
199 |
this.addMouseListener(currentMapTool);
|
200 |
this.addMouseMotionListener(currentMapTool);
|
201 |
this.addMouseWheelListener(currentMapTool);
|
202 |
*/
|
203 |
this.setCursor(mapTool.getListener().getCursor());
|
204 |
} |
205 |
|
206 |
/**
|
207 |
* DOCUMENT ME!
|
208 |
*
|
209 |
* @return DOCUMENT ME!
|
210 |
*/
|
211 |
public String getTool() { |
212 |
return currentTool;
|
213 |
} |
214 |
|
215 |
/**
|
216 |
* Cancela el dibujado. Se espera a que la cancelaci?n surta efecto
|
217 |
*/
|
218 |
public void cancelDrawing() { |
219 |
if (drawer != null) { |
220 |
if (!drawer.isAlive()) {
|
221 |
return;
|
222 |
} |
223 |
} |
224 |
|
225 |
canceldraw.setCancel(true);
|
226 |
|
227 |
while (!isCancelled) {
|
228 |
} |
229 |
|
230 |
canceldraw.setCancel(false);
|
231 |
isCancelled = false;
|
232 |
} |
233 |
|
234 |
/**
|
235 |
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
|
236 |
*/
|
237 |
protected void paintComponent(Graphics g) { |
238 |
if (status == ACTUALIZADO) {
|
239 |
if (image != null) { |
240 |
g.drawImage(image, 0, 0, this); |
241 |
} else {
|
242 |
image = new BufferedImage(this.getWidth(), this.getHeight(), |
243 |
BufferedImage.TYPE_INT_ARGB);
|
244 |
} |
245 |
|
246 |
if (currentMapTool != null) { |
247 |
currentMapTool.paintComponent(g); |
248 |
} |
249 |
} else if (status == DESACTUALIZADO) { |
250 |
logger.debug("Pintando MapControl");
|
251 |
image = new BufferedImage(this.getWidth(), this.getHeight(), |
252 |
BufferedImage.TYPE_INT_ARGB);
|
253 |
image.getGraphics().setColor(backColor); |
254 |
image.getGraphics().fillRect(0, 0, image.getWidth(), |
255 |
image.getHeight()); |
256 |
|
257 |
cancelDrawing(); |
258 |
|
259 |
drawer = new Drawer(image, (Graphics2D) image.getGraphics(), |
260 |
canceldraw); |
261 |
drawer.start(); |
262 |
timer.start(); |
263 |
status = ACTUALIZADO; |
264 |
} |
265 |
} |
266 |
|
267 |
/**
|
268 |
* DOCUMENT ME!
|
269 |
*
|
270 |
* @return
|
271 |
*/
|
272 |
public BufferedImage getImage() { |
273 |
return image;
|
274 |
} |
275 |
|
276 |
/**
|
277 |
* DOCUMENT ME!
|
278 |
*/
|
279 |
public void drawMap() { |
280 |
status = DESACTUALIZADO; |
281 |
vp.setImageSize(new Dimension(getWidth(), getHeight())); |
282 |
|
283 |
repaint(); |
284 |
} |
285 |
|
286 |
/**
|
287 |
* @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
|
288 |
*/
|
289 |
public void componentHidden(ComponentEvent e) { |
290 |
} |
291 |
|
292 |
/**
|
293 |
* @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
|
294 |
*/
|
295 |
public void componentMoved(ComponentEvent e) { |
296 |
} |
297 |
|
298 |
/**
|
299 |
* @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
|
300 |
*/
|
301 |
public void componentResized(ComponentEvent e) { |
302 |
drawMap(); |
303 |
} |
304 |
|
305 |
/**
|
306 |
* @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
|
307 |
*/
|
308 |
public void componentShown(ComponentEvent e) { |
309 |
} |
310 |
|
311 |
/**
|
312 |
* DOCUMENT ME!
|
313 |
*
|
314 |
* @return DOCUMENT ME!
|
315 |
*/
|
316 |
public Color getBackColor() { |
317 |
return backColor;
|
318 |
} |
319 |
|
320 |
/**
|
321 |
* DOCUMENT ME!
|
322 |
*
|
323 |
* @param backColor DOCUMENT ME!
|
324 |
*/
|
325 |
public void setBackColor(Color backColor) { |
326 |
this.backColor = backColor;
|
327 |
} |
328 |
|
329 |
/**
|
330 |
* DOCUMENT ME!
|
331 |
*
|
332 |
* @author Vicente Caballero Navarro
|
333 |
*/
|
334 |
public class Drawer extends Thread { |
335 |
private Graphics g; |
336 |
private BufferedImage image; |
337 |
private CancelDraw cancel;
|
338 |
private boolean threadCancel = false; |
339 |
|
340 |
/**
|
341 |
* Crea un nuevo Drawer.
|
342 |
*
|
343 |
* @param image DOCUMENT ME!
|
344 |
* @param g DOCUMENT ME!
|
345 |
* @param cancel DOCUMENT ME!
|
346 |
*/
|
347 |
public Drawer(BufferedImage image, Graphics g, CancelDraw cancel) { |
348 |
this.g = g;
|
349 |
this.image = image;
|
350 |
this.cancel = cancel;
|
351 |
} |
352 |
|
353 |
/**
|
354 |
* @see java.lang.Runnable#run()
|
355 |
*/
|
356 |
public void run() { |
357 |
try {
|
358 |
synchronized (Drawer.class) {
|
359 |
mapContext.draw(image, (Graphics2D) image.getGraphics(),
|
360 |
cancel); |
361 |
timer.stop(); |
362 |
isCancelled = true;
|
363 |
System.err.println("Cancelado"); |
364 |
repaint(); |
365 |
} |
366 |
} catch (DriverException e) {
|
367 |
// TODO Auto-generated catch block
|
368 |
e.printStackTrace(); |
369 |
} finally {
|
370 |
} |
371 |
} |
372 |
} |
373 |
|
374 |
/**
|
375 |
* DOCUMENT ME!
|
376 |
*
|
377 |
* @author Fernando Gonz?lez Cort?s
|
378 |
*/
|
379 |
public class CancelDraw implements Cancellable { |
380 |
private boolean cancel = false; |
381 |
|
382 |
/**
|
383 |
* Crea un nuevo CancelDraw.
|
384 |
*/
|
385 |
public CancelDraw() {
|
386 |
} |
387 |
|
388 |
/**
|
389 |
* DOCUMENT ME!
|
390 |
*
|
391 |
* @param b DOCUMENT ME!
|
392 |
*/
|
393 |
public void setCancel(boolean b) { |
394 |
cancel = b; |
395 |
} |
396 |
|
397 |
/**
|
398 |
* @see com.iver.cit.gvsig.fmap.operations.Cancellable#isCanceled()
|
399 |
*/
|
400 |
public boolean isCanceled() { |
401 |
return cancel;
|
402 |
} |
403 |
} |
404 |
|
405 |
public class MapToolListener implements MouseListener, MouseWheelListener, MouseMotionListener{ |
406 |
|
407 |
/**
|
408 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
409 |
*/
|
410 |
public void mouseClicked(MouseEvent e) { |
411 |
try{
|
412 |
currentMapTool.mouseClicked(e); |
413 |
}catch(Throwable t){ |
414 |
throwException(t); |
415 |
} |
416 |
} |
417 |
|
418 |
/**
|
419 |
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
420 |
*/
|
421 |
public void mouseEntered(MouseEvent e) { |
422 |
try{
|
423 |
currentMapTool.mouseEntered(e); |
424 |
}catch(Throwable t){ |
425 |
throwException(t); |
426 |
} |
427 |
} |
428 |
|
429 |
/**
|
430 |
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
431 |
*/
|
432 |
public void mouseExited(MouseEvent e) { |
433 |
try{
|
434 |
currentMapTool.mouseExited(e); |
435 |
}catch(Throwable t){ |
436 |
throwException(t); |
437 |
} |
438 |
} |
439 |
|
440 |
/**
|
441 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
442 |
*/
|
443 |
public void mousePressed(MouseEvent e) { |
444 |
try{
|
445 |
currentMapTool.mousePressed(e); |
446 |
}catch(Throwable t){ |
447 |
throwException(t); |
448 |
} |
449 |
} |
450 |
|
451 |
/**
|
452 |
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
453 |
*/
|
454 |
public void mouseReleased(MouseEvent e) { |
455 |
try{
|
456 |
currentMapTool.mouseReleased(e); |
457 |
}catch(Throwable t){ |
458 |
throwException(t); |
459 |
} |
460 |
} |
461 |
|
462 |
/**
|
463 |
* @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
|
464 |
*/
|
465 |
public void mouseWheelMoved(MouseWheelEvent e) { |
466 |
try{
|
467 |
currentMapTool.mouseWheelMoved(e); |
468 |
}catch(Throwable t){ |
469 |
throwException(t); |
470 |
} |
471 |
} |
472 |
|
473 |
/**
|
474 |
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
475 |
*/
|
476 |
public void mouseDragged(MouseEvent e) { |
477 |
try{
|
478 |
currentMapTool.mouseDragged(e); |
479 |
}catch(Throwable t){ |
480 |
throwException(t); |
481 |
} |
482 |
} |
483 |
|
484 |
/**
|
485 |
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
486 |
*/
|
487 |
public void mouseMoved(MouseEvent e) { |
488 |
try{
|
489 |
currentMapTool.mouseMoved(e); |
490 |
}catch(Throwable t){ |
491 |
throwException(t); |
492 |
} |
493 |
} |
494 |
} |
495 |
|
496 |
public class MapContextListener implements InvalidationListener{ |
497 |
|
498 |
/**
|
499 |
* @see com.iver.cit.gvsig.fmap.InvalidationListener#invalid()
|
500 |
*/
|
501 |
public void invalid() { |
502 |
NewMapControl.this.drawMap(); |
503 |
} |
504 |
|
505 |
} |
506 |
} |