root / tags / v1_1_Build_1012 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / MapContext.java @ 12987
History | View | Annotate | Download (26.5 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 | 10525 | caballero | 0.9144, 0.3048, 0.0254, 1/8.983152841195214E-6 }; |
94 | 6878 | cesar | |
95 | public static final double[] CHANGE = { 100000, 100, 1, 0.1, 160934.4, |
||
96 | 10525 | caballero | 91.44, 30.48, 2.54, 1/8.983152841195214E-4 }; |
97 | 6878 | cesar | |
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 | 10525 | caballero | * como fuentes de datos los drivers ya existentes. As?
|
340 | * evitamos consumir m?s memoria, a la vez que ganamos
|
||
341 | 9225 | fjp | * 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 | 10525 | caballero | } |
361 | 9225 | fjp | } |
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 | 12227 | caballero | return (long) (viewPort.getAdjustedExtent().getWidth() / w * CHANGEM[getViewPort() |
421 | .getMapUnits()]/CHANGEM[getViewPort().getDistanceUnits()]); |
||
422 | 6878 | cesar | } |
423 | 12227 | caballero | if (proj.isProjected())
|
424 | return Math.round(proj.getScale((viewPort.getAdjustedExtent().getMinX()*CHANGEM[getViewPort().getMapUnits()])/CHANGEM[getViewPort().getDistanceUnits()], |
||
425 | (viewPort.getAdjustedExtent().getMaxX()*CHANGEM[getViewPort().getMapUnits()])/CHANGEM[getViewPort().getDistanceUnits()], viewPort.getImageSize() |
||
426 | 6878 | cesar | .getWidth(), dpi)); |
427 | 12227 | caballero | return Math.round(proj.getScale((viewPort.getAdjustedExtent().getMinX()*CHANGEM[1])/CHANGEM[getViewPort().getDistanceUnits()], |
428 | (viewPort.getAdjustedExtent().getMaxX()*CHANGEM[1])/CHANGEM[getViewPort().getDistanceUnits()], viewPort.getImageSize()
|
||
429 | .getWidth(), dpi)); |
||
430 | 6878 | cesar | } |
431 | /**
|
||
432 | * Introduce un nuevo extent en la vista a partir de la escala que se pasa como par?metro.
|
||
433 | *
|
||
434 | * @return escala de la vista.
|
||
435 | */
|
||
436 | public void setScaleView(long scale) { |
||
437 | 10752 | caballero | clearAllCachingImageDrawnLayers(); |
438 | 9552 | caballero | Preferences prefsResolution = Preferences.userRoot().node( "gvsig.configuration.screen" ); |
439 | 6878 | cesar | Toolkit kit = Toolkit.getDefaultToolkit(); |
440 | 9552 | caballero | double dpi = prefsResolution.getInt("dpi",kit.getScreenResolution()); |
441 | 6878 | cesar | if (viewPort.getImageSize() == null) |
442 | return;
|
||
443 | 7196 | caballero | IProjection proj = viewPort.getProjection(); |
444 | 6878 | cesar | if (viewPort.getAdjustedExtent() == null) { |
445 | return;
|
||
446 | } |
||
447 | 12231 | jmvivo | Rectangle2D rec=proj.getExtent(
|
448 | viewPort.getAdjustedExtent(), |
||
449 | scale,viewPort.getImageWidth(), |
||
450 | viewPort.getImageHeight(), |
||
451 | CHANGE[getViewPort().getMapUnits()], |
||
452 | // CHANGEM[getViewPort().getDistanceUnits()],
|
||
453 | dpi); |
||
454 | 12227 | caballero | // double dif=CHANGEM[getViewPort().getDistanceUnits()];
|
455 | // double w=rec.getWidth()/dif;
|
||
456 | // double h=rec.getHeight()/dif;
|
||
457 | // rec.setRect(rec.getCenterX()-w/2,rec.getCenterY()-h/2,w,h);
|
||
458 | 7196 | caballero | getViewPort().setExtent(rec); |
459 | 6878 | cesar | } |
460 | |||
461 | /**
|
||
462 | * @see org.cresques.geo.Projected#getProjection()
|
||
463 | */
|
||
464 | public IProjection getProjection() {
|
||
465 | return getViewPort().getProjection();
|
||
466 | } |
||
467 | |||
468 | /**
|
||
469 | * Inserta la proyecci?n.
|
||
470 | *
|
||
471 | * @param proj
|
||
472 | * Proyecci?n.
|
||
473 | */
|
||
474 | public void setProjection(IProjection proj) { |
||
475 | if (getViewPort() != null) { |
||
476 | getViewPort().setProjection(proj); |
||
477 | } |
||
478 | } |
||
479 | |||
480 | /**
|
||
481 | * @see org.cresques.geo.Projected#reProject(org.cresques.cts.ICoordTrans)
|
||
482 | */
|
||
483 | public void reProject(ICoordTrans arg0) { |
||
484 | // TODO implementar reprojecci?n (lo que sea eso)
|
||
485 | } |
||
486 | |||
487 | |||
488 | /**
|
||
489 | * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#getSelectionBounds()
|
||
490 | */
|
||
491 | public Rectangle2D getSelectionBounds() { |
||
492 | SelectedZoomVisitor visitor = new SelectedZoomVisitor();
|
||
493 | |||
494 | try {
|
||
495 | layers.process(visitor); |
||
496 | } catch (DriverException e1) {
|
||
497 | throw new RuntimeException( |
||
498 | "No se espera que SelectByPointVisitor lance esta excepci?n",
|
||
499 | e1); |
||
500 | } catch (VisitException e) {
|
||
501 | throw new RuntimeException( |
||
502 | "No se espera que SelectByPointVisitor lance esta excepci?n",
|
||
503 | e); |
||
504 | } |
||
505 | |||
506 | return visitor.getSelectBound();
|
||
507 | } |
||
508 | |||
509 | /**
|
||
510 | * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
|
||
511 | 8765 | jjdelcerro | * java.awt.Graphics2D, ISymbol)
|
512 | 6878 | cesar | */
|
513 | public void draw(BufferedImage image, Graphics2D g, Cancellable cancel, |
||
514 | double scale) throws DriverException { |
||
515 | if (viewPort.getExtent() == null) { |
||
516 | // System.err.println("viewPort.getExtent() = null");
|
||
517 | return;
|
||
518 | } |
||
519 | System.out.println("Viewport despues: " + viewPort.toString()); |
||
520 | /*
|
||
521 | * if ((viewPort.getImageWidth() <=0) || (viewPort.getImageHeight() <=
|
||
522 | * 0)) { return; }
|
||
523 | */
|
||
524 | |||
525 | prepareDrawing(image, g, scale); |
||
526 | |||
527 | // M?s c?lidad al texto
|
||
528 | RenderingHints renderHints = new RenderingHints( |
||
529 | RenderingHints.KEY_ANTIALIASING,
|
||
530 | RenderingHints.VALUE_ANTIALIAS_ON);
|
||
531 | renderHints.put(RenderingHints.KEY_RENDERING,
|
||
532 | RenderingHints.VALUE_RENDER_QUALITY);
|
||
533 | renderHints.put(RenderingHints.KEY_TEXT_ANTIALIASING,
|
||
534 | RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||
535 | g.setRenderingHints(renderHints); |
||
536 | |||
537 | long t1 = System.currentTimeMillis(); |
||
538 | layers.draw(image, g, viewPort, cancel, scale); |
||
539 | |||
540 | LayerDrawEvent beforeTracLayerEvent = new LayerDrawEvent(tracLayer,
|
||
541 | g, viewPort, LayerDrawEvent.GRAPHICLAYER_BEFORE_DRAW); |
||
542 | fireLayerDrawingEvent(beforeTracLayerEvent); |
||
543 | tracLayer.draw(image, g, viewPort, cancel, scale); |
||
544 | LayerDrawEvent afterTracLayerEvent = new LayerDrawEvent(tracLayer,
|
||
545 | g, viewPort, LayerDrawEvent.GRAPHICLAYER_AFTER_DRAW); |
||
546 | fireLayerDrawingEvent(afterTracLayerEvent); |
||
547 | 10525 | caballero | |
548 | 10656 | caballero | //layers.setDirty(false);
|
549 | 6878 | cesar | |
550 | long t2 = System.currentTimeMillis(); |
||
551 | System.err.println("Tiempo de dibujado:" + (t2 - t1) + |
||
552 | " mseg. Memoria libre:" + Runtime.getRuntime().freeMemory() / 1024 + " KB"); |
||
553 | /*
|
||
554 | * g.setColor(Color.BLUE); GeneralPath shpR = new
|
||
555 | * GeneralPath(viewPort.getExtent());
|
||
556 | * shpR.transform(viewPort.getAffineTransform()); g.draw(shpR);
|
||
557 | */
|
||
558 | System.gc();
|
||
559 | } |
||
560 | |||
561 | /**
|
||
562 | * En esta funci?n vamos a revisar las capas que necesitan repintarse,
|
||
563 | * por si hay alguna que se haya guardado la imagen de las anteriores
|
||
564 | * y puede acelerar el dibujado.
|
||
565 | * @param image
|
||
566 | * @param g
|
||
567 | * @param scale
|
||
568 | */
|
||
569 | private void prepareDrawing(BufferedImage image, Graphics2D g, double scale) { |
||
570 | |||
571 | // Primera pasada: si alguna capa necesita repintarse por debajo
|
||
572 | // de la que tiene la cache, TODAS necesitan
|
||
573 | // ser repintadas.
|
||
574 | boolean bNeedRepaint = false; |
||
575 | boolean bMayExistAceleration = false; |
||
576 | for (int i = 0; i < layers.getLayersCount(); i++) |
||
577 | { |
||
578 | FLayer lyr = layers.getLayer(i); |
||
579 | if (lyr.isCachingDrawnLayers() && (lyr.getCacheImageDrawnLayers() != null)) |
||
580 | { |
||
581 | bMayExistAceleration = true;
|
||
582 | } |
||
583 | else
|
||
584 | { |
||
585 | if (lyr.isDirty())
|
||
586 | bNeedRepaint = true;
|
||
587 | } |
||
588 | } |
||
589 | if (bMayExistAceleration==false) |
||
590 | bNeedRepaint = true;
|
||
591 | if (bNeedRepaint)
|
||
592 | layers.setDirty(true);
|
||
593 | else
|
||
594 | recursivePrepareDrawing(layers, 0);
|
||
595 | } |
||
596 | |||
597 | private void validatePreviousLayers(FLayers layers, int index) |
||
598 | { |
||
599 | // TODO: Aqu? quiz?s habr?a que explorar los padres de las capas
|
||
600 | // para marcar y/o asignar la imagen cacheada.
|
||
601 | for (int i = 0; i < index; i++) |
||
602 | { |
||
603 | FLayer lyr = layers.getLayer(i); |
||
604 | lyr.setDirty(false);
|
||
605 | } |
||
606 | // Las de arriba las marcamos como sucias
|
||
607 | // for (int i = index; i < layers.getLayersCount(); i++)
|
||
608 | // {
|
||
609 | // FLayer lyr = layers.getLayer(i);
|
||
610 | // lyr.setDirty(true);
|
||
611 | // }
|
||
612 | } |
||
613 | |||
614 | private void recursivePrepareDrawing(FLayers parent, int indexInParent) |
||
615 | { |
||
616 | for (int i = indexInParent; i < parent.getLayersCount(); i++) |
||
617 | { |
||
618 | FLayer lyr = layers.getLayer(i); |
||
619 | if (lyr.isCachingDrawnLayers() && (lyr.getCacheImageDrawnLayers() != null)) |
||
620 | { |
||
621 | // les decimos a las anteriores que est?n validadas (not dirty)
|
||
622 | // para que no se dibujen.
|
||
623 | if (lyr.isDirty())
|
||
624 | validatePreviousLayers(parent, i); |
||
625 | } |
||
626 | |||
627 | if (lyr instanceof FLayers) |
||
628 | { |
||
629 | recursivePrepareDrawing((FLayers)lyr, 0);
|
||
630 | } |
||
631 | } |
||
632 | |||
633 | } |
||
634 | |||
635 | public void drawGraphics(BufferedImage image, Graphics2D g, |
||
636 | Cancellable cancel, double scale) throws DriverException { |
||
637 | if (viewPort == null) |
||
638 | return;
|
||
639 | tracLayer.draw(image, g, viewPort, cancel, scale); |
||
640 | } |
||
641 | |||
642 | /**
|
||
643 | * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
|
||
644 | 8765 | jjdelcerro | * java.awt.Graphics2D, ISymbol)
|
645 | 6878 | cesar | */
|
646 | public void draw(BufferedImage image, Graphics2D g, double scale) |
||
647 | throws DriverException {
|
||
648 | layers.setDirty(true);
|
||
649 | draw(image, g, new Cancellable() {
|
||
650 | /**
|
||
651 | * @see com.iver.utiles.swing.threads.Cancellable#isCanceled()
|
||
652 | */
|
||
653 | public boolean isCanceled() { |
||
654 | return false; |
||
655 | } |
||
656 | |||
657 | public void setCanceled(boolean canceled) { |
||
658 | // TODO Auto-generated method stub
|
||
659 | |||
660 | } |
||
661 | }, scale); |
||
662 | } |
||
663 | |||
664 | /**
|
||
665 | * Devuelve el ViewPort.
|
||
666 | *
|
||
667 | * @return Returns the viewPort.
|
||
668 | */
|
||
669 | public ViewPort getViewPort() {
|
||
670 | return viewPort;
|
||
671 | } |
||
672 | |||
673 | /**
|
||
674 | * Inserta un ViewPort.
|
||
675 | *
|
||
676 | * @param viewPort
|
||
677 | * The viewPort to set.
|
||
678 | */
|
||
679 | public void setViewPort(ViewPort viewPort) { |
||
680 | if (this.viewPort != null) { |
||
681 | this.viewPort.removeViewPortListener(eventBuffer);
|
||
682 | } |
||
683 | |||
684 | this.viewPort = viewPort;
|
||
685 | 8765 | jjdelcerro | if (viewPort != null) |
686 | viewPort.addViewPortListener(eventBuffer); |
||
687 | 6878 | cesar | } |
688 | 9013 | caballero | |
689 | 8765 | jjdelcerro | /**
|
690 | * Sets the given zoom extent to the viewport.
|
||
691 | *
|
||
692 | * @param extent
|
||
693 | * The extent to zoom to.
|
||
694 | */
|
||
695 | public void zoomToExtent(Rectangle2D extent) { |
||
696 | getViewPort().setExtent(extent); |
||
697 | } |
||
698 | 6878 | cesar | |
699 | /**
|
||
700 | * M?todo de conveniencia. Recorre las capas y te da el fullExtent
|
||
701 | *
|
||
702 | * @return fullExtent de todas las capas.
|
||
703 | *
|
||
704 | * @throws DriverException
|
||
705 | */
|
||
706 | public Rectangle2D getFullExtent() throws DriverException { |
||
707 | return layers.getFullExtent();
|
||
708 | } |
||
709 | |||
710 | /**
|
||
711 | * Devuelve el XMLEntity.
|
||
712 | *
|
||
713 | * @return XMLEntity.
|
||
714 | * @throws XMLException
|
||
715 | */
|
||
716 | public XMLEntity getXMLEntity() throws XMLException { |
||
717 | XMLEntity xml = new XMLEntity();
|
||
718 | xml.putProperty("className", this.getClass().getName()); |
||
719 | xml.addChild(viewPort.getXMLEntity()); |
||
720 | xml.addChild(layers.getXMLEntity()); |
||
721 | |||
722 | return xml;
|
||
723 | } |
||
724 | |||
725 | /**
|
||
726 | * Crea un nuevo FMAp a partir del XMLEntity.
|
||
727 | *
|
||
728 | * @param xml
|
||
729 | * XMLEntity
|
||
730 | *
|
||
731 | * @return Nuevo FMap.
|
||
732 | *
|
||
733 | * @throws XMLException
|
||
734 | */
|
||
735 | public static MapContext createFromXML03(XMLEntity xml) throws XMLException { |
||
736 | ViewPort vp = ViewPort.createFromXML03(xml.getChild(0));
|
||
737 | MapContext fmap = new MapContext(vp);
|
||
738 | fmap.layers.setXMLEntity03(xml.getChild(1));
|
||
739 | |||
740 | return fmap;
|
||
741 | } |
||
742 | |||
743 | /**
|
||
744 | * Crea un nuevo FMAp a partir del XMLEntity.
|
||
745 | *
|
||
746 | * @param xml
|
||
747 | * XMLEntity
|
||
748 | *
|
||
749 | * @return Nuevo FMap.
|
||
750 | *
|
||
751 | * @throws XMLException
|
||
752 | */
|
||
753 | public static MapContext createFromXML(XMLEntity xml) throws XMLException { |
||
754 | ViewPort vp = ViewPort.createFromXML(xml.getChild(0));
|
||
755 | MapContext fmap = new MapContext(vp);
|
||
756 | fmap.layers.setXMLEntity(xml.getChild(1));
|
||
757 | |||
758 | return fmap;
|
||
759 | } |
||
760 | |||
761 | /**
|
||
762 | * A?ade un AtomicEventListener.
|
||
763 | *
|
||
764 | * @param listener
|
||
765 | * AtomicEventListener.
|
||
766 | *
|
||
767 | * @return True si se ha a?adido correctamente.
|
||
768 | */
|
||
769 | public boolean addAtomicEventListener(AtomicEventListener listener) { |
||
770 | return eventBuffer.addAtomicEventListener(listener);
|
||
771 | } |
||
772 | |||
773 | /**
|
||
774 | * Borra un AtomicEventListener de la lista de listeners.
|
||
775 | *
|
||
776 | * @param listener
|
||
777 | * AtomicEventListener a borrar.
|
||
778 | *
|
||
779 | * @return True si se ha borrado correctamente.
|
||
780 | */
|
||
781 | public boolean removeAtomicEventListener(AtomicEventListener listener) { |
||
782 | return eventBuffer.removeAtomicEventListener(listener);
|
||
783 | } |
||
784 | |||
785 | /**
|
||
786 | * Inicializa los AtomicEvent.
|
||
787 | */
|
||
788 | public void beginAtomicEvent() { |
||
789 | eventBuffer.beginAtomicEvent(); |
||
790 | } |
||
791 | |||
792 | /**
|
||
793 | * Finaliza los AtomicEvent.
|
||
794 | */
|
||
795 | public void endAtomicEvent() { |
||
796 | eventBuffer.endAtomicEvent(); |
||
797 | } |
||
798 | |||
799 | /**
|
||
800 | * Evento Layer.
|
||
801 | *
|
||
802 | * @author Fernando Gonz?lez Cort?s
|
||
803 | */
|
||
804 | public class LayerEventListener implements LayerCollectionListener { |
||
805 | /**
|
||
806 | * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdded(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
||
807 | */
|
||
808 | public void layerAdded(LayerCollectionEvent e) { |
||
809 | // Si es la primera capa, fijamos su extent al ViewPort
|
||
810 | // if (getLayers().getLayersCount() == 1) {
|
||
811 | if (getViewPort().getExtent() == null) { |
||
812 | FLayer lyr = e.getAffectedLayer(); |
||
813 | |||
814 | try {
|
||
815 | getViewPort().setExtent(lyr.getFullExtent()); |
||
816 | } catch (DriverException e1) {
|
||
817 | } |
||
818 | } |
||
819 | |||
820 | // Registramos al FMap como listener del legend de las capas
|
||
821 | FLayer lyr = e.getAffectedLayer(); |
||
822 | 9473 | fdiaz | selectionListener(lyr); |
823 | } |
||
824 | 6878 | cesar | |
825 | 9473 | fdiaz | private void selectionListener(FLayer lyr){ |
826 | 6878 | cesar | lyr.addLayerListener(eventBuffer); |
827 | |||
828 | if (lyr instanceof Classifiable) { |
||
829 | Classifiable c = (Classifiable) lyr; |
||
830 | c.addLegendListener(eventBuffer); |
||
831 | } |
||
832 | |||
833 | if (lyr instanceof AlphanumericData) { |
||
834 | Selectable s=null;
|
||
835 | try {
|
||
836 | s = ((AlphanumericData) lyr).getRecordset(); |
||
837 | if (s!=null) { |
||
838 | s.addSelectionListener(eventBuffer); |
||
839 | } |
||
840 | } catch (DriverException e1) {
|
||
841 | // TODO Auto-generated catch block
|
||
842 | e1.printStackTrace(); |
||
843 | } |
||
844 | |||
845 | } |
||
846 | 9473 | fdiaz | if (lyr instanceof FLayers){ |
847 | FLayers lyrs=(FLayers)lyr; |
||
848 | for(int i=0;i<lyrs.getLayersCount();i++){ |
||
849 | selectionListener(lyrs.getLayer(i)); |
||
850 | } |
||
851 | } |
||
852 | |||
853 | 6878 | cesar | } |
854 | /**
|
||
855 | * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoved(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
|
||
856 | */
|
||
857 | public void layerMoved(LayerPositionEvent e) { |
||
858 | } |
||
859 | |||
860 | /**
|
||
861 | * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoved(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
||
862 | */
|
||
863 | public void layerRemoved(LayerCollectionEvent e) { |
||
864 | FLayer lyr = e.getAffectedLayer(); |
||
865 | |||
866 | lyr.removeLayerListener(eventBuffer); |
||
867 | |||
868 | if (lyr instanceof Classifiable) { |
||
869 | Classifiable c = (Classifiable) lyr; |
||
870 | c.removeLegendListener(eventBuffer); |
||
871 | } |
||
872 | |||
873 | if (lyr instanceof Selectable) { |
||
874 | Selectable s = (Selectable) lyr; |
||
875 | s.addSelectionListener(eventBuffer); |
||
876 | } |
||
877 | } |
||
878 | |||
879 | /**
|
||
880 | * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdding(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
||
881 | */
|
||
882 | public void layerAdding(LayerCollectionEvent e) |
||
883 | throws CancelationException {
|
||
884 | } |
||
885 | |||
886 | /**
|
||
887 | * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoving(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
|
||
888 | */
|
||
889 | public void layerMoving(LayerPositionEvent e) |
||
890 | throws CancelationException {
|
||
891 | } |
||
892 | |||
893 | /**
|
||
894 | * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoving(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
||
895 | */
|
||
896 | public void layerRemoving(LayerCollectionEvent e) |
||
897 | throws CancelationException {
|
||
898 | } |
||
899 | |||
900 | |||
901 | /**
|
||
902 | * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
||
903 | */
|
||
904 | public void visibilityChanged(LayerCollectionEvent e) |
||
905 | throws CancelationException {
|
||
906 | } |
||
907 | } |
||
908 | |||
909 | public void addAsCollectionListener(FLayers layers2) { |
||
910 | layers2.addLayerCollectionListener(layerEventListener); |
||
911 | } |
||
912 | |||
913 | public GraphicLayer getGraphicsLayer() {
|
||
914 | return tracLayer;
|
||
915 | } |
||
916 | |||
917 | /**
|
||
918 | * Asigna la capa gr?fica
|
||
919 | * @param graphicLayer
|
||
920 | */
|
||
921 | public void setGraphicsLayer(GraphicLayer graphicLayer) { |
||
922 | tracLayer = graphicLayer; |
||
923 | } |
||
924 | |||
925 | public boolean equals(Object arg0) { |
||
926 | MapContext map = (MapContext) arg0; |
||
927 | if (super.equals(arg0)) |
||
928 | return true; |
||
929 | if (getLayers() == map.getLayers())
|
||
930 | return true; |
||
931 | boolean isEqual = true; |
||
932 | if (map.getLayers().getLayersCount() == getLayers().getLayersCount()) {
|
||
933 | for (int i = 0; i < getLayers().getLayersCount(); i++) { |
||
934 | |||
935 | if (!getLayers().getLayer(i).getName().equals(
|
||
936 | map.getLayers().getLayer(i).getName())) { |
||
937 | isEqual = false;
|
||
938 | } |
||
939 | |||
940 | } |
||
941 | } else {
|
||
942 | isEqual = false;
|
||
943 | } |
||
944 | return isEqual;
|
||
945 | } |
||
946 | |||
947 | public void addLayerError(String stringProperty) { |
||
948 | layersError.add(stringProperty); |
||
949 | } |
||
950 | |||
951 | public ArrayList getLayersError() { |
||
952 | return layersError;
|
||
953 | } |
||
954 | |||
955 | public void clearErrors() { |
||
956 | layersError.clear(); |
||
957 | } |
||
958 | 8765 | jjdelcerro | |
959 | 7366 | jmvivo | public void clearAllCachingImageDrawnLayers() { |
960 | clearCachingImageDrawnLayers(this.layers);
|
||
961 | 8765 | jjdelcerro | } |
962 | 7366 | jmvivo | private void clearCachingImageDrawnLayers(FLayers layers){ |
963 | int i;
|
||
964 | FLayer layer; |
||
965 | for (i=0;i< layers.getLayersCount();i++){ |
||
966 | layer = layers.getLayer(i); |
||
967 | if (layer instanceof FLayers) { |
||
968 | clearCachingImageDrawnLayers((FLayers)layer); |
||
969 | } else {
|
||
970 | layer.setCacheImageDrawnLayers(null);
|
||
971 | } |
||
972 | 8765 | jjdelcerro | } |
973 | 7366 | jmvivo | } |
974 | 8765 | jjdelcerro | |
975 | public void redraw() { |
||
976 | // truco
|
||
977 | if (getLayers().getLayersCount() > 0) |
||
978 | getLayers().moveTo(0,0); |
||
979 | 9013 | caballero | |
980 | 8765 | jjdelcerro | } |
981 | |||
982 | 6878 | cesar | } |