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 / impl / DefaultMapContextManager.java @ 44599
History | View | Annotate | Download (30.6 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | 42457 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
11 | 42457 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
16 | 42457 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
20 | 42457 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40435 | jjdelcerro | */
|
23 | package org.gvsig.fmap.mapcontext.impl; |
||
24 | |||
25 | import java.awt.Color; |
||
26 | import java.awt.Font; |
||
27 | import java.io.File; |
||
28 | import java.io.FileFilter; |
||
29 | 44161 | jjdelcerro | import java.io.InputStream; |
30 | 41258 | jjdelcerro | import java.lang.reflect.InvocationTargetException; |
31 | 40435 | jjdelcerro | import java.lang.reflect.Method; |
32 | 40927 | jldominguez | import java.util.ArrayList; |
33 | 40435 | jjdelcerro | import java.util.HashMap; |
34 | 40927 | jldominguez | import java.util.Iterator; |
35 | 43780 | jjdelcerro | import java.util.LinkedHashMap; |
36 | 40927 | jldominguez | import java.util.List; |
37 | 40435 | jjdelcerro | import java.util.Map; |
38 | 44161 | jjdelcerro | import org.apache.commons.io.IOUtils; |
39 | 43215 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
40 | 40435 | jjdelcerro | |
41 | import org.cresques.cts.IProjection; |
||
42 | import org.gvsig.fmap.crs.CRSFactory; |
||
43 | import org.gvsig.fmap.dal.DataServerExplorer; |
||
44 | import org.gvsig.fmap.dal.DataStore; |
||
45 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
46 | import org.gvsig.fmap.dal.exception.DataException; |
||
47 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
48 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
49 | 41141 | jjdelcerro | import org.gvsig.fmap.mapcontext.MapContext; |
50 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.MapContextDrawer; |
51 | import org.gvsig.fmap.mapcontext.MapContextException; |
||
52 | import org.gvsig.fmap.mapcontext.MapContextLocator; |
||
53 | import org.gvsig.fmap.mapcontext.MapContextManager; |
||
54 | import org.gvsig.fmap.mapcontext.MapContextRuntimeException; |
||
55 | 41141 | jjdelcerro | import org.gvsig.fmap.mapcontext.ViewPort; |
56 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
57 | 43141 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.DefaultLayerInformationBuilder; |
58 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.FLayer; |
59 | import org.gvsig.fmap.mapcontext.layers.LayerFactory; |
||
60 | 43141 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.LayerInformationBuilder; |
61 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer; |
62 | import org.gvsig.fmap.mapcontext.layers.vectorial.impl.DefaultGraphicLayer; |
||
63 | import org.gvsig.fmap.mapcontext.rendering.legend.ILegend; |
||
64 | import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend; |
||
65 | import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend; |
||
66 | import org.gvsig.fmap.mapcontext.rendering.legend.driver.ILegendReader; |
||
67 | import org.gvsig.fmap.mapcontext.rendering.legend.driver.ILegendWriter; |
||
68 | import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy; |
||
69 | import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol; |
||
70 | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
||
71 | import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol; |
||
72 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolException; |
||
73 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
||
74 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolPreferences; |
||
75 | import org.gvsig.tools.ToolsLocator; |
||
76 | import org.gvsig.tools.dynobject.exception.DynMethodException; |
||
77 | import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException; |
||
78 | 41302 | jjdelcerro | import org.gvsig.tools.observer.Notification; |
79 | import org.gvsig.tools.observer.ObservableHelper; |
||
80 | import org.gvsig.tools.observer.Observer; |
||
81 | 40435 | jjdelcerro | import org.gvsig.tools.persistence.PersistenceManager; |
82 | 44297 | jjdelcerro | import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
83 | import org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource; |
||
84 | 41141 | jjdelcerro | import org.slf4j.Logger; |
85 | import org.slf4j.LoggerFactory; |
||
86 | 40435 | jjdelcerro | |
87 | /**
|
||
88 | * Default implementation of the {@link MapContextManager}.
|
||
89 | *
|
||
90 | * @author <a href="mailto:cordinyana@gvsig.org">C?sar Ordi?ana</a>
|
||
91 | */
|
||
92 | public class DefaultMapContextManager implements MapContextManager { |
||
93 | |||
94 | 44297 | jjdelcerro | private static final Logger LOGGER = LoggerFactory |
95 | 42457 | jjdelcerro | .getLogger(DefaultMapContextManager.class); |
96 | 40435 | jjdelcerro | |
97 | 42457 | jjdelcerro | private Class drawerClazz = DefaultMapContextDrawer.class; |
98 | 40435 | jjdelcerro | |
99 | 43780 | jjdelcerro | private final Map legends = new HashMap(); |
100 | 40435 | jjdelcerro | |
101 | 43780 | jjdelcerro | private final Map<String, Class<ILegendReader>> legendReaders = new LinkedHashMap<>(); |
102 | 40435 | jjdelcerro | |
103 | 43780 | jjdelcerro | private final Map<String, Map<Class<ILegend>,Class<ILegendWriter>>> legendWriters = new LinkedHashMap<>(); |
104 | 40435 | jjdelcerro | |
105 | 42457 | jjdelcerro | private String defaultVectorLegend; |
106 | 40435 | jjdelcerro | |
107 | 44297 | jjdelcerro | private final ObservableHelper observableHelper = new ObservableHelper(); |
108 | 41302 | jjdelcerro | |
109 | 42457 | jjdelcerro | private File colorTableLibraryFolder = null; |
110 | 40435 | jjdelcerro | |
111 | 42457 | jjdelcerro | public MapContext createMapContext() {
|
112 | MapContext mapcontext = new MapContext(new ViewPort()); |
||
113 | return (MapContext) notifyObservers(CREATE_MAPCONTEXT, mapcontext).getValue();
|
||
114 | } |
||
115 | 40435 | jjdelcerro | |
116 | 42457 | jjdelcerro | public SymbolManager getSymbolManager() {
|
117 | return MapContextLocator.getSymbolManager();
|
||
118 | } |
||
119 | 40435 | jjdelcerro | |
120 | 42457 | jjdelcerro | private SymbolPreferences getSymbolPreferences() {
|
121 | return getSymbolManager().getSymbolPreferences();
|
||
122 | } |
||
123 | 40435 | jjdelcerro | |
124 | 42457 | jjdelcerro | public String getSymbolLibraryPath() { |
125 | return getSymbolPreferences().getSymbolLibraryPath();
|
||
126 | } |
||
127 | 40435 | jjdelcerro | |
128 | 42457 | jjdelcerro | public void setSymbolLibraryPath(String symbolLibraryPath) { |
129 | getSymbolPreferences().setSymbolLibraryPath(symbolLibraryPath); |
||
130 | } |
||
131 | 40435 | jjdelcerro | |
132 | 42457 | jjdelcerro | public void resetSymbolLibraryPath() { |
133 | getSymbolPreferences().resetSymbolLibraryPath(); |
||
134 | } |
||
135 | 40435 | jjdelcerro | |
136 | 42457 | jjdelcerro | public Color getDefaultSymbolColor() { |
137 | return getSymbolPreferences().getDefaultSymbolColor();
|
||
138 | } |
||
139 | 40435 | jjdelcerro | |
140 | 42457 | jjdelcerro | public Color getDefaultSymbolFillColor() { |
141 | return getSymbolPreferences().getDefaultSymbolFillColor();
|
||
142 | } |
||
143 | 40435 | jjdelcerro | |
144 | 42457 | jjdelcerro | public Font getDefaultSymbolFont() { |
145 | return getSymbolPreferences().getDefaultSymbolFont();
|
||
146 | } |
||
147 | 40435 | jjdelcerro | |
148 | 42457 | jjdelcerro | public String getSymbolFileExtension() { |
149 | return getSymbolPreferences().getSymbolFileExtension();
|
||
150 | } |
||
151 | 40435 | jjdelcerro | |
152 | 42457 | jjdelcerro | public boolean isDefaultSymbolFillColorAleatory() { |
153 | return getSymbolPreferences().isDefaultSymbolFillColorAleatory();
|
||
154 | } |
||
155 | 40435 | jjdelcerro | |
156 | 42457 | jjdelcerro | public void resetDefaultSymbolColor() { |
157 | getSymbolPreferences().resetDefaultSymbolColor(); |
||
158 | } |
||
159 | 40435 | jjdelcerro | |
160 | 42457 | jjdelcerro | public void resetDefaultSymbolFillColor() { |
161 | getSymbolPreferences().resetDefaultSymbolFillColor(); |
||
162 | } |
||
163 | 40435 | jjdelcerro | |
164 | 42457 | jjdelcerro | public void resetDefaultSymbolFillColorAleatory() { |
165 | getSymbolPreferences().resetDefaultSymbolFillColorAleatory(); |
||
166 | } |
||
167 | 40435 | jjdelcerro | |
168 | 42457 | jjdelcerro | public void resetDefaultSymbolFont() { |
169 | getSymbolPreferences().resetDefaultSymbolFont(); |
||
170 | } |
||
171 | 40435 | jjdelcerro | |
172 | 42457 | jjdelcerro | public void setDefaultSymbolColor(Color defaultSymbolColor) { |
173 | getSymbolPreferences().setDefaultSymbolColor(defaultSymbolColor); |
||
174 | } |
||
175 | 40435 | jjdelcerro | |
176 | 42457 | jjdelcerro | public void setDefaultSymbolFillColor(Color defaultSymbolFillColor) { |
177 | getSymbolPreferences().setDefaultSymbolFillColor(defaultSymbolFillColor); |
||
178 | } |
||
179 | 40435 | jjdelcerro | |
180 | 42457 | jjdelcerro | public void setDefaultSymbolFillColorAleatory( |
181 | boolean defaultSymbolFillColorAleatory) {
|
||
182 | getSymbolPreferences().setDefaultSymbolFillColorAleatory( |
||
183 | defaultSymbolFillColorAleatory); |
||
184 | } |
||
185 | 40435 | jjdelcerro | |
186 | 42457 | jjdelcerro | public void setDefaultSymbolFont(Font defaultSymbolFont) { |
187 | getSymbolPreferences().setDefaultSymbolFont(defaultSymbolFont); |
||
188 | } |
||
189 | 40435 | jjdelcerro | |
190 | 42457 | jjdelcerro | public void setSymbolFileExtension(String extension) { |
191 | getSymbolPreferences().setSymbolFileExtension(extension); |
||
192 | } |
||
193 | 40435 | jjdelcerro | |
194 | 42457 | jjdelcerro | public int getDefaultCartographicSupportMeasureUnit() { |
195 | return getSymbolPreferences().getDefaultCartographicSupportMeasureUnit();
|
||
196 | } |
||
197 | 40435 | jjdelcerro | |
198 | 42457 | jjdelcerro | public void setDefaultCartographicSupportMeasureUnit( |
199 | int defaultCartographicSupportMeasureUnit) {
|
||
200 | getSymbolPreferences().setDefaultCartographicSupportMeasureUnit( |
||
201 | defaultCartographicSupportMeasureUnit); |
||
202 | } |
||
203 | 40435 | jjdelcerro | |
204 | 42457 | jjdelcerro | public int getDefaultCartographicSupportReferenceSystem() { |
205 | return getSymbolPreferences().getDefaultCartographicSupportReferenceSystem();
|
||
206 | } |
||
207 | 40435 | jjdelcerro | |
208 | 42457 | jjdelcerro | public void setDefaultCartographicSupportReferenceSystem( |
209 | int defaultCartographicSupportReferenceSystem) {
|
||
210 | getSymbolPreferences().setDefaultCartographicSupportReferenceSystem( |
||
211 | defaultCartographicSupportReferenceSystem); |
||
212 | } |
||
213 | 40435 | jjdelcerro | |
214 | 42457 | jjdelcerro | public MapContextDrawer createMapContextDrawerInstance(Class drawerClazz) |
215 | throws MapContextException {
|
||
216 | return createMapContextDrawerInstance(drawerClazz, "NONE"); |
||
217 | } |
||
218 | 40435 | jjdelcerro | |
219 | 42457 | jjdelcerro | public MapContextDrawer createDefaultMapContextDrawerInstance()
|
220 | throws MapContextException {
|
||
221 | 40435 | jjdelcerro | |
222 | 42457 | jjdelcerro | return createMapContextDrawerInstance(drawerClazz, "default"); |
223 | } |
||
224 | 40435 | jjdelcerro | |
225 | 42457 | jjdelcerro | private MapContextDrawer createMapContextDrawerInstance(Class drawerClazz, |
226 | String name) throws RegisteredClassInstantiationException { |
||
227 | try {
|
||
228 | MapContextDrawer drawer = (MapContextDrawer) drawerClazz.newInstance(); |
||
229 | notifyObservers(CREATE_MAPCONTEXT_DRAWER, drawer); |
||
230 | return drawer;
|
||
231 | } catch (Exception ex) { |
||
232 | throw new RegisteredClassInstantiationException( |
||
233 | MapContextDrawer.class, drawerClazz, name, ex); |
||
234 | } |
||
235 | } |
||
236 | 40435 | jjdelcerro | |
237 | 42457 | jjdelcerro | public void setDefaultMapContextDrawer(Class drawerClazz) |
238 | throws MapContextException {
|
||
239 | 40435 | jjdelcerro | |
240 | 42457 | jjdelcerro | validateMapContextDrawer(drawerClazz); |
241 | this.drawerClazz = drawerClazz;
|
||
242 | notifyObservers(SET_MAPCONTEXT_DRAWER, drawerClazz); |
||
243 | } |
||
244 | 40435 | jjdelcerro | |
245 | 42457 | jjdelcerro | public void validateMapContextDrawer(Class drawerClazz) |
246 | throws MapContextException {
|
||
247 | if (!MapContextDrawer.class.isAssignableFrom(drawerClazz)) {
|
||
248 | throw new InvalidRegisteredClassException(MapContextDrawer.class, |
||
249 | drawerClazz, "UNKNOWN");
|
||
250 | } |
||
251 | } |
||
252 | 40435 | jjdelcerro | |
253 | 42457 | jjdelcerro | public GraphicLayer createGraphicsLayer(IProjection projection) {
|
254 | DefaultGraphicLayer layer = new DefaultGraphicLayer();
|
||
255 | try {
|
||
256 | layer.initialize(projection); |
||
257 | layer.setLegend((IVectorLegend) createLegend(IVectorialUniqueValueLegend.LEGEND_NAME)); |
||
258 | } catch (Exception e) { |
||
259 | 44297 | jjdelcerro | LOGGER.error("Error initializing the graphics layer", e);
|
260 | 42457 | jjdelcerro | } |
261 | return (GraphicLayer) notifyObservers(CREATE_GRAPHICS_LAYER, layer).getValue();
|
||
262 | } |
||
263 | 40435 | jjdelcerro | |
264 | 42457 | jjdelcerro | public String getDefaultVectorLegend() { |
265 | return defaultVectorLegend;
|
||
266 | } |
||
267 | 40435 | jjdelcerro | |
268 | 42457 | jjdelcerro | public void setDefaultVectorLegend(String defaultVectorLegend) { |
269 | this.defaultVectorLegend = defaultVectorLegend;
|
||
270 | } |
||
271 | 40435 | jjdelcerro | |
272 | 42457 | jjdelcerro | public void registerLegend(String legendName, Class legendClass) |
273 | throws MapContextRuntimeException {
|
||
274 | 40435 | jjdelcerro | |
275 | 42457 | jjdelcerro | if (legendClass == null || !ILegend.class.isAssignableFrom(legendClass)) { |
276 | throw new InvalidRegisteredClassException(ILegend.class, |
||
277 | legendClass, legendName); |
||
278 | } |
||
279 | 40435 | jjdelcerro | |
280 | 42457 | jjdelcerro | legends.put(legendName, legendClass); |
281 | notifyObservers(REGISTER_LEGEND, legendName, legendClass); |
||
282 | } |
||
283 | 40435 | jjdelcerro | |
284 | 42457 | jjdelcerro | public ILegend createLegend(String legendName) |
285 | throws MapContextRuntimeException {
|
||
286 | Class legendClass = (Class) legends.get(legendName); |
||
287 | |||
288 | if (legendClass != null) { |
||
289 | try {
|
||
290 | ILegend legend = (ILegend) legendClass.newInstance(); |
||
291 | return (ILegend) notifyObservers(CREATE_LEGEND, legend).getValue();
|
||
292 | } catch (InstantiationException e) { |
||
293 | throw new RegisteredClassInstantiationException(ILegend.class, |
||
294 | legendClass, legendName, e); |
||
295 | } catch (IllegalAccessException e) { |
||
296 | throw new RegisteredClassInstantiationException(ILegend.class, |
||
297 | legendClass, legendName, e); |
||
298 | } |
||
299 | } |
||
300 | return null; |
||
301 | } |
||
302 | |||
303 | 40435 | jjdelcerro | public IVectorLegend createDefaultVectorLegend(int shapeType) |
304 | 42457 | jjdelcerro | throws MapContextRuntimeException {
|
305 | 40435 | jjdelcerro | try {
|
306 | // Create legend
|
||
307 | 42457 | jjdelcerro | IVectorLegend legend |
308 | = (IVectorLegend) createLegend(getDefaultVectorLegend()); |
||
309 | 40435 | jjdelcerro | if (legend == null) { |
310 | return null; |
||
311 | } |
||
312 | // Set legend values
|
||
313 | legend.setShapeType(shapeType); |
||
314 | 41214 | jjdelcerro | ISymbol symbol = getSymbolManager().createSymbol(shapeType); |
315 | 42457 | jjdelcerro | if (symbol == null) { |
316 | String msg = "Can't create a legend for the shape type " + shapeType + ". The type can be incorrect or there is not registered a symbol by default for that value. If this a was obtained from the store settings, review your FeatureType have correctly configured this value."; |
||
317 | 41214 | jjdelcerro | throw new RuntimeException(msg); |
318 | } |
||
319 | legend.setDefaultSymbol(symbol); |
||
320 | 40435 | jjdelcerro | return legend;
|
321 | 42457 | jjdelcerro | } catch (Exception e) { |
322 | 40435 | jjdelcerro | throw new MapContextRuntimeException(e); |
323 | } |
||
324 | } |
||
325 | |||
326 | 40927 | jldominguez | // =============================================================
|
327 | // Legend reading/writing
|
||
328 | 43780 | jjdelcerro | |
329 | private Map<Class<ILegend>,Class<ILegendWriter>> getLegendWritersForFormat(String format) { |
||
330 | synchronized(legendWriters) {
|
||
331 | return legendWriters.get(format);
|
||
332 | } |
||
333 | } |
||
334 | |||
335 | private Class<ILegendReader> getLegendReaderForFormat(String format) { |
||
336 | synchronized(legendReaders) {
|
||
337 | return legendReaders.get(format);
|
||
338 | } |
||
339 | } |
||
340 | |||
341 | @Override
|
||
342 | public List<String> getLegendReadingFormats() { |
||
343 | synchronized(legendReaders) {
|
||
344 | List<String> resp = new ArrayList(); |
||
345 | resp.addAll(legendReaders.keySet()); |
||
346 | return resp;
|
||
347 | } |
||
348 | } |
||
349 | |||
350 | @Override
|
||
351 | public List<String> getLegendWritingFormats() { |
||
352 | synchronized(legendWriters) {
|
||
353 | List<String> resp = new ArrayList(); |
||
354 | resp.addAll(legendWriters.keySet()); |
||
355 | return resp;
|
||
356 | } |
||
357 | } |
||
358 | |||
359 | @Override
|
||
360 | 42457 | jjdelcerro | public void registerLegendReader(String format, Class readerClass) |
361 | throws MapContextRuntimeException {
|
||
362 | if (readerClass == null |
||
363 | || !ILegendReader.class.isAssignableFrom(readerClass)) { |
||
364 | throw new InvalidRegisteredClassException(ILegendReader.class, |
||
365 | readerClass, format); |
||
366 | } |
||
367 | 43780 | jjdelcerro | synchronized (legendReaders) {
|
368 | legendReaders.put(format, readerClass); |
||
369 | } |
||
370 | 42457 | jjdelcerro | notifyObservers(REGISTER_LEGEND_READER, format, readerClass); |
371 | } |
||
372 | 40435 | jjdelcerro | |
373 | 43780 | jjdelcerro | @Override
|
374 | 42457 | jjdelcerro | public void registerLegendWriter(Class legendClass, String format, |
375 | Class writerClass) throws MapContextRuntimeException { |
||
376 | 43780 | jjdelcerro | if (writerClass == null || legendClass == null |
377 | 42457 | jjdelcerro | || !ILegendWriter.class.isAssignableFrom(writerClass) |
378 | || !ILegend.class.isAssignableFrom(legendClass)) { |
||
379 | 40435 | jjdelcerro | |
380 | 42457 | jjdelcerro | throw new InvalidRegisteredClassException(ILegendWriter.class, |
381 | writerClass, format.concat("-").concat(
|
||
382 | legendClass == null ? "Null" : legendClass.getName())); |
||
383 | } |
||
384 | 40435 | jjdelcerro | |
385 | 42457 | jjdelcerro | synchronized (legendWriters) {
|
386 | 43780 | jjdelcerro | Map<Class<ILegend>, Class<ILegendWriter>> legendWriterOfFormat = legendWriters.get(format); |
387 | if (legendWriterOfFormat == null) { |
||
388 | legendWriterOfFormat = new LinkedHashMap(); |
||
389 | legendWriters.put(format, legendWriterOfFormat); |
||
390 | 42457 | jjdelcerro | } |
391 | 43780 | jjdelcerro | legendWriterOfFormat.put(legendClass, writerClass); |
392 | 42457 | jjdelcerro | } |
393 | notifyObservers(REGISTER_LEGEND_WRITER, format, writerClass); |
||
394 | } |
||
395 | 40435 | jjdelcerro | |
396 | 43780 | jjdelcerro | @Override
|
397 | public ILegendReader createLegendReader(String format) |
||
398 | throws MapContextRuntimeException {
|
||
399 | Class<ILegendReader> legendReaderClazz = getLegendReaderForFormat(format);
|
||
400 | |||
401 | if (legendReaderClazz != null) { |
||
402 | try {
|
||
403 | ILegendReader reader = (ILegendReader) legendReaderClazz.newInstance(); |
||
404 | return (ILegendReader) notifyObservers(CREATE_LEGEND_READER, reader).getValue();
|
||
405 | } catch (InstantiationException|IllegalAccessException e) { |
||
406 | throw new RegisteredClassInstantiationException( |
||
407 | ILegendReader.class, legendReaderClazz, format, e); |
||
408 | } |
||
409 | } |
||
410 | return null; |
||
411 | } |
||
412 | |||
413 | @Override
|
||
414 | 42457 | jjdelcerro | public ILegendWriter createLegendWriter(Class legendClass, String format) |
415 | throws MapContextRuntimeException {
|
||
416 | 40435 | jjdelcerro | |
417 | 42457 | jjdelcerro | if (legendClass == null || format == null) { |
418 | return null; |
||
419 | } |
||
420 | 40435 | jjdelcerro | |
421 | 43780 | jjdelcerro | Map<Class<ILegend>, Class<ILegendWriter>> legendFormatWriters = getLegendWritersForFormat(format); |
422 | 42457 | jjdelcerro | |
423 | if (legendFormatWriters != null) { |
||
424 | 43780 | jjdelcerro | Class<ILegendWriter> legendWriterClazz = legendFormatWriters.get(legendClass);
|
425 | 42457 | jjdelcerro | if (legendWriterClazz != null) { |
426 | /*
|
||
427 | * Found exact match
|
||
428 | */
|
||
429 | try {
|
||
430 | ILegendWriter writer = (ILegendWriter) legendWriterClazz.newInstance(); |
||
431 | return (ILegendWriter) notifyObservers(CREATE_LEGEND_READER, writer).getValue();
|
||
432 | 43780 | jjdelcerro | } catch (InstantiationException|IllegalAccessException e) { |
433 | 42457 | jjdelcerro | throw new RegisteredClassInstantiationException( |
434 | 43780 | jjdelcerro | ILegendWriter.class, |
435 | legendWriterClazz, |
||
436 | format.concat("-").concat(legendClass.getName()),
|
||
437 | e |
||
438 | ); |
||
439 | 42457 | jjdelcerro | } |
440 | } else {
|
||
441 | /*
|
||
442 | * Trying to find superclass/superinterface of parameter
|
||
443 | */
|
||
444 | try {
|
||
445 | return getSuperClassLegendWriter(legendFormatWriters, legendClass);
|
||
446 | } catch (Exception exc) { |
||
447 | throw new MapContextRuntimeException(exc); |
||
448 | } |
||
449 | } |
||
450 | } |
||
451 | return null; |
||
452 | } |
||
453 | |||
454 | 43780 | jjdelcerro | private ILegendWriter getSuperClassLegendWriter(Map<Class<ILegend>, Class<ILegendWriter>> clsToWtr, Class<ILegend> legclass) |
455 | 42457 | jjdelcerro | throws Exception { |
456 | |||
457 | Iterator kiter = clsToWtr.keySet().iterator();
|
||
458 | Object oitem = null; |
||
459 | 40927 | jldominguez | Class citem = null; |
460 | 42457 | jjdelcerro | while (kiter.hasNext()) {
|
461 | oitem = kiter.next(); |
||
462 | if (oitem instanceof Class) { |
||
463 | citem = (Class) oitem;
|
||
464 | if (citem.isAssignableFrom(legclass)) {
|
||
465 | /*
|
||
466 | * Found superclass/superinterface
|
||
467 | */
|
||
468 | citem = (Class) clsToWtr.get(oitem);
|
||
469 | return (ILegendWriter) citem.newInstance();
|
||
470 | } |
||
471 | } |
||
472 | } |
||
473 | /*
|
||
474 | * No superclass/superinterface found
|
||
475 | */
|
||
476 | 40927 | jldominguez | return null; |
477 | } |
||
478 | |||
479 | 42457 | jjdelcerro | // =============================================================
|
480 | 40927 | jldominguez | |
481 | 42457 | jjdelcerro | public IMultiLayerSymbol createMultiLayerSymbol(int shapeType) |
482 | throws MapContextRuntimeException {
|
||
483 | IMultiLayerSymbol symbol = getSymbolManager().createMultiLayerSymbol(shapeType); |
||
484 | return (IMultiLayerSymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
|
||
485 | } |
||
486 | 40435 | jjdelcerro | |
487 | 42457 | jjdelcerro | public IMultiLayerSymbol createMultiLayerSymbol(String symbolName) |
488 | throws MapContextRuntimeException {
|
||
489 | IMultiLayerSymbol symbol = getSymbolManager().createMultiLayerSymbol(symbolName); |
||
490 | return (IMultiLayerSymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
|
||
491 | } |
||
492 | 40435 | jjdelcerro | |
493 | 42457 | jjdelcerro | public ISymbol createSymbol(int shapeType, Color color) |
494 | throws MapContextRuntimeException {
|
||
495 | ISymbol symbol = getSymbolManager().createSymbol(shapeType, color); |
||
496 | return (ISymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
|
||
497 | } |
||
498 | 40435 | jjdelcerro | |
499 | 42457 | jjdelcerro | public ISymbol createSymbol(int shapeType) |
500 | throws MapContextRuntimeException {
|
||
501 | ISymbol symbol = getSymbolManager().createSymbol(shapeType); |
||
502 | return (ISymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
|
||
503 | } |
||
504 | 40435 | jjdelcerro | |
505 | 42457 | jjdelcerro | public ISymbol createSymbol(String symbolName, Color color) |
506 | throws MapContextRuntimeException {
|
||
507 | ISymbol symbol = getSymbolManager().createSymbol(symbolName, color); |
||
508 | return (ISymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
|
||
509 | } |
||
510 | 40435 | jjdelcerro | |
511 | 42457 | jjdelcerro | public ISymbol createSymbol(String symbolName) |
512 | throws MapContextRuntimeException {
|
||
513 | ISymbol symbol = getSymbolManager().createSymbol(symbolName); |
||
514 | return (ISymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
|
||
515 | } |
||
516 | 40435 | jjdelcerro | |
517 | 42457 | jjdelcerro | public IWarningSymbol getWarningSymbol(String message, String symbolDesc, |
518 | int symbolDrawExceptionType) throws MapContextRuntimeException { |
||
519 | return getSymbolManager().getWarningSymbol(message, symbolDesc,
|
||
520 | symbolDrawExceptionType); |
||
521 | } |
||
522 | 40435 | jjdelcerro | |
523 | 42457 | jjdelcerro | public ISymbol[] loadSymbols(File folder, FileFilter filter) |
524 | throws SymbolException {
|
||
525 | ISymbol[] symbols = getSymbolManager().loadSymbols(folder, filter);
|
||
526 | return (ISymbol[]) notifyObservers(LOAD_SYMBOLS, symbols).getValue(); |
||
527 | } |
||
528 | 40435 | jjdelcerro | |
529 | 42457 | jjdelcerro | public ISymbol[] loadSymbols(File folder) throws SymbolException { |
530 | ISymbol[] symbols = getSymbolManager().loadSymbols(folder);
|
||
531 | return (ISymbol[]) notifyObservers(LOAD_SYMBOLS, symbols).getValue(); |
||
532 | } |
||
533 | 40435 | jjdelcerro | |
534 | 42457 | jjdelcerro | public void registerMultiLayerSymbol(String symbolName, Class symbolClass) |
535 | throws MapContextRuntimeException {
|
||
536 | getSymbolManager().registerMultiLayerSymbol(symbolName, symbolClass); |
||
537 | notifyObservers(REGISTER_MULTILAYER_SYMBOL, symbolName, symbolClass); |
||
538 | } |
||
539 | 40435 | jjdelcerro | |
540 | 42457 | jjdelcerro | public void registerMultiLayerSymbol(String symbolName, int[] shapeTypes, |
541 | Class symbolClass) throws MapContextRuntimeException { |
||
542 | getSymbolManager().registerMultiLayerSymbol(symbolName, shapeTypes, |
||
543 | symbolClass); |
||
544 | notifyObservers(REGISTER_MULTILAYER_SYMBOL, symbolName, symbolClass, shapeTypes); |
||
545 | } |
||
546 | 40435 | jjdelcerro | |
547 | 42457 | jjdelcerro | public void registerSymbol(String symbolName, Class symbolClass) |
548 | throws MapContextRuntimeException {
|
||
549 | getSymbolManager().registerSymbol(symbolName, symbolClass); |
||
550 | notifyObservers(REGISTER_SYMBOL, symbolName, symbolClass); |
||
551 | } |
||
552 | 40435 | jjdelcerro | |
553 | 42457 | jjdelcerro | public void registerSymbol(String symbolName, int[] shapeTypes, |
554 | Class symbolClass) throws MapContextException { |
||
555 | getSymbolManager().registerSymbol(symbolName, shapeTypes, symbolClass); |
||
556 | notifyObservers(REGISTER_SYMBOL, symbolName, symbolClass, shapeTypes); |
||
557 | } |
||
558 | 40435 | jjdelcerro | |
559 | 42457 | jjdelcerro | public void saveSymbol(ISymbol symbol, String fileName, File folder, |
560 | boolean overwrite) throws SymbolException { |
||
561 | getSymbolManager().saveSymbol(symbol, fileName, folder, overwrite); |
||
562 | } |
||
563 | 40435 | jjdelcerro | |
564 | 42457 | jjdelcerro | public void saveSymbol(ISymbol symbol, String fileName, File folder) |
565 | throws SymbolException {
|
||
566 | getSymbolManager().saveSymbol(symbol, fileName, folder); |
||
567 | } |
||
568 | 40435 | jjdelcerro | |
569 | 42457 | jjdelcerro | public FLayer createLayer(String layerName, DataStoreParameters parameters) |
570 | throws LoadLayerException {
|
||
571 | FLayer layer = LayerFactory.getInstance().createLayer(layerName, parameters); |
||
572 | return (FLayer) notifyObservers(CREATE_LAYER, layer).getValue();
|
||
573 | } |
||
574 | 40435 | jjdelcerro | |
575 | 42457 | jjdelcerro | public FLayer createLayer(String layerName, DataStore store) |
576 | throws LoadLayerException {
|
||
577 | FLayer layer = LayerFactory.getInstance().createLayer(layerName, store); |
||
578 | return (FLayer) notifyObservers(CREATE_LAYER, layer).getValue();
|
||
579 | } |
||
580 | 40435 | jjdelcerro | |
581 | 44297 | jjdelcerro | @Override
|
582 | 41302 | jjdelcerro | public ILegend getLegend(DataStore dataStore) {
|
583 | 41258 | jjdelcerro | ILegend legend = null;
|
584 | 44297 | jjdelcerro | ResourcesStorage resourcesStorage = dataStore.getResourcesStorage(); |
585 | if( resourcesStorage!=null ) { |
||
586 | Resource resource = resourcesStorage.getResource(SymbolManager.LEGEND_FILE_EXTENSION.substring(1));
|
||
587 | try {
|
||
588 | if ((resource != null) && (resource.exists())) { |
||
589 | PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager(); |
||
590 | InputStream is = resource.asInputStream();
|
||
591 | legend = (ILegend) persistenceManager.getObject(is); |
||
592 | is.close(); |
||
593 | } |
||
594 | } catch (Exception e) { |
||
595 | LOGGER.warn("Can't loasd legend", e);
|
||
596 | } finally {
|
||
597 | IOUtils.closeQuietly(resource); |
||
598 | 41258 | jjdelcerro | } |
599 | 40435 | jjdelcerro | } |
600 | 41258 | jjdelcerro | //If the legend is null, next option is to check if the store has the getLegend method
|
601 | 42457 | jjdelcerro | if (legend == null) { |
602 | 41258 | jjdelcerro | try {
|
603 | legend = (IVectorLegend) dataStore.invokeDynMethod("getLegend", null); |
||
604 | } catch (DynMethodNotSupportedException e) {
|
||
605 | 44297 | jjdelcerro | LOGGER.debug("This store {} does not provide a legend.",
|
606 | 41258 | jjdelcerro | dataStore.getName()); |
607 | } catch (DynMethodException e) {
|
||
608 | 44297 | jjdelcerro | LOGGER.error( |
609 | 41258 | jjdelcerro | "Can't load the specific legend provided for the store {}.",
|
610 | dataStore.getName(), e); |
||
611 | } |
||
612 | } |
||
613 | 40435 | jjdelcerro | |
614 | 41258 | jjdelcerro | //If legend is null, last step is just try to create the legend by default
|
615 | 42457 | jjdelcerro | if (legend == null) { |
616 | 41258 | jjdelcerro | FeatureType featureType; |
617 | try {
|
||
618 | featureType = (((FeatureStore) dataStore).getDefaultFeatureType()); |
||
619 | int indexGeom = featureType.getDefaultGeometryAttributeIndex();
|
||
620 | 42457 | jjdelcerro | if (indexGeom < 0) { |
621 | 41614 | jjdelcerro | throw new IllegalArgumentException("The layer don't has a geometry column."); |
622 | } |
||
623 | 41258 | jjdelcerro | int typeShape = featureType.getAttributeDescriptor(indexGeom).getGeometryType();
|
624 | legend = createDefaultVectorLegend(typeShape); |
||
625 | } catch (DataException e) {
|
||
626 | 44297 | jjdelcerro | LOGGER.error("Error getting the default feature type", e);
|
627 | 41258 | jjdelcerro | } |
628 | } |
||
629 | 40435 | jjdelcerro | |
630 | 41258 | jjdelcerro | return legend;
|
631 | } |
||
632 | 40435 | jjdelcerro | |
633 | 44297 | jjdelcerro | @Override
|
634 | 40435 | jjdelcerro | public ILabelingStrategy getLabelingStrategy(DataStore dataStore) {
|
635 | ILabelingStrategy labelingStrategy = null;
|
||
636 | |||
637 | 44297 | jjdelcerro | ResourcesStorage resourcesStorage = dataStore.getResourcesStorage(); |
638 | if( resourcesStorage!=null ) { |
||
639 | Resource resource = resourcesStorage.getResource(SymbolManager.LABELINGSTRATEGY_FILE_EXTENSION.substring(1));
|
||
640 | try {
|
||
641 | if ((resource != null) && (resource.exists())) { |
||
642 | PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager(); |
||
643 | InputStream is = resource.asInputStream();
|
||
644 | labelingStrategy = (ILabelingStrategy) persistenceManager.getObject(is); |
||
645 | is.close(); |
||
646 | } |
||
647 | } catch (Exception e) { |
||
648 | LOGGER.warn("Can't load Label strategy", e);
|
||
649 | } finally {
|
||
650 | IOUtils.closeQuietly(resource); |
||
651 | 40435 | jjdelcerro | } |
652 | } |
||
653 | 42457 | jjdelcerro | |
654 | 44297 | jjdelcerro | //If the legend is null, next option is to check if the store has the getLabeling method
|
655 | 42457 | jjdelcerro | if (labelingStrategy == null) { |
656 | 40435 | jjdelcerro | try {
|
657 | 42457 | jjdelcerro | labelingStrategy |
658 | = (ILabelingStrategy) dataStore.invokeDynMethod("getLabeling",
|
||
659 | null);
|
||
660 | 40435 | jjdelcerro | } catch (DynMethodNotSupportedException e1) {
|
661 | labelingStrategy = null;
|
||
662 | } catch (DynMethodException e1) {
|
||
663 | 44297 | jjdelcerro | LOGGER.error("Can't load the specific labeling strategy provided for the datastore {}.",
|
664 | 42457 | jjdelcerro | dataStore.getName(), |
665 | e1); |
||
666 | 40435 | jjdelcerro | } |
667 | 42457 | jjdelcerro | } |
668 | |||
669 | 40435 | jjdelcerro | return labelingStrategy;
|
670 | } |
||
671 | 42457 | jjdelcerro | |
672 | 41258 | jjdelcerro | private Object call(Object instance, String methodName, Class[] signature, Object[] params) { |
673 | try {
|
||
674 | Method method = instance.getClass().getMethod(methodName, signature);
|
||
675 | 42457 | jjdelcerro | if (method == null) { |
676 | 41258 | jjdelcerro | return null; |
677 | } |
||
678 | Object value = method.invoke(instance, params);
|
||
679 | return value;
|
||
680 | } catch (NoSuchMethodException ex) { |
||
681 | return null; |
||
682 | } catch (SecurityException ex) { |
||
683 | return null; |
||
684 | } catch (IllegalAccessException ex) { |
||
685 | return null; |
||
686 | } catch (IllegalArgumentException ex) { |
||
687 | return null; |
||
688 | } catch (InvocationTargetException ex) { |
||
689 | return null; |
||
690 | } |
||
691 | } |
||
692 | |||
693 | 40435 | jjdelcerro | private Map iconLayers = new HashMap(); // (Map<String storeProviderName, String iconName>) |
694 | 42457 | jjdelcerro | |
695 | 40435 | jjdelcerro | public void registerIconLayer(String storeProviderName, String iconName) { |
696 | 42457 | jjdelcerro | if (storeProviderName == null || iconName == null) { |
697 | 44297 | jjdelcerro | LOGGER.info("registerIconLayer, storeProviderName or iconName are null");
|
698 | 42457 | jjdelcerro | return;
|
699 | } |
||
700 | 41302 | jjdelcerro | String storeName = storeProviderName.trim().toLowerCase();
|
701 | 42457 | jjdelcerro | if (storeName.length() == 0 || iconName.trim().length() == 0) { |
702 | 44297 | jjdelcerro | LOGGER.info("registerIconLayer, invalid storeProviderName or iconName");
|
703 | 42457 | jjdelcerro | return;
|
704 | } |
||
705 | iconLayers.put(storeName, iconName); |
||
706 | notifyObservers(REGISTER_ICON_LAYER, storeName, iconName); |
||
707 | 40435 | jjdelcerro | } |
708 | 42457 | jjdelcerro | |
709 | 43215 | jjdelcerro | @Override
|
710 | 40435 | jjdelcerro | public String getIconLayer(DataStore store) { |
711 | 43753 | fdiaz | try {
|
712 | return this.getIconLayer(store.getProviderName()); |
||
713 | } catch (Throwable th){ |
||
714 | return "layer-icon"; |
||
715 | } |
||
716 | 43574 | jjdelcerro | } |
717 | |||
718 | @Override
|
||
719 | public String getIconLayer(String providerName) { |
||
720 | 43215 | jjdelcerro | String name = null; |
721 | try {
|
||
722 | 43574 | jjdelcerro | name = (String) iconLayers.get(providerName.trim().toLowerCase());
|
723 | 43215 | jjdelcerro | } catch(Throwable th) { |
724 | // Do nothing
|
||
725 | } |
||
726 | if( StringUtils.isEmpty(name) ) {
|
||
727 | name = "layer-icon";
|
||
728 | } |
||
729 | 42457 | jjdelcerro | return name;
|
730 | 40435 | jjdelcerro | } |
731 | |||
732 | /* (non-Javadoc)
|
||
733 | * @see org.gvsig.fmap.mapcontext.MapContextManager#getDefaultCRS()
|
||
734 | */
|
||
735 | public IProjection getDefaultCRS() {
|
||
736 | 41302 | jjdelcerro | IProjection crs = CRSFactory.getCRS("EPSG:4326");
|
737 | 42457 | jjdelcerro | return (IProjection) notifyObservers(GET_DEFAULT_CRS, crs).getValue();
|
738 | 40435 | jjdelcerro | } |
739 | 40927 | jldominguez | |
740 | 42457 | jjdelcerro | public Notification notifyLoadMapContext(MapContext mapContext) { |
741 | 41302 | jjdelcerro | return this.observableHelper.notifyObservers(this, LOAD_MAPCONTEXT, mapContext); |
742 | } |
||
743 | 42457 | jjdelcerro | |
744 | public Notification notifyLoadLayer(FLayer layer) { |
||
745 | 41302 | jjdelcerro | return this.observableHelper.notifyObservers(this, LOAD_LAYER, layer); |
746 | } |
||
747 | 42457 | jjdelcerro | |
748 | 41302 | jjdelcerro | public void addObserver(Observer o) { |
749 | this.observableHelper.addObserver(o);
|
||
750 | } |
||
751 | |||
752 | public void deleteObserver(Observer o) { |
||
753 | this.observableHelper.deleteObserver(o);
|
||
754 | } |
||
755 | |||
756 | public void deleteObservers() { |
||
757 | this.observableHelper.deleteObservers();
|
||
758 | } |
||
759 | |||
760 | protected Notification notifyObservers(String type, Object value) { |
||
761 | 42457 | jjdelcerro | return this.observableHelper.notifyObservers(this, type, value); |
762 | 41302 | jjdelcerro | } |
763 | |||
764 | protected Notification notifyObservers(String type, Object value1, Object value2) { |
||
765 | return this.observableHelper.notifyObservers(this, type, value1, value2); |
||
766 | } |
||
767 | |||
768 | protected Notification notifyObservers(String type, Object value1, Object value2, Object value3) { |
||
769 | return this.observableHelper.notifyObservers(this, type, value1, value2, value3); |
||
770 | } |
||
771 | 41347 | jjdelcerro | |
772 | public File getColorTableLibraryFolder() { |
||
773 | if (this.colorTableLibraryFolder == null) { |
||
774 | // Provide a default value to the location for the color
|
||
775 | // table library.
|
||
776 | String colorTableLibraryPath = System.getProperty("user.home") |
||
777 | + File.separator
|
||
778 | + "gvSIG"
|
||
779 | + File.separator
|
||
780 | + "colortable";
|
||
781 | this.colorTableLibraryFolder = new File(colorTableLibraryPath); |
||
782 | } |
||
783 | return this.colorTableLibraryFolder; |
||
784 | } |
||
785 | |||
786 | public void setColorTableLibraryFolder(File colorTableLibraryFolder) { |
||
787 | this.colorTableLibraryFolder = colorTableLibraryFolder;
|
||
788 | } |
||
789 | 42457 | jjdelcerro | |
790 | 43141 | jjdelcerro | @Override
|
791 | public LayerInformationBuilder createLayerInformationBuilder() {
|
||
792 | return new DefaultLayerInformationBuilder(); |
||
793 | } |
||
794 | |||
795 | 43753 | fdiaz | |
796 | 40435 | jjdelcerro | } |