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