svn-gvsig-desktop / tags / v1_0_2_Build_897 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / MapContext.java @ 10444
History | View | Annotate | Download (25.7 KB)
1 | 6878 | cesar | /* 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 | import java.awt.Graphics2D; |
||
44 | import java.awt.RenderingHints; |
||
45 | import java.awt.Toolkit; |
||
46 | import java.awt.geom.Point2D; |
||
47 | import java.awt.geom.Rectangle2D; |
||
48 | import java.awt.image.BufferedImage; |
||
49 | import java.util.ArrayList; |
||
50 | 8765 | jjdelcerro | import java.util.List; |
51 | import java.util.prefs.Preferences; |
||
52 | 6878 | cesar | |
53 | 9013 | caballero | import javax.print.attribute.PrintRequestAttributeSet; |
54 | |||
55 | 6878 | cesar | import org.cresques.cts.ICoordTrans; |
56 | import org.cresques.cts.IProjection; |
||
57 | import org.cresques.geo.Projected; |
||
58 | |||
59 | 9225 | fjp | import com.hardcode.gdbms.engine.instruction.FieldNotFoundException; |
60 | 6878 | cesar | import com.iver.cit.gvsig.fmap.core.IGeometry; |
61 | 8765 | jjdelcerro | import com.iver.cit.gvsig.fmap.core.ISymbol; |
62 | 6878 | cesar | import com.iver.cit.gvsig.fmap.layers.CancelationException; |
63 | import com.iver.cit.gvsig.fmap.layers.FLayer; |
||
64 | import com.iver.cit.gvsig.fmap.layers.FLayers; |
||
65 | 9225 | fjp | import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
66 | 6878 | cesar | import com.iver.cit.gvsig.fmap.layers.GraphicLayer; |
67 | import com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent; |
||
68 | import com.iver.cit.gvsig.fmap.layers.LayerCollectionListener; |
||
69 | import com.iver.cit.gvsig.fmap.layers.LayerDrawEvent; |
||
70 | import com.iver.cit.gvsig.fmap.layers.LayerDrawingListener; |
||
71 | import com.iver.cit.gvsig.fmap.layers.LayerPositionEvent; |
||
72 | import com.iver.cit.gvsig.fmap.layers.LegendListener; |
||
73 | import com.iver.cit.gvsig.fmap.layers.VectorialAdapter; |
||
74 | import com.iver.cit.gvsig.fmap.layers.XMLException; |
||
75 | import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
||
76 | import com.iver.cit.gvsig.fmap.layers.layerOperations.Classifiable; |
||
77 | import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable; |
||
78 | import com.iver.cit.gvsig.fmap.operations.selection.Record; |
||
79 | import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor; |
||
80 | import com.iver.cit.gvsig.fmap.operations.strategies.SelectedZoomVisitor; |
||
81 | import com.iver.cit.gvsig.fmap.operations.strategies.VisitException; |
||
82 | 9225 | fjp | import com.iver.cit.gvsig.fmap.rendering.VectorialLegend; |
83 | 6878 | cesar | import com.iver.utiles.XMLEntity; |
84 | import com.iver.utiles.swing.threads.Cancellable; |
||
85 | |||
86 | /**
|
||
87 | * Modelo del mapa.
|
||
88 | *
|
||
89 | * @author Fernando Gonz?lez Cort?s
|
||
90 | */
|
||
91 | public class MapContext implements Projected { |
||
92 | public static final double[] CHANGEM = { 1000, 1, 0.01, 0.001, 1609.344, |
||
93 | 0.9144, 0.3048, 0.0254 }; |
||
94 | |||
95 | public static final double[] CHANGE = { 100000, 100, 1, 0.1, 160934.4, |
||
96 | 91.44, 30.48, 2.54 }; |
||
97 | |||
98 | public static final int EQUALS = 0; |
||
99 | |||
100 | public static final int DISJOINT = 1; |
||
101 | |||
102 | public static final int INTERSECTS = 2; |
||
103 | |||
104 | public static final int TOUCHES = 3; |
||
105 | |||
106 | public static final int CROSSES = 4; |
||
107 | |||
108 | public static final int WITHIN = 5; |
||
109 | |||
110 | public static final int CONTAINS = 6; |
||
111 | |||
112 | public static final int OVERLAPS = 7; |
||
113 | |||
114 | 8765 | jjdelcerro | protected FLayers layers;
|
115 | 6878 | cesar | |
116 | private GraphicLayer tracLayer = new GraphicLayer(); |
||
117 | |||
118 | private ViewPort viewPort;
|
||
119 | |||
120 | // private ArrayList invalidationListeners = new ArrayList();
|
||
121 | private ArrayList legendListeners = new ArrayList(); |
||
122 | |||
123 | private ArrayList layerDrawingListeners = new ArrayList(); |
||
124 | |||
125 | private EventBuffer eventBuffer = new EventBuffer(); |
||
126 | |||
127 | private LayerEventListener layerEventListener = null; |
||
128 | |||
129 | private ArrayList layersError = new ArrayList(); |
||
130 | |||
131 | private ArrayList errorListeners = new ArrayList(); |
||
132 | |||
133 | // public static ResourceBundle myResourceBundle =
|
||
134 | // ResourceBundle.getBundle("FMap");
|
||
135 | public static double ZOOMINFACTOR=2; |
||
136 | public static double ZOOMOUTFACTOR=0.5; |
||
137 | /**
|
||
138 | * Crea un nuevo FMap.
|
||
139 | *
|
||
140 | * @param vp
|
||
141 | * ViewPort.
|
||
142 | */
|
||
143 | public MapContext(ViewPort vp) {
|
||
144 | 8765 | jjdelcerro | this.layers = new FLayers(this, null); |
145 | |||
146 | 6878 | cesar | layerEventListener = new LayerEventListener();
|
147 | layers.addLayerCollectionListener(layerEventListener); |
||
148 | layers.addLayerCollectionListener(eventBuffer); |
||
149 | |||
150 | 8765 | jjdelcerro | setViewPort(vp); |
151 | |||
152 | } |
||
153 | |||
154 | public MapContext(FLayers fLayers, ViewPort vp) {
|
||
155 | this.layers = fLayers;
|
||
156 | |||
157 | layerEventListener = new LayerEventListener();
|
||
158 | layers.addLayerCollectionListener(layerEventListener); |
||
159 | layers.addLayerCollectionListener(eventBuffer); |
||
160 | |||
161 | setViewPort(vp); |
||
162 | } |
||
163 | |||
164 | /**
|
||
165 | * Reports to all registered driver listener a group of driverexceptions
|
||
166 | * produced in the same fmap atomic transaction
|
||
167 | * @param driverExceptions
|
||
168 | */
|
||
169 | public synchronized void reportDriverExceptions(String introductoryText, |
||
170 | List driverExceptions){
|
||
171 | for (int i = 0; i < errorListeners.size(); i++) { |
||
172 | ((ErrorListener) errorListeners.get(i)).
|
||
173 | reportDriverExceptions(introductoryText, driverExceptions); |
||
174 | 6878 | cesar | } |
175 | } |
||
176 | |||
177 | 8765 | jjdelcerro | |
178 | 6878 | cesar | /**
|
179 | * A?ade un LegendListener.
|
||
180 | *
|
||
181 | * @param listener
|
||
182 | * LegendListener a a?adir.
|
||
183 | */
|
||
184 | public void addLayerListener(LegendListener listener) { |
||
185 | 8765 | jjdelcerro | if (!legendListeners.contains(listener))
|
186 | legendListeners.add(listener); |
||
187 | 6878 | cesar | } |
188 | |||
189 | public void addLayerDrawingListener(LayerDrawingListener listener) { |
||
190 | layerDrawingListeners.add(listener); |
||
191 | } |
||
192 | |||
193 | public void removeLayerDrawListener(LayerDrawingListener listener) { |
||
194 | layerDrawingListeners.remove(listener); |
||
195 | } |
||
196 | |||
197 | public void addErrorListener(ErrorListener listener) { |
||
198 | errorListeners.add(listener); |
||
199 | } |
||
200 | |||
201 | public void removeErrorListener(LegendListener listener) { |
||
202 | legendListeners.remove(listener); |
||
203 | } |
||
204 | |||
205 | /**
|
||
206 | * M?todo ejecutado cuando hay un cambio de leyenda que se quiera reflejar.
|
||
207 | *
|
||
208 | * @param e
|
||
209 | * LegendChangedEvent.
|
||
210 | */
|
||
211 | public synchronized void callLegendChanged() { |
||
212 | for (int i = 0; i < legendListeners.size(); i++) { |
||
213 | ((LegendListener) legendListeners.get(i)).legendChanged(null);
|
||
214 | } |
||
215 | // getLayers().moveTo(0,0);
|
||
216 | } |
||
217 | |||
218 | public synchronized void fireLayerDrawingEvent(LayerDrawEvent e) { |
||
219 | for (int i = 0; i < layerDrawingListeners.size(); i++) |
||
220 | { |
||
221 | LayerDrawingListener listener = (LayerDrawingListener) layerDrawingListeners.get(i); |
||
222 | switch (e.getEventType())
|
||
223 | { |
||
224 | case LayerDrawEvent.LAYER_BEFORE_DRAW:
|
||
225 | listener.beforeLayerDraw(e); |
||
226 | break;
|
||
227 | case LayerDrawEvent.LAYER_AFTER_DRAW:
|
||
228 | listener.afterLayerDraw(e); |
||
229 | break;
|
||
230 | case LayerDrawEvent.GRAPHICLAYER_BEFORE_DRAW:
|
||
231 | listener.beforeGraphicLayerDraw(e); |
||
232 | break;
|
||
233 | case LayerDrawEvent.GRAPHICLAYER_AFTER_DRAW:
|
||
234 | listener.afterLayerGraphicDraw(e); |
||
235 | break;
|
||
236 | } |
||
237 | } |
||
238 | // getLayers().moveTo(0,0);
|
||
239 | } |
||
240 | |||
241 | public synchronized void callNewErrorEvent(ErrorEvent e) { |
||
242 | for (int i = 0; i < errorListeners.size(); i++) { |
||
243 | ((ErrorListener) errorListeners.get(i)).errorThrown(e);
|
||
244 | } |
||
245 | // getLayers().moveTo(0,0);
|
||
246 | } |
||
247 | |||
248 | /**
|
||
249 | * Borra un LegendListener.
|
||
250 | *
|
||
251 | * @param listener
|
||
252 | * LegendListener a borrar.
|
||
253 | */
|
||
254 | public void removeLayerListener(LegendListener listener) { |
||
255 | legendListeners.remove(listener); |
||
256 | } |
||
257 | |||
258 | /**
|
||
259 | * Devuelve las capas que contiene el mapa.
|
||
260 | *
|
||
261 | * @return Capas.
|
||
262 | */
|
||
263 | public FLayers getLayers() {
|
||
264 | return layers;
|
||
265 | } |
||
266 | |||
267 | /**
|
||
268 | * Dibuja en la imagen que se pasa como par?metro el contenido de las capas
|
||
269 | * visibles del mapa y teniendo en cuenta los datos del ViewPort contenido
|
||
270 | * en este FMap
|
||
271 | *
|
||
272 | * @param b
|
||
273 | * Imagen.
|
||
274 | */
|
||
275 | public void drawLabels(BufferedImage b) { |
||
276 | } |
||
277 | |||
278 | /**
|
||
279 | * M?todo de conveniencia que se usa provisionalmente para solicitar un
|
||
280 | * refresco de todo lo que dependa del FMap (MapContext). Esto provocar? un
|
||
281 | * evento de cambio de orden de capas que obligar? a redibujar todo lo que
|
||
282 | * depende de FMap (TOC, MapControl, FFrameView, etc).
|
||
283 | */
|
||
284 | public void invalidate() { |
||
285 | getLayers().moveTo(0, 0); |
||
286 | } |
||
287 | |||
288 | /**
|
||
289 | * Imprime el las capas que contiene el FMap sobre el Graphics2D que se pasa
|
||
290 | * como par?metro, normalmente es el Graphics de la impresora.
|
||
291 | *
|
||
292 | * @param g
|
||
293 | * Graphics2D
|
||
294 | *
|
||
295 | * @throws DriverException
|
||
296 | */
|
||
297 | 9013 | caballero | public void print(Graphics2D g, double scale, PrintRequestAttributeSet properties) throws DriverException { |
298 | 6878 | cesar | RenderingHints renderHints = new RenderingHints( |
299 | RenderingHints.KEY_ANTIALIASING,
|
||
300 | RenderingHints.VALUE_ANTIALIAS_ON);
|
||
301 | renderHints.put(RenderingHints.KEY_RENDERING,
|
||
302 | RenderingHints.VALUE_RENDER_QUALITY);
|
||
303 | g.setRenderingHints(renderHints); |
||
304 | |||
305 | Cancellable cancel = new Cancellable() {
|
||
306 | public boolean isCanceled() { |
||
307 | return false; |
||
308 | } |
||
309 | |||
310 | public void setCanceled(boolean canceled) { |
||
311 | // No queremos que se pueda cancelar la impresi?n.
|
||
312 | |||
313 | } |
||
314 | }; |
||
315 | 9013 | caballero | layers.print(g, viewPort, cancel, scale, properties); |
316 | 6878 | cesar | tracLayer.draw(null, g, viewPort, cancel, scale);
|
317 | } |
||
318 | |||
319 | /**
|
||
320 | * Crea un nuevo FMap con la informaci?n del ViewPort que se pasa como
|
||
321 | * par?metro.
|
||
322 | *
|
||
323 | * @param vp
|
||
324 | * ViewPort.
|
||
325 | *
|
||
326 | * @return FMap nuevo.
|
||
327 | */
|
||
328 | public MapContext createNewFMap(ViewPort vp) {
|
||
329 | MapContext ret = new MapContext(vp);
|
||
330 | ret.layers = this.layers;
|
||
331 | |||
332 | return ret;
|
||
333 | } |
||
334 | |||
335 | /**
|
||
336 | * Crea un nuevo FMap totalmente desligado, se replican las capas y el
|
||
337 | * ViewPort
|
||
338 | 9225 | fjp | * Se crean unas nuevas capas, pero clonadas para usar
|
339 | * como fuentes de datos los drivers ya existentes. As?
|
||
340 | * evitamos consumir m?s memoria, a la vez que ganamos
|
||
341 | * rapidez.
|
||
342 | 6878 | cesar | *
|
343 | * @return FMap clonado.
|
||
344 | *
|
||
345 | * @throws XMLException
|
||
346 | */
|
||
347 | public MapContext cloneFMap() throws XMLException { |
||
348 | 9225 | fjp | ViewPort vp = getViewPort().cloneViewPort(); |
349 | FLayers antLayers = getLayers(); |
||
350 | MapContext ret = new MapContext(vp);
|
||
351 | FLayers aux = new FLayers(ret, null); |
||
352 | for (int i=0; i < antLayers.getLayersCount(); i++) |
||
353 | { |
||
354 | FLayer lyr = antLayers.getLayer(i); |
||
355 | try {
|
||
356 | aux.addLayer(lyr.cloneLayer()); |
||
357 | } catch (Exception e) { |
||
358 | // TODO Auto-generated catch block
|
||
359 | e.printStackTrace(); |
||
360 | } |
||
361 | } |
||
362 | ret.layers = aux; |
||
363 | return ret;
|
||
364 | |||
365 | // return createFromXML(getXMLEntity());
|
||
366 | 6878 | cesar | } |
367 | 8887 | fjp | public MapContext cloneToDraw() {
|
368 | ViewPort vp = getViewPort().cloneViewPort(); |
||
369 | MapContext mapContext=new MapContext(getLayers(),vp);
|
||
370 | return mapContext;
|
||
371 | } |
||
372 | 6878 | cesar | |
373 | /**
|
||
374 | * A?ade la capa que se pasa como par?metro al nodo que se pasa como
|
||
375 | * parametro y lanza ProjectionMismatchException si no est?n todas las capas
|
||
376 | * de este FMap en la misma proyecci?n. Lanza un ChildNotAllowedException si
|
||
377 | * la capa no es un FLayers y no permite hijos
|
||
378 | *
|
||
379 | * @param vectorial
|
||
380 | * DOCUMENT ME!
|
||
381 | */
|
||
382 | |||
383 | /*
|
||
384 | * public void addLayer(LayerPath parent, FLayer layer) throws
|
||
385 | * ProjectionMismatchException, ChildrenNotAllowedException {
|
||
386 | * layers.addLayer(parent, layer); } public void removeLayer(LayerPath
|
||
387 | * parent)throws ChildrenNotAllowedException{ layers.removeLayer(parent); }
|
||
388 | */
|
||
389 | |||
390 | /**
|
||
391 | * A?ade una capa al grupo de capas que se sit?a por encima de todas las
|
||
392 | * otras capas
|
||
393 | *
|
||
394 | * @param vectorial
|
||
395 | * FLayer.
|
||
396 | */
|
||
397 | public void addToTrackLayer(FLayer vectorial) { |
||
398 | } |
||
399 | |||
400 | /**
|
||
401 | * Devuelve la escala de la vista en pantalla.
|
||
402 | *
|
||
403 | * @return escala de la vista.
|
||
404 | */
|
||
405 | public long getScaleView() { |
||
406 | 8765 | jjdelcerro | Preferences prefsResolution = Preferences.userRoot().node( "gvsig.configuration.screen" ); |
407 | 6878 | cesar | Toolkit kit = Toolkit.getDefaultToolkit(); |
408 | 8765 | jjdelcerro | double dpi = prefsResolution.getInt("dpi",kit.getScreenResolution()); |
409 | 6878 | cesar | IProjection proj = viewPort.getProjection(); |
410 | |||
411 | if (viewPort.getImageSize() == null) |
||
412 | return -1; |
||
413 | |||
414 | if (viewPort.getAdjustedExtent() == null) { |
||
415 | return 0; |
||
416 | } |
||
417 | |||
418 | if (proj == null) { |
||
419 | 9552 | caballero | double w = ((viewPort.getImageSize().getWidth() / dpi) * 2.54); |
420 | 6878 | cesar | return (long) (viewPort.getAdjustedExtent().getWidth() / w * CHANGE[getViewPort() |
421 | .getMapUnits()]); |
||
422 | } |
||
423 | |||
424 | return Math.round(proj.getScale(viewPort.getAdjustedExtent().getMinX(), |
||
425 | viewPort.getAdjustedExtent().getMaxX(), viewPort.getImageSize() |
||
426 | .getWidth(), dpi)); |
||
427 | } |
||
428 | /**
|
||
429 | * Introduce un nuevo extent en la vista a partir de la escala que se pasa como par?metro.
|
||
430 | *
|
||
431 | * @return escala de la vista.
|
||
432 | */
|
||
433 | public void setScaleView(long scale) { |
||
434 | 9552 | caballero | Preferences prefsResolution = Preferences.userRoot().node( "gvsig.configuration.screen" ); |
435 | 6878 | cesar | Toolkit kit = Toolkit.getDefaultToolkit(); |
436 | 9552 | caballero | double dpi = prefsResolution.getInt("dpi",kit.getScreenResolution()); |
437 | 6878 | cesar | if (viewPort.getImageSize() == null) |
438 | return;
|
||
439 | 7196 | caballero | IProjection proj = viewPort.getProjection(); |
440 | 6878 | cesar | if (viewPort.getAdjustedExtent() == null) { |
441 | return;
|
||
442 | } |
||
443 | 7196 | caballero | Rectangle2D rec=proj.getExtent(viewPort.getAdjustedExtent(),scale,viewPort.getImageWidth(),viewPort.getImageHeight(),CHANGE[getViewPort().getMapUnits()],dpi);
|
444 | getViewPort().setExtent(rec); |
||
445 | 6878 | cesar | } |
446 | |||
447 | /**
|
||
448 | * @see org.cresques.geo.Projected#getProjection()
|
||
449 | */
|
||
450 | public IProjection getProjection() {
|
||
451 | return getViewPort().getProjection();
|
||
452 | } |
||
453 | |||
454 | /**
|
||
455 | * Inserta la proyecci?n.
|
||
456 | *
|
||
457 | * @param proj
|
||
458 | * Proyecci?n.
|
||
459 | */
|
||
460 | public void setProjection(IProjection proj) { |
||
461 | if (getViewPort() != null) { |
||
462 | getViewPort().setProjection(proj); |
||
463 | } |
||
464 | } |
||
465 | |||
466 | /**
|
||
467 | * @see org.cresques.geo.Projected#reProject(org.cresques.cts.ICoordTrans)
|
||
468 | */
|
||
469 | public void reProject(ICoordTrans arg0) { |
||
470 | // TODO implementar reprojecci?n (lo que sea eso)
|
||
471 | } |
||
472 | |||
473 | |||
474 | /**
|
||
475 | * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#getSelectionBounds()
|
||
476 | */
|
||
477 | public Rectangle2D getSelectionBounds() { |
||
478 | SelectedZoomVisitor visitor = new SelectedZoomVisitor();
|
||
479 | |||
480 | try {
|
||
481 | layers.process(visitor); |
||
482 | } catch (DriverException e1) {
|
||
483 | throw new RuntimeException( |
||
484 | "No se espera que SelectByPointVisitor lance esta excepci?n",
|
||
485 | e1); |
||
486 | } catch (VisitException e) {
|
||
487 | throw new RuntimeException( |
||
488 | "No se espera que SelectByPointVisitor lance esta excepci?n",
|
||
489 | e); |
||
490 | } |
||
491 | |||
492 | return visitor.getSelectBound();
|
||
493 | } |
||
494 | |||
495 | /**
|
||
496 | * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
|
||
497 | 8765 | jjdelcerro | * java.awt.Graphics2D, ISymbol)
|
498 | 6878 | cesar | */
|
499 | public void draw(BufferedImage image, Graphics2D g, Cancellable cancel, |
||
500 | double scale) throws DriverException { |
||
501 | if (viewPort.getExtent() == null) { |
||
502 | // System.err.println("viewPort.getExtent() = null");
|
||
503 | return;
|
||
504 | } |
||
505 | System.out.println("Viewport despues: " + viewPort.toString()); |
||
506 | /*
|
||
507 | * if ((viewPort.getImageWidth() <=0) || (viewPort.getImageHeight() <=
|
||
508 | * 0)) { return; }
|
||
509 | */
|
||
510 | |||
511 | prepareDrawing(image, g, scale); |
||
512 | |||
513 | // M?s c?lidad al texto
|
||
514 | RenderingHints renderHints = new RenderingHints( |
||
515 | RenderingHints.KEY_ANTIALIASING,
|
||
516 | RenderingHints.VALUE_ANTIALIAS_ON);
|
||
517 | renderHints.put(RenderingHints.KEY_RENDERING,
|
||
518 | RenderingHints.VALUE_RENDER_QUALITY);
|
||
519 | renderHints.put(RenderingHints.KEY_TEXT_ANTIALIASING,
|
||
520 | RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||
521 | g.setRenderingHints(renderHints); |
||
522 | |||
523 | long t1 = System.currentTimeMillis(); |
||
524 | layers.draw(image, g, viewPort, cancel, scale); |
||
525 | |||
526 | LayerDrawEvent beforeTracLayerEvent = new LayerDrawEvent(tracLayer,
|
||
527 | g, viewPort, LayerDrawEvent.GRAPHICLAYER_BEFORE_DRAW); |
||
528 | fireLayerDrawingEvent(beforeTracLayerEvent); |
||
529 | tracLayer.draw(image, g, viewPort, cancel, scale); |
||
530 | LayerDrawEvent afterTracLayerEvent = new LayerDrawEvent(tracLayer,
|
||
531 | g, viewPort, LayerDrawEvent.GRAPHICLAYER_AFTER_DRAW); |
||
532 | fireLayerDrawingEvent(afterTracLayerEvent); |
||
533 | 9047 | fjp | |
534 | layers.setDirty(false);
|
||
535 | 6878 | cesar | |
536 | long t2 = System.currentTimeMillis(); |
||
537 | System.err.println("Tiempo de dibujado:" + (t2 - t1) + |
||
538 | " mseg. Memoria libre:" + Runtime.getRuntime().freeMemory() / 1024 + " KB"); |
||
539 | /*
|
||
540 | * g.setColor(Color.BLUE); GeneralPath shpR = new
|
||
541 | * GeneralPath(viewPort.getExtent());
|
||
542 | * shpR.transform(viewPort.getAffineTransform()); g.draw(shpR);
|
||
543 | */
|
||
544 | System.gc();
|
||
545 | } |
||
546 | |||
547 | /**
|
||
548 | * En esta funci?n vamos a revisar las capas que necesitan repintarse,
|
||
549 | * por si hay alguna que se haya guardado la imagen de las anteriores
|
||
550 | * y puede acelerar el dibujado.
|
||
551 | * @param image
|
||
552 | * @param g
|
||
553 | * @param scale
|
||
554 | */
|
||
555 | private void prepareDrawing(BufferedImage image, Graphics2D g, double scale) { |
||
556 | |||
557 | // Primera pasada: si alguna capa necesita repintarse por debajo
|
||
558 | // de la que tiene la cache, TODAS necesitan
|
||
559 | // ser repintadas.
|
||
560 | boolean bNeedRepaint = false; |
||
561 | boolean bMayExistAceleration = false; |
||
562 | for (int i = 0; i < layers.getLayersCount(); i++) |
||
563 | { |
||
564 | FLayer lyr = layers.getLayer(i); |
||
565 | if (lyr.isCachingDrawnLayers() && (lyr.getCacheImageDrawnLayers() != null)) |
||
566 | { |
||
567 | bMayExistAceleration = true;
|
||
568 | } |
||
569 | else
|
||
570 | { |
||
571 | if (lyr.isDirty())
|
||
572 | bNeedRepaint = true;
|
||
573 | } |
||
574 | } |
||
575 | if (bMayExistAceleration==false) |
||
576 | bNeedRepaint = true;
|
||
577 | if (bNeedRepaint)
|
||
578 | layers.setDirty(true);
|
||
579 | else
|
||
580 | recursivePrepareDrawing(layers, 0);
|
||
581 | } |
||
582 | |||
583 | private void validatePreviousLayers(FLayers layers, int index) |
||
584 | { |
||
585 | // TODO: Aqu? quiz?s habr?a que explorar los padres de las capas
|
||
586 | // para marcar y/o asignar la imagen cacheada.
|
||
587 | for (int i = 0; i < index; i++) |
||
588 | { |
||
589 | FLayer lyr = layers.getLayer(i); |
||
590 | lyr.setDirty(false);
|
||
591 | } |
||
592 | // Las de arriba las marcamos como sucias
|
||
593 | // for (int i = index; i < layers.getLayersCount(); i++)
|
||
594 | // {
|
||
595 | // FLayer lyr = layers.getLayer(i);
|
||
596 | // lyr.setDirty(true);
|
||
597 | // }
|
||
598 | } |
||
599 | |||
600 | private void recursivePrepareDrawing(FLayers parent, int indexInParent) |
||
601 | { |
||
602 | for (int i = indexInParent; i < parent.getLayersCount(); i++) |
||
603 | { |
||
604 | FLayer lyr = layers.getLayer(i); |
||
605 | if (lyr.isCachingDrawnLayers() && (lyr.getCacheImageDrawnLayers() != null)) |
||
606 | { |
||
607 | // les decimos a las anteriores que est?n validadas (not dirty)
|
||
608 | // para que no se dibujen.
|
||
609 | if (lyr.isDirty())
|
||
610 | validatePreviousLayers(parent, i); |
||
611 | } |
||
612 | |||
613 | if (lyr instanceof FLayers) |
||
614 | { |
||
615 | recursivePrepareDrawing((FLayers)lyr, 0);
|
||
616 | } |
||
617 | } |
||
618 | |||
619 | } |
||
620 | |||
621 | public void drawGraphics(BufferedImage image, Graphics2D g, |
||
622 | Cancellable cancel, double scale) throws DriverException { |
||
623 | if (viewPort == null) |
||
624 | return;
|
||
625 | tracLayer.draw(image, g, viewPort, cancel, scale); |
||
626 | } |
||
627 | |||
628 | /**
|
||
629 | * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
|
||
630 | 8765 | jjdelcerro | * java.awt.Graphics2D, ISymbol)
|
631 | 6878 | cesar | */
|
632 | public void draw(BufferedImage image, Graphics2D g, double scale) |
||
633 | throws DriverException {
|
||
634 | layers.setDirty(true);
|
||
635 | draw(image, g, new Cancellable() {
|
||
636 | /**
|
||
637 | * @see com.iver.utiles.swing.threads.Cancellable#isCanceled()
|
||
638 | */
|
||
639 | public boolean isCanceled() { |
||
640 | return false; |
||
641 | } |
||
642 | |||
643 | public void setCanceled(boolean canceled) { |
||
644 | // TODO Auto-generated method stub
|
||
645 | |||
646 | } |
||
647 | }, scale); |
||
648 | } |
||
649 | |||
650 | /**
|
||
651 | * Devuelve el ViewPort.
|
||
652 | *
|
||
653 | * @return Returns the viewPort.
|
||
654 | */
|
||
655 | public ViewPort getViewPort() {
|
||
656 | return viewPort;
|
||
657 | } |
||
658 | |||
659 | /**
|
||
660 | * Inserta un ViewPort.
|
||
661 | *
|
||
662 | * @param viewPort
|
||
663 | * The viewPort to set.
|
||
664 | */
|
||
665 | public void setViewPort(ViewPort viewPort) { |
||
666 | if (this.viewPort != null) { |
||
667 | this.viewPort.removeViewPortListener(eventBuffer);
|
||
668 | } |
||
669 | |||
670 | this.viewPort = viewPort;
|
||
671 | 8765 | jjdelcerro | if (viewPort != null) |
672 | viewPort.addViewPortListener(eventBuffer); |
||
673 | 6878 | cesar | } |
674 | 9013 | caballero | |
675 | 8765 | jjdelcerro | /**
|
676 | * Sets the given zoom extent to the viewport.
|
||
677 | *
|
||
678 | * @param extent
|
||
679 | * The extent to zoom to.
|
||
680 | */
|
||
681 | public void zoomToExtent(Rectangle2D extent) { |
||
682 | getViewPort().setExtent(extent); |
||
683 | } |
||
684 | 6878 | cesar | |
685 | /**
|
||
686 | * M?todo de conveniencia. Recorre las capas y te da el fullExtent
|
||
687 | *
|
||
688 | * @return fullExtent de todas las capas.
|
||
689 | *
|
||
690 | * @throws DriverException
|
||
691 | */
|
||
692 | public Rectangle2D getFullExtent() throws DriverException { |
||
693 | return layers.getFullExtent();
|
||
694 | } |
||
695 | |||
696 | /**
|
||
697 | * Devuelve el XMLEntity.
|
||
698 | *
|
||
699 | * @return XMLEntity.
|
||
700 | * @throws XMLException
|
||
701 | */
|
||
702 | public XMLEntity getXMLEntity() throws XMLException { |
||
703 | XMLEntity xml = new XMLEntity();
|
||
704 | xml.putProperty("className", this.getClass().getName()); |
||
705 | xml.addChild(viewPort.getXMLEntity()); |
||
706 | xml.addChild(layers.getXMLEntity()); |
||
707 | |||
708 | return xml;
|
||
709 | } |
||
710 | |||
711 | /**
|
||
712 | * Crea un nuevo FMAp a partir del XMLEntity.
|
||
713 | *
|
||
714 | * @param xml
|
||
715 | * XMLEntity
|
||
716 | *
|
||
717 | * @return Nuevo FMap.
|
||
718 | *
|
||
719 | * @throws XMLException
|
||
720 | */
|
||
721 | public static MapContext createFromXML03(XMLEntity xml) throws XMLException { |
||
722 | ViewPort vp = ViewPort.createFromXML03(xml.getChild(0));
|
||
723 | MapContext fmap = new MapContext(vp);
|
||
724 | fmap.layers.setXMLEntity03(xml.getChild(1));
|
||
725 | |||
726 | return fmap;
|
||
727 | } |
||
728 | |||
729 | /**
|
||
730 | * Crea un nuevo FMAp a partir del XMLEntity.
|
||
731 | *
|
||
732 | * @param xml
|
||
733 | * XMLEntity
|
||
734 | *
|
||
735 | * @return Nuevo FMap.
|
||
736 | *
|
||
737 | * @throws XMLException
|
||
738 | */
|
||
739 | public static MapContext createFromXML(XMLEntity xml) throws XMLException { |
||
740 | ViewPort vp = ViewPort.createFromXML(xml.getChild(0));
|
||
741 | MapContext fmap = new MapContext(vp);
|
||
742 | fmap.layers.setXMLEntity(xml.getChild(1));
|
||
743 | |||
744 | return fmap;
|
||
745 | } |
||
746 | |||
747 | /**
|
||
748 | * A?ade un AtomicEventListener.
|
||
749 | *
|
||
750 | * @param listener
|
||
751 | * AtomicEventListener.
|
||
752 | *
|
||
753 | * @return True si se ha a?adido correctamente.
|
||
754 | */
|
||
755 | public boolean addAtomicEventListener(AtomicEventListener listener) { |
||
756 | return eventBuffer.addAtomicEventListener(listener);
|
||
757 | } |
||
758 | |||
759 | /**
|
||
760 | * Borra un AtomicEventListener de la lista de listeners.
|
||
761 | *
|
||
762 | * @param listener
|
||
763 | * AtomicEventListener a borrar.
|
||
764 | *
|
||
765 | * @return True si se ha borrado correctamente.
|
||
766 | */
|
||
767 | public boolean removeAtomicEventListener(AtomicEventListener listener) { |
||
768 | return eventBuffer.removeAtomicEventListener(listener);
|
||
769 | } |
||
770 | |||
771 | /**
|
||
772 | * Inicializa los AtomicEvent.
|
||
773 | */
|
||
774 | public void beginAtomicEvent() { |
||
775 | eventBuffer.beginAtomicEvent(); |
||
776 | } |
||
777 | |||
778 | /**
|
||
779 | * Finaliza los AtomicEvent.
|
||
780 | */
|
||
781 | public void endAtomicEvent() { |
||
782 | eventBuffer.endAtomicEvent(); |
||
783 | } |
||
784 | |||
785 | /**
|
||
786 | * Evento Layer.
|
||
787 | *
|
||
788 | * @author Fernando Gonz?lez Cort?s
|
||
789 | */
|
||
790 | public class LayerEventListener implements LayerCollectionListener { |
||
791 | /**
|
||
792 | * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdded(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
||
793 | */
|
||
794 | public void layerAdded(LayerCollectionEvent e) { |
||
795 | // Si es la primera capa, fijamos su extent al ViewPort
|
||
796 | // if (getLayers().getLayersCount() == 1) {
|
||
797 | if (getViewPort().getExtent() == null) { |
||
798 | FLayer lyr = e.getAffectedLayer(); |
||
799 | |||
800 | try {
|
||
801 | getViewPort().setExtent(lyr.getFullExtent()); |
||
802 | } catch (DriverException e1) {
|
||
803 | } |
||
804 | } |
||
805 | |||
806 | // Registramos al FMap como listener del legend de las capas
|
||
807 | FLayer lyr = e.getAffectedLayer(); |
||
808 | 9473 | fdiaz | selectionListener(lyr); |
809 | } |
||
810 | 6878 | cesar | |
811 | 9473 | fdiaz | private void selectionListener(FLayer lyr){ |
812 | 6878 | cesar | lyr.addLayerListener(eventBuffer); |
813 | |||
814 | if (lyr instanceof Classifiable) { |
||
815 | Classifiable c = (Classifiable) lyr; |
||
816 | c.addLegendListener(eventBuffer); |
||
817 | } |
||
818 | |||
819 | if (lyr instanceof AlphanumericData) { |
||
820 | Selectable s=null;
|
||
821 | try {
|
||
822 | s = ((AlphanumericData) lyr).getRecordset(); |
||
823 | if (s!=null) { |
||
824 | s.addSelectionListener(eventBuffer); |
||
825 | } |
||
826 | } catch (DriverException e1) {
|
||
827 | // TODO Auto-generated catch block
|
||
828 | e1.printStackTrace(); |
||
829 | } |
||
830 | |||
831 | } |
||
832 | 9473 | fdiaz | if (lyr instanceof FLayers){ |
833 | FLayers lyrs=(FLayers)lyr; |
||
834 | for(int i=0;i<lyrs.getLayersCount();i++){ |
||
835 | selectionListener(lyrs.getLayer(i)); |
||
836 | } |
||
837 | } |
||
838 | |||
839 | 6878 | cesar | } |
840 | /**
|
||
841 | * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoved(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
|
||
842 | */
|
||
843 | public void layerMoved(LayerPositionEvent e) { |
||
844 | } |
||
845 | |||
846 | /**
|
||
847 | * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoved(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
||
848 | */
|
||
849 | public void layerRemoved(LayerCollectionEvent e) { |
||
850 | FLayer lyr = e.getAffectedLayer(); |
||
851 | |||
852 | lyr.removeLayerListener(eventBuffer); |
||
853 | |||
854 | if (lyr instanceof Classifiable) { |
||
855 | Classifiable c = (Classifiable) lyr; |
||
856 | c.removeLegendListener(eventBuffer); |
||
857 | } |
||
858 | |||
859 | if (lyr instanceof Selectable) { |
||
860 | Selectable s = (Selectable) lyr; |
||
861 | s.addSelectionListener(eventBuffer); |
||
862 | } |
||
863 | } |
||
864 | |||
865 | /**
|
||
866 | * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdding(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
||
867 | */
|
||
868 | public void layerAdding(LayerCollectionEvent e) |
||
869 | throws CancelationException {
|
||
870 | } |
||
871 | |||
872 | /**
|
||
873 | * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoving(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
|
||
874 | */
|
||
875 | public void layerMoving(LayerPositionEvent e) |
||
876 | throws CancelationException {
|
||
877 | } |
||
878 | |||
879 | /**
|
||
880 | * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoving(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
||
881 | */
|
||
882 | public void layerRemoving(LayerCollectionEvent e) |
||
883 | throws CancelationException {
|
||
884 | } |
||
885 | |||
886 | |||
887 | /**
|
||
888 | * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
||
889 | */
|
||
890 | public void visibilityChanged(LayerCollectionEvent e) |
||
891 | throws CancelationException {
|
||
892 | } |
||
893 | } |
||
894 | |||
895 | public void addAsCollectionListener(FLayers layers2) { |
||
896 | layers2.addLayerCollectionListener(layerEventListener); |
||
897 | } |
||
898 | |||
899 | public GraphicLayer getGraphicsLayer() {
|
||
900 | return tracLayer;
|
||
901 | } |
||
902 | |||
903 | /**
|
||
904 | * Asigna la capa gr?fica
|
||
905 | * @param graphicLayer
|
||
906 | */
|
||
907 | public void setGraphicsLayer(GraphicLayer graphicLayer) { |
||
908 | tracLayer = graphicLayer; |
||
909 | } |
||
910 | |||
911 | public boolean equals(Object arg0) { |
||
912 | MapContext map = (MapContext) arg0; |
||
913 | if (super.equals(arg0)) |
||
914 | return true; |
||
915 | if (getLayers() == map.getLayers())
|
||
916 | return true; |
||
917 | boolean isEqual = true; |
||
918 | if (map.getLayers().getLayersCount() == getLayers().getLayersCount()) {
|
||
919 | for (int i = 0; i < getLayers().getLayersCount(); i++) { |
||
920 | |||
921 | if (!getLayers().getLayer(i).getName().equals(
|
||
922 | map.getLayers().getLayer(i).getName())) { |
||
923 | isEqual = false;
|
||
924 | } |
||
925 | |||
926 | } |
||
927 | } else {
|
||
928 | isEqual = false;
|
||
929 | } |
||
930 | return isEqual;
|
||
931 | } |
||
932 | |||
933 | public void addLayerError(String stringProperty) { |
||
934 | layersError.add(stringProperty); |
||
935 | } |
||
936 | |||
937 | public ArrayList getLayersError() { |
||
938 | return layersError;
|
||
939 | } |
||
940 | |||
941 | public void clearErrors() { |
||
942 | layersError.clear(); |
||
943 | } |
||
944 | 8765 | jjdelcerro | |
945 | 7366 | jmvivo | public void clearAllCachingImageDrawnLayers() { |
946 | clearCachingImageDrawnLayers(this.layers);
|
||
947 | 8765 | jjdelcerro | } |
948 | 7366 | jmvivo | private void clearCachingImageDrawnLayers(FLayers layers){ |
949 | int i;
|
||
950 | FLayer layer; |
||
951 | for (i=0;i< layers.getLayersCount();i++){ |
||
952 | layer = layers.getLayer(i); |
||
953 | if (layer instanceof FLayers) { |
||
954 | clearCachingImageDrawnLayers((FLayers)layer); |
||
955 | } else {
|
||
956 | layer.setCacheImageDrawnLayers(null);
|
||
957 | } |
||
958 | 8765 | jjdelcerro | } |
959 | 7366 | jmvivo | } |
960 | 8765 | jjdelcerro | |
961 | public void redraw() { |
||
962 | // truco
|
||
963 | if (getLayers().getLayersCount() > 0) |
||
964 | getLayers().moveTo(0,0); |
||
965 | 9013 | caballero | |
966 | 8765 | jjdelcerro | } |
967 | |||
968 | 6878 | cesar | } |