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