svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / MapContext.java @ 47409
History | View | Annotate | Download (64.2 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.mapcontext; |
24 |
|
25 |
import java.awt.Color; |
26 |
import java.awt.Graphics; |
27 |
import java.awt.Graphics2D; |
28 |
import java.awt.geom.Rectangle2D; |
29 |
import java.awt.image.BufferedImage; |
30 |
import java.util.ArrayList; |
31 |
import java.util.Collection; |
32 |
import java.util.HashSet; |
33 |
import java.util.Iterator; |
34 |
import java.util.LinkedHashMap; |
35 |
import java.util.List; |
36 |
import java.util.Map; |
37 |
import java.util.Set; |
38 |
import org.apache.commons.lang3.ArrayUtils; |
39 |
|
40 |
import org.cresques.cts.ICoordTrans; |
41 |
import org.cresques.cts.IProjection; |
42 |
import org.cresques.geo.Projected; |
43 |
import org.slf4j.Logger; |
44 |
import org.slf4j.LoggerFactory; |
45 |
|
46 |
import org.gvsig.compat.CompatLocator; |
47 |
import org.gvsig.compat.print.PrintAttributes; |
48 |
import org.gvsig.fmap.dal.DataStore; |
49 |
import org.gvsig.fmap.dal.exception.ReadException; |
50 |
import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
51 |
import org.gvsig.fmap.geom.Geometry; |
52 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
53 |
import org.gvsig.fmap.geom.GeometryLocator; |
54 |
import org.gvsig.fmap.geom.GeometryManager; |
55 |
import org.gvsig.fmap.geom.GeometryUtils; |
56 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
57 |
import org.gvsig.fmap.geom.primitive.Envelope; |
58 |
import org.gvsig.fmap.geom.primitive.Point; |
59 |
import org.gvsig.fmap.mapcontext.events.ErrorEvent; |
60 |
import org.gvsig.fmap.mapcontext.events.listeners.AtomicEventListener; |
61 |
import org.gvsig.fmap.mapcontext.events.listeners.ErrorListener; |
62 |
import org.gvsig.fmap.mapcontext.events.listeners.EventBuffer; |
63 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
64 |
import org.gvsig.fmap.mapcontext.impl.DefaultMapContextManager; |
65 |
import org.gvsig.fmap.mapcontext.layers.BaseLayerCollectionListener; |
66 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
67 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
68 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent; |
69 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener; |
70 |
import org.gvsig.fmap.mapcontext.layers.LayerDrawEvent; |
71 |
import org.gvsig.fmap.mapcontext.layers.LayerDrawingListener; |
72 |
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent; |
73 |
import org.gvsig.fmap.mapcontext.layers.operations.Classifiable; |
74 |
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer; |
75 |
import org.gvsig.fmap.mapcontext.layers.order.LayerOrderManager; |
76 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
77 |
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer; |
78 |
import org.gvsig.fmap.mapcontext.layers.vectorial.VectorLayer; |
79 |
import org.gvsig.fmap.mapcontext.rendering.legend.events.listeners.LegendListener; |
80 |
import org.gvsig.fmap.mapcontext.rendering.strategies.SelectedEnvelopeVisitor; |
81 |
import org.gvsig.timesupport.AbsoluteInstant; |
82 |
import org.gvsig.timesupport.AbsoluteIntervalTypeNotRegisteredException; |
83 |
import org.gvsig.timesupport.Instant; |
84 |
import org.gvsig.timesupport.Interval; |
85 |
import org.gvsig.timesupport.RelativeInstant; |
86 |
import org.gvsig.timesupport.TimeSupportLocator; |
87 |
import org.gvsig.timesupport.TimeSupportManager; |
88 |
import org.gvsig.tools.ToolsLocator; |
89 |
import org.gvsig.tools.dispose.Disposable; |
90 |
import org.gvsig.tools.dispose.DisposeUtils; |
91 |
import org.gvsig.tools.dispose.impl.AbstractDisposable; |
92 |
import org.gvsig.tools.dynobject.DynStruct; |
93 |
import org.gvsig.tools.exception.BaseException; |
94 |
import org.gvsig.tools.observer.Observable; |
95 |
import org.gvsig.tools.observer.Observer; |
96 |
import org.gvsig.tools.persistence.PersistenceManager; |
97 |
import org.gvsig.tools.persistence.Persistent; |
98 |
import org.gvsig.tools.persistence.PersistentState; |
99 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
100 |
import org.gvsig.tools.task.Cancellable; |
101 |
import org.gvsig.tools.util.Callable; |
102 |
import org.gvsig.tools.visitor.Visitor; |
103 |
|
104 |
/**
|
105 |
* <p>
|
106 |
* The <code>MapContext</code> class represents the model and a part of the
|
107 |
* control and view around graphical layers used by
|
108 |
* {@link MapControl MapControl}.</p>
|
109 |
*
|
110 |
* <p>
|
111 |
* An instance of <code>MapContext</code> is made up with:
|
112 |
* <ul>
|
113 |
* <li>a hierarchy of {@link FLayers FLayers} nodes
|
114 |
* <li>a {@link GraphicLayer GraphicLayer} layer
|
115 |
* <li>a {@link ViewPort ViewPort}
|
116 |
* <li>an {@link EventBuffer EventButter}
|
117 |
* <li>some
|
118 |
* {@link com.iver.cit.gvsig.fmap.layers.LegendListener LegendListener}s
|
119 |
* <li>some {@link LayerDrawingListener LayerDrawingListener}s
|
120 |
* <li>some {@link ErrorListener ErrorListener}s
|
121 |
* </ul>
|
122 |
* </p>
|
123 |
*/
|
124 |
public class MapContext extends AbstractDisposable implements Projected, |
125 |
Persistent, Observer, Iterable<FLayer> { |
126 |
|
127 |
public interface MapTimeContext { |
128 |
public Interval getInterval();
|
129 |
public List<Instant> getTimes(); |
130 |
} |
131 |
|
132 |
/**
|
133 |
* @deprecated use getDistanceTrans2Meter()
|
134 |
*/
|
135 |
public static final double[] CHANGEM = {1000, 1, 0.01, 0.001, 1609.344, |
136 |
0.9144, 0.3048, 0.0254, 1 / 8.983152841195214E-6}; |
137 |
|
138 |
public static ArrayList AREANAMES = new ArrayList(); |
139 |
public static ArrayList AREAABBR = new ArrayList(); |
140 |
public static ArrayList AREATRANS2METER = new ArrayList(); |
141 |
|
142 |
public static ArrayList DISTANCENAMES = new ArrayList(); |
143 |
public static ArrayList DISTANCEABBR = new ArrayList(); |
144 |
public static ArrayList DISTANCETRANS2METER = new ArrayList(); |
145 |
|
146 |
static {
|
147 |
MapContext.addDistanceUnit("Kilometros", "Km", 1000); |
148 |
MapContext.addDistanceUnit("Metros", "m", 1); |
149 |
MapContext.addDistanceUnit("Centimetros", "cm", 0.01); |
150 |
MapContext.addDistanceUnit("Milimetros", "mm", 0.001); |
151 |
MapContext.addDistanceUnit("Millas", "mi", 1609.344); |
152 |
MapContext.addDistanceUnit("Yardas", "Ya", 0.9144); |
153 |
MapContext.addDistanceUnit("Pies", "ft", 0.3048); |
154 |
MapContext.addDistanceUnit("Pulgadas", "inche", 0.0254); |
155 |
MapContext.addDistanceUnit("Grados", "?", 1 / 8.983152841195214E-6); |
156 |
|
157 |
MapContext.addAreaUnit("Kilometros", "Km", true, 1000); |
158 |
MapContext.addAreaUnit("Metros", "m", true, 1); |
159 |
MapContext.addAreaUnit("Centimetros", "cm", true, 0.01); |
160 |
MapContext.addAreaUnit("Milimetros", "mm", true, 0.001); |
161 |
MapContext.addAreaUnit("Millas", "mi", true, 1609.344); |
162 |
MapContext.addAreaUnit("Yardas", "Ya", true, 0.9144); |
163 |
MapContext.addAreaUnit("Pies", "ft", true, 0.3048); |
164 |
MapContext.addAreaUnit("Pulgadas", "inche", true, 0.0254); |
165 |
MapContext.addAreaUnit("Grados", "?", true, 1 / 8.983152841195214E-6); |
166 |
|
167 |
} |
168 |
|
169 |
private static final GeometryManager GEOM_MANAGER = GeometryLocator.getGeometryManager(); |
170 |
|
171 |
private static final MapContextManager MAPCONTEXT_MANAGER = MapContextLocator.getMapContextManager(); |
172 |
|
173 |
private static final Logger LOGGER = LoggerFactory.getLogger(MapContext.class); |
174 |
|
175 |
private long scaleToUseWhenEnvelopeCollapse = 10000000; |
176 |
|
177 |
private boolean printGraphicsLayer; |
178 |
|
179 |
/**
|
180 |
* <p>
|
181 |
* Determines the number of frames.</p>
|
182 |
*
|
183 |
* <p>
|
184 |
* Number of updates per second that the timer will invoke repaint this
|
185 |
* component.</p>
|
186 |
*/
|
187 |
private static int drawFrameRate = 3; |
188 |
|
189 |
/**
|
190 |
* <p>
|
191 |
* Returns the draw frame rate.</p>
|
192 |
*
|
193 |
* <p>
|
194 |
* Draw frame rate is the number of repaints of this <code>MapControl</code>
|
195 |
* instance that timer invokes per second.</p>
|
196 |
*
|
197 |
* @return number of repaints of this <code>MapControl</code> instance that
|
198 |
* timer invokes per second
|
199 |
*
|
200 |
* @see #applyFrameRate()
|
201 |
* @see #setDrawFrameRate(int)
|
202 |
*/
|
203 |
public static int getDrawFrameRate() { |
204 |
return drawFrameRate;
|
205 |
} |
206 |
|
207 |
/**
|
208 |
* <p>
|
209 |
* Sets the draw frame rate.</p>
|
210 |
*
|
211 |
* <p>
|
212 |
* Draw frame rate is the number of repaints of this <code>MapControl</code>
|
213 |
* instance that timer invokes per second.</p>
|
214 |
*
|
215 |
* @param theDrawFrameRate number of repaints of this <code>MapControl</code>
|
216 |
* instance that timer invokes per second
|
217 |
*
|
218 |
* @see #applyFrameRate()
|
219 |
* @see #getDrawFrameRate()
|
220 |
*/
|
221 |
public static void setDrawFrameRate(int theDrawFrameRate) { |
222 |
drawFrameRate = theDrawFrameRate; |
223 |
} |
224 |
|
225 |
public static void addAreaUnit(String name, String abbr, boolean isLinear, double trans2meter) { |
226 |
if (!AREANAMES.contains(name)) {
|
227 |
AREANAMES.add(name); |
228 |
String pow = ""; |
229 |
if (isLinear) {
|
230 |
pow = String.valueOf((char) 178); |
231 |
} |
232 |
AREAABBR.add(abbr + pow); |
233 |
AREATRANS2METER.add(trans2meter); |
234 |
} |
235 |
} |
236 |
|
237 |
public static String[] getAreaNames() { |
238 |
return (String[]) AREANAMES.toArray(new String[0]); |
239 |
} |
240 |
|
241 |
public static String[] getAreaAbbr() { |
242 |
return (String[]) AREAABBR.toArray(new String[0]); |
243 |
} |
244 |
|
245 |
public static double[] getAreaTrans2Meter() { |
246 |
int size = AREATRANS2METER.size();
|
247 |
double[] trans2meters = new double[size]; |
248 |
for (int i = 0; i < size; i++) { |
249 |
trans2meters[i] = ((Double) AREATRANS2METER.get(i));
|
250 |
} |
251 |
return trans2meters;
|
252 |
} |
253 |
|
254 |
public static String getOfLinear(int i) { |
255 |
if (((String) AREAABBR.get(i)).toLowerCase().endsWith(String.valueOf((char) 178))) { |
256 |
return String.valueOf((char) 178); |
257 |
} |
258 |
return ""; |
259 |
} |
260 |
|
261 |
public static void addDistanceUnit(String name, String abbr, double trans2meter) { |
262 |
if (!DISTANCENAMES.contains(name)) {
|
263 |
DISTANCENAMES.add(name); |
264 |
DISTANCEABBR.add(abbr); |
265 |
DISTANCETRANS2METER.add(trans2meter); |
266 |
} |
267 |
} |
268 |
|
269 |
public static String[] getDistanceNames() { |
270 |
return (String[]) DISTANCENAMES.toArray(new String[0]); |
271 |
} |
272 |
|
273 |
public String getDistanceName() { |
274 |
return (String) DISTANCENAMES.get(this.getViewPort().getDistanceUnits()); |
275 |
} |
276 |
|
277 |
public static String[] getDistanceAbbr() { |
278 |
return (String[]) DISTANCEABBR.toArray(new String[0]); |
279 |
} |
280 |
|
281 |
public static double[] getDistanceTrans2Meter() { |
282 |
int size = DISTANCETRANS2METER.size();
|
283 |
double[] trans2meters = new double[size]; |
284 |
for (int i = 0; i < size; i++) { |
285 |
trans2meters[i] = ((Double) DISTANCETRANS2METER.get(i));
|
286 |
} |
287 |
return trans2meters;
|
288 |
} |
289 |
|
290 |
public static int getDistancePosition(String s) { |
291 |
for (int i = 0; i < DISTANCENAMES.size(); i++) { |
292 |
if (DISTANCENAMES.get(i).equals(s)) {
|
293 |
return i;
|
294 |
} |
295 |
} |
296 |
return 0; |
297 |
} |
298 |
|
299 |
/**
|
300 |
* <p>
|
301 |
* Defines the value which a unit of a distance measurement must be divided
|
302 |
* to obtain its equivalent <b>in centimeters</b>.</p>
|
303 |
*
|
304 |
* <p>
|
305 |
* <b><i>Conversion values of distance measurements:</i></b>
|
306 |
* <ul>
|
307 |
* <li><code>MapContext.CHANGE[0]</code>: kilometer
|
308 |
* <li><code>MapContext.CHANGE[1]</code>: meter
|
309 |
* <li><code>MapContext.CHANGE[2]</code>: centimeter
|
310 |
* <li><code>MapContext.CHANGE[3]</code>: millimeter
|
311 |
* <li><code>MapContext.CHANGE[4]</code>: international statute mile
|
312 |
* <li><code>MapContext.CHANGE[5]</code>: yard
|
313 |
* <li><code>MapContext.CHANGE[6]</code>: foot
|
314 |
* <li><code>MapContext.CHANGE[7]</code>: inch
|
315 |
* <li><code>MapContext.CHANGE[8]</code>: grade
|
316 |
* </ul>
|
317 |
*
|
318 |
* <p>
|
319 |
* <h3>Examples:</h3>
|
320 |
* <pre>1 international statute mile / MapContext.CHANGE[4] = X centimeters</pre>
|
321 |
* <pre>1 kilometer / MapContext.CHANGE[0] = X centimeters</pre>
|
322 |
* <pre>1 grade / MapContext.CHANGE[8] = X centimeters</pre>
|
323 |
* </p>
|
324 |
*
|
325 |
* <p>
|
326 |
* <h3>Grade conversion value: <code>MapContext.CHANGE[8]</code></h3>
|
327 |
* The value of <code>MapContext.CHANGE[8]</code> represents the centimeters
|
328 |
* of a straight line between two points on the Earth surface that are 1
|
329 |
* grade far each other of the center of the Earth. This value has been
|
330 |
* calculated using a radius approximated of
|
331 |
* R<sub>Earth</sub>=6.37846082678100774672e6 meters, according these
|
332 |
* equations:
|
333 |
* <pre>D = 2 * (sin (1)) * R<sub>Earth</sub></pre>
|
334 |
* <pre>MapContext.CHANGE[8] = 1 / D</pre>
|
335 |
* <h4>Explanation:</h4>
|
336 |
* We get an isosceles triangle with the center of the Earth and the 2
|
337 |
* points on the surface. This triangle can be divided into two rectangle
|
338 |
* triangles. We know two values, the angle of 1 grade, that will be 0.50
|
339 |
* grades in each triangle, and the Earth radius that is the hypotenuse.
|
340 |
* Then we apply trigonometry and get the distance <i>D</i> between both
|
341 |
* points on the Earth surface.</p>
|
342 |
* <p>
|
343 |
* Now we only must invert that value to obtain
|
344 |
* <code>MapContext.CHANGE[8]</code>.</p>
|
345 |
*
|
346 |
* @deprecated use getDistanceTrans2Meter() * 100
|
347 |
*/
|
348 |
public static final double[] CHANGE = {100000, 100, 1, 0.1, 160934.4, |
349 |
91.44, 30.48, 2.54, 1 / 8.983152841195214E-4}; |
350 |
|
351 |
public static final int EQUALS = 0; |
352 |
public static final int DISJOINT = 1; |
353 |
public static final int INTERSECTS = 2; |
354 |
public static final int TOUCHES = 3; |
355 |
public static final int CROSSES = 4; |
356 |
public static final int WITHIN = 5; |
357 |
public static final int CONTAINS = 6; |
358 |
public static final int OVERLAPS = 7; |
359 |
|
360 |
/**
|
361 |
* A hierarchy of {@link FLayers FLayers} nodes.
|
362 |
*/
|
363 |
protected FLayers layers;
|
364 |
|
365 |
/**
|
366 |
* A List of layers with graphical items: geometries and symbols.
|
367 |
*/
|
368 |
private static final String DEFAULT_TRACTLAYER = "Default"; |
369 |
private Map<String, VectorLayer> tracLayers; |
370 |
|
371 |
/**
|
372 |
* Information for draw layers in a view.
|
373 |
*
|
374 |
* @see #getViewPort()
|
375 |
* @see #setViewPort(ViewPort)
|
376 |
*/
|
377 |
private ViewPort viewPort;
|
378 |
// private ArrayList invalidationListeners = new ArrayList();
|
379 |
/**
|
380 |
* Array list with all {@link LegendListener LegendListener} registered to
|
381 |
* this map.
|
382 |
*
|
383 |
* @see #addLayerListener(LegendListener)
|
384 |
* @see #removeLayerListener(LegendListener)
|
385 |
* @see #callLegendChanged()
|
386 |
*/
|
387 |
private List legendListeners; |
388 |
|
389 |
/**
|
390 |
* Array list with all {@link LayerDrawingListener LayerDrawingListener}
|
391 |
* registered to this map.
|
392 |
*
|
393 |
* @see #addLayerDrawingListener(LayerDrawingListener)
|
394 |
* @see #removeLayerDrawListener(LayerDrawingListener)
|
395 |
* @see #fireLayerDrawingEvent(LayerDrawEvent)
|
396 |
*/
|
397 |
private ArrayList layerDrawingListeners = new ArrayList(); |
398 |
|
399 |
/**
|
400 |
* <p>
|
401 |
* Buffer that is used to store and eject events produced on this map:
|
402 |
* <ul>
|
403 |
* <li>Layer collection events.
|
404 |
* <li>View port events.
|
405 |
* <li>Atomic events.
|
406 |
* <li>Layer events.
|
407 |
* <li>Legend events on a {@link Classificable Classificable} layer.
|
408 |
* <li>Selection events on an {@link AlphanumericData AlphanumericData} data
|
409 |
* layer.
|
410 |
* </ul>
|
411 |
* </p>
|
412 |
*
|
413 |
* @see #addAtomicEventListener(AtomicEventListener)
|
414 |
* @see #removeAtomicEventListener(AtomicEventListener)
|
415 |
* @see #beginAtomicEvent()
|
416 |
* @see #endAtomicEvent()
|
417 |
*/
|
418 |
private EventBuffer eventBuffer = new EventBuffer(); |
419 |
|
420 |
/**
|
421 |
* Event listener for the collection of layers of this map.
|
422 |
*/
|
423 |
private LayerEventListener layerEventListener = null; |
424 |
|
425 |
/**
|
426 |
* List with information of all errors produced on all layers.
|
427 |
*
|
428 |
* @see #addLayerError(String)
|
429 |
* @see #getLayersError()
|
430 |
* @see #clearErrors()
|
431 |
*/
|
432 |
private ArrayList layersError = new ArrayList(); |
433 |
|
434 |
/**
|
435 |
* Array list with all {@link ErrorListener ErrorListener} registered to
|
436 |
* this map.
|
437 |
*
|
438 |
* @see #addErrorListener(ErrorListener)
|
439 |
* @see #removeErrorListener(LegendListener)
|
440 |
* @see #reportDriverExceptions(String, List)
|
441 |
*/
|
442 |
private ArrayList errorListeners = new ArrayList(); |
443 |
|
444 |
/**
|
445 |
* Layer order manager decides position of layers added to this map context.
|
446 |
*/
|
447 |
private LayerOrderManager orderManager = null; |
448 |
|
449 |
// public static ResourceBundle myResourceBundle =
|
450 |
// ResourceBundle.getBundle("FMap");
|
451 |
/**
|
452 |
* <p>
|
453 |
* Default <i>zoom in</i> factor.</p>
|
454 |
* <p>
|
455 |
* Doing a <i>zoom in</i> operation, decreases the focal distance and
|
456 |
* increases the eyesight angle to the surface. This allows view an smaller
|
457 |
* area but with the items bigger.</p>
|
458 |
*/
|
459 |
public static double ZOOMINFACTOR = 2; |
460 |
|
461 |
/**
|
462 |
* <p>
|
463 |
* Default <i>zoom out</i> factor.</p>
|
464 |
* <p>
|
465 |
* Doing a <i>zoom out</i> operation, increases the focal distance and
|
466 |
* decreases the eyesight angle to the surface. This allows view a bigger
|
467 |
* area but with the items smaller.</p>
|
468 |
*/
|
469 |
public static double ZOOMOUTFACTOR = 0.5; |
470 |
|
471 |
/**
|
472 |
* * Draw version of the context. It's used for know when de componend has
|
473 |
* changed any visualization property
|
474 |
*
|
475 |
* @see getDrawVersion
|
476 |
* @see updateDrawVersion
|
477 |
*/
|
478 |
private long drawVersion = 0L; |
479 |
|
480 |
private long layersVersion = 0L; |
481 |
private long viewPortVersion = 0L; |
482 |
private long graphicsLayerVersion = 0L; |
483 |
|
484 |
/**
|
485 |
* Object to Manage Draw of MapContext
|
486 |
*/
|
487 |
private MapContextDrawer mapContextDrawer = null; |
488 |
|
489 |
/**
|
490 |
* Object to Manage Draw of MapContext
|
491 |
*/
|
492 |
private Class mapContextDrawerClass = null; |
493 |
|
494 |
/**
|
495 |
* <p>
|
496 |
* Color used to represent the selections.</p>
|
497 |
*/
|
498 |
private static Color selectionColor = new Color(Color.YELLOW.getRed(), Color.YELLOW.getGreen(), Color.YELLOW.getBlue(), 127); |
499 |
private Set<FLyrVect> layersToSnap = new HashSet<>(); |
500 |
|
501 |
/**
|
502 |
* <p>
|
503 |
* Gets the color used to represent the selections.</p>
|
504 |
*
|
505 |
* @return color used to represent the selections
|
506 |
*/
|
507 |
public static Color getSelectionColor() { |
508 |
return selectionColor;
|
509 |
} |
510 |
|
511 |
/**
|
512 |
* <p>
|
513 |
* Sets the color used to represent the selections.</p>
|
514 |
*
|
515 |
* @param selectionColor color used to represent the selections
|
516 |
*/
|
517 |
public static void setSelectionColor(Color selectionColor) { |
518 |
MapContext.selectionColor = selectionColor; |
519 |
} |
520 |
|
521 |
/**
|
522 |
* <p>
|
523 |
* Creates a new map context with the drawing information defined in the
|
524 |
* view port argument, and without layers.</p>
|
525 |
*
|
526 |
* @param vp information for drawing the layers of this map in the available
|
527 |
* rectangular area according a projection
|
528 |
*/
|
529 |
public MapContext(ViewPort vp) {
|
530 |
this(new FLayers(), vp); |
531 |
} |
532 |
|
533 |
public MapContext() {
|
534 |
this.legendListeners = new ArrayList(); |
535 |
this.tracLayers = new LinkedHashMap<>(); |
536 |
this.layerEventListener = new LayerEventListener(); |
537 |
} |
538 |
|
539 |
/**
|
540 |
* <p>
|
541 |
* Creates a new map context with the layers and the drawing information
|
542 |
* defined in the view port arguments.</p>
|
543 |
*
|
544 |
* @param fLayers the initial hierarchy of nodes of layers that this map
|
545 |
* will have
|
546 |
* @param vp information for drawing the layers of this map in the available
|
547 |
* rectangular area according a projection
|
548 |
*/
|
549 |
public MapContext(FLayers fLayers, ViewPort vp) {
|
550 |
this();
|
551 |
this.layers = fLayers;
|
552 |
if (layers != null) { |
553 |
layers.setMapContext(this);
|
554 |
layers.addLayerCollectionListener(layerEventListener); |
555 |
layers.addLayerCollectionListener(eventBuffer); |
556 |
} |
557 |
setViewPort(vp); |
558 |
} |
559 |
|
560 |
/**
|
561 |
* <p>
|
562 |
* Reports to all driver error listeners registered of a bundle of driver
|
563 |
* exceptions caused in the same map atomic transaction.</p>
|
564 |
*
|
565 |
* @param introductoryText introductory text specified by developer. If
|
566 |
* <code>null</code>, use ""
|
567 |
* @param driverExceptions list with a bundle of driver exceptions caught
|
568 |
* during an atomic event
|
569 |
*
|
570 |
* @see #addErrorListener(ErrorListener)
|
571 |
* @see #removeErrorListener(LegendListener)
|
572 |
*/
|
573 |
public synchronized void reportDriverExceptions(String introductoryText, |
574 |
List driverExceptions) {
|
575 |
for (int i = 0; i < errorListeners.size(); i++) { |
576 |
((ErrorListener) errorListeners.get(i)).
|
577 |
reportDriverExceptions(introductoryText, driverExceptions); |
578 |
} |
579 |
} |
580 |
|
581 |
/**
|
582 |
* <p>
|
583 |
* Adds the specified legend listener (if didn't exist) to receive legend
|
584 |
* events from this map.</p>
|
585 |
*
|
586 |
* @param listener the legend listener
|
587 |
*
|
588 |
* @see #removeLayerListener(LegendListener)
|
589 |
* @see #callLegendChanged()
|
590 |
*/
|
591 |
public void addLayerListener(LegendListener listener) { |
592 |
if (!legendListeners.contains(listener)) {
|
593 |
legendListeners.add(listener); |
594 |
} |
595 |
} |
596 |
|
597 |
/**
|
598 |
* <p>
|
599 |
* Adds the specified layer drawing listener to catch and handle drawing
|
600 |
* events from layers of this map.</p>
|
601 |
*
|
602 |
* @param listener the listener to add
|
603 |
*
|
604 |
* @see #removeLayerDrawListener(LayerDrawingListener)
|
605 |
* @see #fireLayerDrawingEvent(LayerDrawEvent)
|
606 |
*/
|
607 |
public void addLayerDrawingListener(LayerDrawingListener listener) { |
608 |
layerDrawingListeners.add(listener); |
609 |
} |
610 |
|
611 |
/**
|
612 |
* <p>
|
613 |
* Removes the specified layer drawing listener from this map.</p>
|
614 |
*
|
615 |
* @param listener the listener to remove
|
616 |
*
|
617 |
* @see #addLayerDrawingListener(LayerDrawingListener)
|
618 |
* @see #fireLayerDrawingEvent(LayerDrawEvent)
|
619 |
*/
|
620 |
public void removeLayerDrawListener(LayerDrawingListener listener) { |
621 |
layerDrawingListeners.remove(listener); |
622 |
} |
623 |
|
624 |
/**
|
625 |
* <p>
|
626 |
* Adds the specified error listener to receive error events from this
|
627 |
* map.</p>
|
628 |
*
|
629 |
* @param listener the listener to add
|
630 |
*
|
631 |
* @see #removeErrorListener(LegendListener)
|
632 |
* @see #reportDriverExceptions(String, List)
|
633 |
*/
|
634 |
public void addErrorListener(ErrorListener listener) { |
635 |
errorListeners.add(listener); |
636 |
} |
637 |
|
638 |
/**
|
639 |
* <p>
|
640 |
* Removes the specified error listener from this map.</p>
|
641 |
*
|
642 |
* @param listener the listener to remove
|
643 |
*
|
644 |
* @see #addErrorListener(ErrorListener)
|
645 |
* @see #reportDriverExceptions(String, List)
|
646 |
*/
|
647 |
public void removeErrorListener(LegendListener listener) { |
648 |
legendListeners.remove(listener); |
649 |
} |
650 |
|
651 |
/**
|
652 |
* Notifies to all legend listeners registered, that one legend has
|
653 |
* changed.
|
654 |
*
|
655 |
* This method must be called only if it's wanted to reflect a legend
|
656 |
* change.
|
657 |
*
|
658 |
* @see #addLayerListener(LegendListener)
|
659 |
* @see #removeLayerListener(LegendListener)
|
660 |
*/
|
661 |
public synchronized void callLegendChanged() { |
662 |
for (int i = 0; i < legendListeners.size(); i++) { |
663 |
((LegendListener) legendListeners.get(i)).legendChanged(null);
|
664 |
} |
665 |
// getLayers().moveTo(0,0);
|
666 |
} |
667 |
|
668 |
/**
|
669 |
* <p>
|
670 |
* Fires a layer drawing event to all
|
671 |
* {@link LayerDrawingListener LayerDrawingListener} listeners registered,
|
672 |
* distinguishing the kind of event.</p>
|
673 |
*
|
674 |
* @param e the event
|
675 |
*
|
676 |
* @see #addLayerDrawingListener(LayerDrawingListener)
|
677 |
* @see #removeLayerDrawListener(LayerDrawingListener)
|
678 |
*/
|
679 |
public synchronized void fireLayerDrawingEvent(LayerDrawEvent e) { |
680 |
for (int i = 0; i < layerDrawingListeners.size(); i++) { |
681 |
LayerDrawingListener listener = (LayerDrawingListener) layerDrawingListeners.get(i); |
682 |
switch (e.getEventType()) {
|
683 |
case LayerDrawEvent.LAYER_BEFORE_DRAW:
|
684 |
listener.beforeLayerDraw(e); |
685 |
break;
|
686 |
case LayerDrawEvent.LAYER_AFTER_DRAW:
|
687 |
listener.afterLayerDraw(e); |
688 |
break;
|
689 |
case LayerDrawEvent.GRAPHICLAYER_BEFORE_DRAW:
|
690 |
listener.beforeGraphicLayerDraw(e); |
691 |
break;
|
692 |
case LayerDrawEvent.GRAPHICLAYER_AFTER_DRAW:
|
693 |
listener.afterLayerGraphicDraw(e); |
694 |
break;
|
695 |
} |
696 |
} |
697 |
// getLayers().moveTo(0,0);
|
698 |
} |
699 |
|
700 |
/**
|
701 |
* <p>
|
702 |
* Notifies to all error listeners registered, that one error has been
|
703 |
* produced.</p>
|
704 |
*
|
705 |
* @param e the event with information of the error
|
706 |
*
|
707 |
* @see #addErrorListener(ErrorListener)
|
708 |
* @see #removeErrorListener(LegendListener)
|
709 |
* @see #reportDriverExceptions(String, List)
|
710 |
*/
|
711 |
public synchronized void callNewErrorEvent(ErrorEvent e) { |
712 |
for (int i = 0; i < errorListeners.size(); i++) { |
713 |
((ErrorListener) errorListeners.get(i)).errorThrown(e);
|
714 |
} |
715 |
errorListeners.clear(); |
716 |
// getLayers().moveTo(0,0);
|
717 |
} |
718 |
|
719 |
/**
|
720 |
* <p>
|
721 |
* Removes the specified layer listener from this map.</p>
|
722 |
*
|
723 |
* @param listener the listener to remove
|
724 |
*
|
725 |
* @see #addLayerListener(LegendListener)
|
726 |
* @see #callLegendChanged()
|
727 |
*/
|
728 |
public void removeLayerListener(LegendListener listener) { |
729 |
legendListeners.remove(listener); |
730 |
} |
731 |
|
732 |
/**
|
733 |
* <p>
|
734 |
* Returns the hierarchy of {@link FLayers FLayers} nodes stored in this
|
735 |
* map.</p>
|
736 |
*
|
737 |
* @return the hierarchy of nodes of layers stored in this map
|
738 |
*/
|
739 |
public FLayers getLayers() {
|
740 |
return layers;
|
741 |
} |
742 |
|
743 |
/**
|
744 |
* <p>
|
745 |
* Draws the visible layers of this map according its view port, on the
|
746 |
* image parameter.</p>
|
747 |
*
|
748 |
* @param b image with an accessible buffer of image data
|
749 |
*/
|
750 |
public void drawLabels(BufferedImage b) { |
751 |
} |
752 |
|
753 |
/**
|
754 |
* @see #redraw()
|
755 |
*/
|
756 |
public void invalidate() { |
757 |
updateDrawVersion(); |
758 |
this.viewPort.updateDrawVersion();
|
759 |
// Small hack to let the MapControl receive an event and repaint
|
760 |
FLayer layer; |
761 |
if (layers.getLayersCount() > 0) { |
762 |
layer = layers.getLayer(layers.getLayersCount() - 1);
|
763 |
} else {
|
764 |
layer = getGraphicsLayer(); |
765 |
} |
766 |
LayerPositionEvent event = LayerPositionEvent.createLayerMovedEvent(layer, 0, 0); |
767 |
eventBuffer.layerMoved(event); |
768 |
} |
769 |
|
770 |
/**
|
771 |
* <p>
|
772 |
* Prints the layers of this map using the {@link Graphics2D Graphics2D}
|
773 |
* argument, that usually is the {@link Graphics Graphics} of the printer.
|
774 |
* </p>
|
775 |
*
|
776 |
* @param g for rendering 2-dimensional shapes, text and images on the
|
777 |
* Java(tm) platform
|
778 |
* @param scale the scale of the view. Must be between
|
779 |
* {@linkplain FLayer#getMinScale()} and {@linkplain FLayer#getMaxScale()}.
|
780 |
* @param properties a set with the settings to be applied to a whole print
|
781 |
* job and to all the documents in the print job
|
782 |
* @throws MapContextException if there is an error getting the instance of
|
783 |
* MapContextDrawer
|
784 |
*
|
785 |
* @throws ReadDriverException if fails reading with driver.
|
786 |
*
|
787 |
* @see GraphicLayer#draw(BufferedImage, Graphics2D, ViewPort, Cancellable,
|
788 |
* double)
|
789 |
*/
|
790 |
public void print(Graphics2D g, double scale, |
791 |
PrintAttributes properties) throws ReadException,
|
792 |
MapContextException { |
793 |
|
794 |
CompatLocator.getGraphicsUtils().setRenderingHintsForPrinting(g); |
795 |
|
796 |
Cancellable cancel = new Cancellable() {
|
797 |
public boolean isCanceled() { |
798 |
return false; |
799 |
} |
800 |
|
801 |
public void setCanceled(boolean canceled) { |
802 |
// No queremos que se pueda cancelar la impresi?n.
|
803 |
|
804 |
} |
805 |
}; |
806 |
MapContextDrawer drawer = this.getMapContextDrawer();
|
807 |
drawer.setPrintGraphicsLayer(this.isPrintGraphicsLayer());
|
808 |
drawer.print(this.layers, g, cancel, scale, properties);
|
809 |
} |
810 |
|
811 |
/**
|
812 |
* <p>
|
813 |
* Returns a new <code>MapContext</code> instance with the information of
|
814 |
* the <code>vp</code> argument, and the layers of this map.</p>
|
815 |
*
|
816 |
* @param vp information for drawing the layers of this map in the available
|
817 |
* rectangular area according a projection
|
818 |
*
|
819 |
* @return a new <code>MapContext</code> instance projected by
|
820 |
* <code>vp</code>
|
821 |
*/
|
822 |
public MapContext createNewFMap(ViewPort vp) {
|
823 |
MapContext ret = new MapContext(vp);
|
824 |
ret.layers = this.layers;
|
825 |
DisposeUtils.bind((Disposable)ret.layers); |
826 |
DisposeUtils.bind((List)ret.layers);
|
827 |
|
828 |
return ret;
|
829 |
} |
830 |
|
831 |
/**
|
832 |
* <p>
|
833 |
* Creates a new independent <code>MapContext</code> instance, that has a
|
834 |
* clone of the layers and the view port of this one.</p>
|
835 |
* <p>
|
836 |
* The new map will have the same data source drivers to avoid waste memory,
|
837 |
* and work faster.</p>
|
838 |
*
|
839 |
* @return the new <code>MapContext</code> instance
|
840 |
*
|
841 |
* @throws XMLException if fails cloning the view port or a layer
|
842 |
*
|
843 |
* @see FLayer#cloneLayer()
|
844 |
* @see ViewPort#cloneViewPort()
|
845 |
*/
|
846 |
public MapContext cloneFMap() {
|
847 |
ViewPort vp; |
848 |
try {
|
849 |
vp = (ViewPort) getViewPort().clone(); |
850 |
} catch (CloneNotSupportedException e) { |
851 |
throw new RuntimeException(e); |
852 |
} |
853 |
FLayers antLayers = getLayers(); |
854 |
MapContext ret = new MapContext(vp);
|
855 |
|
856 |
/*
|
857 |
* Cloning order manager
|
858 |
*/
|
859 |
LayerOrderManager lom = this.getOrderManager();
|
860 |
try {
|
861 |
lom = (LayerOrderManager) lom.clone(); |
862 |
ret.setOrderManager(lom); |
863 |
} catch (CloneNotSupportedException e1) { |
864 |
LOGGER.error("While cloning order manager", e1);
|
865 |
} |
866 |
|
867 |
for (int i = 0; i < antLayers.getLayersCount(); i++) { |
868 |
FLayer lyr = antLayers.getLayer(i); |
869 |
try {
|
870 |
FLayer auxLayer = lyr.cloneLayer(); |
871 |
ret.layers.addLayer(auxLayer); |
872 |
auxLayer.dispose(); |
873 |
} catch (Exception e) { |
874 |
throw new RuntimeException(e); |
875 |
} |
876 |
} |
877 |
return ret;
|
878 |
|
879 |
} |
880 |
|
881 |
/**
|
882 |
* Like {@linkplain #cloneFMap()}, but now doesn't clone the layers, rather
|
883 |
* copies them.
|
884 |
*
|
885 |
* @return the new map
|
886 |
*/
|
887 |
public MapContext cloneToDraw() {
|
888 |
ViewPort vp; |
889 |
try {
|
890 |
vp = (ViewPort) getViewPort().clone(); |
891 |
MapContext mapContext = new MapContext(getLayers(), vp);
|
892 |
return mapContext;
|
893 |
} catch (CloneNotSupportedException e) { |
894 |
throw new RuntimeException(e); |
895 |
} |
896 |
} |
897 |
|
898 |
/**
|
899 |
* <p>
|
900 |
* Adds a layer to the group of layers that are at a upper level in the
|
901 |
* tree.</p>
|
902 |
*
|
903 |
* @param vectorial the layer to add
|
904 |
*/
|
905 |
public void addToTrackLayer(FLayer vectorial) { |
906 |
} |
907 |
|
908 |
/**
|
909 |
* <p>
|
910 |
* Returns the scale of the view in the screen.</p>
|
911 |
*
|
912 |
* @return one of this values:
|
913 |
* <ul>
|
914 |
* <li>the scale of the adjusted extent scale of the view in the screen
|
915 |
* <li><code>-1</code> if there is no image
|
916 |
* <li><code>0</code> if there is no extent defined for the image
|
917 |
* </ul>
|
918 |
*
|
919 |
* @see #setScaleView(long)
|
920 |
* @see ViewPort#getAdjustedExtent()
|
921 |
* @see IProjection#getScale(double, double, double, double)
|
922 |
*/
|
923 |
public long getScaleView() { |
924 |
double dpi = this.getViewPort().getDPI(); |
925 |
IProjection proj = viewPort.getProjection(); |
926 |
|
927 |
if (viewPort.getImageSize() == null) { |
928 |
return -1; |
929 |
} |
930 |
|
931 |
if (viewPort.getAdjustedEnvelope() == null) { |
932 |
return 0; |
933 |
} |
934 |
double[] trans2Meter = getDistanceTrans2Meter(); |
935 |
int mUnits = getViewPort().getMapUnits();
|
936 |
|
937 |
if (proj == null) { |
938 |
double w = ((viewPort.getImageSize().width / dpi) * 0.0254); |
939 |
return (long) (viewPort.getAdjustedEnvelope().getLength(0) |
940 |
/ w * trans2Meter[mUnits]); |
941 |
} |
942 |
|
943 |
return Math.round(proj.getScale( |
944 |
viewPort.getAdjustedEnvelope().getMinimum(0) * trans2Meter[mUnits],
|
945 |
viewPort.getAdjustedEnvelope().getMaximum(0) * trans2Meter[mUnits],
|
946 |
viewPort.getImageSize().width, |
947 |
dpi)); |
948 |
|
949 |
} |
950 |
|
951 |
/**
|
952 |
* <p>
|
953 |
* Sets the new extent of the view, calculated using the scale argument.</p>
|
954 |
* <p>
|
955 |
* Doesn't updates the scale if there isn't information about the dimension
|
956 |
* of the image or the adjusted extent.</p>
|
957 |
*
|
958 |
* @param scale the new scale for the view
|
959 |
*
|
960 |
* @see ViewPort#setProjection(IProjection)
|
961 |
* @see #getScaleView()
|
962 |
*/
|
963 |
public void setScaleView(long scale) { |
964 |
double dpi = this.getViewPort().getDPI(); |
965 |
if (viewPort.getImageSize() == null) { |
966 |
return;
|
967 |
} |
968 |
IProjection proj = viewPort.getProjection(); |
969 |
if (viewPort.getAdjustedExtent() == null) { |
970 |
return;
|
971 |
} |
972 |
double[] trans2Meter = getDistanceTrans2Meter(); |
973 |
Envelope env = viewPort.getAdjustedExtent(); |
974 |
Rectangle2D r = new Rectangle2D.Double(env.getMinimum(0), env.getMinimum(1), env.getLength(0), env.getLength(1)); |
975 |
Rectangle2D rec = proj.getExtent(r, scale, viewPort.getImageWidth(), viewPort.getImageHeight(), 100 * getDistanceTrans2Meter()[getViewPort().getMapUnits()], trans2Meter[getViewPort().getDistanceUnits()], dpi); |
976 |
try {
|
977 |
getViewPort().setEnvelope(GEOM_MANAGER.createEnvelope(rec.getX(), rec.getY(), rec.getMaxX(), rec.getMaxY(), SUBTYPES.GEOM2D)); |
978 |
} catch (CreateEnvelopeException e) {
|
979 |
LOGGER.error("Error seting the bounding box");
|
980 |
} |
981 |
} |
982 |
|
983 |
/**
|
984 |
* <p>
|
985 |
* Returns the screen resolution (Dots Per Inch) as it was defined by the
|
986 |
* user's preference, or by default as it is defined in the default
|
987 |
* Toolkit.</p>
|
988 |
*
|
989 |
* Be care, use ViewPort#getDPI to ensure are using the corrects DPI.
|
990 |
*
|
991 |
* @return double with the screen's dpi
|
992 |
*/
|
993 |
public static double getScreenDPI() { |
994 |
return CompatLocator.getGraphicsUtils().getScreenDPI();
|
995 |
} |
996 |
|
997 |
/**
|
998 |
* @see
|
999 |
* org.gvsig.fmap.mapcontext.rendering.strategies.Strategy#process(com.iver.cit.gvsig.fmap.FeatureSelectorVisitor)
|
1000 |
*/
|
1001 |
public void process(Visitor visitor) { |
1002 |
} |
1003 |
|
1004 |
/**
|
1005 |
* @see
|
1006 |
* org.gvsig.fmap.mapcontext.rendering.strategies.Strategy#processSelected(com.iver.cit.gvsig.fmap.FeatureVisitor)
|
1007 |
*/
|
1008 |
public void processSelected(Visitor visitor) { |
1009 |
} |
1010 |
|
1011 |
/**
|
1012 |
* @see
|
1013 |
* org.gvsig.fmap.mapcontext.rendering.strategies.Strategy#select(com.iver.cit.gvsig.fmap.FeatureSelectorVisitor,
|
1014 |
* VectorialSubSet)
|
1015 |
*/
|
1016 |
public void select(Visitor visitor) { |
1017 |
} |
1018 |
|
1019 |
/**
|
1020 |
* @see
|
1021 |
* org.gvsig.fmap.mapcontext.rendering.strategies.Strategy#selectFromSelection()
|
1022 |
*/
|
1023 |
public void selectFromSelection() { |
1024 |
} |
1025 |
|
1026 |
/**
|
1027 |
* @see
|
1028 |
* org.gvsig.fmap.mapcontext.rendering.strategies.Strategy#createIndex()
|
1029 |
*/
|
1030 |
public void createIndex() { |
1031 |
} |
1032 |
|
1033 |
/**
|
1034 |
* @see org.cresques.geo.Projected#getProjection()
|
1035 |
*
|
1036 |
* @see ViewPort#getProjection()
|
1037 |
* @see #setProjection(IProjection)
|
1038 |
* @see #reProject(ICoordTrans)
|
1039 |
*/
|
1040 |
public IProjection getProjection() {
|
1041 |
return getViewPort().getProjection();
|
1042 |
} |
1043 |
|
1044 |
/**
|
1045 |
* <p>
|
1046 |
* Sets the new projection.</p>
|
1047 |
*
|
1048 |
* @param proj the new projection
|
1049 |
*
|
1050 |
* @see #getProjection()
|
1051 |
* @see ViewPort#setProjection(IProjection)
|
1052 |
* @see #reProject(ICoordTrans)
|
1053 |
*/
|
1054 |
public void setProjection(IProjection proj) { |
1055 |
if (getViewPort() != null) { |
1056 |
getViewPort().setProjection(proj); |
1057 |
} |
1058 |
} |
1059 |
|
1060 |
/**
|
1061 |
* @see org.cresques.geo.Projected#reProject(org.cresques.cts.ICoordTrans)
|
1062 |
*/
|
1063 |
public void reProject(ICoordTrans arg0) { |
1064 |
// TODO implementar reprojecci?n (lo que sea eso)
|
1065 |
} |
1066 |
|
1067 |
public Envelope getSelectionBounds() throws BaseException { |
1068 |
|
1069 |
SelectedEnvelopeVisitor visitor = new SelectedEnvelopeVisitor();
|
1070 |
|
1071 |
layers.accept(visitor); |
1072 |
Envelope env_in_data_crs = visitor.getSelectioEnvelope(); |
1073 |
return env_in_data_crs;
|
1074 |
} |
1075 |
|
1076 |
/**
|
1077 |
* <p>
|
1078 |
* Draws this map if its {@link ViewPort ViewPort} has an extent
|
1079 |
* defined:<br>
|
1080 |
* <ol>
|
1081 |
* <li>Selects only the layers that have to be drawn:
|
1082 |
* {@linkplain #prepareDrawing(BufferedImage, Graphics2D, double)}.
|
1083 |
* <li>Sets quality: antialiasing by text and images, and quality rendering.
|
1084 |
* <li>Draws the layers.
|
1085 |
* <li>Fires a <code>LayerDrawEvent.GRAPHICLAYER_BEFORE_DRAW</code>.
|
1086 |
* <li>Draws the graphic layer.
|
1087 |
* <li>Fires a <code>LayerDrawEvent.GRAPHICLAYER_AFTER_DRAW</code>.
|
1088 |
* <li>Invokes the garbage collector and memory clean.
|
1089 |
* </ol>
|
1090 |
* </p>
|
1091 |
*
|
1092 |
* @param image buffer used sometimes instead <code>g</code> to accelerate
|
1093 |
* the draw. For example, if two points are as closed that can't be
|
1094 |
* distinguished, draws only one.
|
1095 |
* @param g for rendering 2-dimensional shapes, text and images on the
|
1096 |
* Java(tm) platform
|
1097 |
* @param cancel shared object that determines if this layer can continue
|
1098 |
* being drawn
|
1099 |
* @param scale the scale of the view. Must be between
|
1100 |
* {@linkplain FLayer#getMinScale()} and {@linkplain FLayer#getMaxScale()}.
|
1101 |
* @throws MapContextException if there is an error getting the instance of
|
1102 |
* MapContextDrawer
|
1103 |
* @throws ReadDriverException if fails reading with the driver.
|
1104 |
*/
|
1105 |
public void draw(BufferedImage image, Graphics2D g, Cancellable cancel, |
1106 |
double scale) throws ReadException, MapContextException { |
1107 |
if (viewPort.getEnvelope() == null) { |
1108 |
return;
|
1109 |
} |
1110 |
|
1111 |
CompatLocator.getGraphicsUtils().setRenderingHintsForDrawing(g); |
1112 |
|
1113 |
this.getMapContextDrawer().draw(this.layers, image, g, cancel, scale); |
1114 |
} |
1115 |
|
1116 |
/**
|
1117 |
* <p>
|
1118 |
* Draws only the internal graphic layer using the information of the
|
1119 |
* {@link ViewPort ViewPort} of this map.</p>
|
1120 |
*
|
1121 |
* @param image image used to accelerate the screen draw
|
1122 |
* @param g for rendering 2-dimensional shapes, text and images on the
|
1123 |
* Java(tm) platform
|
1124 |
* @param cancel shared object that determines if this layer can continue
|
1125 |
* being drawn
|
1126 |
* @param scale value that represents the scale
|
1127 |
* @throws ReadDriverException if fails reading with the driver.
|
1128 |
* @deprecated use
|
1129 |
* {@link #draw(BufferedImage, Graphics2D, Cancellable, double)} instead
|
1130 |
* @see GraphicLayer#draw(BufferedImage, Graphics2D, ViewPort, Cancellable,
|
1131 |
* double)
|
1132 |
*/
|
1133 |
public void drawGraphics(BufferedImage image, Graphics2D g, |
1134 |
Cancellable cancel, double scale) throws ReadException { |
1135 |
|
1136 |
// From now on the graphics layer is handled by the MapContextDrawer,
|
1137 |
// so call the draw method instead.
|
1138 |
try {
|
1139 |
draw(image, g, cancel, scale); |
1140 |
} catch (MapContextException e) {
|
1141 |
throw new RuntimeException(e); |
1142 |
} |
1143 |
} |
1144 |
|
1145 |
/**
|
1146 |
* <p>
|
1147 |
* Like
|
1148 |
* {@linkplain MapContext#draw(BufferedImage, Graphics2D, Cancellable, double)}
|
1149 |
* , but creating the task as cancellable.
|
1150 |
* </p>
|
1151 |
*
|
1152 |
* @param image buffer used sometimes instead <code>g</code> to accelerate
|
1153 |
* the draw. For example, if two points are as closed that can't be
|
1154 |
* distinguished, draws only one.
|
1155 |
* @param g for rendering 2-dimensional shapes, text and images on the
|
1156 |
* Java(tm) platform
|
1157 |
* @param scale the scale of the view. Must be between
|
1158 |
* {@linkplain FLayer#getMinScale()} and {@linkplain FLayer#getMaxScale()}.
|
1159 |
* @throws MapContextException if there is an error getting the instance of
|
1160 |
* MapContextDrawer
|
1161 |
*
|
1162 |
* @throws ReadDriverException if the driver fails reading.
|
1163 |
*
|
1164 |
* @see #draw(BufferedImage, Graphics2D, Cancellable, double)
|
1165 |
*/
|
1166 |
public void draw(BufferedImage image, Graphics2D g, double scale) |
1167 |
throws ReadException, MapContextException {
|
1168 |
draw(image, g, new Cancellable() {
|
1169 |
/**
|
1170 |
* @see org.gvsig.utils.swing.threads.Cancellable#isCanceled()
|
1171 |
*/
|
1172 |
public boolean isCanceled() { |
1173 |
return false; |
1174 |
} |
1175 |
|
1176 |
public void setCanceled(boolean canceled) { |
1177 |
// Do nothing
|
1178 |
} |
1179 |
}, scale); |
1180 |
} |
1181 |
|
1182 |
/**
|
1183 |
* <p>
|
1184 |
* Gets the {@link ViewPort ViewPort} associated to this map.</p>
|
1185 |
*
|
1186 |
* @return the view port
|
1187 |
*
|
1188 |
* @see #setViewPort(ViewPort)
|
1189 |
*/
|
1190 |
public ViewPort getViewPort() {
|
1191 |
return viewPort;
|
1192 |
} |
1193 |
|
1194 |
/**
|
1195 |
* <p>
|
1196 |
* Sets a {@link ViewPort ViewPort} with the drawing information of this
|
1197 |
* map.</p>
|
1198 |
* <p>
|
1199 |
* If there was a previous view port, removes its
|
1200 |
* {@link EventBuffer EventBuffer} and adds the new one.</p>
|
1201 |
*
|
1202 |
* @param viewPort the viewPort
|
1203 |
*
|
1204 |
* @see #getViewPort()
|
1205 |
*/
|
1206 |
public void setViewPort(ViewPort viewPort) { |
1207 |
if (this.viewPort != null) { |
1208 |
this.viewPort.removeViewPortListener(eventBuffer);
|
1209 |
} |
1210 |
|
1211 |
if (this.mapContextDrawer != null) { |
1212 |
this.mapContextDrawer.setViewPort(viewPort);
|
1213 |
} |
1214 |
|
1215 |
this.viewPort = viewPort;
|
1216 |
if (viewPort != null) { |
1217 |
viewPort.addViewPortListener(eventBuffer); |
1218 |
} |
1219 |
} |
1220 |
|
1221 |
/**
|
1222 |
* <p>
|
1223 |
* Sets the given extent to the {@link ViewPort ViewPort} and updates the
|
1224 |
* view with the new zoom.</p>
|
1225 |
*
|
1226 |
* @param extent the extent of the new zoom
|
1227 |
*/
|
1228 |
public void zoomToEnvelope(Envelope env) { |
1229 |
if (env == null || env.isEmpty()) { |
1230 |
return;
|
1231 |
} |
1232 |
if( env.isCollapsed(Geometry.SUBTYPES.GEOM2D) ) {
|
1233 |
// El envelop ha colapsado en X e Y.
|
1234 |
// Fijamos la escala por defecto para cuando colapsa y centramos
|
1235 |
// sobre el centro del envelop.
|
1236 |
|
1237 |
this.setScaleView(this.getScaleToUseWhenEnvelopeCollapse()); |
1238 |
|
1239 |
Envelope curenv = viewPort.getEnvelope(); |
1240 |
double halfWidth = curenv.getLength(Geometry.DIMENSIONS.X) / 2; |
1241 |
double halfHeight = curenv.getLength(Geometry.DIMENSIONS.Y) / 2; |
1242 |
double centerx = env.getCenter(Geometry.DIMENSIONS.X);
|
1243 |
double centery = env.getCenter(Geometry.DIMENSIONS.Y);
|
1244 |
|
1245 |
Point lowerCorner = GeometryUtils.createPoint(halfWidth + centerx, halfHeight + centery);
|
1246 |
Point upperCorner = GeometryUtils.createPoint(halfWidth - centerx, halfHeight - centery);
|
1247 |
|
1248 |
env.setLowerCorner(lowerCorner); |
1249 |
env.setUpperCorner(upperCorner); |
1250 |
} |
1251 |
this.viewPort.setEnvelope(env);
|
1252 |
} |
1253 |
|
1254 |
/**
|
1255 |
* <p>
|
1256 |
* Returns the union of all extents of all layers of this map.</p>
|
1257 |
*
|
1258 |
* @return full extent of layers of this map
|
1259 |
* @throws ReadDriverException if the driver fails reading.
|
1260 |
*
|
1261 |
* @see FLayers#getFullEnvelope()
|
1262 |
*/
|
1263 |
public Envelope getFullEnvelope() throws ReadException { |
1264 |
Envelope envelope = layers.getFullEnvelope(); |
1265 |
|
1266 |
for (VectorLayer tracLayer : this.tracLayers.values()) { |
1267 |
if (tracLayer != null) { |
1268 |
Envelope graphicsEnvelope = tracLayer.getFullEnvelope(); |
1269 |
if( graphicsEnvelope==null ) { |
1270 |
continue;
|
1271 |
} |
1272 |
if (envelope == null) { |
1273 |
try {
|
1274 |
envelope = (Envelope) graphicsEnvelope.clone(); |
1275 |
} catch (CloneNotSupportedException ex) { |
1276 |
} |
1277 |
} else if (graphicsEnvelope != null) { |
1278 |
envelope.add(graphicsEnvelope); |
1279 |
} |
1280 |
} |
1281 |
} |
1282 |
return envelope;
|
1283 |
} |
1284 |
|
1285 |
/**
|
1286 |
* <p>
|
1287 |
* Adds a listener of atomic events to the internal
|
1288 |
* {@link EventBuffer EventBuffer}.</p>
|
1289 |
*
|
1290 |
* @param listener the new listener
|
1291 |
*
|
1292 |
* @return <code>true</code> if has added the listener successfully
|
1293 |
*
|
1294 |
* @see #removeAtomicEventListener(AtomicEventListener)
|
1295 |
* @see EventBuffer#addAtomicEventListener(AtomicEventListener)
|
1296 |
*/
|
1297 |
public boolean addAtomicEventListener(AtomicEventListener listener) { |
1298 |
return eventBuffer.addAtomicEventListener(listener);
|
1299 |
} |
1300 |
|
1301 |
/**
|
1302 |
* <p>
|
1303 |
* Removes a listener of atomic events from the internal
|
1304 |
* {@link EventBuffer EventBuffer}.</p>
|
1305 |
*
|
1306 |
* @param listener the listener to remove
|
1307 |
*
|
1308 |
* @return <tt>true</tt> if the list contained the specified element
|
1309 |
*
|
1310 |
* @see #addAtomicEventListener(AtomicEventListener)
|
1311 |
* @see EventBuffer#removeAtomicEventListener(AtomicEventListener)
|
1312 |
*/
|
1313 |
public boolean removeAtomicEventListener(AtomicEventListener listener) { |
1314 |
return eventBuffer.removeAtomicEventListener(listener);
|
1315 |
} |
1316 |
|
1317 |
/**
|
1318 |
* @see EventBuffer#beginAtomicEvent()
|
1319 |
*
|
1320 |
* @see #endAtomicEvent()
|
1321 |
*/
|
1322 |
public void beginAtomicEvent() { |
1323 |
eventBuffer.beginAtomicEvent(); |
1324 |
} |
1325 |
|
1326 |
/**
|
1327 |
* @see EventBuffer#endAtomicEvent()
|
1328 |
*
|
1329 |
* @see #beginAtomicEvent()
|
1330 |
*/
|
1331 |
public void endAtomicEvent() { |
1332 |
eventBuffer.endAtomicEvent(); |
1333 |
} |
1334 |
|
1335 |
/**
|
1336 |
* <p>
|
1337 |
* The class <code>LayerEventListener</code> implements the methods of
|
1338 |
* {@link LayerCollectionListener LayerCollectionListener} that handles the
|
1339 |
* "layer added" or "layer removed" events in a map.</p>
|
1340 |
* <p>
|
1341 |
* Is designed as a listener for all layers in a
|
1342 |
* {@link MapContext MapContext}.</p>
|
1343 |
*
|
1344 |
* @author Fernando Gonz?lez Cort?s
|
1345 |
*/
|
1346 |
public class LayerEventListener extends BaseLayerCollectionListener { |
1347 |
|
1348 |
@Override
|
1349 |
public void layerAdded(LayerCollectionEvent e) { |
1350 |
// Voy a mover todo esto a la vista
|
1351 |
// // Si aun no tenemos envelope, problablemente sera la primera capa,
|
1352 |
// // asi que asignamos el envelope de la capa al mapcontext.
|
1353 |
// if (getViewPort().getEnvelope() == null) {
|
1354 |
// FLayer lyr = e.getAffectedLayer();
|
1355 |
// if (lyr.isAvailable()) {
|
1356 |
// try {
|
1357 |
// getViewPort().setEnvelope(lyr.getFullEnvelope());
|
1358 |
// } catch (Exception ex) {
|
1359 |
// logger.warn(
|
1360 |
// MessageFormat.format(
|
1361 |
// "Can''t set envelope to view port from layer ''{0}''",
|
1362 |
// new Object[]{lyr.getName()}
|
1363 |
// ),
|
1364 |
// ex
|
1365 |
// );
|
1366 |
// }
|
1367 |
// }
|
1368 |
// }
|
1369 |
|
1370 |
// Registramos al FMap como listener del legend de las capas
|
1371 |
FLayer lyr = e.getAffectedLayer(); |
1372 |
addSelectionListener(lyr); |
1373 |
} |
1374 |
|
1375 |
@Override
|
1376 |
public void layerRemoved(LayerCollectionEvent e) { |
1377 |
FLayer lyr = e.getAffectedLayer(); |
1378 |
|
1379 |
lyr.removeLayerListener(eventBuffer); |
1380 |
|
1381 |
if (lyr instanceof Classifiable) { |
1382 |
Classifiable c = (Classifiable) lyr; |
1383 |
c.removeLegendListener(eventBuffer); |
1384 |
} |
1385 |
|
1386 |
if (lyr instanceof SingleLayer && ((SingleLayer) lyr).getDataStore() != null) { |
1387 |
((SingleLayer) lyr).getDataStore().deleteObserver( |
1388 |
MapContext.this); |
1389 |
} |
1390 |
|
1391 |
} |
1392 |
|
1393 |
} |
1394 |
|
1395 |
/**
|
1396 |
* <p>
|
1397 |
* Adds the {@link LayerEventListener LayerEventListener} of this map to the
|
1398 |
* collection of layers argument.</p>
|
1399 |
*
|
1400 |
* @param a collection of layers
|
1401 |
*/
|
1402 |
public void addAsCollectionListener(FLayers layers2) { |
1403 |
layers2.addLayerCollectionListener(layerEventListener); |
1404 |
layers2.addLayerCollectionListener(eventBuffer); |
1405 |
} |
1406 |
|
1407 |
public VectorLayer getGraphicsLayer(String name) { |
1408 |
return this.tracLayers.get(name); |
1409 |
} |
1410 |
|
1411 |
public Collection<VectorLayer> getGraphicsLayers() { |
1412 |
return this.tracLayers.values(); |
1413 |
} |
1414 |
|
1415 |
public void setGraphicsLayer(String name, FLyrVect layer) { |
1416 |
this.tracLayers.put(name, layer);
|
1417 |
} |
1418 |
|
1419 |
public void removeGraphicsLayer(String name) { |
1420 |
this.tracLayers.remove(name);
|
1421 |
} |
1422 |
|
1423 |
/**
|
1424 |
* <p>
|
1425 |
* Returns the internal {@link GraphicLayer GraphicLayer}.</p>
|
1426 |
*
|
1427 |
* @return the graphic layer of this map
|
1428 |
*
|
1429 |
* @see #setGraphicsLayer(GraphicLayer)
|
1430 |
*/
|
1431 |
public GraphicLayer getGraphicsLayer() {
|
1432 |
GraphicLayer tracLayer = (GraphicLayer) this.tracLayers.get(DEFAULT_TRACTLAYER);
|
1433 |
if (tracLayer == null) { |
1434 |
if (getViewPort() != null) { |
1435 |
tracLayer |
1436 |
= MapContextLocator.getMapContextManager() |
1437 |
.createGraphicsLayer( |
1438 |
getViewPort().getProjection()); |
1439 |
} else {
|
1440 |
tracLayer |
1441 |
= MapContextLocator.getMapContextManager() |
1442 |
.createGraphicsLayer(null);
|
1443 |
} |
1444 |
this.tracLayers.put(DEFAULT_TRACTLAYER, tracLayer);
|
1445 |
} |
1446 |
return tracLayer;
|
1447 |
} |
1448 |
|
1449 |
/**
|
1450 |
* <p>
|
1451 |
* Sets a new {@link GraphicLayer GraphicLayer} to this map.</p>
|
1452 |
*
|
1453 |
* @param graphicLayer the new graphic layer
|
1454 |
*
|
1455 |
* @see #getGraphicsLayer()
|
1456 |
*/
|
1457 |
public void setGraphicsLayer(GraphicLayer graphicLayer) { |
1458 |
this.tracLayers.put(DEFAULT_TRACTLAYER, graphicLayer);
|
1459 |
} |
1460 |
|
1461 |
/**
|
1462 |
* <p>
|
1463 |
* Indicates whether some other object is "equal to" this map.</p>
|
1464 |
* <p>
|
1465 |
* Returns <code>true</code> if success one of this options:
|
1466 |
* <ul>
|
1467 |
* <li>Both objects are equal according to
|
1468 |
* {@linkplain Object#equals(Object)}.
|
1469 |
* <li>Both maps have the same layers.
|
1470 |
* <li>Both maps have the same number of layers and with the same name.
|
1471 |
* </ul>
|
1472 |
* </p>
|
1473 |
*
|
1474 |
* @param obj the reference object with which to compare.
|
1475 |
* @return <code>true</code> if this object is the same as the
|
1476 |
* <code>arg0</code> argument; otherwise <code>false</code>.
|
1477 |
*
|
1478 |
* @see Object#equals(Object)
|
1479 |
*/
|
1480 |
public boolean equals(Object arg0) { |
1481 |
if (!(arg0 instanceof MapContext)) { |
1482 |
return false; |
1483 |
} |
1484 |
MapContext map = (MapContext) arg0; |
1485 |
if (super.equals(arg0)) { |
1486 |
return true; |
1487 |
} |
1488 |
if (getLayers() == map.getLayers()) {
|
1489 |
return true; |
1490 |
} |
1491 |
boolean isEqual = true; |
1492 |
if (map.getLayers().getLayersCount() == getLayers().getLayersCount()) {
|
1493 |
for (int i = 0; i < getLayers().getLayersCount(); i++) { |
1494 |
|
1495 |
if (!getLayers().getLayer(i).getName().equals(
|
1496 |
map.getLayers().getLayer(i).getName())) { |
1497 |
isEqual = false;
|
1498 |
} |
1499 |
|
1500 |
} |
1501 |
} else {
|
1502 |
isEqual = false;
|
1503 |
} |
1504 |
return isEqual;
|
1505 |
} |
1506 |
|
1507 |
/**
|
1508 |
* <p>
|
1509 |
* Registers the message of an error associated to this map.</p>
|
1510 |
*
|
1511 |
* @param stringProperty the error message
|
1512 |
*
|
1513 |
* @see #getLayersError()
|
1514 |
* @see #clearErrors()
|
1515 |
*/
|
1516 |
public void addLayerError(String stringProperty) { |
1517 |
layersError.add(stringProperty); |
1518 |
} |
1519 |
|
1520 |
/**
|
1521 |
* <p>
|
1522 |
* Gets the list with all error messages registered to this map.</p>
|
1523 |
*
|
1524 |
* @return the list of errors registered to this map
|
1525 |
*
|
1526 |
* @see #addLayerError(String)
|
1527 |
* @see #clearErrors()
|
1528 |
*/
|
1529 |
public ArrayList getLayersError() { |
1530 |
return layersError;
|
1531 |
} |
1532 |
|
1533 |
/**
|
1534 |
* <p>
|
1535 |
* Removes all error messages associated to this map.</p>
|
1536 |
*
|
1537 |
* @see #addLayerError(String)
|
1538 |
* @see #getLayersError()
|
1539 |
*/
|
1540 |
public void clearErrors() { |
1541 |
layersError.clear(); |
1542 |
} |
1543 |
|
1544 |
/**
|
1545 |
* <p>
|
1546 |
* Creates and returns a new group of layers that belongs to this
|
1547 |
* <code>MapContext</code>.</p>
|
1548 |
*
|
1549 |
* @param parent layer node in this <code>MapContexte</code> that will be
|
1550 |
* the parent of the new node
|
1551 |
* @return the new layer node
|
1552 |
*/
|
1553 |
public FLayers getNewGroupLayer(FLayers parent) {
|
1554 |
FLayers group1 = new FLayers();//(this,parent); |
1555 |
group1.setMapContext(this);
|
1556 |
group1.setParentLayer(parent); |
1557 |
return group1;
|
1558 |
} |
1559 |
|
1560 |
public String getClassName() { |
1561 |
return null; |
1562 |
} |
1563 |
|
1564 |
public Set<FLyrVect> getLayersToSnap() { |
1565 |
return layersToSnap;
|
1566 |
} |
1567 |
|
1568 |
public void setLayersToSnap(Set<FLyrVect> layersToSnap) { |
1569 |
this.layersToSnap = layersToSnap;
|
1570 |
|
1571 |
} |
1572 |
|
1573 |
public void update(Observable observable, Object notification) { |
1574 |
// TODO REVISAR ESTO!!!
|
1575 |
String ntype = null; |
1576 |
if (notification instanceof FeatureStoreNotification) { |
1577 |
FeatureStoreNotification fsNotification = (FeatureStoreNotification) notification; |
1578 |
ntype = fsNotification.getType(); |
1579 |
if (ntype.equals(FeatureStoreNotification.LOAD_FINISHED)
|
1580 |
|| ntype.equals(FeatureStoreNotification.SELECTION_CHANGE)) { |
1581 |
getLayers().moveTo(0, 0); |
1582 |
} |
1583 |
} |
1584 |
} |
1585 |
|
1586 |
public long getDrawVersion() { |
1587 |
if (getViewPort().getDrawVersion() > this.viewPortVersion |
1588 |
|| getLayers().getDrawVersion() > this.layersVersion
|
1589 |
|| getGraphicsLayer().getDrawVersion() > graphicsLayerVersion) { |
1590 |
updateDrawVersion(); |
1591 |
} |
1592 |
return this.drawVersion; |
1593 |
} |
1594 |
|
1595 |
protected void updateDrawVersion() { |
1596 |
this.layersVersion = getLayers().getDrawVersion();
|
1597 |
this.viewPortVersion = getViewPort().getDrawVersion();
|
1598 |
this.graphicsLayerVersion = getGraphicsLayer().getDrawVersion();
|
1599 |
this.drawVersion++;
|
1600 |
} |
1601 |
|
1602 |
public MapContextDrawer getMapContextDrawer() throws ReadException, |
1603 |
MapContextException { |
1604 |
if (this.mapContextDrawer == null) { |
1605 |
if (mapContextDrawerClass == null) { |
1606 |
this.mapContextDrawer = MAPCONTEXT_MANAGER
|
1607 |
.createDefaultMapContextDrawerInstance(); |
1608 |
} else {
|
1609 |
this.mapContextDrawer = MAPCONTEXT_MANAGER
|
1610 |
.createMapContextDrawerInstance(mapContextDrawerClass); |
1611 |
} |
1612 |
this.mapContextDrawer.setMapContext(this); |
1613 |
this.mapContextDrawer.setViewPort(viewPort);
|
1614 |
} |
1615 |
|
1616 |
return this.mapContextDrawer; |
1617 |
} |
1618 |
|
1619 |
public void setMapContextDrawerClass(Class mapContextDrawerClass) |
1620 |
throws MapContextException {
|
1621 |
MAPCONTEXT_MANAGER.validateMapContextDrawer(mapContextDrawerClass); |
1622 |
this.mapContextDrawerClass = mapContextDrawerClass;
|
1623 |
if (this.mapContextDrawer != null) { |
1624 |
this.mapContextDrawer.dispose();
|
1625 |
this.mapContextDrawer = null; |
1626 |
} |
1627 |
} |
1628 |
|
1629 |
public void setMapContextDrawer(MapContextDrawer drawer) { |
1630 |
if (this.mapContextDrawer != null) { |
1631 |
this.mapContextDrawer.dispose();
|
1632 |
this.mapContextDrawer = null; |
1633 |
} |
1634 |
this.mapContextDrawer = drawer;
|
1635 |
if (this.mapContextDrawer != null) { |
1636 |
this.mapContextDrawer.setMapContext(this); |
1637 |
this.mapContextDrawer.setViewPort(viewPort);
|
1638 |
} |
1639 |
} |
1640 |
|
1641 |
public void loadFromState(PersistentState state) |
1642 |
throws PersistenceException {
|
1643 |
|
1644 |
ViewPort vp = (ViewPort) state.get("ViewPort");
|
1645 |
setViewPort(vp); |
1646 |
|
1647 |
layers = (FLayers) state.get("layers");
|
1648 |
layers.setName("root layer");
|
1649 |
loadLayers(layers); |
1650 |
layers.setMapContext(this);
|
1651 |
|
1652 |
layerEventListener = new LayerEventListener();
|
1653 |
layers.addLayerCollectionListener(layerEventListener); |
1654 |
|
1655 |
layers.addLayerCollectionListener(eventBuffer); |
1656 |
layers.setProjection(vp.getProjection()); |
1657 |
|
1658 |
//Add the listener for the selection
|
1659 |
addSelectionListener(layers); |
1660 |
|
1661 |
// ======================
|
1662 |
if (state.hasValue("orderManager")) { |
1663 |
LayerOrderManager lom = (LayerOrderManager) state.get("orderManager");
|
1664 |
this.setOrderManager(lom);
|
1665 |
} |
1666 |
DefaultMapContextManager manager = (DefaultMapContextManager) MapContextLocator.getMapContextManager(); |
1667 |
manager.notifyLoadMapContext(this);
|
1668 |
} |
1669 |
|
1670 |
private void loadLayers(FLayers lyrs) { |
1671 |
|
1672 |
int sz = lyrs.getLayersCount();
|
1673 |
for (int i = 0; i < sz; i++) { |
1674 |
try {
|
1675 |
lyrs.getLayer(i).load(); |
1676 |
} catch (LoadLayerException e) {
|
1677 |
LOGGER.error("While loading layer: " + lyrs.getLayer(i).getName());
|
1678 |
} |
1679 |
} |
1680 |
} |
1681 |
|
1682 |
public void saveToState(PersistentState state) throws PersistenceException { |
1683 |
state.set("ViewPort", viewPort);
|
1684 |
state.set("layers", (Persistent)layers);
|
1685 |
state.set("orderManager", this.getOrderManager()); |
1686 |
} |
1687 |
|
1688 |
public static class RegisterPersistence implements Callable { |
1689 |
|
1690 |
public Object call() { |
1691 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
1692 |
DynStruct definition = manager.addDefinition( |
1693 |
MapContext.class, |
1694 |
"MapContext",
|
1695 |
"MapContext Persistence definition",
|
1696 |
null,
|
1697 |
null
|
1698 |
); |
1699 |
definition.addDynFieldObject("ViewPort")
|
1700 |
.setClassOfValue(ViewPort.class) |
1701 |
.setMandatory(true);
|
1702 |
|
1703 |
definition.addDynFieldObject("layers")
|
1704 |
.setClassOfValue(FLayers.class) |
1705 |
.setMandatory(true);
|
1706 |
|
1707 |
definition.addDynFieldObject("orderManager")
|
1708 |
.setClassOfValue(LayerOrderManager.class) |
1709 |
.setMandatory(false);
|
1710 |
|
1711 |
return Boolean.TRUE; |
1712 |
} |
1713 |
} |
1714 |
|
1715 |
protected void doDispose() throws BaseException { |
1716 |
dispose(layers); |
1717 |
for (VectorLayer tracLayer : this.tracLayers.values()) { |
1718 |
dispose(tracLayer); |
1719 |
} |
1720 |
} |
1721 |
|
1722 |
/**
|
1723 |
* <p>
|
1724 |
* Registers an event buffer as a listener for all layers as argument.</p>
|
1725 |
*
|
1726 |
* <p>
|
1727 |
* Each {@link FLayer FLayer} of this map must have an event buffer for all
|
1728 |
* kind of specific listeners of that layer. This method distinguish between {@link Classifiable Classifiable},
|
1729 |
* {@link AlphanumericData AlphanumericData}, and {@link FLayers FLayers}
|
1730 |
* layers, and for each one, registers, for their specific listeners, the
|
1731 |
* <code>eventBuffer</code> as a listener.</p>
|
1732 |
*
|
1733 |
* @param the layer or layers
|
1734 |
*/
|
1735 |
public void addSelectionListener(FLayer lyr) { |
1736 |
lyr.addLayerListener(eventBuffer); |
1737 |
|
1738 |
if (lyr instanceof Classifiable) { |
1739 |
Classifiable c = (Classifiable) lyr; |
1740 |
c.addLegendListener(eventBuffer); |
1741 |
} |
1742 |
|
1743 |
if (lyr instanceof FLayers) { |
1744 |
FLayers lyrs = (FLayers) lyr; |
1745 |
for (int i = 0; i < lyrs.getLayersCount(); i++) { |
1746 |
addSelectionListener(lyrs.getLayer(i)); |
1747 |
} |
1748 |
} |
1749 |
if (lyr instanceof SingleLayer) { |
1750 |
if (((SingleLayer) lyr).getDataStore() != null) { |
1751 |
((SingleLayer) lyr).getDataStore().addObserver( |
1752 |
MapContext.this); |
1753 |
} |
1754 |
} |
1755 |
} |
1756 |
|
1757 |
public void setOrderManager(LayerOrderManager lom) { |
1758 |
orderManager = lom; |
1759 |
} |
1760 |
|
1761 |
public LayerOrderManager getOrderManager() {
|
1762 |
|
1763 |
if (orderManager == null) { |
1764 |
orderManager = MapContextLocator.getDefaultOrderManager(); |
1765 |
} |
1766 |
return orderManager;
|
1767 |
} |
1768 |
|
1769 |
public boolean hasVectorLayers() { |
1770 |
return this.hasVectorLayers(this.getLayers()); |
1771 |
} |
1772 |
|
1773 |
public boolean hasActiveVectorLayers() { |
1774 |
FLayer[] layers = this.getLayers().getActives(); |
1775 |
for (int i = 0; i < layers.length; i++) { |
1776 |
FLayer layer = layers[i]; |
1777 |
if (layer.isAvailable() && layer instanceof FLyrVect) { |
1778 |
return true; |
1779 |
} |
1780 |
} |
1781 |
return false; |
1782 |
} |
1783 |
|
1784 |
public boolean hasActiveLayers() { |
1785 |
FLayer[] layers = this.getLayers().getActives(); |
1786 |
return !ArrayUtils.isEmpty(layers);
|
1787 |
} |
1788 |
|
1789 |
public boolean hasLayers() { |
1790 |
return !this.getLayers().isEmpty(); |
1791 |
} |
1792 |
|
1793 |
private boolean hasVectorLayers(FLayers layers) { |
1794 |
for (int i = 0; i < layers.getLayersCount(); i++) { |
1795 |
FLayer lyr = layers.getLayer(i); |
1796 |
if (lyr instanceof FLayers) { |
1797 |
if (hasVectorLayers((FLayers) lyr)) {
|
1798 |
return true; |
1799 |
} |
1800 |
} else if (lyr instanceof FLyrVect) { |
1801 |
return true; |
1802 |
} |
1803 |
} |
1804 |
return false; |
1805 |
} |
1806 |
|
1807 |
@Override
|
1808 |
public Iterator<FLayer> iterator() { |
1809 |
return this.layers.iterator(); |
1810 |
} |
1811 |
|
1812 |
public Iterator deepiterator() { |
1813 |
return this.layers.deepiterator(); |
1814 |
} |
1815 |
|
1816 |
public MapTimeContext getTimeContext() {
|
1817 |
TimeSupportManager timeSupportManager = TimeSupportLocator.getManager(); |
1818 |
Interval interval = null;
|
1819 |
final List<Instant> times = new ArrayList<>(); |
1820 |
Instant minInstant = null;
|
1821 |
Instant maxInstant = null;
|
1822 |
|
1823 |
//TODO Separate absolute and relative time
|
1824 |
FLayers layers = this.getLayers();
|
1825 |
for( Iterator<FLayer> iterator = layers.deepiterator(); iterator.hasNext(); ) { |
1826 |
FLayer layer = iterator.next(); |
1827 |
if( layer instanceof SingleLayer ) { |
1828 |
DataStore dataStore = ((SingleLayer) layer).getDataStore(); |
1829 |
if( dataStore.getInterval() != null ) { |
1830 |
Instant startInstant = dataStore.getInterval().getStart(); |
1831 |
Instant endInstant = dataStore.getInterval().getEnd(); |
1832 |
times.addAll(dataStore.getTimes()); |
1833 |
if( minInstant == null ) { |
1834 |
minInstant = startInstant; |
1835 |
maxInstant = endInstant; |
1836 |
} else {
|
1837 |
if( minInstant.isAfter(startInstant) ) {
|
1838 |
minInstant = startInstant; |
1839 |
} |
1840 |
if( maxInstant.isBefore(endInstant) ) {
|
1841 |
maxInstant = endInstant; |
1842 |
} |
1843 |
} |
1844 |
} |
1845 |
} |
1846 |
} |
1847 |
|
1848 |
if( minInstant != null ) { |
1849 |
if( minInstant.isAbsolute() ) {
|
1850 |
try {
|
1851 |
interval = timeSupportManager.createAbsoluteInterval((AbsoluteInstant) minInstant, (AbsoluteInstant) maxInstant); |
1852 |
} catch (AbsoluteIntervalTypeNotRegisteredException e) {
|
1853 |
LOGGER.warn("Error creating the time interval", e);
|
1854 |
} |
1855 |
} else {
|
1856 |
interval = timeSupportManager.createRelativeInterval(((RelativeInstant) minInstant).toMillis(), ((RelativeInstant) maxInstant).toMillis()); |
1857 |
} |
1858 |
} |
1859 |
final Interval tmp_interval = interval;
|
1860 |
return new MapTimeContext() { |
1861 |
@Override
|
1862 |
public Interval getInterval() {
|
1863 |
return tmp_interval;
|
1864 |
} |
1865 |
|
1866 |
@Override
|
1867 |
public List<Instant> getTimes() { |
1868 |
return times;
|
1869 |
} |
1870 |
}; |
1871 |
} |
1872 |
|
1873 |
public long getScaleToUseWhenEnvelopeCollapse() { |
1874 |
return this.scaleToUseWhenEnvelopeCollapse; |
1875 |
} |
1876 |
|
1877 |
public void setScaleToUseWhenEnvelopeCollapse(long scale) { |
1878 |
this.scaleToUseWhenEnvelopeCollapse = scale;
|
1879 |
} |
1880 |
|
1881 |
public void setPrintGraphicsLayer(boolean b){ |
1882 |
this.printGraphicsLayer = b;
|
1883 |
} |
1884 |
|
1885 |
public boolean isPrintGraphicsLayer(){ |
1886 |
return this.printGraphicsLayer; |
1887 |
} |
1888 |
|
1889 |
} |