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