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 @ 40559
History | View | Annotate | Download (20.4 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 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | 40559 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | /*
|
||
25 | * AUTHORS (In addition to CIT):
|
||
26 | * 2009 {DiSiD Technologies} {Create Manager to register MapContextDrawer implementation}
|
||
27 | */
|
||
28 | package org.gvsig.fmap.mapcontext.impl; |
||
29 | |||
30 | import java.awt.Color; |
||
31 | import java.awt.Font; |
||
32 | import java.io.File; |
||
33 | import java.io.FileFilter; |
||
34 | import java.io.FileInputStream; |
||
35 | import java.io.FileNotFoundException; |
||
36 | import java.io.IOException; |
||
37 | import java.lang.reflect.Method; |
||
38 | import java.util.Collections; |
||
39 | import java.util.HashMap; |
||
40 | import java.util.Map; |
||
41 | |||
42 | import org.cresques.cts.IProjection; |
||
43 | import org.slf4j.Logger; |
||
44 | import org.slf4j.LoggerFactory; |
||
45 | |||
46 | import org.gvsig.fmap.crs.CRSFactory; |
||
47 | import org.gvsig.fmap.dal.DataServerExplorer; |
||
48 | import org.gvsig.fmap.dal.DataStore; |
||
49 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
50 | import org.gvsig.fmap.dal.exception.DataException; |
||
51 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
52 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
53 | import org.gvsig.fmap.mapcontext.MapContextDrawer; |
||
54 | import org.gvsig.fmap.mapcontext.MapContextException; |
||
55 | import org.gvsig.fmap.mapcontext.MapContextLocator; |
||
56 | import org.gvsig.fmap.mapcontext.MapContextManager; |
||
57 | import org.gvsig.fmap.mapcontext.MapContextRuntimeException; |
||
58 | import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
||
59 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
60 | import org.gvsig.fmap.mapcontext.layers.LayerFactory; |
||
61 | 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 | import org.gvsig.tools.persistence.PersistenceManager; |
||
79 | |||
80 | /**
|
||
81 | * Default implementation of the {@link MapContextManager}.
|
||
82 | *
|
||
83 | * @author <a href="mailto:cordinyana@gvsig.org">C?sar Ordi?ana</a>
|
||
84 | */
|
||
85 | public class DefaultMapContextManager implements MapContextManager { |
||
86 | |||
87 | private static final Logger logger = LoggerFactory |
||
88 | .getLogger(DefaultMapContextManager.class); |
||
89 | |||
90 | private Class drawerClazz = DefaultMapContextDrawer.class; |
||
91 | |||
92 | private Map legends = Collections.synchronizedMap(new HashMap()); |
||
93 | |||
94 | private Map legendReaders = Collections.synchronizedMap(new HashMap()); |
||
95 | |||
96 | private Map legendWriters = Collections.synchronizedMap(new HashMap()); |
||
97 | |||
98 | private String defaultVectorLegend; |
||
99 | |||
100 | public SymbolManager getSymbolManager() {
|
||
101 | return MapContextLocator.getSymbolManager();
|
||
102 | } |
||
103 | |||
104 | private SymbolPreferences getSymbolPreferences() {
|
||
105 | return getSymbolManager().getSymbolPreferences();
|
||
106 | } |
||
107 | |||
108 | public String getSymbolLibraryPath() { |
||
109 | return getSymbolPreferences().getSymbolLibraryPath();
|
||
110 | } |
||
111 | |||
112 | public void setSymbolLibraryPath(String symbolLibraryPath) { |
||
113 | getSymbolPreferences().setSymbolLibraryPath(symbolLibraryPath); |
||
114 | } |
||
115 | |||
116 | public void resetSymbolLibraryPath() { |
||
117 | getSymbolPreferences().resetSymbolLibraryPath(); |
||
118 | } |
||
119 | |||
120 | public Color getDefaultSymbolColor() { |
||
121 | return getSymbolPreferences().getDefaultSymbolColor();
|
||
122 | } |
||
123 | |||
124 | public Color getDefaultSymbolFillColor() { |
||
125 | return getSymbolPreferences().getDefaultSymbolFillColor();
|
||
126 | } |
||
127 | |||
128 | public Font getDefaultSymbolFont() { |
||
129 | return getSymbolPreferences().getDefaultSymbolFont();
|
||
130 | } |
||
131 | |||
132 | public String getSymbolFileExtension() { |
||
133 | return getSymbolPreferences().getSymbolFileExtension();
|
||
134 | } |
||
135 | |||
136 | public boolean isDefaultSymbolFillColorAleatory() { |
||
137 | return getSymbolPreferences().isDefaultSymbolFillColorAleatory();
|
||
138 | } |
||
139 | |||
140 | public void resetDefaultSymbolColor() { |
||
141 | getSymbolPreferences().resetDefaultSymbolColor(); |
||
142 | } |
||
143 | |||
144 | public void resetDefaultSymbolFillColor() { |
||
145 | getSymbolPreferences().resetDefaultSymbolFillColor(); |
||
146 | } |
||
147 | |||
148 | public void resetDefaultSymbolFillColorAleatory() { |
||
149 | getSymbolPreferences().resetDefaultSymbolFillColorAleatory(); |
||
150 | } |
||
151 | |||
152 | public void resetDefaultSymbolFont() { |
||
153 | getSymbolPreferences().resetDefaultSymbolFont(); |
||
154 | } |
||
155 | |||
156 | public void setDefaultSymbolColor(Color defaultSymbolColor) { |
||
157 | getSymbolPreferences().setDefaultSymbolColor(defaultSymbolColor); |
||
158 | } |
||
159 | |||
160 | public void setDefaultSymbolFillColor(Color defaultSymbolFillColor) { |
||
161 | getSymbolPreferences().setDefaultSymbolFillColor(defaultSymbolFillColor); |
||
162 | } |
||
163 | |||
164 | public void setDefaultSymbolFillColorAleatory( |
||
165 | boolean defaultSymbolFillColorAleatory) {
|
||
166 | getSymbolPreferences().setDefaultSymbolFillColorAleatory( |
||
167 | defaultSymbolFillColorAleatory); |
||
168 | } |
||
169 | |||
170 | public void setDefaultSymbolFont(Font defaultSymbolFont) { |
||
171 | getSymbolPreferences().setDefaultSymbolFont(defaultSymbolFont); |
||
172 | } |
||
173 | |||
174 | public void setSymbolFileExtension(String extension) { |
||
175 | getSymbolPreferences().setSymbolFileExtension(extension); |
||
176 | } |
||
177 | |||
178 | public int getDefaultCartographicSupportMeasureUnit() { |
||
179 | return getSymbolPreferences().getDefaultCartographicSupportMeasureUnit();
|
||
180 | } |
||
181 | |||
182 | public void setDefaultCartographicSupportMeasureUnit( |
||
183 | int defaultCartographicSupportMeasureUnit) {
|
||
184 | getSymbolPreferences().setDefaultCartographicSupportMeasureUnit( |
||
185 | defaultCartographicSupportMeasureUnit); |
||
186 | } |
||
187 | |||
188 | public int getDefaultCartographicSupportReferenceSystem() { |
||
189 | return getSymbolPreferences().getDefaultCartographicSupportReferenceSystem();
|
||
190 | } |
||
191 | |||
192 | public void setDefaultCartographicSupportReferenceSystem( |
||
193 | int defaultCartographicSupportReferenceSystem) {
|
||
194 | getSymbolPreferences().setDefaultCartographicSupportReferenceSystem( |
||
195 | defaultCartographicSupportReferenceSystem); |
||
196 | } |
||
197 | |||
198 | public MapContextDrawer createMapContextDrawerInstance(Class drawerClazz) |
||
199 | throws MapContextException {
|
||
200 | return createMapContextDrawerInstance(drawerClazz, "NONE"); |
||
201 | } |
||
202 | |||
203 | public MapContextDrawer createDefaultMapContextDrawerInstance()
|
||
204 | throws MapContextException {
|
||
205 | |||
206 | return createMapContextDrawerInstance(drawerClazz, "default"); |
||
207 | } |
||
208 | |||
209 | private MapContextDrawer createMapContextDrawerInstance(Class drawerClazz, |
||
210 | String name) throws RegisteredClassInstantiationException { |
||
211 | try {
|
||
212 | return (MapContextDrawer) drawerClazz.newInstance();
|
||
213 | } catch (Exception ex) { |
||
214 | throw new RegisteredClassInstantiationException( |
||
215 | MapContextDrawer.class, drawerClazz, name, ex); |
||
216 | } |
||
217 | } |
||
218 | |||
219 | public void setDefaultMapContextDrawer(Class drawerClazz) |
||
220 | throws MapContextException {
|
||
221 | |||
222 | validateMapContextDrawer(drawerClazz); |
||
223 | this.drawerClazz = drawerClazz;
|
||
224 | } |
||
225 | |||
226 | public void validateMapContextDrawer(Class drawerClazz) |
||
227 | throws MapContextException {
|
||
228 | if (!MapContextDrawer.class.isAssignableFrom(drawerClazz)) {
|
||
229 | throw new InvalidRegisteredClassException(MapContextDrawer.class, |
||
230 | drawerClazz, "UNKNOWN");
|
||
231 | } |
||
232 | } |
||
233 | |||
234 | public GraphicLayer createGraphicsLayer(IProjection projection) {
|
||
235 | DefaultGraphicLayer layer = new DefaultGraphicLayer();
|
||
236 | try {
|
||
237 | layer.initialize(projection); |
||
238 | layer.setLegend((IVectorLegend)createLegend(IVectorialUniqueValueLegend.LEGEND_NAME)); |
||
239 | } catch (Exception e) { |
||
240 | logger.error("Error initializing the graphics layer", e);
|
||
241 | } |
||
242 | return layer;
|
||
243 | } |
||
244 | |||
245 | public String getDefaultVectorLegend() { |
||
246 | return defaultVectorLegend;
|
||
247 | } |
||
248 | |||
249 | public void setDefaultVectorLegend(String defaultVectorLegend) { |
||
250 | this.defaultVectorLegend = defaultVectorLegend;
|
||
251 | } |
||
252 | |||
253 | public void registerLegend(String legendName, Class legendClass) |
||
254 | throws MapContextRuntimeException {
|
||
255 | |||
256 | if (legendClass == null || !ILegend.class.isAssignableFrom(legendClass)) { |
||
257 | throw new InvalidRegisteredClassException(ILegend.class, |
||
258 | legendClass, legendName); |
||
259 | } |
||
260 | |||
261 | legends.put(legendName, legendClass); |
||
262 | } |
||
263 | |||
264 | public ILegend createLegend(String legendName) |
||
265 | throws MapContextRuntimeException {
|
||
266 | Class legendClass = (Class) legends.get(legendName); |
||
267 | |||
268 | if (legendClass != null) { |
||
269 | try {
|
||
270 | return (ILegend) legendClass.newInstance();
|
||
271 | } catch (InstantiationException e) { |
||
272 | throw new RegisteredClassInstantiationException(ILegend.class, |
||
273 | legendClass, legendName, e); |
||
274 | } catch (IllegalAccessException e) { |
||
275 | throw new RegisteredClassInstantiationException(ILegend.class, |
||
276 | legendClass, legendName, e); |
||
277 | } |
||
278 | } |
||
279 | return null; |
||
280 | } |
||
281 | |||
282 | public IVectorLegend createDefaultVectorLegend(int shapeType) |
||
283 | throws MapContextRuntimeException {
|
||
284 | try {
|
||
285 | // Create legend
|
||
286 | IVectorLegend legend = |
||
287 | (IVectorLegend) createLegend(getDefaultVectorLegend()); |
||
288 | if (legend == null) { |
||
289 | return null; |
||
290 | } |
||
291 | // Set legend values
|
||
292 | legend.setShapeType(shapeType); |
||
293 | legend.setDefaultSymbol(getSymbolManager().createSymbol(shapeType)); |
||
294 | return legend;
|
||
295 | } catch(Exception e) { |
||
296 | throw new MapContextRuntimeException(e); |
||
297 | } |
||
298 | } |
||
299 | |||
300 | public void registerLegendReader(String format, Class readerClass) |
||
301 | throws MapContextRuntimeException {
|
||
302 | if (readerClass == null |
||
303 | || !ILegendReader.class.isAssignableFrom(readerClass)) { |
||
304 | throw new InvalidRegisteredClassException(ILegendReader.class, |
||
305 | readerClass, format); |
||
306 | } |
||
307 | |||
308 | legendReaders.put(format, readerClass); |
||
309 | } |
||
310 | |||
311 | public ILegendReader createLegendReader(String format) |
||
312 | throws MapContextRuntimeException {
|
||
313 | Class legendReaderClazz = (Class) legendReaders.get(format); |
||
314 | |||
315 | if (legendReaderClazz != null) { |
||
316 | try {
|
||
317 | return (ILegendReader) legendReaderClazz.newInstance();
|
||
318 | } catch (InstantiationException e) { |
||
319 | throw new RegisteredClassInstantiationException( |
||
320 | ILegendReader.class, legendReaderClazz, format, e); |
||
321 | } catch (IllegalAccessException e) { |
||
322 | throw new RegisteredClassInstantiationException( |
||
323 | ILegendReader.class, legendReaderClazz, format, e); |
||
324 | } |
||
325 | } |
||
326 | return null; |
||
327 | } |
||
328 | |||
329 | public void registerLegendWriter(String legendName, String format, |
||
330 | Class writerClass) throws MapContextRuntimeException { |
||
331 | if (writerClass == null |
||
332 | || !ILegendWriter.class.isAssignableFrom(writerClass)) { |
||
333 | throw new InvalidRegisteredClassException(ILegendWriter.class, |
||
334 | writerClass, format.concat("-").concat(legendName));
|
||
335 | } |
||
336 | |||
337 | Map legendFormatWriters = (Map) legendWriters.get(format); |
||
338 | |||
339 | synchronized (legendWriters) {
|
||
340 | if (legendFormatWriters == null) { |
||
341 | legendFormatWriters = Collections
|
||
342 | .synchronizedMap(new HashMap()); |
||
343 | legendWriters.put(format, legendFormatWriters); |
||
344 | } |
||
345 | } |
||
346 | |||
347 | legendFormatWriters.put(legendName, writerClass); |
||
348 | } |
||
349 | |||
350 | public ILegendWriter createLegendWriter(String legendName, String format) |
||
351 | throws MapContextRuntimeException {
|
||
352 | Map legendFormatWriters = getLegendWritersForFormat(format);
|
||
353 | |||
354 | if (legendFormatWriters != null) { |
||
355 | Class legendWriterClazz = (Class) legendFormatWriters |
||
356 | .get(legendName); |
||
357 | |||
358 | if (legendWriterClazz != null) { |
||
359 | try {
|
||
360 | return (ILegendWriter) legendWriterClazz.newInstance();
|
||
361 | } catch (InstantiationException e) { |
||
362 | throw new RegisteredClassInstantiationException( |
||
363 | ILegendWriter.class, legendWriterClazz, format |
||
364 | .concat("-").concat(legendName), e);
|
||
365 | } catch (IllegalAccessException e) { |
||
366 | throw new RegisteredClassInstantiationException( |
||
367 | ILegendWriter.class, legendWriterClazz, format |
||
368 | .concat("-").concat(legendName), e);
|
||
369 | } |
||
370 | } |
||
371 | } |
||
372 | return null; |
||
373 | } |
||
374 | |||
375 | private Map getLegendWritersForFormat(String format) { |
||
376 | return (Map) legendWriters.get(format); |
||
377 | } |
||
378 | |||
379 | public IMultiLayerSymbol createMultiLayerSymbol(int shapeType) |
||
380 | throws MapContextRuntimeException {
|
||
381 | return getSymbolManager().createMultiLayerSymbol(shapeType);
|
||
382 | } |
||
383 | |||
384 | public IMultiLayerSymbol createMultiLayerSymbol(String symbolName) |
||
385 | throws MapContextRuntimeException {
|
||
386 | return getSymbolManager().createMultiLayerSymbol(symbolName);
|
||
387 | } |
||
388 | |||
389 | public ISymbol createSymbol(int shapeType, Color color) |
||
390 | throws MapContextRuntimeException {
|
||
391 | return getSymbolManager().createSymbol(shapeType, color);
|
||
392 | } |
||
393 | |||
394 | public ISymbol createSymbol(int shapeType) |
||
395 | throws MapContextRuntimeException {
|
||
396 | return getSymbolManager().createSymbol(shapeType);
|
||
397 | } |
||
398 | |||
399 | public ISymbol createSymbol(String symbolName, Color color) |
||
400 | throws MapContextRuntimeException {
|
||
401 | return getSymbolManager().createSymbol(symbolName, color);
|
||
402 | } |
||
403 | |||
404 | public ISymbol createSymbol(String symbolName) |
||
405 | throws MapContextRuntimeException {
|
||
406 | return getSymbolManager().createSymbol(symbolName);
|
||
407 | } |
||
408 | |||
409 | public IWarningSymbol getWarningSymbol(String message, String symbolDesc, |
||
410 | int symbolDrawExceptionType) throws MapContextRuntimeException { |
||
411 | return getSymbolManager().getWarningSymbol(message, symbolDesc,
|
||
412 | symbolDrawExceptionType); |
||
413 | } |
||
414 | |||
415 | public ISymbol[] loadSymbols(File folder, FileFilter filter) |
||
416 | throws SymbolException {
|
||
417 | return getSymbolManager().loadSymbols(folder, filter);
|
||
418 | } |
||
419 | |||
420 | public ISymbol[] loadSymbols(File folder) throws SymbolException { |
||
421 | return getSymbolManager().loadSymbols(folder);
|
||
422 | } |
||
423 | |||
424 | public void registerMultiLayerSymbol(String symbolName, Class symbolClass) |
||
425 | throws MapContextRuntimeException {
|
||
426 | getSymbolManager().registerMultiLayerSymbol(symbolName, symbolClass); |
||
427 | } |
||
428 | |||
429 | public void registerMultiLayerSymbol(String symbolName, int[] shapeTypes, |
||
430 | Class symbolClass) throws MapContextRuntimeException { |
||
431 | getSymbolManager().registerMultiLayerSymbol(symbolName, shapeTypes, |
||
432 | symbolClass); |
||
433 | } |
||
434 | |||
435 | public void registerSymbol(String symbolName, Class symbolClass) |
||
436 | throws MapContextRuntimeException {
|
||
437 | getSymbolManager().registerSymbol(symbolName, symbolClass); |
||
438 | } |
||
439 | |||
440 | public void registerSymbol(String symbolName, int[] shapeTypes, |
||
441 | Class symbolClass) throws MapContextException { |
||
442 | getSymbolManager().registerSymbol(symbolName, shapeTypes, symbolClass); |
||
443 | } |
||
444 | |||
445 | public void saveSymbol(ISymbol symbol, String fileName, File folder, |
||
446 | boolean overwrite) throws SymbolException { |
||
447 | getSymbolManager().saveSymbol(symbol, fileName, folder, overwrite); |
||
448 | } |
||
449 | |||
450 | public void saveSymbol(ISymbol symbol, String fileName, File folder) |
||
451 | throws SymbolException {
|
||
452 | getSymbolManager().saveSymbol(symbol, fileName, folder); |
||
453 | } |
||
454 | |||
455 | public FLayer createLayer(String layerName, DataStoreParameters parameters) |
||
456 | throws LoadLayerException {
|
||
457 | return LayerFactory.getInstance().createLayer(layerName, parameters);
|
||
458 | } |
||
459 | |||
460 | public FLayer createLayer(String layerName, DataStore store) |
||
461 | throws LoadLayerException {
|
||
462 | return LayerFactory.getInstance().createLayer(layerName, store);
|
||
463 | } |
||
464 | |||
465 | public ILegend getLegend(DataStore dataStore) {
|
||
466 | ILegend legend = null;
|
||
467 | |||
468 | File file = getResourcePathByReflection(dataStore, SymbolManager.LEGEND_FILE_EXTENSION.substring(1)); |
||
469 | try{
|
||
470 | if ((file != null) && (file.exists())){ |
||
471 | PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager(); |
||
472 | FileInputStream is = new FileInputStream(file); |
||
473 | legend = (ILegend) persistenceManager.getObject(is); |
||
474 | is.close(); |
||
475 | } |
||
476 | } catch (FileNotFoundException e){ |
||
477 | logger.error("Legend not found", e);
|
||
478 | } catch (IOException e) { |
||
479 | logger.error("Error reading the legend", e);
|
||
480 | } |
||
481 | |||
482 | //If the legend is null, next option is to check if the store has the getLegend method
|
||
483 | if (legend == null){ |
||
484 | try {
|
||
485 | legend = (IVectorLegend) dataStore.invokeDynMethod( |
||
486 | "getLegend", null); |
||
487 | } catch (DynMethodNotSupportedException e) {
|
||
488 | logger.debug("This store {} does not provide a legend.",
|
||
489 | dataStore.getName()); |
||
490 | } catch (DynMethodException e) {
|
||
491 | logger.error( |
||
492 | "Can't load the specific legend provided for the store {}.",
|
||
493 | dataStore.getName(), e); |
||
494 | } |
||
495 | } |
||
496 | |||
497 | //If legend is null, last step is just try to create the legend by default
|
||
498 | if( legend == null ) { |
||
499 | FeatureType featureType; |
||
500 | try {
|
||
501 | featureType = (((FeatureStore)dataStore).getDefaultFeatureType()); |
||
502 | int indexGeom = featureType.getDefaultGeometryAttributeIndex();
|
||
503 | int typeShape = featureType.getAttributeDescriptor(indexGeom).getGeometryType();
|
||
504 | legend = createDefaultVectorLegend(typeShape); |
||
505 | } catch (DataException e) {
|
||
506 | logger.error("Error getting the default feature type", e);
|
||
507 | } |
||
508 | } |
||
509 | |||
510 | return legend;
|
||
511 | } |
||
512 | |||
513 | public ILabelingStrategy getLabelingStrategy(DataStore dataStore) {
|
||
514 | ILabelingStrategy labelingStrategy = null;
|
||
515 | |||
516 | File file = getResourcePathByReflection(dataStore, SymbolManager.LABELINGSTRATEGY_FILE_EXTENSION.substring(1)); |
||
517 | try{
|
||
518 | if ((file != null) && (file.exists())){ |
||
519 | PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager(); |
||
520 | FileInputStream is = new FileInputStream(file); |
||
521 | labelingStrategy = (ILabelingStrategy) persistenceManager.getObject(is); |
||
522 | is.close(); |
||
523 | } |
||
524 | } catch (FileNotFoundException e){ |
||
525 | logger.error("Label strategy not found", e);
|
||
526 | } catch (IOException e) { |
||
527 | logger.error("Error reading the labeling strategy", e);
|
||
528 | } |
||
529 | |||
530 | //If the legend is null, next option is to check if the store has the getLegend method
|
||
531 | if (labelingStrategy == null){ |
||
532 | try {
|
||
533 | labelingStrategy = |
||
534 | (ILabelingStrategy) dataStore.invokeDynMethod("getLabeling",
|
||
535 | null);
|
||
536 | } catch (DynMethodNotSupportedException e1) {
|
||
537 | labelingStrategy = null;
|
||
538 | } catch (DynMethodException e1) {
|
||
539 | logger.error("Can't load the specific lebeling strategy provided for the datastore {}.",
|
||
540 | dataStore.getName(), |
||
541 | e1); |
||
542 | } |
||
543 | } |
||
544 | |||
545 | return labelingStrategy;
|
||
546 | } |
||
547 | |||
548 | private File getResourcePathByReflection(DataStore dataStore, String resource){ |
||
549 | //Loading the file from a store based on file
|
||
550 | DataServerExplorer dataServerExplorer = null;
|
||
551 | try {
|
||
552 | dataServerExplorer = dataStore.getExplorer(); |
||
553 | Class[] args = new Class[2]; |
||
554 | args[0] = DataStore.class;
|
||
555 | args[1] = String.class; |
||
556 | Method method = dataStore.getExplorer().getClass().getMethod("getResourcePath", args); |
||
557 | if (method != null){ |
||
558 | Object[] params = new Object[2]; |
||
559 | params[0] = dataStore;
|
||
560 | params[1] = resource;
|
||
561 | return (File)method.invoke(dataServerExplorer, params); |
||
562 | } |
||
563 | } catch (Exception e) { |
||
564 | logger.debug( |
||
565 | "Can't load the specific legend provided by the explorer "
|
||
566 | + dataServerExplorer, e); |
||
567 | } |
||
568 | return null; |
||
569 | } |
||
570 | |||
571 | private Map iconLayers = new HashMap(); // (Map<String storeProviderName, String iconName>) |
||
572 | |||
573 | public void registerIconLayer(String storeProviderName, String iconName) { |
||
574 | if( storeProviderName == null || iconName == null ) { |
||
575 | logger.info("registerIconLayer, storeProviderName or iconName are null");
|
||
576 | return;
|
||
577 | } |
||
578 | if( storeProviderName.trim().length()==0 || iconName.trim().length()==0 ) { |
||
579 | logger.info("registerIconLayer, invalid storeProviderName or iconName");
|
||
580 | return;
|
||
581 | } |
||
582 | iconLayers.put(storeProviderName.trim().toLowerCase(), iconName); |
||
583 | } |
||
584 | |||
585 | public String getIconLayer(DataStore store) { |
||
586 | String name = (String) iconLayers.get(store.getProviderName().trim().toLowerCase()); |
||
587 | if( name == null ) { |
||
588 | return "layer-icon"; |
||
589 | } |
||
590 | return name;
|
||
591 | } |
||
592 | |||
593 | /* (non-Javadoc)
|
||
594 | * @see org.gvsig.fmap.mapcontext.MapContextManager#getDefaultCRS()
|
||
595 | */
|
||
596 | public IProjection getDefaultCRS() {
|
||
597 | return CRSFactory.getCRS("EPSG:4326"); |
||
598 | } |
||
599 | } |