Statistics
| Revision:

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 @ 47740

History | View | Annotate | Download (44.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 45888 jjdelcerro
import java.net.URL;
33 40927 jldominguez
import java.util.ArrayList;
34 40435 jjdelcerro
import java.util.HashMap;
35 40927 jldominguez
import java.util.Iterator;
36 43780 jjdelcerro
import java.util.LinkedHashMap;
37 40927 jldominguez
import java.util.List;
38 40435 jjdelcerro
import java.util.Map;
39 45888 jjdelcerro
import javax.swing.Icon;
40
import javax.swing.ImageIcon;
41 44161 jjdelcerro
import org.apache.commons.io.IOUtils;
42 43215 jjdelcerro
import org.apache.commons.lang3.StringUtils;
43 44831 jjdelcerro
import org.cresques.cts.ICoordTrans;
44 40435 jjdelcerro
45
import org.cresques.cts.IProjection;
46
import org.gvsig.fmap.crs.CRSFactory;
47 44831 jjdelcerro
import org.gvsig.fmap.dal.DALLocator;
48 46361 jjdelcerro
import org.gvsig.fmap.dal.DataFactory;
49 44831 jjdelcerro
import org.gvsig.fmap.dal.DataManager;
50 40435 jjdelcerro
import org.gvsig.fmap.dal.DataStore;
51
import org.gvsig.fmap.dal.DataStoreParameters;
52 44831 jjdelcerro
import org.gvsig.fmap.dal.DataStoreProviderFactory;
53 40435 jjdelcerro
import org.gvsig.fmap.dal.exception.DataException;
54
import org.gvsig.fmap.dal.feature.FeatureStore;
55
import org.gvsig.fmap.dal.feature.FeatureType;
56 44831 jjdelcerro
import org.gvsig.fmap.dal.raster.BandDescriptor;
57
import org.gvsig.fmap.dal.raster.RasterStore;
58 45888 jjdelcerro
import org.gvsig.fmap.geom.Geometry;
59 46383 jjdelcerro
import org.gvsig.fmap.geom.GeometryUtils;
60 41141 jjdelcerro
import org.gvsig.fmap.mapcontext.MapContext;
61 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.MapContextDrawer;
62
import org.gvsig.fmap.mapcontext.MapContextException;
63
import org.gvsig.fmap.mapcontext.MapContextLocator;
64
import org.gvsig.fmap.mapcontext.MapContextManager;
65
import org.gvsig.fmap.mapcontext.MapContextRuntimeException;
66 41141 jjdelcerro
import org.gvsig.fmap.mapcontext.ViewPort;
67 44831 jjdelcerro
import org.gvsig.fmap.mapcontext.exceptions.CantRetrieveLayerByStoreException;
68 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
69 43141 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.DefaultLayerInformationBuilder;
70 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.FLayer;
71 43141 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.LayerInformationBuilder;
72 44831 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
73 46361 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
74 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer;
75
import org.gvsig.fmap.mapcontext.layers.vectorial.impl.DefaultGraphicLayer;
76
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
77
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
78
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend;
79
import org.gvsig.fmap.mapcontext.rendering.legend.driver.ILegendReader;
80
import org.gvsig.fmap.mapcontext.rendering.legend.driver.ILegendWriter;
81
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
82
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
83
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
84
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol;
85
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolException;
86
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
87
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolPreferences;
88 44831 jjdelcerro
import org.gvsig.raster.lib.legend.api.RasterLegend;
89
import org.gvsig.raster.lib.legend.api.RasterLegendLocator;
90
import org.gvsig.raster.lib.legend.api.RasterLegendManager;
91
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation;
92 40435 jjdelcerro
import org.gvsig.tools.ToolsLocator;
93 46285 jjdelcerro
import org.gvsig.tools.dispose.DisposeUtils;
94 44831 jjdelcerro
import org.gvsig.tools.dynobject.DynObject;
95 40435 jjdelcerro
import org.gvsig.tools.dynobject.exception.DynMethodException;
96
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException;
97 44831 jjdelcerro
import org.gvsig.tools.folders.FoldersManager;
98 41302 jjdelcerro
import org.gvsig.tools.observer.Notification;
99
import org.gvsig.tools.observer.ObservableHelper;
100
import org.gvsig.tools.observer.Observer;
101 40435 jjdelcerro
import org.gvsig.tools.persistence.PersistenceManager;
102 44297 jjdelcerro
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
103
import org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource;
104 46383 jjdelcerro
import org.gvsig.tools.swing.api.TransparencySupport;
105 47740 jjdelcerro
import org.gvsig.tools.util.Factory;
106 41141 jjdelcerro
import org.slf4j.Logger;
107
import org.slf4j.LoggerFactory;
108 40435 jjdelcerro
109
/**
110
 * Default implementation of the {@link MapContextManager}.
111
 *
112
 */
113 44831 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
114 40435 jjdelcerro
public class DefaultMapContextManager implements MapContextManager {
115
116 44297 jjdelcerro
    private static final Logger LOGGER = LoggerFactory
117 42457 jjdelcerro
            .getLogger(DefaultMapContextManager.class);
118 40435 jjdelcerro
119 47471 fdiaz
    private static final double DEFAULT_SYMBOL_TRANSPARENCY = 0.6;
120
121 42457 jjdelcerro
    private Class drawerClazz = DefaultMapContextDrawer.class;
122 40435 jjdelcerro
123 44831 jjdelcerro
    private final Map<String,Class<ILegend>> legends = new HashMap();
124 40435 jjdelcerro
125 43780 jjdelcerro
    private final Map<String, Class<ILegendReader>> legendReaders = new LinkedHashMap<>();
126 40435 jjdelcerro
127 43780 jjdelcerro
    private final Map<String, Map<Class<ILegend>,Class<ILegendWriter>>> legendWriters = new LinkedHashMap<>();
128 44831 jjdelcerro
129 47740 jjdelcerro
    private final Map<Object,Class<? extends FLayer>> layerClassFromStoreClass = new HashMap<>();
130 40435 jjdelcerro
131 44831 jjdelcerro
    private final Map<String,String> iconLayers = new HashMap(); //  (Map<String storeProviderName, String iconName>)
132
133 42457 jjdelcerro
    private String defaultVectorLegend;
134 40435 jjdelcerro
135 44297 jjdelcerro
    private final ObservableHelper observableHelper = new ObservableHelper();
136 41302 jjdelcerro
137 42457 jjdelcerro
    private File colorTableLibraryFolder = null;
138 44831 jjdelcerro
    private String defaultRasterLegend;
139 40435 jjdelcerro
140 44831 jjdelcerro
    @Override
141 42457 jjdelcerro
    public MapContext createMapContext() {
142
        MapContext mapcontext = new MapContext(new ViewPort());
143
        return (MapContext) notifyObservers(CREATE_MAPCONTEXT, mapcontext).getValue();
144
    }
145 40435 jjdelcerro
146 44831 jjdelcerro
    @Override
147 42457 jjdelcerro
    public SymbolManager getSymbolManager() {
148
        return MapContextLocator.getSymbolManager();
149
    }
150 40435 jjdelcerro
151 42457 jjdelcerro
    private SymbolPreferences getSymbolPreferences() {
152
        return getSymbolManager().getSymbolPreferences();
153
    }
154 40435 jjdelcerro
155 44831 jjdelcerro
    @Override
156 42457 jjdelcerro
    public String getSymbolLibraryPath() {
157
        return getSymbolPreferences().getSymbolLibraryPath();
158
    }
159 40435 jjdelcerro
160 44831 jjdelcerro
    @Override
161 42457 jjdelcerro
    public void setSymbolLibraryPath(String symbolLibraryPath) {
162
        getSymbolPreferences().setSymbolLibraryPath(symbolLibraryPath);
163
    }
164 40435 jjdelcerro
165 44831 jjdelcerro
    @Override
166 42457 jjdelcerro
    public void resetSymbolLibraryPath() {
167
        getSymbolPreferences().resetSymbolLibraryPath();
168
    }
169 40435 jjdelcerro
170 44831 jjdelcerro
    @Override
171 42457 jjdelcerro
    public Color getDefaultSymbolColor() {
172
        return getSymbolPreferences().getDefaultSymbolColor();
173
    }
174 40435 jjdelcerro
175 44831 jjdelcerro
    @Override
176 42457 jjdelcerro
    public Color getDefaultSymbolFillColor() {
177
        return getSymbolPreferences().getDefaultSymbolFillColor();
178
    }
179 40435 jjdelcerro
180 44831 jjdelcerro
    @Override
181 42457 jjdelcerro
    public Font getDefaultSymbolFont() {
182
        return getSymbolPreferences().getDefaultSymbolFont();
183
    }
184 40435 jjdelcerro
185 42457 jjdelcerro
    public String getSymbolFileExtension() {
186
        return getSymbolPreferences().getSymbolFileExtension();
187
    }
188 40435 jjdelcerro
189 44831 jjdelcerro
    @Override
190 42457 jjdelcerro
    public boolean isDefaultSymbolFillColorAleatory() {
191
        return getSymbolPreferences().isDefaultSymbolFillColorAleatory();
192
    }
193 40435 jjdelcerro
194 44831 jjdelcerro
    @Override
195 42457 jjdelcerro
    public void resetDefaultSymbolColor() {
196
        getSymbolPreferences().resetDefaultSymbolColor();
197
    }
198 40435 jjdelcerro
199 44831 jjdelcerro
    @Override
200 42457 jjdelcerro
    public void resetDefaultSymbolFillColor() {
201
        getSymbolPreferences().resetDefaultSymbolFillColor();
202
    }
203 40435 jjdelcerro
204 44831 jjdelcerro
    @Override
205 42457 jjdelcerro
    public void resetDefaultSymbolFillColorAleatory() {
206
        getSymbolPreferences().resetDefaultSymbolFillColorAleatory();
207
    }
208 40435 jjdelcerro
209 44831 jjdelcerro
    @Override
210 42457 jjdelcerro
    public void resetDefaultSymbolFont() {
211
        getSymbolPreferences().resetDefaultSymbolFont();
212
    }
213 40435 jjdelcerro
214 44831 jjdelcerro
    @Override
215 42457 jjdelcerro
    public void setDefaultSymbolColor(Color defaultSymbolColor) {
216
        getSymbolPreferences().setDefaultSymbolColor(defaultSymbolColor);
217
    }
218 40435 jjdelcerro
219 44831 jjdelcerro
    @Override
220 42457 jjdelcerro
    public void setDefaultSymbolFillColor(Color defaultSymbolFillColor) {
221
        getSymbolPreferences().setDefaultSymbolFillColor(defaultSymbolFillColor);
222
    }
223 40435 jjdelcerro
224 44831 jjdelcerro
    @Override
225 42457 jjdelcerro
    public void setDefaultSymbolFillColorAleatory(
226
            boolean defaultSymbolFillColorAleatory) {
227
        getSymbolPreferences().setDefaultSymbolFillColorAleatory(
228
                defaultSymbolFillColorAleatory);
229
    }
230 40435 jjdelcerro
231 44831 jjdelcerro
    @Override
232 42457 jjdelcerro
    public void setDefaultSymbolFont(Font defaultSymbolFont) {
233
        getSymbolPreferences().setDefaultSymbolFont(defaultSymbolFont);
234
    }
235 40435 jjdelcerro
236 42457 jjdelcerro
    public void setSymbolFileExtension(String extension) {
237
        getSymbolPreferences().setSymbolFileExtension(extension);
238
    }
239 40435 jjdelcerro
240 44831 jjdelcerro
    @Override
241 42457 jjdelcerro
    public int getDefaultCartographicSupportMeasureUnit() {
242
        return getSymbolPreferences().getDefaultCartographicSupportMeasureUnit();
243
    }
244 40435 jjdelcerro
245 44831 jjdelcerro
    @Override
246 42457 jjdelcerro
    public void setDefaultCartographicSupportMeasureUnit(
247
            int defaultCartographicSupportMeasureUnit) {
248
        getSymbolPreferences().setDefaultCartographicSupportMeasureUnit(
249
                defaultCartographicSupportMeasureUnit);
250
    }
251 40435 jjdelcerro
252 44831 jjdelcerro
    @Override
253 42457 jjdelcerro
    public int getDefaultCartographicSupportReferenceSystem() {
254
        return getSymbolPreferences().getDefaultCartographicSupportReferenceSystem();
255
    }
256 40435 jjdelcerro
257 44831 jjdelcerro
    @Override
258 42457 jjdelcerro
    public void setDefaultCartographicSupportReferenceSystem(
259
            int defaultCartographicSupportReferenceSystem) {
260
        getSymbolPreferences().setDefaultCartographicSupportReferenceSystem(
261
                defaultCartographicSupportReferenceSystem);
262
    }
263 40435 jjdelcerro
264 44831 jjdelcerro
    @Override
265 42457 jjdelcerro
    public MapContextDrawer createMapContextDrawerInstance(Class drawerClazz)
266
            throws MapContextException {
267
        return createMapContextDrawerInstance(drawerClazz, "NONE");
268
    }
269 40435 jjdelcerro
270 44831 jjdelcerro
    @Override
271 42457 jjdelcerro
    public MapContextDrawer createDefaultMapContextDrawerInstance()
272
            throws MapContextException {
273 40435 jjdelcerro
274 42457 jjdelcerro
        return createMapContextDrawerInstance(drawerClazz, "default");
275
    }
276 40435 jjdelcerro
277 42457 jjdelcerro
    private MapContextDrawer createMapContextDrawerInstance(Class drawerClazz,
278
            String name) throws RegisteredClassInstantiationException {
279
        try {
280
            MapContextDrawer drawer = (MapContextDrawer) drawerClazz.newInstance();
281
            notifyObservers(CREATE_MAPCONTEXT_DRAWER, drawer);
282
            return drawer;
283
        } catch (Exception ex) {
284
            throw new RegisteredClassInstantiationException(
285
                    MapContextDrawer.class, drawerClazz, name, ex);
286
        }
287
    }
288 40435 jjdelcerro
289 44831 jjdelcerro
    @Override
290 42457 jjdelcerro
    public void setDefaultMapContextDrawer(Class drawerClazz)
291
            throws MapContextException {
292 40435 jjdelcerro
293 42457 jjdelcerro
        validateMapContextDrawer(drawerClazz);
294
        this.drawerClazz = drawerClazz;
295
        notifyObservers(SET_MAPCONTEXT_DRAWER, drawerClazz);
296
    }
297 40435 jjdelcerro
298 44831 jjdelcerro
    @Override
299 42457 jjdelcerro
    public void validateMapContextDrawer(Class drawerClazz)
300
            throws MapContextException {
301
        if (!MapContextDrawer.class.isAssignableFrom(drawerClazz)) {
302
            throw new InvalidRegisteredClassException(MapContextDrawer.class,
303
                    drawerClazz, "UNKNOWN");
304
        }
305
    }
306 40435 jjdelcerro
307 44831 jjdelcerro
    @Override
308 42457 jjdelcerro
    public GraphicLayer createGraphicsLayer(IProjection projection) {
309
        DefaultGraphicLayer layer = new DefaultGraphicLayer();
310
        try {
311
            layer.initialize(projection);
312
            layer.setLegend((IVectorLegend) createLegend(IVectorialUniqueValueLegend.LEGEND_NAME));
313
        } catch (Exception e) {
314 44297 jjdelcerro
            LOGGER.error("Error initializing the graphics layer", e);
315 42457 jjdelcerro
        }
316
        return (GraphicLayer) notifyObservers(CREATE_GRAPHICS_LAYER, layer).getValue();
317
    }
318 40435 jjdelcerro
319 44831 jjdelcerro
    @Override
320 42457 jjdelcerro
    public String getDefaultVectorLegend() {
321
        return defaultVectorLegend;
322
    }
323 40435 jjdelcerro
324 44831 jjdelcerro
    @Override
325 42457 jjdelcerro
    public void setDefaultVectorLegend(String defaultVectorLegend) {
326
        this.defaultVectorLegend = defaultVectorLegend;
327
    }
328 40435 jjdelcerro
329 44831 jjdelcerro
    @Override
330
    public String getDefaultRasterLegend() {
331
        return defaultRasterLegend;
332
    }
333
334
    @Override
335
    public void setDefaultRasterLegend(String defaultRasterLegend) {
336
        this.defaultRasterLegend = defaultRasterLegend;
337
    }
338
339
    @Override
340 42457 jjdelcerro
    public void registerLegend(String legendName, Class legendClass)
341
            throws MapContextRuntimeException {
342 40435 jjdelcerro
343 42457 jjdelcerro
        if (legendClass == null || !ILegend.class.isAssignableFrom(legendClass)) {
344
            throw new InvalidRegisteredClassException(ILegend.class,
345
                    legendClass, legendName);
346
        }
347 40435 jjdelcerro
348 42457 jjdelcerro
        legends.put(legendName, legendClass);
349
        notifyObservers(REGISTER_LEGEND, legendName, legendClass);
350
    }
351 40435 jjdelcerro
352 44831 jjdelcerro
    @Override
353 42457 jjdelcerro
    public ILegend createLegend(String legendName)
354
            throws MapContextRuntimeException {
355
        Class legendClass = (Class) legends.get(legendName);
356
357
        if (legendClass != null) {
358
            try {
359
                ILegend legend = (ILegend) legendClass.newInstance();
360
                return (ILegend) notifyObservers(CREATE_LEGEND, legend).getValue();
361 44831 jjdelcerro
            } catch (InstantiationException | IllegalAccessException e) {
362 42457 jjdelcerro
                throw new RegisteredClassInstantiationException(ILegend.class,
363
                        legendClass, legendName, e);
364
            }
365
        }
366
        return null;
367
    }
368
369 44831 jjdelcerro
    @Override
370 40435 jjdelcerro
    public IVectorLegend createDefaultVectorLegend(int shapeType)
371 42457 jjdelcerro
            throws MapContextRuntimeException {
372 40435 jjdelcerro
        try {
373
            // Create legend
374 42457 jjdelcerro
            IVectorLegend legend
375
                    = (IVectorLegend) createLegend(getDefaultVectorLegend());
376 40435 jjdelcerro
            if (legend == null) {
377
                return null;
378
            }
379
            // Set legend values
380
            legend.setShapeType(shapeType);
381 41214 jjdelcerro
            ISymbol symbol = getSymbolManager().createSymbol(shapeType);
382 42457 jjdelcerro
            if (symbol == null) {
383
                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.";
384 41214 jjdelcerro
                throw new RuntimeException(msg);
385 46383 jjdelcerro
            }
386
            if( symbol instanceof TransparencySupport && (
387
                    GeometryUtils.isSubtype(Geometry.TYPES.SURFACE,shapeType) ||
388
                    GeometryUtils.isSubtype(Geometry.TYPES.MULTISURFACE,shapeType) )
389
                ) {
390 47471 fdiaz
                ((TransparencySupport)symbol).setTransparency(DEFAULT_SYMBOL_TRANSPARENCY);
391 41214 jjdelcerro
            }
392
            legend.setDefaultSymbol(symbol);
393 40435 jjdelcerro
            return legend;
394 42457 jjdelcerro
        } catch (Exception e) {
395 40435 jjdelcerro
            throw new MapContextRuntimeException(e);
396
        }
397
    }
398
399 44831 jjdelcerro
    @Override
400
    public RasterLegend createDefaultRasterLegend(List<BandDescriptor> bands)
401
            throws MapContextRuntimeException {
402
        try {
403
            // Create legend
404
            RasterLegend legend = (RasterLegend) createLegend(getDefaultRasterLegend());
405
            if (legend == null) {
406
                return null;
407
            }
408
            ColorInterpretation colorInterpretation;
409
            RasterLegendManager legendManager = RasterLegendLocator.getRasterLegendManager();
410
            switch (bands.size()) {
411
                case 3:
412
                    colorInterpretation = legendManager.createColorInterpretation(ColorInterpretation.RGB);
413
                    break;
414
                case 4:
415
                    colorInterpretation = legendManager.createColorInterpretation(ColorInterpretation.ARGB);
416
                    break;
417
                default:
418
                    String[] colorBands = new String[bands.size()];
419
                    colorBands[0] = ColorInterpretation.GRAY_BAND;
420
                    for (int i = 1; i < colorBands.length; i++) {
421
                        colorBands[i] = ColorInterpretation.UNDEFINED_BAND;
422
                    }   colorInterpretation = legendManager.createColorInterpretation(colorBands);
423
                    break;
424
            }
425
            legend.setColorInterpretation(colorInterpretation);
426
            legend.addLinearStretchEnhancementOperationIfNeeded(colorInterpretation, bands, 0);
427
            return legend;
428
        } catch (Exception e) {
429
            throw new MapContextRuntimeException(e);
430
        }
431
    }
432
433 40927 jldominguez
    // =============================================================
434
    // Legend reading/writing
435 43780 jjdelcerro
436
    private Map<Class<ILegend>,Class<ILegendWriter>> getLegendWritersForFormat(String format) {
437
        synchronized(legendWriters) {
438
            return legendWriters.get(format);
439
        }
440
    }
441
442
    private Class<ILegendReader> getLegendReaderForFormat(String format) {
443
        synchronized(legendReaders) {
444
            return legendReaders.get(format);
445
        }
446
    }
447
448
    @Override
449
    public List<String> getLegendReadingFormats() {
450
        synchronized(legendReaders) {
451
            List<String> resp = new ArrayList();
452
            resp.addAll(legendReaders.keySet());
453
            return resp;
454
        }
455
    }
456
457
    @Override
458
    public List<String> getLegendWritingFormats() {
459
        synchronized(legendWriters) {
460
            List<String> resp = new ArrayList();
461
            resp.addAll(legendWriters.keySet());
462
            return resp;
463
        }
464
    }
465
466
    @Override
467 42457 jjdelcerro
    public void registerLegendReader(String format, Class readerClass)
468
            throws MapContextRuntimeException {
469
        if (readerClass == null
470
                || !ILegendReader.class.isAssignableFrom(readerClass)) {
471
            throw new InvalidRegisteredClassException(ILegendReader.class,
472
                    readerClass, format);
473
        }
474 43780 jjdelcerro
        synchronized (legendReaders) {
475
            legendReaders.put(format, readerClass);
476
        }
477 42457 jjdelcerro
        notifyObservers(REGISTER_LEGEND_READER, format, readerClass);
478
    }
479 40435 jjdelcerro
480 43780 jjdelcerro
    @Override
481 42457 jjdelcerro
    public void registerLegendWriter(Class legendClass, String format,
482
            Class writerClass) throws MapContextRuntimeException {
483 43780 jjdelcerro
        if (writerClass == null || legendClass == null
484 42457 jjdelcerro
                || !ILegendWriter.class.isAssignableFrom(writerClass)
485
                || !ILegend.class.isAssignableFrom(legendClass)) {
486 40435 jjdelcerro
487 42457 jjdelcerro
            throw new InvalidRegisteredClassException(ILegendWriter.class,
488
                    writerClass, format.concat("-").concat(
489
                            legendClass == null ? "Null" : legendClass.getName()));
490
        }
491 40435 jjdelcerro
492 42457 jjdelcerro
        synchronized (legendWriters) {
493 43780 jjdelcerro
            Map<Class<ILegend>, Class<ILegendWriter>> legendWriterOfFormat = legendWriters.get(format);
494
            if (legendWriterOfFormat == null) {
495
                legendWriterOfFormat = new LinkedHashMap();
496
                legendWriters.put(format, legendWriterOfFormat);
497 42457 jjdelcerro
            }
498 43780 jjdelcerro
            legendWriterOfFormat.put(legendClass, writerClass);
499 42457 jjdelcerro
        }
500
        notifyObservers(REGISTER_LEGEND_WRITER, format, writerClass);
501
    }
502 40435 jjdelcerro
503 43780 jjdelcerro
    @Override
504
    public ILegendReader createLegendReader(String format)
505
            throws MapContextRuntimeException {
506
        Class<ILegendReader> legendReaderClazz = getLegendReaderForFormat(format);
507
508
        if (legendReaderClazz != null) {
509
            try {
510
                ILegendReader reader = (ILegendReader) legendReaderClazz.newInstance();
511
                return (ILegendReader) notifyObservers(CREATE_LEGEND_READER, reader).getValue();
512
            } catch (InstantiationException|IllegalAccessException e) {
513
                throw new RegisteredClassInstantiationException(
514
                        ILegendReader.class, legendReaderClazz, format, e);
515
            }
516
        }
517
        return null;
518
    }
519
520
    @Override
521 42457 jjdelcerro
    public ILegendWriter createLegendWriter(Class legendClass, String format)
522
            throws MapContextRuntimeException {
523 40435 jjdelcerro
524 42457 jjdelcerro
        if (legendClass == null || format == null) {
525
            return null;
526
        }
527 40435 jjdelcerro
528 43780 jjdelcerro
        Map<Class<ILegend>, Class<ILegendWriter>> legendFormatWriters = getLegendWritersForFormat(format);
529 42457 jjdelcerro
530
        if (legendFormatWriters != null) {
531 43780 jjdelcerro
            Class<ILegendWriter> legendWriterClazz = legendFormatWriters.get(legendClass);
532 42457 jjdelcerro
            if (legendWriterClazz != null) {
533
                /*
534
                 * Found exact match
535
                 */
536
                try {
537
                    ILegendWriter writer = (ILegendWriter) legendWriterClazz.newInstance();
538
                    return (ILegendWriter) notifyObservers(CREATE_LEGEND_READER, writer).getValue();
539 43780 jjdelcerro
                } catch (InstantiationException|IllegalAccessException e) {
540 42457 jjdelcerro
                    throw new RegisteredClassInstantiationException(
541 43780 jjdelcerro
                            ILegendWriter.class,
542
                            legendWriterClazz,
543
                            format.concat("-").concat(legendClass.getName()),
544
                            e
545
                    );
546 42457 jjdelcerro
                }
547
            } else {
548
                /*
549
                 * Trying to find superclass/superinterface of parameter
550
                 */
551
                try {
552
                    return getSuperClassLegendWriter(legendFormatWriters, legendClass);
553
                } catch (Exception exc) {
554
                    throw new MapContextRuntimeException(exc);
555
                }
556
            }
557
        }
558
        return null;
559
    }
560
561 43780 jjdelcerro
    private ILegendWriter getSuperClassLegendWriter(Map<Class<ILegend>, Class<ILegendWriter>> clsToWtr, Class<ILegend> legclass)
562 42457 jjdelcerro
            throws Exception {
563
564
        Iterator kiter = clsToWtr.keySet().iterator();
565 44831 jjdelcerro
        Object oitem;
566
        Class<ILegendWriter> citem;
567 42457 jjdelcerro
        while (kiter.hasNext()) {
568
            oitem = kiter.next();
569
            if (oitem instanceof Class) {
570
                citem = (Class) oitem;
571
                if (citem.isAssignableFrom(legclass)) {
572
                    /*
573
                     * Found superclass/superinterface
574
                     */
575 44831 jjdelcerro
                    citem = clsToWtr.get(oitem);
576 42457 jjdelcerro
                    return (ILegendWriter) citem.newInstance();
577
                }
578
            }
579
        }
580
        /*
581
         * No superclass/superinterface found
582
         */
583 40927 jldominguez
        return null;
584
    }
585
586 42457 jjdelcerro
    // =============================================================
587 40927 jldominguez
588 44831 jjdelcerro
    @Override
589 42457 jjdelcerro
    public IMultiLayerSymbol createMultiLayerSymbol(int shapeType)
590
            throws MapContextRuntimeException {
591
        IMultiLayerSymbol symbol = getSymbolManager().createMultiLayerSymbol(shapeType);
592
        return (IMultiLayerSymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
593
    }
594 40435 jjdelcerro
595 44831 jjdelcerro
    @Override
596 42457 jjdelcerro
    public IMultiLayerSymbol createMultiLayerSymbol(String symbolName)
597
            throws MapContextRuntimeException {
598
        IMultiLayerSymbol symbol = getSymbolManager().createMultiLayerSymbol(symbolName);
599
        return (IMultiLayerSymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
600
    }
601 40435 jjdelcerro
602 44831 jjdelcerro
    @Override
603 42457 jjdelcerro
    public ISymbol createSymbol(int shapeType, Color color)
604
            throws MapContextRuntimeException {
605
        ISymbol symbol = getSymbolManager().createSymbol(shapeType, color);
606
        return (ISymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
607
    }
608 40435 jjdelcerro
609 44831 jjdelcerro
    @Override
610 42457 jjdelcerro
    public ISymbol createSymbol(int shapeType)
611
            throws MapContextRuntimeException {
612
        ISymbol symbol = getSymbolManager().createSymbol(shapeType);
613
        return (ISymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
614
    }
615 40435 jjdelcerro
616 44831 jjdelcerro
    @Override
617 42457 jjdelcerro
    public ISymbol createSymbol(String symbolName, Color color)
618
            throws MapContextRuntimeException {
619
        ISymbol symbol = getSymbolManager().createSymbol(symbolName, color);
620
        return (ISymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
621
    }
622 40435 jjdelcerro
623 44831 jjdelcerro
    @Override
624 42457 jjdelcerro
    public ISymbol createSymbol(String symbolName)
625
            throws MapContextRuntimeException {
626
        ISymbol symbol = getSymbolManager().createSymbol(symbolName);
627
        return (ISymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
628
    }
629 40435 jjdelcerro
630 44831 jjdelcerro
    @Override
631 42457 jjdelcerro
    public IWarningSymbol getWarningSymbol(String message, String symbolDesc,
632
            int symbolDrawExceptionType) throws MapContextRuntimeException {
633
        return getSymbolManager().getWarningSymbol(message, symbolDesc,
634
                symbolDrawExceptionType);
635
    }
636 40435 jjdelcerro
637 42457 jjdelcerro
    public ISymbol[] loadSymbols(File folder, FileFilter filter)
638
            throws SymbolException {
639
        ISymbol[] symbols = getSymbolManager().loadSymbols(folder, filter);
640
        return (ISymbol[]) notifyObservers(LOAD_SYMBOLS, symbols).getValue();
641
    }
642 40435 jjdelcerro
643 42457 jjdelcerro
    public ISymbol[] loadSymbols(File folder) throws SymbolException {
644
        ISymbol[] symbols = getSymbolManager().loadSymbols(folder);
645
        return (ISymbol[]) notifyObservers(LOAD_SYMBOLS, symbols).getValue();
646
    }
647 40435 jjdelcerro
648 44831 jjdelcerro
    @Override
649 42457 jjdelcerro
    public void registerMultiLayerSymbol(String symbolName, Class symbolClass)
650
            throws MapContextRuntimeException {
651
        getSymbolManager().registerMultiLayerSymbol(symbolName, symbolClass);
652
        notifyObservers(REGISTER_MULTILAYER_SYMBOL, symbolName, symbolClass);
653
    }
654 40435 jjdelcerro
655 44831 jjdelcerro
    @Override
656 42457 jjdelcerro
    public void registerMultiLayerSymbol(String symbolName, int[] shapeTypes,
657
            Class symbolClass) throws MapContextRuntimeException {
658
        getSymbolManager().registerMultiLayerSymbol(symbolName, shapeTypes,
659
                symbolClass);
660
        notifyObservers(REGISTER_MULTILAYER_SYMBOL, symbolName, symbolClass, shapeTypes);
661
    }
662 40435 jjdelcerro
663 44831 jjdelcerro
    @Override
664 42457 jjdelcerro
    public void registerSymbol(String symbolName, Class symbolClass)
665
            throws MapContextRuntimeException {
666
        getSymbolManager().registerSymbol(symbolName, symbolClass);
667
        notifyObservers(REGISTER_SYMBOL, symbolName, symbolClass);
668
    }
669 40435 jjdelcerro
670 44831 jjdelcerro
    @Override
671 42457 jjdelcerro
    public void registerSymbol(String symbolName, int[] shapeTypes,
672
            Class symbolClass) throws MapContextException {
673
        getSymbolManager().registerSymbol(symbolName, shapeTypes, symbolClass);
674
        notifyObservers(REGISTER_SYMBOL, symbolName, symbolClass, shapeTypes);
675
    }
676 40435 jjdelcerro
677 42457 jjdelcerro
    public void saveSymbol(ISymbol symbol, String fileName, File folder,
678
            boolean overwrite) throws SymbolException {
679
        getSymbolManager().saveSymbol(symbol, fileName, folder, overwrite);
680
    }
681 40435 jjdelcerro
682 42457 jjdelcerro
    public void saveSymbol(ISymbol symbol, String fileName, File folder)
683
            throws SymbolException {
684
        getSymbolManager().saveSymbol(symbol, fileName, folder);
685
    }
686 40435 jjdelcerro
687 44831 jjdelcerro
    @Override
688
    public boolean registerLayer(
689 47739 fdiaz
        Class theClass,
690 44831 jjdelcerro
        Class<? extends FLayer> layerClass)
691
    {
692 47739 fdiaz
        this.layerClassFromStoreClass.put(theClass, layerClass);
693 44831 jjdelcerro
        return true;
694
    }
695 47740 jjdelcerro
696
    @Override
697
    public boolean registerLayer(Factory<FLayer> factory) {
698
        this.layerClassFromStoreClass.put(factory, null);
699
        return true;
700
    }
701 44831 jjdelcerro
702 47740 jjdelcerro
    private FLayer newLayer(DataStore dataStore) {
703
        for (Map.Entry<Object, Class<? extends FLayer>> entry : layerClassFromStoreClass.entrySet()) {
704
            Object  currentKey = entry.getKey();
705
            if( currentKey instanceof Factory ) {
706
                Factory<FLayer> factory = (Factory<FLayer>) currentKey;
707
                FLayer layer = factory.create(dataStore.getParameters(), dataStore);
708
                if( layer != null ) {
709
                    return layer;
710
                }
711
            }
712
        }
713
        return null;
714
    }
715
716 47739 fdiaz
    private Class<? extends FLayer> getLayerClass(
717
            Class keyClass) {
718
        Class<? extends FLayer> layerClass = this.layerClassFromStoreClass.get(keyClass);
719 44831 jjdelcerro
        if( layerClass!=null ) {
720
            return layerClass;
721
        }
722 47740 jjdelcerro
        for (Map.Entry<Object, Class<? extends FLayer>> entry : layerClassFromStoreClass.entrySet()) {
723
            Object  currentKey = entry.getKey();
724
            if( currentKey instanceof Class ) {
725
                Class  currentKeyClass = (Class) currentKey;
726
                layerClass = entry.getValue();
727
                if( currentKeyClass.isAssignableFrom(keyClass) ) {
728
                    return layerClass;
729
                }
730 44831 jjdelcerro
            }
731
        }
732
        return null;
733
    }
734
735
    @Override
736
    public FLayer createLayer(String layerName, DataStoreParameters dataParameters)
737 42457 jjdelcerro
            throws LoadLayerException {
738 44831 jjdelcerro
        try {
739
            DataManager dataManager = DALLocator.getDataManager();
740
            DataStore dataStore = dataManager.openStore(
741
                    dataParameters.getDataStoreName(),
742
                    dataParameters
743
            );
744
            return this.createLayer(layerName, dataStore, null);
745
        } catch (Exception e) {
746
            throw new LoadLayerException(layerName, e);
747
        }
748 42457 jjdelcerro
    }
749 40435 jjdelcerro
750 44831 jjdelcerro
    @Override
751
    public FLayer createLayer(String layerName, DataStore dataStore)
752 42457 jjdelcerro
            throws LoadLayerException {
753 44831 jjdelcerro
        return this.createLayer(layerName, dataStore, null);
754 42457 jjdelcerro
    }
755 40435 jjdelcerro
756 44831 jjdelcerro
757 44297 jjdelcerro
    @Override
758 44831 jjdelcerro
    public FLayer createLayer(String layerName, CreateLayerParameters parameters) throws LoadLayerException {
759
        try {
760
            DataManager dataManager = DALLocator.getDataManager();
761
            DataStoreParameters dataParameters = parameters.getDataParameters();
762 46361 jjdelcerro
            DataFactory sf = dataManager.getStoreFactory(dataParameters);
763
764
            if( sf==null
765
                    || !StringUtils.equalsIgnoreCase(sf.getName(), "FeatureStore")
766
                    || parameters.getCoordTrans()!=null
767
                    || parameters.useCache()
768
                    || parameters.isVisible()
769
                ) {
770
                DataStore dataStore = dataManager.openStore(
771
                        dataParameters.getProviderName(),
772
                        dataParameters
773
                );
774
                return this.createLayer(layerName, dataStore, parameters);
775
            }
776 47739 fdiaz
            Class<? extends FLayer> layerClass = this.getLayerClass(FeatureStore.class);
777 46361 jjdelcerro
            if (layerClass == null) {
778
                throw new CantRetrieveLayerByStoreException(layerName, FeatureStore.class.getName());
779
            }
780
            FLayer layer;
781
            try {
782
                layer = (FLayer) layerClass.newInstance();
783
            } catch (InstantiationException | IllegalAccessException e) {
784
                throw new LoadLayerException(layerName, e);
785
            }
786
787
            layer.setName(layerName);
788
            layer.setVisible(false);
789
            ((FLyrVect) layer).setDataStoreParameters(dataParameters);
790
            layer.load();
791
            return (FLayer) notifyObservers(CREATE_LAYER, layer).getValue();
792
793 44831 jjdelcerro
        } catch (Exception e) {
794
            throw new LoadLayerException(layerName, e);
795
        }
796
    }
797
798
    @Override
799
    public FLayer createLayer(String layerName, DataStore dataStore, CreateLayerParameters parameters) throws LoadLayerException {
800
        try {
801
            boolean useCache = false;
802
            ICoordTrans coordTrans = null;
803
            IProjection projection = (IProjection) dataStore.getDynValue(FeatureStore.METADATA_CRS);
804
805
            if( parameters!=null ) {
806
                if( parameters.getDataParameters()!=dataStore.getParameters() ) {
807
                    throw new IllegalArgumentException("The dataStore parameters are not the same of the store pased to create layer.");
808
                }
809
                useCache = parameters.useCache();
810
811
                coordTrans = parameters.getCoordTrans();
812
                // TODO: Aqui comprobacion de que si hay transformacion es adecuada
813
                // para la proyeccion del store.
814
            }
815
816
            if( useCache ) {
817
                DataStoreProviderFactory factory = dataStore.getProviderFactory();
818
                if( factory.isTiledSupported() != DataStoreProviderFactory.NO ) {
819
                    if( !factory.getClass().getSimpleName().equals("TileProviderFactory")) {
820
                        DataManager dataManager = DALLocator.getDataManager();
821
                        DynObject tileParameters = dataManager.createStoreParameters("TileCache");
822
                        File cacheFolder;
823
                        FoldersManager folderManager = ToolsLocator.getFoldersManager();
824
                        File applicationHome = folderManager.get("ApplicationHome");
825
                        if( applicationHome==null ) {
826
                            cacheFolder = folderManager.getTemporaryFile("gvsig_rcache");
827
                        } else {
828
                            cacheFolder = new File(applicationHome, "gvsig_rcache");
829
                        }
830
                        if (tileParameters.getDynClass().getDynField("rootFolder") != null) {
831
                            tileParameters.setDynValue("rootFolder", cacheFolder);
832
                        }
833
                        try {
834
                            dataStore.useCache("TileCache", tileParameters);
835
                        } catch (DataException e) {
836
                            LOGGER.warn("Can't cache the layer.", e);
837
                        }
838
                    }
839
                }
840
            }
841 47740 jjdelcerro
            FLayer layer = this.newLayer(dataStore);
842
            if( layer == null ) {
843
                Class<? extends FLayer> layerClass = this.getLayerClass(dataStore.getParameters().getClass());
844 47739 fdiaz
                if (layerClass == null) {
845 47740 jjdelcerro
                    layerClass = this.getLayerClass(dataStore.getProviderFactory().getClass());
846
                    if (layerClass == null) {
847
                        layerClass = this.getLayerClass(dataStore.getClass());
848
                    }
849
                    if (layerClass == null) {
850
                        throw new CantRetrieveLayerByStoreException(layerName, dataStore.getName());
851
                    }
852 47739 fdiaz
                }
853 47740 jjdelcerro
                try {
854
                    layer = (FLayer) layerClass.newInstance();
855
                } catch (InstantiationException | IllegalAccessException e) {
856
                    throw new LoadLayerException(layerName, e);
857 47739 fdiaz
                }
858 44831 jjdelcerro
            }
859
            layer.setName(layerName);
860
            ((SingleLayer) layer).setDataStore(dataStore);
861
            if (projection != null) {
862
                layer.setProjection(projection);
863
                if( coordTrans != null ) {
864
                    layer.setCoordTrans(coordTrans);
865
                }
866
            }
867
            layer.load();
868
            return (FLayer) notifyObservers(CREATE_LAYER, layer).getValue();
869
        } catch (Exception e) {
870
            throw new LoadLayerException(layerName, e);
871
        }
872
    }
873
874
    @Override
875 41302 jjdelcerro
    public ILegend getLegend(DataStore dataStore) {
876 41258 jjdelcerro
        ILegend legend = null;
877 46285 jjdelcerro
        ResourcesStorage resourcesStorage = null;
878
        try {
879
            resourcesStorage = dataStore.getResourcesStorage();
880
            if( resourcesStorage!=null ) {
881
                Resource resource = resourcesStorage.getResource(SymbolManager.LEGEND_FILE_EXTENSION.substring(1));
882
                try {
883
                    if ((resource != null) && (resource.exists())) {
884
                        PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
885
                        InputStream is = resource.asInputStream();
886
                        legend = (ILegend) persistenceManager.getObject(is);
887
                        is.close();
888
                    }
889
                } catch (Exception e) {
890
                    LOGGER.warn("Can't loasd legend", e);
891
                } finally {
892
                    IOUtils.closeQuietly(resource);
893 44297 jjdelcerro
                }
894 41258 jjdelcerro
            }
895 46285 jjdelcerro
        } finally {
896
            DisposeUtils.disposeQuietly(resourcesStorage);
897 40435 jjdelcerro
        }
898 41258 jjdelcerro
        //If the legend is null, next option is to check if the store has the getLegend method
899 42457 jjdelcerro
        if (legend == null) {
900 41258 jjdelcerro
            try {
901 44831 jjdelcerro
                legend = (ILegend) dataStore.invokeDynMethod("getLegend", null);
902 41258 jjdelcerro
            } catch (DynMethodNotSupportedException e) {
903 44297 jjdelcerro
                LOGGER.debug("This store {} does not provide a legend.",
904 41258 jjdelcerro
                        dataStore.getName());
905
            } catch (DynMethodException e) {
906 44831 jjdelcerro
                LOGGER.warn(
907 41258 jjdelcerro
                        "Can't load the specific legend provided for the store {}.",
908
                        dataStore.getName(), e);
909
            }
910
        }
911 40435 jjdelcerro
912 41258 jjdelcerro
        //If legend is null, last step is just try to create the legend by default
913 42457 jjdelcerro
        if (legend == null) {
914 44831 jjdelcerro
            if( dataStore instanceof FeatureStore ) {
915
                FeatureType featureType;
916
                try {
917
                    featureType = (((FeatureStore) dataStore).getDefaultFeatureType());
918
                    int indexGeom = featureType.getDefaultGeometryAttributeIndex();
919
                    if (indexGeom < 0) {
920
                        throw new IllegalArgumentException("The layer don't has a geometry column.");
921
                    }
922
                    int typeShape = featureType.getAttributeDescriptor(indexGeom).getGeometryType();
923
                    legend = createDefaultVectorLegend(typeShape);
924
                } catch (DataException e) {
925
                    LOGGER.warn("Error getting the default feature type", e);
926 41614 jjdelcerro
                }
927 44831 jjdelcerro
928
            } else if( dataStore instanceof RasterStore ) {
929
                RasterStore rasterStore = (RasterStore) dataStore;
930
                List<BandDescriptor> bands = rasterStore.getBandDescriptors();
931
                legend = createDefaultRasterLegend(bands);
932 41258 jjdelcerro
            }
933
        }
934 40435 jjdelcerro
935 44831 jjdelcerro
        if( legend instanceof RasterLegend ) {
936
            RasterLegend rasterLegend = (RasterLegend) legend;
937
            RasterStore rasterStore = (RasterStore) dataStore;
938
            ColorInterpretation colorInterpretation = rasterLegend.getColorInterpretation();
939
            if (colorInterpretation.isPalette()) {
940
                rasterLegend.addColorTableOperation(colorInterpretation, 0);
941
            } else {
942
                rasterLegend.addLinearStretchEnhancementOperationIfNeeded(
943
                        colorInterpretation,
944
                        rasterStore.getBandDescriptors(),
945
                        0);
946
            }
947
            if (colorInterpretation.hasAnyHSLBand()) {
948
                rasterLegend.addHSLToRGBOperation(colorInterpretation);
949
            } else if (colorInterpretation.hasAnyCMYKBand()) {
950
                rasterLegend.addCMYKToRGBOperation(colorInterpretation);
951
            } else if (colorInterpretation.hasAnyYCBCRBand()) {
952
              //TODO: Not implemented yet, meanwhile do nothing
953
    //                legend.addYCBCRToRGBOperation(colorInterpretation);
954
            }
955
        }
956 41258 jjdelcerro
        return legend;
957
    }
958 40435 jjdelcerro
959 44297 jjdelcerro
    @Override
960 40435 jjdelcerro
    public ILabelingStrategy getLabelingStrategy(DataStore dataStore) {
961
        ILabelingStrategy labelingStrategy = null;
962
963 44297 jjdelcerro
        ResourcesStorage resourcesStorage = dataStore.getResourcesStorage();
964
        if( resourcesStorage!=null ) {
965
            Resource resource = resourcesStorage.getResource(SymbolManager.LABELINGSTRATEGY_FILE_EXTENSION.substring(1));
966
            try {
967
                if ((resource != null) && (resource.exists())) {
968
                    PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
969
                    InputStream is = resource.asInputStream();
970
                    labelingStrategy = (ILabelingStrategy) persistenceManager.getObject(is);
971
                    is.close();
972
                }
973
            } catch (Exception e) {
974
                LOGGER.warn("Can't load Label strategy", e);
975
            } finally {
976
                IOUtils.closeQuietly(resource);
977 40435 jjdelcerro
            }
978
        }
979 42457 jjdelcerro
980 44297 jjdelcerro
        //If the legend is null, next option is to check if the store has the getLabeling method
981 42457 jjdelcerro
        if (labelingStrategy == null) {
982 40435 jjdelcerro
            try {
983 42457 jjdelcerro
                labelingStrategy
984
                        = (ILabelingStrategy) dataStore.invokeDynMethod("getLabeling",
985
                                null);
986 40435 jjdelcerro
            } catch (DynMethodNotSupportedException e1) {
987
                labelingStrategy = null;
988
            } catch (DynMethodException e1) {
989 44297 jjdelcerro
                LOGGER.error("Can't load the specific labeling strategy provided for the datastore {}.",
990 42457 jjdelcerro
                        dataStore.getName(),
991
                        e1);
992 40435 jjdelcerro
            }
993 42457 jjdelcerro
        }
994
995 40435 jjdelcerro
        return labelingStrategy;
996
    }
997 42457 jjdelcerro
998 41258 jjdelcerro
    private Object call(Object instance, String methodName, Class[] signature, Object[] params) {
999
        try {
1000
            Method method = instance.getClass().getMethod(methodName, signature);
1001 42457 jjdelcerro
            if (method == null) {
1002 41258 jjdelcerro
                return null;
1003
            }
1004
            Object value = method.invoke(instance, params);
1005
            return value;
1006 44831 jjdelcerro
        } catch (Exception ex) {
1007 41258 jjdelcerro
            return null;
1008
        }
1009
    }
1010
1011 44831 jjdelcerro
    @Override
1012 40435 jjdelcerro
    public void registerIconLayer(String storeProviderName, String iconName) {
1013 42457 jjdelcerro
        if (storeProviderName == null || iconName == null) {
1014 44297 jjdelcerro
            LOGGER.info("registerIconLayer, storeProviderName or iconName are null");
1015 42457 jjdelcerro
            return;
1016
        }
1017 41302 jjdelcerro
        String storeName = storeProviderName.trim().toLowerCase();
1018 42457 jjdelcerro
        if (storeName.length() == 0 || iconName.trim().length() == 0) {
1019 44297 jjdelcerro
            LOGGER.info("registerIconLayer, invalid storeProviderName or iconName");
1020 42457 jjdelcerro
            return;
1021
        }
1022
        iconLayers.put(storeName, iconName);
1023
        notifyObservers(REGISTER_ICON_LAYER, storeName, iconName);
1024 40435 jjdelcerro
    }
1025 42457 jjdelcerro
1026 43215 jjdelcerro
        @Override
1027 40435 jjdelcerro
    public String getIconLayer(DataStore store) {
1028 43753 fdiaz
        try {
1029
            return this.getIconLayer(store.getProviderName());
1030
        } catch (Throwable th){
1031
            return "layer-icon";
1032
        }
1033 43574 jjdelcerro
    }
1034
1035
    @Override
1036
    public String getIconLayer(String providerName) {
1037 43215 jjdelcerro
                String name = null;
1038
                try {
1039 43574 jjdelcerro
                        name = (String) iconLayers.get(providerName.trim().toLowerCase());
1040 43215 jjdelcerro
                } catch(Throwable th) {
1041
                        // Do nothing
1042
                }
1043
                if( StringUtils.isEmpty(name) ) {
1044
                        name = "layer-icon";
1045
                }
1046 42457 jjdelcerro
        return name;
1047 40435 jjdelcerro
    }
1048
1049
    /* (non-Javadoc)
1050
     * @see org.gvsig.fmap.mapcontext.MapContextManager#getDefaultCRS()
1051
     */
1052 44831 jjdelcerro
    @Override
1053 40435 jjdelcerro
    public IProjection getDefaultCRS() {
1054 41302 jjdelcerro
        IProjection crs = CRSFactory.getCRS("EPSG:4326");
1055 42457 jjdelcerro
        return (IProjection) notifyObservers(GET_DEFAULT_CRS, crs).getValue();
1056 40435 jjdelcerro
    }
1057 40927 jldominguez
1058 42457 jjdelcerro
    public Notification notifyLoadMapContext(MapContext mapContext) {
1059 41302 jjdelcerro
        return this.observableHelper.notifyObservers(this, LOAD_MAPCONTEXT, mapContext);
1060
    }
1061 42457 jjdelcerro
1062
    public Notification notifyLoadLayer(FLayer layer) {
1063 41302 jjdelcerro
        return this.observableHelper.notifyObservers(this, LOAD_LAYER, layer);
1064
    }
1065 42457 jjdelcerro
1066 44831 jjdelcerro
    @Override
1067 41302 jjdelcerro
    public void addObserver(Observer o) {
1068
        this.observableHelper.addObserver(o);
1069
    }
1070
1071 44831 jjdelcerro
    @Override
1072 41302 jjdelcerro
    public void deleteObserver(Observer o) {
1073
        this.observableHelper.deleteObserver(o);
1074
    }
1075
1076 44831 jjdelcerro
    @Override
1077 41302 jjdelcerro
    public void deleteObservers() {
1078
        this.observableHelper.deleteObservers();
1079
    }
1080
1081
    protected Notification notifyObservers(String type, Object value) {
1082 42457 jjdelcerro
        return this.observableHelper.notifyObservers(this, type, value);
1083 41302 jjdelcerro
    }
1084
1085
    protected Notification notifyObservers(String type, Object value1, Object value2) {
1086
        return this.observableHelper.notifyObservers(this, type, value1, value2);
1087
    }
1088
1089
    protected Notification notifyObservers(String type, Object value1, Object value2, Object value3) {
1090
        return this.observableHelper.notifyObservers(this, type, value1, value2, value3);
1091
    }
1092 41347 jjdelcerro
1093 44831 jjdelcerro
    @Override
1094 41347 jjdelcerro
    public File getColorTableLibraryFolder() {
1095
        if (this.colorTableLibraryFolder == null) {
1096
            // Provide a default value to the location for the color
1097
            // table library.
1098
            String colorTableLibraryPath = System.getProperty("user.home")
1099
                    + File.separator
1100
                    + "gvSIG"
1101
                    + File.separator
1102
                    + "colortable";
1103
            this.colorTableLibraryFolder = new File(colorTableLibraryPath);
1104
        }
1105
        return this.colorTableLibraryFolder;
1106
    }
1107
1108 44831 jjdelcerro
    @Override
1109 41347 jjdelcerro
    public void setColorTableLibraryFolder(File colorTableLibraryFolder) {
1110
        this.colorTableLibraryFolder = colorTableLibraryFolder;
1111
    }
1112 42457 jjdelcerro
1113 43141 jjdelcerro
    @Override
1114
    public LayerInformationBuilder createLayerInformationBuilder() {
1115
        return new DefaultLayerInformationBuilder();
1116
    }
1117
1118 45888 jjdelcerro
    @Override
1119
    public Icon getIcon(int geometryType, boolean withSelection) {
1120
        try {
1121
            String name;
1122
            switch (geometryType) {
1123
                case Geometry.TYPES.LINE:
1124
                    name = "layer-type-line";
1125
                    break;
1126
                case Geometry.TYPES.POINT:
1127
                    name = "layer-type-point";
1128
                    break;
1129
                case Geometry.TYPES.POLYGON:
1130
                    name = "layer-type-polygon";
1131
                    break;
1132
                case Geometry.TYPES.MULTILINE:
1133
                    name = "layer-type-multiline";
1134
                    break;
1135
                case Geometry.TYPES.MULTIPOINT:
1136
                    name = "layer-type-multipoint";
1137
                    break;
1138
                case Geometry.TYPES.MULTIPOLYGON:
1139
                    name = "layer-type-multipolygon";
1140
                    break;
1141
                case Geometry.TYPES.GEOMETRY:
1142
                    name = "layer-type-mix";
1143
                    break;
1144
                default:
1145
                    return null;
1146
            }
1147
            if (withSelection) {
1148
                name = name + "-sel";
1149
            }
1150
            name = name + ".png";
1151
            URL url = this.getClass().getResource("/layertypes/" + name);
1152
            if (url == null) {
1153
                return null;
1154
            }
1155
            Icon icon = new ImageIcon(url);
1156
            return icon;
1157
        } catch (Exception ex) {
1158
            return null;
1159
        }
1160
    }
1161 43753 fdiaz
1162 40435 jjdelcerro
}