svn-gvsig-desktop / tags / v1_0_2_Build_902 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / MapControl.java @ 10681
History | View | Annotate | Download (27.5 KB)
1 | 1223 | fernando | /* 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 | 1282 | fjp | import java.awt.Color; |
44 | 6266 | fjp | import java.awt.Component; |
45 | 1223 | fernando | import java.awt.Dimension; |
46 | import java.awt.Graphics; |
||
47 | import java.awt.Graphics2D; |
||
48 | 6212 | fjp | import java.awt.GraphicsEnvironment; |
49 | 1223 | fernando | import java.awt.event.ActionEvent; |
50 | import java.awt.event.ActionListener; |
||
51 | import java.awt.event.ComponentEvent; |
||
52 | import java.awt.event.ComponentListener; |
||
53 | import java.awt.event.MouseEvent; |
||
54 | import java.awt.event.MouseListener; |
||
55 | import java.awt.event.MouseMotionListener; |
||
56 | import java.awt.event.MouseWheelEvent; |
||
57 | import java.awt.event.MouseWheelListener; |
||
58 | 5874 | fjp | import java.awt.geom.Point2D; |
59 | import java.awt.geom.Rectangle2D; |
||
60 | 1223 | fernando | import java.awt.image.BufferedImage; |
61 | import java.util.HashMap; |
||
62 | 6777 | jmvivo | import java.util.Set; |
63 | 1223 | fernando | |
64 | import javax.swing.JComponent; |
||
65 | import javax.swing.Timer; |
||
66 | |||
67 | import org.cresques.cts.IProjection; |
||
68 | |||
69 | 6164 | luisw2 | import com.iver.cit.gvsig.fmap.crs.CRSFactory; |
70 | 4120 | caballero | import com.iver.cit.gvsig.fmap.edition.commands.CommandListener; |
71 | 10401 | fjp | import com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent; |
72 | 1223 | fernando | import com.iver.cit.gvsig.fmap.layers.LayerEvent; |
73 | import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
||
74 | import com.iver.cit.gvsig.fmap.tools.CompoundBehavior; |
||
75 | import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior; |
||
76 | import com.iver.utiles.exceptionHandling.ExceptionHandlingSupport; |
||
77 | import com.iver.utiles.exceptionHandling.ExceptionListener; |
||
78 | 5317 | fjp | import com.iver.utiles.swing.threads.Cancellable; |
79 | 1223 | fernando | |
80 | |||
81 | /**
|
||
82 | * MapControl.
|
||
83 | *
|
||
84 | * @author Fernando Gonz?lez Cort?s
|
||
85 | */
|
||
86 | 5874 | fjp | public class MapControl extends JComponent implements ComponentListener, |
87 | CommandListener { |
||
88 | 1223 | fernando | /** Cuando la vista est? actualizada. */
|
89 | public static final int ACTUALIZADO = 0; |
||
90 | |||
91 | /** Cuando la vista est? desactualizada. */
|
||
92 | public static final int DESACTUALIZADO = 1; |
||
93 | 2901 | fjp | public static final int ONLY_GRAPHICS = 2; |
94 | 3346 | fjp | // public static final int FAST_PAINT = 3;
|
95 | 2531 | caballero | //private static Logger logger = Logger.getLogger(MapControl.class.getName());
|
96 | 6878 | cesar | private MapContext mapContext = null; |
97 | 3719 | caballero | //private boolean drawerAlive = false;
|
98 | 1223 | fernando | private HashMap namesMapTools = new HashMap(); |
99 | private Behavior currentMapTool = null; |
||
100 | private int status = DESACTUALIZADO; |
||
101 | private BufferedImage image = null; |
||
102 | private String currentTool; |
||
103 | private CancelDraw canceldraw;
|
||
104 | 3719 | caballero | //private boolean isCancelled = true;
|
105 | 1223 | fernando | private Timer timer; |
106 | protected ViewPort vp;
|
||
107 | 3719 | caballero | //private Drawer drawer;
|
108 | 3368 | fjp | private Drawer2 drawer2;
|
109 | 3372 | fjp | // private boolean firstDraw = true;
|
110 | 10519 | ldiaz | protected MapToolListener mapToolListener = new MapToolListener(); |
111 | 1223 | fernando | private MapContextListener mapContextListener = new MapContextListener(); |
112 | private ExceptionHandlingSupport exceptionHandlingSupport = new ExceptionHandlingSupport(); |
||
113 | 4619 | caballero | |
114 | 4888 | caballero | private String prevTool; |
115 | |||
116 | 3719 | caballero | /**
|
117 | 3331 | fjp | * We need this to avoid not wanted refresh. REMEMBER TO SET TO TRUE!!
|
118 | */
|
||
119 | // private boolean paintEnabled = false;
|
||
120 | 1223 | fernando | |
121 | /**
|
||
122 | * Crea un nuevo NewMapControl.
|
||
123 | */
|
||
124 | public MapControl() {
|
||
125 | 5787 | jaume | this.setName("MapControl"); |
126 | 1223 | fernando | setDoubleBuffered(false);
|
127 | setOpaque(true);
|
||
128 | status = DESACTUALIZADO; |
||
129 | |||
130 | //Clase usada para cancelar el dibujado
|
||
131 | canceldraw = new CancelDraw();
|
||
132 | |||
133 | //Modelo de datos y ventana del mismo
|
||
134 | // TODO: Cuando creamos un mapControl, deber?amos asignar
|
||
135 | // la projecci?n por defecto con la que vayamos a trabajar.
|
||
136 | // 23030 es el c?digo EPSG del UTM30 elipsoide ED50
|
||
137 | 6164 | luisw2 | vp = new ViewPort(CRSFactory.getCRS("EPSG:23030")); |
138 | 6878 | cesar | setMapContext(new MapContext(vp));
|
139 | 3719 | caballero | |
140 | 1223 | fernando | //eventos
|
141 | this.addComponentListener(this); |
||
142 | this.addMouseListener(mapToolListener);
|
||
143 | this.addMouseMotionListener(mapToolListener);
|
||
144 | this.addMouseWheelListener(mapToolListener);
|
||
145 | |||
146 | 3368 | fjp | this.drawer2 = new Drawer2(); |
147 | 1223 | fernando | //Timer para mostrar el redibujado mientras se dibuja
|
148 | 8765 | jjdelcerro | timer = new Timer(360, |
149 | 1223 | fernando | new ActionListener() { |
150 | public void actionPerformed(ActionEvent e) { |
||
151 | MapControl.this.repaint(); |
||
152 | } |
||
153 | }); |
||
154 | } |
||
155 | |||
156 | /**
|
||
157 | * Inserta el modelo.
|
||
158 | *
|
||
159 | * @param model FMap.
|
||
160 | */
|
||
161 | 6878 | cesar | public void setMapContext(MapContext model) { |
162 | 1223 | fernando | if (mapContext != null) { |
163 | mapContext.removeAtomicEventListener(mapContextListener); |
||
164 | } |
||
165 | |||
166 | mapContext = model; |
||
167 | |||
168 | if (mapContext.getViewPort() == null) { |
||
169 | mapContext.setViewPort(vp); |
||
170 | } else {
|
||
171 | vp = mapContext.getViewPort(); |
||
172 | |||
173 | // vp.setImageSize(new Dimension(getWidth(), getHeight()));
|
||
174 | //System.err.println("Viewport en setMapContext:" + vp);
|
||
175 | } |
||
176 | |||
177 | mapContext.addAtomicEventListener(mapContextListener); |
||
178 | |||
179 | status = DESACTUALIZADO; |
||
180 | } |
||
181 | |||
182 | /**
|
||
183 | * Devuelve la proyecci?n.
|
||
184 | *
|
||
185 | * @return Proyecci?n.
|
||
186 | */
|
||
187 | public IProjection getProjection() {
|
||
188 | return getMapContext().getProjection();
|
||
189 | } |
||
190 | |||
191 | /**
|
||
192 | * Inserta una proyecci?n.
|
||
193 | *
|
||
194 | * @param proj Proyecci?n.
|
||
195 | */
|
||
196 | public void setProjection(IProjection proj) { |
||
197 | getMapContext().setProjection(proj); |
||
198 | } |
||
199 | |||
200 | /**
|
||
201 | * Devuelve el modelo.
|
||
202 | *
|
||
203 | * @return FMap.
|
||
204 | */
|
||
205 | 6878 | cesar | public MapContext getMapContext() {
|
206 | 1223 | fernando | return mapContext;
|
207 | } |
||
208 | |||
209 | /**
|
||
210 | * Registra una herramienta (tool).
|
||
211 | *
|
||
212 | * @param name Nombre de la herramienta.
|
||
213 | * @param tool Herramienta.
|
||
214 | */
|
||
215 | public void addMapTool(String name, Behavior tool) { |
||
216 | namesMapTools.put(name, tool); |
||
217 | tool.setMapControl(this);
|
||
218 | } |
||
219 | |||
220 | public void addMapTool(String name, Behavior[] tools){ |
||
221 | CompoundBehavior tool = new CompoundBehavior(tools);
|
||
222 | addMapTool(name, tool); |
||
223 | } |
||
224 | 6777 | jmvivo | |
225 | /**
|
||
226 | * Devuelve una herramienta (tool) registrada.
|
||
227 | *
|
||
228 | * @param name Nombre de la herramienta.
|
||
229 | * @return tool Herramienta.
|
||
230 | */
|
||
231 | public Behavior getMapTool(String name) { |
||
232 | return (Behavior)namesMapTools.get(name);
|
||
233 | } |
||
234 | 3719 | caballero | |
235 | 1223 | fernando | /**
|
236 | 6777 | jmvivo | * Devuelve los nombres de herramientas (tool) registradas.
|
237 | *
|
||
238 | * @return Set (String) nombres de las herramientas.
|
||
239 | */
|
||
240 | public Set getMapToolsKeySet() { |
||
241 | return namesMapTools.keySet();
|
||
242 | } |
||
243 | |||
244 | |||
245 | /**
|
||
246 | 6378 | jaume | * Returns true if this map control contains a tool named with the value
|
247 | * passed in the toolName parameter. If you have added two tools with the
|
||
248 | * same name, the last tool will overwrite the previous ones.
|
||
249 | * @param toolName
|
||
250 | * @return true if the map control contains a tool with the same name than
|
||
251 | * toolName. False, otherwise.
|
||
252 | */
|
||
253 | public boolean hasTool(String toolName) { |
||
254 | return namesMapTools.containsKey(toolName);
|
||
255 | } |
||
256 | /**
|
||
257 | 1223 | fernando | * DOCUMENT ME!
|
258 | *
|
||
259 | * @param toolName DOCUMENT ME!
|
||
260 | */
|
||
261 | public void setTool(String toolName) { |
||
262 | 6378 | jaume | prevTool=getCurrentTool(); |
263 | 1223 | fernando | Behavior mapTool = (Behavior) namesMapTools.get(toolName); |
264 | currentMapTool = mapTool; |
||
265 | currentTool = toolName; |
||
266 | this.setCursor(mapTool.getCursor());
|
||
267 | } |
||
268 | 3719 | caballero | public Behavior getCurrentMapTool(){
|
269 | return currentMapTool;
|
||
270 | } |
||
271 | 1223 | fernando | /**
|
272 | 6378 | jaume | * Returns the name of the current selected tool on this MapControl
|
273 | 1223 | fernando | *
|
274 | 6378 | jaume | * @return A tool name.
|
275 | 1223 | fernando | */
|
276 | 6378 | jaume | public String getCurrentTool() { |
277 | 1223 | fernando | return currentTool;
|
278 | } |
||
279 | 6777 | jmvivo | |
280 | 1223 | fernando | |
281 | /**
|
||
282 | * Cancela el dibujado. Se espera a que la cancelaci?n surta efecto
|
||
283 | */
|
||
284 | public void cancelDrawing() { |
||
285 | 3368 | fjp | /* if (drawer != null) {
|
286 | 1223 | fernando | if (!drawer.isAlive()) {
|
287 | return;
|
||
288 | }
|
||
289 | }
|
||
290 | 3368 | fjp | */
|
291 | 4832 | fjp | canceldraw.setCanceled(true);
|
292 | 1223 | fernando | |
293 | 3368 | fjp | /* while (!isCancelled) {
|
294 | 1243 | fjp | if (!drawer.isAlive()) {
|
295 | 1327 | fjp | // Si hemos llegado aqu? con un thread vivo, seguramente
|
296 | // no estamos actualizados.
|
||
297 | |||
298 | break;
|
||
299 | 1243 | fjp | }
|
300 | 3719 | caballero | |
301 | 1223 | fernando | }
|
302 | canceldraw.setCancel(false);
|
||
303 | isCancelled = false;
|
||
304 | 3368 | fjp | drawerAlive = false; */
|
305 | 1223 | fernando | } |
306 | 3719 | caballero | |
307 | 3375 | fjp | private boolean adaptToImageSize() |
308 | { |
||
309 | if ((image == null) || (vp.getImageWidth() != this.getWidth()) || (vp.getImageHeight() != this.getHeight())) |
||
310 | { |
||
311 | image = new BufferedImage(this.getWidth(), this.getHeight(), |
||
312 | BufferedImage.TYPE_INT_ARGB);
|
||
313 | 6212 | fjp | // ESTILO MAC
|
314 | // image = GraphicsEnvironment.getLocalGraphicsEnvironment()
|
||
315 | // .getDefaultScreenDevice().getDefaultConfiguration()
|
||
316 | // .createCompatibleImage(this.getWidth(), this.getHeight());
|
||
317 | 3375 | fjp | vp.setImageSize(new Dimension(getWidth(), getHeight())); |
318 | 6630 | caballero | getMapContext().getViewPort().refreshExtent(); |
319 | 3719 | caballero | |
320 | |||
321 | 3375 | fjp | Graphics gTemp = image.createGraphics();
|
322 | Color theBackColor = vp.getBackColor();
|
||
323 | if (theBackColor == null) |
||
324 | gTemp.setColor(Color.WHITE);
|
||
325 | else
|
||
326 | gTemp.setColor(theBackColor); |
||
327 | 3719 | caballero | |
328 | 3375 | fjp | gTemp.fillRect(0,0,getWidth(), getHeight()); |
329 | gTemp.dispose(); |
||
330 | status = DESACTUALIZADO; |
||
331 | // g.drawImage(image,0,0,null);
|
||
332 | return true; |
||
333 | } |
||
334 | return false; |
||
335 | } |
||
336 | 1223 | fernando | |
337 | /**
|
||
338 | * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
|
||
339 | */
|
||
340 | protected void paintComponent(Graphics g) { |
||
341 | 3375 | fjp | adaptToImageSize(); |
342 | 3346 | fjp | /* if (status == FAST_PAINT) {
|
343 | 3331 | fjp | System.out.println("FAST_PAINT");
|
344 | g.drawImage(image,0,0,null);
|
||
345 | status = ACTUALIZADO;
|
||
346 | return;
|
||
347 | 3346 | fjp | } */
|
348 | 4568 | fjp | // System.out.println("PINTANDO MAPCONTROL" + this);
|
349 | 1223 | fernando | if (status == ACTUALIZADO) {
|
350 | 1680 | luisw | // LWS logger.debug("Dibujando la imagen obtenida");
|
351 | 1223 | fernando | |
352 | /*
|
||
353 | * Si hay un behaviour y la imagen es distinta de null se delega el dibujado
|
||
354 | * en dicho behaviour
|
||
355 | */
|
||
356 | 3372 | fjp | if (image != null) |
357 | { |
||
358 | if (currentMapTool != null) |
||
359 | currentMapTool.paintComponent(g); |
||
360 | else
|
||
361 | g.drawImage(image,0,0,null); |
||
362 | 3719 | caballero | |
363 | 3372 | fjp | // System.out.println("Pinto ACTUALIZADO");
|
364 | 1223 | fernando | } |
365 | 3719 | caballero | } else if ((status == DESACTUALIZADO) |
366 | 2946 | fjp | || (status == ONLY_GRAPHICS)) { |
367 | 1680 | luisw | // LWS System.out.println("DESACTUALIZADO: Obteniendo la imagen de la cartograf?a");
|
368 | 1223 | fernando | /* if (isOpaque())
|
369 | {
|
||
370 | if (image==null)
|
||
371 | {
|
||
372 | g.setColor(vp.getBackColor());
|
||
373 | 3719 | caballero | g.fillRect(0,0,getWidth(), getHeight());
|
374 | }
|
||
375 | 1223 | fernando | // else g.drawImage(image,0,0,null);
|
376 | } */
|
||
377 | 3368 | fjp | // cancelDrawing();
|
378 | 1223 | fernando | //Se crea la imagen con el color de fonde deseado
|
379 | 3375 | fjp | /* if (image == null)
|
380 | 1223 | fernando | {
|
381 | image = new BufferedImage(this.getWidth(), this.getHeight(),
|
||
382 | BufferedImage.TYPE_INT_ARGB);
|
||
383 | vp.setImageSize(new Dimension(getWidth(), getHeight()));
|
||
384 | Graphics gTemp = image.createGraphics();
|
||
385 | 1303 | fjp | Color theBackColor = vp.getBackColor();
|
386 | if (theBackColor == null)
|
||
387 | gTemp.setColor(Color.WHITE);
|
||
388 | else
|
||
389 | gTemp.setColor(theBackColor);
|
||
390 | 3719 | caballero | |
391 | 1223 | fernando | gTemp.fillRect(0,0,getWidth(), getHeight());
|
392 | gTemp.dispose();
|
||
393 | // g.drawImage(image,0,0,null);
|
||
394 | 3174 | ldiaz | System.out.println("Imagen con null en DESACTUALIZADO. Width = " + this.getWidth());
|
395 | 3375 | fjp | } */
|
396 | 3331 | fjp | // else
|
397 | // {
|
||
398 | 3719 | caballero | |
399 | |||
400 | 3372 | fjp | // if (image != null)
|
401 | // {
|
||
402 | 3331 | fjp | g.drawImage(image,0,0,null); |
403 | 3719 | caballero | |
404 | 3372 | fjp | drawer2.put(new PaintingRequest());
|
405 | timer.start(); |
||
406 | /* }
|
||
407 | else
|
||
408 | return; */
|
||
409 | 3331 | fjp | // }
|
410 | 3719 | caballero | |
411 | 3368 | fjp | /* if (drawerAlive == false)
|
412 | 3331 | fjp | {
|
413 | drawer = new Drawer(image, canceldraw);
|
||
414 | drawer.start();
|
||
415 | 3719 | caballero | //Se lanza el tread de dibujado
|
416 | 3368 | fjp | } */
|
417 | 3719 | caballero | |
418 | 2946 | fjp | // status = ACTUALIZADO;
|
419 | 1223 | fernando | } |
420 | } |
||
421 | |||
422 | /**
|
||
423 | * Devuelve la imagen de la vista.
|
||
424 | *
|
||
425 | * @return imagen.
|
||
426 | */
|
||
427 | public BufferedImage getImage() { |
||
428 | return image;
|
||
429 | } |
||
430 | |||
431 | /**
|
||
432 | * Marca el mapa para que en el pr?ximo redibujado se acceda a la
|
||
433 | * cartograf?a para reobtener la imagen
|
||
434 | 3422 | fjp | * @param doClear Solo deber?a ser true cuando se llama desde pan
|
435 | 1223 | fernando | */
|
436 | public void drawMap(boolean doClear) { |
||
437 | 3979 | caballero | cancelDrawing(); |
438 | System.out.println("drawMap con doClear=" + doClear); |
||
439 | 3346 | fjp | status = DESACTUALIZADO; |
440 | 5923 | fjp | getMapContext().getLayers().setDirty(true);
|
441 | 3331 | fjp | if (doClear)
|
442 | { |
||
443 | 3372 | fjp | // image = null; // Se usa para el PAN
|
444 | if (image != null) |
||
445 | { |
||
446 | Graphics2D g = image.createGraphics();
|
||
447 | Color theBackColor = vp.getBackColor();
|
||
448 | if (theBackColor == null) |
||
449 | g.setColor(Color.WHITE);
|
||
450 | else
|
||
451 | g.setColor(theBackColor); |
||
452 | g.fillRect(0, 0, vp.getImageWidth(), vp.getImageHeight()); |
||
453 | g.dispose(); |
||
454 | 3719 | caballero | } |
455 | 3331 | fjp | } |
456 | 1223 | fernando | repaint(); |
457 | } |
||
458 | 6306 | caballero | |
459 | 5923 | fjp | public void rePaintDirtyLayers() |
460 | { |
||
461 | cancelDrawing(); |
||
462 | status = DESACTUALIZADO; |
||
463 | repaint(); |
||
464 | } |
||
465 | 6306 | caballero | |
466 | 2901 | fjp | public void drawGraphics() { |
467 | status = ONLY_GRAPHICS; |
||
468 | repaint(); |
||
469 | } |
||
470 | 1223 | fernando | |
471 | /**
|
||
472 | * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
|
||
473 | */
|
||
474 | public void componentHidden(ComponentEvent e) { |
||
475 | } |
||
476 | |||
477 | /**
|
||
478 | * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
|
||
479 | */
|
||
480 | public void componentMoved(ComponentEvent e) { |
||
481 | } |
||
482 | |||
483 | /**
|
||
484 | * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
|
||
485 | */
|
||
486 | public void componentResized(ComponentEvent e) { |
||
487 | 3375 | fjp | /* image = new BufferedImage(this.getWidth(), this.getHeight(),
|
488 | 3719 | caballero | BufferedImage.TYPE_INT_ARGB);
|
489 | 1223 | fernando | Graphics gTemp = image.createGraphics();
|
490 | gTemp.setColor(vp.getBackColor());
|
||
491 | 3372 | fjp | gTemp.fillRect(0,0,getWidth(), getHeight());
|
492 | System.out.println("MapControl resized");
|
||
493 | // image = null;
|
||
494 | 1223 | fernando | vp.setImageSize(new Dimension(getWidth(), getHeight()));
|
495 | 3375 | fjp | getMapContext().getViewPort().setScale(); */
|
496 | 3372 | fjp | // drawMap(true);
|
497 | 1223 | fernando | } |
498 | |||
499 | /**
|
||
500 | * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
|
||
501 | */
|
||
502 | public void componentShown(ComponentEvent e) { |
||
503 | } |
||
504 | |||
505 | /**
|
||
506 | * A?ade un listener de tipo ExceptionListener.
|
||
507 | *
|
||
508 | * @param o ExceptionListener.
|
||
509 | */
|
||
510 | public void addExceptionListener(ExceptionListener o) { |
||
511 | exceptionHandlingSupport.addExceptionListener(o); |
||
512 | } |
||
513 | |||
514 | /**
|
||
515 | * Borra la ExceptioListener que se pasa como par?metro.
|
||
516 | *
|
||
517 | * @param o ExceptionListener.
|
||
518 | *
|
||
519 | * @return True si se borra correctamente.
|
||
520 | */
|
||
521 | public boolean removeExceptionListener(ExceptionListener o) { |
||
522 | return exceptionHandlingSupport.removeExceptionListener(o);
|
||
523 | } |
||
524 | |||
525 | /**
|
||
526 | * Lanza una Excepci?n.
|
||
527 | *
|
||
528 | * @param t Excepci?n.
|
||
529 | */
|
||
530 | 10520 | ldiaz | protected void throwException(Throwable t) { |
531 | 1223 | fernando | exceptionHandlingSupport.throwException(t); |
532 | } |
||
533 | |||
534 | 3719 | caballero | |
535 | 3368 | fjp | private class PaintingRequest |
536 | { |
||
537 | 3719 | caballero | |
538 | 3372 | fjp | public PaintingRequest()
|
539 | 3368 | fjp | { |
540 | } |
||
541 | 3719 | caballero | |
542 | 3368 | fjp | public void paint() |
543 | { |
||
544 | 3719 | caballero | try
|
545 | { |
||
546 | 4832 | fjp | canceldraw.setCanceled(false);
|
547 | 3372 | fjp | /* if (image == null)
|
548 | {
|
||
549 | image = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(),
|
||
550 | BufferedImage.TYPE_INT_ARGB);
|
||
551 | Graphics gTemp = image.createGraphics();
|
||
552 | Color theBackColor = vp.getBackColor();
|
||
553 | if (theBackColor == null)
|
||
554 | gTemp.setColor(Color.WHITE);
|
||
555 | else
|
||
556 | gTemp.setColor(theBackColor);
|
||
557 | 3719 | caballero | |
558 | 3372 | fjp | gTemp.fillRect(0,0,getWidth(), getHeight());
|
559 | gTemp.dispose();
|
||
560 | // g.drawImage(image,0,0,null);
|
||
561 | System.out.println("Imagen con null en DESACTUALIZADO. Width = " + this.getWidth());
|
||
562 | } */
|
||
563 | 3368 | fjp | Graphics2D g = image.createGraphics();
|
564 | 3719 | caballero | |
565 | 3368 | fjp | ViewPort viewPort = mapContext.getViewPort(); |
566 | 3719 | caballero | |
567 | 3368 | fjp | if (status == DESACTUALIZADO)
|
568 | { |
||
569 | 5874 | fjp | Graphics2D gTemp = image.createGraphics();
|
570 | 3368 | fjp | Color theBackColor = viewPort.getBackColor();
|
571 | if (theBackColor == null) |
||
572 | 5874 | fjp | gTemp.setColor(Color.WHITE);
|
573 | 3368 | fjp | else
|
574 | 5874 | fjp | gTemp.setColor(theBackColor); |
575 | gTemp.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight()); |
||
576 | 3368 | fjp | status = ACTUALIZADO; |
577 | 6261 | fjp | // ESTILO MAC
|
578 | // BufferedImage imgMac = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(),
|
||
579 | // BufferedImage.TYPE_INT_ARGB);
|
||
580 | 6306 | caballero | //
|
581 | 6261 | fjp | // mapContext.draw(imgMac, g, canceldraw, mapContext.getScaleView());
|
582 | // g.drawImage(imgMac, 0, 0, null);
|
||
583 | // FIN ESTILO MAC
|
||
584 | // SIN MAC:
|
||
585 | 3372 | fjp | mapContext.draw(image, g, canceldraw, mapContext.getScaleView()); |
586 | 3719 | caballero | |
587 | 3368 | fjp | } |
588 | else if (status == ONLY_GRAPHICS) |
||
589 | { |
||
590 | 3719 | caballero | status = ACTUALIZADO; |
591 | 3372 | fjp | mapContext.drawGraphics(image, g, canceldraw,mapContext.getScaleView()); |
592 | 3719 | caballero | |
593 | 3368 | fjp | } |
594 | 3719 | caballero | |
595 | |||
596 | 3368 | fjp | // status = FAST_PAINT;
|
597 | 3719 | caballero | // drawerAlive = false;
|
598 | 3372 | fjp | timer.stop(); |
599 | 3368 | fjp | repaint(); |
600 | 3719 | caballero | |
601 | |||
602 | 3368 | fjp | } catch (Throwable e) { |
603 | timer.stop(); |
||
604 | 3719 | caballero | // isCancelled = true;
|
605 | 3368 | fjp | e.printStackTrace(); |
606 | throwException(e); |
||
607 | } finally {
|
||
608 | } |
||
609 | 3719 | caballero | |
610 | 3368 | fjp | } |
611 | 3719 | caballero | |
612 | 3368 | fjp | } |
613 | 3719 | caballero | |
614 | 3368 | fjp | /**
|
615 | * @author fjp
|
||
616 | 3719 | caballero | *
|
617 | 3368 | fjp | * Basasdo en el patr?n WorkerThread
|
618 | *
|
||
619 | */
|
||
620 | public class Drawer2 |
||
621 | { |
||
622 | // Una mini cola de 2. No acumulamos peticiones de dibujado
|
||
623 | // dibujamos solo lo ?ltimo que nos han pedido.
|
||
624 | private PaintingRequest paintingRequest;
|
||
625 | private PaintingRequest waitingRequest;
|
||
626 | 3719 | caballero | |
627 | 3368 | fjp | private boolean waiting; |
628 | private boolean shutdown; |
||
629 | 3719 | caballero | |
630 | 3368 | fjp | public void setShutdown(boolean isShutdown) |
631 | { |
||
632 | shutdown = isShutdown; |
||
633 | } |
||
634 | 3719 | caballero | |
635 | public Drawer2()
|
||
636 | 3368 | fjp | { |
637 | paintingRequest = null;
|
||
638 | waitingRequest = null;
|
||
639 | waiting = false;
|
||
640 | shutdown = false;
|
||
641 | new Thread(new Worker()).start(); |
||
642 | } |
||
643 | 3719 | caballero | |
644 | 3368 | fjp | public void put(PaintingRequest newPaintRequest) |
645 | { |
||
646 | waitingRequest = newPaintRequest; |
||
647 | if (waiting)
|
||
648 | { |
||
649 | synchronized (this) { |
||
650 | notifyAll(); |
||
651 | } |
||
652 | } |
||
653 | } |
||
654 | 3719 | caballero | |
655 | 3368 | fjp | public PaintingRequest take()
|
656 | { |
||
657 | if (waitingRequest == null) |
||
658 | { |
||
659 | synchronized (this) { |
||
660 | waiting = true;
|
||
661 | try {
|
||
662 | wait(); |
||
663 | } |
||
664 | catch (InterruptedException ie) |
||
665 | { |
||
666 | waiting = false;
|
||
667 | } |
||
668 | } |
||
669 | } |
||
670 | paintingRequest = waitingRequest; |
||
671 | waitingRequest = null;
|
||
672 | return paintingRequest;
|
||
673 | } |
||
674 | 3719 | caballero | |
675 | |||
676 | 3368 | fjp | private class Worker implements Runnable |
677 | { |
||
678 | public void run() |
||
679 | { |
||
680 | while (!shutdown)
|
||
681 | { |
||
682 | PaintingRequest p = take(); |
||
683 | System.out.println("Pintando"); |
||
684 | 4120 | caballero | if (image != null){ |
685 | cancelDrawing(); |
||
686 | 3372 | fjp | p.paint(); |
687 | 4120 | caballero | } else
|
688 | 3372 | fjp | status = DESACTUALIZADO; |
689 | 3368 | fjp | } |
690 | } |
||
691 | } |
||
692 | } |
||
693 | 3719 | caballero | |
694 | 1223 | fernando | /**
|
695 | * Clase utilizada para dibujar las capas.
|
||
696 | *
|
||
697 | * @author Vicente Caballero Navarro
|
||
698 | */
|
||
699 | public class Drawer extends Thread { |
||
700 | 2531 | caballero | //private Graphics g;
|
701 | 3331 | fjp | private BufferedImage image = null; |
702 | 1223 | fernando | private CancelDraw cancel;
|
703 | 2531 | caballero | //private boolean threadCancel = false;
|
704 | 1223 | fernando | |
705 | /**
|
||
706 | * Crea un nuevo Drawer.
|
||
707 | *
|
||
708 | */
|
||
709 | 3331 | fjp | public Drawer(BufferedImage image, CancelDraw cancel) |
710 | { |
||
711 | 1223 | fernando | this.image = image;
|
712 | this.cancel = cancel;
|
||
713 | 3719 | caballero | // drawerAlive = true;
|
714 | 1223 | fernando | } |
715 | |||
716 | /**
|
||
717 | * @see java.lang.Runnable#run()
|
||
718 | */
|
||
719 | public void run() { |
||
720 | try {
|
||
721 | 2857 | jaume | // synchronized (Drawer.class) {
|
722 | 1282 | fjp | Graphics2D g = image.createGraphics();
|
723 | 3719 | caballero | |
724 | 1282 | fjp | ViewPort viewPort = mapContext.getViewPort(); |
725 | 2946 | fjp | if (status == DESACTUALIZADO)
|
726 | { |
||
727 | Color theBackColor = viewPort.getBackColor();
|
||
728 | if (theBackColor == null) |
||
729 | g.setColor(Color.WHITE);
|
||
730 | else
|
||
731 | g.setColor(theBackColor); |
||
732 | g.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight()); |
||
733 | status = ACTUALIZADO; |
||
734 | mapContext.draw(image, g, cancel,mapContext.getScaleView()); |
||
735 | } |
||
736 | else if (status == ONLY_GRAPHICS) |
||
737 | { |
||
738 | 2951 | fjp | status = ACTUALIZADO; |
739 | 2946 | fjp | mapContext.drawGraphics(image, g, cancel,mapContext.getScaleView()); |
740 | } |
||
741 | 3719 | caballero | |
742 | 1327 | fjp | timer.stop(); |
743 | 3346 | fjp | // status = FAST_PAINT;
|
744 | 3719 | caballero | // drawerAlive = false;
|
745 | 1223 | fernando | repaint(); |
746 | 3719 | caballero | |
747 | |||
748 | |||
749 | 2857 | jaume | // }
|
750 | 1223 | fernando | } catch (Throwable e) { |
751 | 1266 | fernando | timer.stop(); |
752 | 3719 | caballero | //isCancelled = true;
|
753 | 2943 | fjp | e.printStackTrace(); |
754 | 1223 | fernando | throwException(e); |
755 | } finally {
|
||
756 | } |
||
757 | } |
||
758 | } |
||
759 | |||
760 | /**
|
||
761 | * Clase para cancelar el dibujado.
|
||
762 | *
|
||
763 | * @author Fernando Gonz?lez Cort?s
|
||
764 | */
|
||
765 | public class CancelDraw implements Cancellable { |
||
766 | private boolean cancel = false; |
||
767 | |||
768 | /**
|
||
769 | * Crea un nuevo CancelDraw.
|
||
770 | */
|
||
771 | public CancelDraw() {
|
||
772 | } |
||
773 | |||
774 | /**
|
||
775 | * Insertar si se debe cancelar el dibujado.
|
||
776 | *
|
||
777 | * @param b true si se debe cancelar el dibujado.
|
||
778 | */
|
||
779 | 4832 | fjp | public void setCanceled(boolean b) { |
780 | 1223 | fernando | cancel = b; |
781 | } |
||
782 | |||
783 | /**
|
||
784 | 5317 | fjp | * @see com.iver.utiles.swing.threads.Cancellable#isCanceled()
|
785 | 1223 | fernando | */
|
786 | public boolean isCanceled() { |
||
787 | return cancel;
|
||
788 | } |
||
789 | } |
||
790 | |||
791 | 6306 | caballero | |
792 | 1223 | fernando | /**
|
793 | * Listener del MapTool.
|
||
794 | *
|
||
795 | * @author Fernando Gonz?lez Cort?s
|
||
796 | */
|
||
797 | public class MapToolListener implements MouseListener, MouseWheelListener, |
||
798 | MouseMotionListener {
|
||
799 | 5881 | fjp | |
800 | long t1;
|
||
801 | Point2D pReal;
|
||
802 | 1223 | fernando | /**
|
803 | * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
||
804 | */
|
||
805 | public void mouseClicked(MouseEvent e) { |
||
806 | try {
|
||
807 | currentMapTool.mouseClicked(e); |
||
808 | } catch (BehaviorException t) {
|
||
809 | throwException(t); |
||
810 | } |
||
811 | } |
||
812 | |||
813 | /**
|
||
814 | * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
||
815 | */
|
||
816 | public void mouseEntered(MouseEvent e) { |
||
817 | try {
|
||
818 | currentMapTool.mouseEntered(e); |
||
819 | } catch (BehaviorException t) {
|
||
820 | throwException(t); |
||
821 | } |
||
822 | } |
||
823 | |||
824 | /**
|
||
825 | * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
||
826 | */
|
||
827 | public void mouseExited(MouseEvent e) { |
||
828 | try {
|
||
829 | currentMapTool.mouseExited(e); |
||
830 | } catch (BehaviorException t) {
|
||
831 | throwException(t); |
||
832 | } |
||
833 | } |
||
834 | |||
835 | /**
|
||
836 | * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
||
837 | */
|
||
838 | public void mousePressed(MouseEvent e) { |
||
839 | try {
|
||
840 | currentMapTool.mousePressed(e); |
||
841 | } catch (BehaviorException t) {
|
||
842 | throwException(t); |
||
843 | } |
||
844 | } |
||
845 | |||
846 | /**
|
||
847 | * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
||
848 | */
|
||
849 | public void mouseReleased(MouseEvent e) { |
||
850 | try {
|
||
851 | currentMapTool.mouseReleased(e); |
||
852 | } catch (BehaviorException t) {
|
||
853 | throwException(t); |
||
854 | } |
||
855 | } |
||
856 | |||
857 | /**
|
||
858 | * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
|
||
859 | */
|
||
860 | public void mouseWheelMoved(MouseWheelEvent e) { |
||
861 | try {
|
||
862 | currentMapTool.mouseWheelMoved(e); |
||
863 | 6306 | caballero | |
864 | 5874 | fjp | // Si el tool actual no ha consumido el evento
|
865 | // entendemos que quiere el comportamiento por defecto.
|
||
866 | if (!e.isConsumed())
|
||
867 | { |
||
868 | 5881 | fjp | // Para usar el primer punto sobre el que queremos centrar
|
869 | // el mapa, dejamos pasar un segundo para considerar el siguiente
|
||
870 | // punto como v?lido.
|
||
871 | if (t1 == 0) |
||
872 | { |
||
873 | t1= System.currentTimeMillis();
|
||
874 | pReal = vp.toMapPoint(e.getPoint()); |
||
875 | } |
||
876 | else
|
||
877 | { |
||
878 | long t2 = System.currentTimeMillis(); |
||
879 | if ((t2-t1) > 1000) |
||
880 | t1=0;
|
||
881 | } |
||
882 | 5874 | fjp | cancelDrawing(); |
883 | ViewPort vp = getViewPort(); |
||
884 | |||
885 | 6306 | caballero | |
886 | 5874 | fjp | /* Point2D pReal = new Point2D.Double(vp.getAdjustedExtent().getCenterX(),
|
887 | vp.getAdjustedExtent().getCenterY()); */
|
||
888 | int amount = e.getWheelRotation();
|
||
889 | double nuevoX;
|
||
890 | double nuevoY;
|
||
891 | double factor;
|
||
892 | |||
893 | 6306 | caballero | if (amount < 0) // nos acercamos |
894 | 5874 | fjp | { |
895 | factor = 0.9;
|
||
896 | } else // nos alejamos |
||
897 | { |
||
898 | factor = 1.2;
|
||
899 | } |
||
900 | Rectangle2D.Double r = new Rectangle2D.Double(); |
||
901 | if (vp.getExtent() != null) { |
||
902 | nuevoX = pReal.getX() |
||
903 | - ((vp.getExtent().getWidth() * factor) / 2.0);
|
||
904 | nuevoY = pReal.getY() |
||
905 | - ((vp.getExtent().getHeight() * factor) / 2.0);
|
||
906 | r.x = nuevoX; |
||
907 | r.y = nuevoY; |
||
908 | r.width = vp.getExtent().getWidth() * factor; |
||
909 | r.height = vp.getExtent().getHeight() * factor; |
||
910 | |||
911 | vp.setExtent(r); |
||
912 | } |
||
913 | 6306 | caballero | |
914 | |||
915 | 5874 | fjp | } |
916 | 1223 | fernando | } catch (BehaviorException t) {
|
917 | throwException(t); |
||
918 | } |
||
919 | } |
||
920 | |||
921 | /**
|
||
922 | * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
||
923 | */
|
||
924 | public void mouseDragged(MouseEvent e) { |
||
925 | try {
|
||
926 | currentMapTool.mouseDragged(e); |
||
927 | } catch (BehaviorException t) {
|
||
928 | throwException(t); |
||
929 | } |
||
930 | } |
||
931 | |||
932 | /**
|
||
933 | * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
||
934 | */
|
||
935 | public void mouseMoved(MouseEvent e) { |
||
936 | try {
|
||
937 | currentMapTool.mouseMoved(e); |
||
938 | } catch (BehaviorException t) {
|
||
939 | throwException(t); |
||
940 | } |
||
941 | } |
||
942 | } |
||
943 | |||
944 | /**
|
||
945 | * Listener sobre el MapContext.
|
||
946 | *
|
||
947 | * @author Fernando Gonz?lez Cort?s
|
||
948 | */
|
||
949 | public class MapContextListener implements AtomicEventListener { |
||
950 | /**
|
||
951 | * @see com.iver.cit.gvsig.fmap.AtomicEventListener#atomicEvent(com.iver.cit.gvsig.fmap.AtomicEvent)
|
||
952 | */
|
||
953 | public void atomicEvent(AtomicEvent e) { |
||
954 | boolean redraw = false; |
||
955 | LayerEvent[] layerEvents = e.getLayerEvents();
|
||
956 | |||
957 | for (int i = 0; i < layerEvents.length; i++) { |
||
958 | if (layerEvents[i].getProperty().equals("visible")) { |
||
959 | redraw = true;
|
||
960 | } |
||
961 | } |
||
962 | |||
963 | if (e.getColorEvents().length > 0) { |
||
964 | redraw = true;
|
||
965 | } |
||
966 | |||
967 | if (e.getExtentEvents().length > 0) { |
||
968 | redraw = true;
|
||
969 | } |
||
970 | 6306 | caballero | |
971 | 5941 | jmvivo | if (e.getProjectionEvents().length > 0) { |
972 | //redraw = true;
|
||
973 | } |
||
974 | 1223 | fernando | |
975 | 6306 | caballero | |
976 | 10401 | fjp | LayerCollectionEvent[] aux = e.getLayerCollectionEvents();
|
977 | if (aux.length > 0) { |
||
978 | for (int i=0; i < aux.length; i++) |
||
979 | { |
||
980 | if (aux[i].getAffectedLayer().getFLayerStatus().isDriverLoaded())
|
||
981 | { |
||
982 | redraw = true;
|
||
983 | break;
|
||
984 | } |
||
985 | } |
||
986 | |||
987 | 1223 | fernando | } |
988 | |||
989 | if (e.getLegendEvents().length > 0) { |
||
990 | redraw = true;
|
||
991 | } |
||
992 | |||
993 | if (e.getSelectionEvents().length > 0) { |
||
994 | redraw = true;
|
||
995 | } |
||
996 | |||
997 | if (redraw) {
|
||
998 | 3331 | fjp | System.out.println("MapContextListener redraw"); |
999 | 3422 | fjp | MapControl.this.drawMap(false);
|
1000 | 1223 | fernando | } |
1001 | } |
||
1002 | } |
||
1003 | public ViewPort getViewPort() {
|
||
1004 | return vp;
|
||
1005 | } |
||
1006 | 3719 | caballero | |
1007 | 2877 | caballero | /**
|
1008 | 3719 | caballero | * Returns a HashMap with the tools that have been registered
|
1009 | 2877 | caballero | * in the Mapcontrol.
|
1010 | */
|
||
1011 | 2876 | caballero | public HashMap getNamesMapTools() { |
1012 | return namesMapTools;
|
||
1013 | } |
||
1014 | 4120 | caballero | |
1015 | 4142 | caballero | public void commandRepaint() { |
1016 | 4127 | caballero | drawMap(false);
|
1017 | 4120 | caballero | } |
1018 | 4127 | caballero | |
1019 | public void commandRefresh() { |
||
1020 | // TODO Auto-generated method stub
|
||
1021 | } |
||
1022 | 4888 | caballero | |
1023 | public void setPrevTool() { |
||
1024 | setTool(prevTool); |
||
1025 | } |
||
1026 | 6306 | caballero | |
1027 | 6264 | fjp | public void zoomIn() { |
1028 | Behavior mapTool = (Behavior) namesMapTools.get("zoomIn");
|
||
1029 | ViewPort vp=getViewPort(); |
||
1030 | Rectangle2D r=getViewPort().getAdjustedExtent();
|
||
1031 | Point2D pCenter=vp.fromMapPoint(r.getCenterX(),r.getCenterY());
|
||
1032 | MouseEvent e=new MouseEvent((Component)this,MouseEvent.MOUSE_RELEASED,MouseEvent.ACTION_EVENT_MASK,MouseEvent.BUTTON1,(int)pCenter.getX(),(int)pCenter.getY(),1,true,MouseEvent.BUTTON1); |
||
1033 | try {
|
||
1034 | mapTool.mousePressed(e); |
||
1035 | mapTool.mouseReleased(e); |
||
1036 | } catch (BehaviorException t) {
|
||
1037 | throwException(t); |
||
1038 | } |
||
1039 | } |
||
1040 | public void zoomOut() { |
||
1041 | Behavior mapTool = (Behavior) namesMapTools.get("zoomOut");
|
||
1042 | ViewPort vp=getViewPort(); |
||
1043 | Rectangle2D r=getViewPort().getAdjustedExtent();
|
||
1044 | Point2D pCenter=vp.fromMapPoint(r.getCenterX(),r.getCenterY());
|
||
1045 | MouseEvent e=new MouseEvent((Component)this,MouseEvent.MOUSE_RELEASED,MouseEvent.ACTION_EVENT_MASK,MouseEvent.BUTTON1,(int)pCenter.getX(),(int)pCenter.getY(),1,true,MouseEvent.BUTTON1); |
||
1046 | try {
|
||
1047 | mapTool.mousePressed(e); |
||
1048 | mapTool.mouseReleased(e); |
||
1049 | } catch (BehaviorException t) {
|
||
1050 | throwException(t); |
||
1051 | } |
||
1052 | } |
||
1053 | 6261 | fjp | |
1054 | 6306 | caballero | |
1055 | 1223 | fernando | } |