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

History | View | Annotate | Download (30.9 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
import java.io.FileInputStream;
30
import java.io.FileNotFoundException;
31
import java.io.IOException;
32 41258 jjdelcerro
import java.lang.reflect.InvocationTargetException;
33 40435 jjdelcerro
import java.lang.reflect.Method;
34 40927 jldominguez
import java.util.ArrayList;
35 40435 jjdelcerro
import java.util.Collections;
36
import java.util.HashMap;
37 40927 jldominguez
import java.util.Iterator;
38
import java.util.List;
39 40435 jjdelcerro
import java.util.Map;
40 43215 jjdelcerro
import org.apache.commons.lang3.StringUtils;
41 40435 jjdelcerro
42
import org.cresques.cts.IProjection;
43
import org.gvsig.fmap.crs.CRSFactory;
44
import org.gvsig.fmap.dal.DataServerExplorer;
45
import org.gvsig.fmap.dal.DataStore;
46
import org.gvsig.fmap.dal.DataStoreParameters;
47
import org.gvsig.fmap.dal.exception.DataException;
48
import org.gvsig.fmap.dal.feature.FeatureStore;
49
import org.gvsig.fmap.dal.feature.FeatureType;
50 41141 jjdelcerro
import org.gvsig.fmap.mapcontext.MapContext;
51 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.MapContextDrawer;
52
import org.gvsig.fmap.mapcontext.MapContextException;
53
import org.gvsig.fmap.mapcontext.MapContextLocator;
54
import org.gvsig.fmap.mapcontext.MapContextManager;
55
import org.gvsig.fmap.mapcontext.MapContextRuntimeException;
56 41141 jjdelcerro
import org.gvsig.fmap.mapcontext.ViewPort;
57 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
58 43141 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.DefaultLayerInformationBuilder;
59 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.FLayer;
60
import org.gvsig.fmap.mapcontext.layers.LayerFactory;
61 43141 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.LayerInformationBuilder;
62 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer;
63
import org.gvsig.fmap.mapcontext.layers.vectorial.impl.DefaultGraphicLayer;
64
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
65
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
66
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend;
67
import org.gvsig.fmap.mapcontext.rendering.legend.driver.ILegendReader;
68
import org.gvsig.fmap.mapcontext.rendering.legend.driver.ILegendWriter;
69
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
70
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
71
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
72
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol;
73
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolException;
74
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
75
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolPreferences;
76
import org.gvsig.tools.ToolsLocator;
77 42879 jjdelcerro
import org.gvsig.tools.dispose.DisposeUtils;
78 40435 jjdelcerro
import org.gvsig.tools.dynobject.exception.DynMethodException;
79
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException;
80 41302 jjdelcerro
import org.gvsig.tools.observer.Notification;
81
import org.gvsig.tools.observer.ObservableHelper;
82
import org.gvsig.tools.observer.Observer;
83 40435 jjdelcerro
import org.gvsig.tools.persistence.PersistenceManager;
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
    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 42457 jjdelcerro
    private Map legends = Collections.synchronizedMap(new HashMap());
100 40435 jjdelcerro
101 42457 jjdelcerro
    private Map legendReaders = Collections.synchronizedMap(new HashMap());
102 40435 jjdelcerro
103 42457 jjdelcerro
    private Map legendWriters = Collections.synchronizedMap(new HashMap());
104 40435 jjdelcerro
105 42457 jjdelcerro
    private String defaultVectorLegend;
106 40435 jjdelcerro
107 42457 jjdelcerro
    private 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
            logger.error("Error initializing the graphics layer", e);
260
        }
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 42457 jjdelcerro
    public void registerLegendReader(String format, Class readerClass)
329
            throws MapContextRuntimeException {
330
        if (readerClass == null
331
                || !ILegendReader.class.isAssignableFrom(readerClass)) {
332
            throw new InvalidRegisteredClassException(ILegendReader.class,
333
                    readerClass, format);
334
        }
335 40435 jjdelcerro
336 42457 jjdelcerro
        legendReaders.put(format, readerClass);
337
        notifyObservers(REGISTER_LEGEND_READER, format, readerClass);
338
    }
339 40435 jjdelcerro
340 42457 jjdelcerro
    public ILegendReader createLegendReader(String format)
341
            throws MapContextRuntimeException {
342
        Class legendReaderClazz = (Class) legendReaders.get(format);
343 40435 jjdelcerro
344 42457 jjdelcerro
        if (legendReaderClazz != null) {
345
            try {
346
                ILegendReader reader = (ILegendReader) legendReaderClazz.newInstance();
347
                return (ILegendReader) notifyObservers(CREATE_LEGEND_READER, reader).getValue();
348
            } catch (InstantiationException e) {
349
                throw new RegisteredClassInstantiationException(
350
                        ILegendReader.class, legendReaderClazz, format, e);
351
            } catch (IllegalAccessException e) {
352
                throw new RegisteredClassInstantiationException(
353
                        ILegendReader.class, legendReaderClazz, format, e);
354
            }
355
        }
356
        return null;
357
    }
358 40435 jjdelcerro
359 42457 jjdelcerro
    public void registerLegendWriter(Class legendClass, String format,
360
            Class writerClass) throws MapContextRuntimeException {
361
        if (writerClass == null
362
                || !ILegendWriter.class.isAssignableFrom(writerClass)
363
                || legendClass == null
364
                || !ILegend.class.isAssignableFrom(legendClass)) {
365 40435 jjdelcerro
366 42457 jjdelcerro
            throw new InvalidRegisteredClassException(ILegendWriter.class,
367
                    writerClass, format.concat("-").concat(
368
                            legendClass == null ? "Null" : legendClass.getName()));
369
        }
370 40435 jjdelcerro
371 42457 jjdelcerro
        Map legendFormatWriters = (Map) legendWriters.get(format);
372 40435 jjdelcerro
373 42457 jjdelcerro
        synchronized (legendWriters) {
374
            if (legendFormatWriters == null) {
375
                legendFormatWriters = Collections.synchronizedMap(new HashMap());
376
                legendWriters.put(format, legendFormatWriters);
377
            }
378
        }
379
        legendFormatWriters.put(legendClass, writerClass);
380
        notifyObservers(REGISTER_LEGEND_WRITER, format, writerClass);
381
    }
382 40435 jjdelcerro
383 42457 jjdelcerro
    public ILegendWriter createLegendWriter(Class legendClass, String format)
384
            throws MapContextRuntimeException {
385 40435 jjdelcerro
386 42457 jjdelcerro
        if (legendClass == null || format == null) {
387
            return null;
388
        }
389 40435 jjdelcerro
390 42457 jjdelcerro
        Map legendFormatWriters = getLegendWritersForFormat(format);
391
392
        if (legendFormatWriters != null) {
393
            Class legendWriterClazz = (Class) legendFormatWriters
394
                    .get(legendClass);
395
396
            if (legendWriterClazz != null) {
397
                /*
398
                 * Found exact match
399
                 */
400
                try {
401
                    ILegendWriter writer = (ILegendWriter) legendWriterClazz.newInstance();
402
                    return (ILegendWriter) notifyObservers(CREATE_LEGEND_READER, writer).getValue();
403
                } catch (InstantiationException e) {
404
                    throw new RegisteredClassInstantiationException(
405
                            ILegendWriter.class, legendWriterClazz, format
406
                            .concat("-").concat(
407
                                    legendClass == null ? "Null" : legendClass.getName()), e);
408
                } catch (IllegalAccessException e) {
409
                    throw new RegisteredClassInstantiationException(
410
                            ILegendWriter.class, legendWriterClazz, format
411
                            .concat("-").concat(
412
                                    legendClass == null ? "Null" : legendClass.getName()), e);
413
                }
414
            } else {
415
                /*
416
                 * Trying to find superclass/superinterface of parameter
417
                 */
418
                try {
419
                    return getSuperClassLegendWriter(legendFormatWriters, legendClass);
420
                } catch (Exception exc) {
421
                    throw new MapContextRuntimeException(exc);
422
                }
423
            }
424
        }
425
        return null;
426
    }
427
428
    private ILegendWriter getSuperClassLegendWriter(Map clsToWtr, Class legclass)
429
            throws Exception {
430
431
        if (!ILegend.class.isAssignableFrom(legclass)) {
432
            // Class is not a legend
433
            return null;
434
        }
435
436
        Iterator kiter = clsToWtr.keySet().iterator();
437
        Object oitem = null;
438 40927 jldominguez
        Class citem = null;
439 42457 jjdelcerro
        while (kiter.hasNext()) {
440
            oitem = kiter.next();
441
            if (oitem instanceof Class) {
442
                citem = (Class) oitem;
443
                if (citem.isAssignableFrom(legclass)) {
444
                    /*
445
                     * Found superclass/superinterface
446
                     */
447
                    citem = (Class) clsToWtr.get(oitem);
448
                    return (ILegendWriter) citem.newInstance();
449
                }
450
            }
451
        }
452
        /*
453
         * No superclass/superinterface found
454
         */
455 40927 jldominguez
        return null;
456
    }
457
458
    private Map getLegendWritersForFormat(String format) {
459 42457 jjdelcerro
        return (Map) legendWriters.get(format);
460
    }
461
462 40927 jldominguez
    public List getLegendReadingFormats() {
463
        List resp = new ArrayList();
464
        Iterator iter = legendReaders.keySet().iterator();
465
        while (iter.hasNext()) {
466
            resp.add(iter.next());
467
        }
468
        return resp;
469
    }
470 40435 jjdelcerro
471 40927 jldominguez
    public List getLegendWritingFormats() {
472
        List resp = new ArrayList();
473
        Iterator iter = legendWriters.keySet().iterator();
474
        while (iter.hasNext()) {
475
            resp.add(iter.next());
476
        }
477
        return resp;
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 41302 jjdelcerro
    public ILegend getLegend(DataStore dataStore) {
582 41258 jjdelcerro
        ILegend legend = null;
583 40435 jjdelcerro
584 41258 jjdelcerro
        File file = getResourcePath(dataStore, SymbolManager.LEGEND_FILE_EXTENSION.substring(1));
585
        try {
586 42457 jjdelcerro
            if ((file != null) && (file.exists())) {
587 41258 jjdelcerro
                PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
588
                FileInputStream is = new FileInputStream(file);
589
                legend = (ILegend) persistenceManager.getObject(is);
590
                is.close();
591
            }
592
        } catch (FileNotFoundException e) {
593
            logger.error("Legend not found", e);
594
        } catch (IOException e) {
595
            logger.error("Error reading the legend", e);
596 40435 jjdelcerro
        }
597 41258 jjdelcerro
598
        //If the legend is null, next option is to check if the store has the getLegend method
599 42457 jjdelcerro
        if (legend == null) {
600 41258 jjdelcerro
            try {
601
                legend = (IVectorLegend) dataStore.invokeDynMethod("getLegend", null);
602
            } catch (DynMethodNotSupportedException e) {
603 40435 jjdelcerro
                logger.debug("This store {} does not provide a legend.",
604 41258 jjdelcerro
                        dataStore.getName());
605
            } catch (DynMethodException e) {
606 40435 jjdelcerro
                logger.error(
607 41258 jjdelcerro
                        "Can't load the specific legend provided for the store {}.",
608
                        dataStore.getName(), e);
609
            }
610
        }
611 40435 jjdelcerro
612 41258 jjdelcerro
        //If legend is null, last step is just try to create the legend by default
613 42457 jjdelcerro
        if (legend == null) {
614 41258 jjdelcerro
            FeatureType featureType;
615
            try {
616
                featureType = (((FeatureStore) dataStore).getDefaultFeatureType());
617
                int indexGeom = featureType.getDefaultGeometryAttributeIndex();
618 42457 jjdelcerro
                if (indexGeom < 0) {
619 41614 jjdelcerro
                    throw new IllegalArgumentException("The layer don't has a geometry column.");
620
                }
621 41258 jjdelcerro
                int typeShape = featureType.getAttributeDescriptor(indexGeom).getGeometryType();
622
                legend = createDefaultVectorLegend(typeShape);
623
            } catch (DataException e) {
624
                logger.error("Error getting the default feature type", e);
625
            }
626
        }
627 40435 jjdelcerro
628 41258 jjdelcerro
        return legend;
629
    }
630 40435 jjdelcerro
631
    public ILabelingStrategy getLabelingStrategy(DataStore dataStore) {
632
        ILabelingStrategy labelingStrategy = null;
633
634 41258 jjdelcerro
        File file = getResourcePath(dataStore, SymbolManager.LABELINGSTRATEGY_FILE_EXTENSION.substring(1));
635 42457 jjdelcerro
        try {
636
            if ((file != null) && (file.exists())) {
637 40435 jjdelcerro
                PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
638
                FileInputStream is = new FileInputStream(file);
639
                labelingStrategy = (ILabelingStrategy) persistenceManager.getObject(is);
640
                is.close();
641
            }
642 42457 jjdelcerro
        } catch (FileNotFoundException e) {
643 40435 jjdelcerro
            logger.error("Label strategy not found", e);
644
        } catch (IOException e) {
645
            logger.error("Error reading the labeling strategy", e);
646
        }
647 42457 jjdelcerro
648 40435 jjdelcerro
        //If the legend is null, next option is to check if the store has the getLegend method
649 42457 jjdelcerro
        if (labelingStrategy == null) {
650 40435 jjdelcerro
            try {
651 42457 jjdelcerro
                labelingStrategy
652
                        = (ILabelingStrategy) dataStore.invokeDynMethod("getLabeling",
653
                                null);
654 40435 jjdelcerro
            } catch (DynMethodNotSupportedException e1) {
655
                labelingStrategy = null;
656
            } catch (DynMethodException e1) {
657
                logger.error("Can't load the specific lebeling strategy provided for the datastore {}.",
658 42457 jjdelcerro
                        dataStore.getName(),
659
                        e1);
660 40435 jjdelcerro
            }
661 42457 jjdelcerro
        }
662
663 40435 jjdelcerro
        return labelingStrategy;
664
    }
665 42457 jjdelcerro
666 41258 jjdelcerro
    private Object call(Object instance, String methodName, Class[] signature, Object[] params) {
667
        try {
668
            Method method = instance.getClass().getMethod(methodName, signature);
669 42457 jjdelcerro
            if (method == null) {
670 41258 jjdelcerro
                return null;
671
            }
672
            Object value = method.invoke(instance, params);
673
            return value;
674
        } catch (NoSuchMethodException ex) {
675
            return null;
676
        } catch (SecurityException ex) {
677
            return null;
678
        } catch (IllegalAccessException ex) {
679
            return null;
680
        } catch (IllegalArgumentException ex) {
681
            return null;
682
        } catch (InvocationTargetException ex) {
683
            return null;
684
        }
685
    }
686
687 42457 jjdelcerro
    private File getResourcePath(DataStore dataStore, String resource) {
688 40435 jjdelcerro
        //Loading the file from a store based on file
689 41258 jjdelcerro
        DataServerExplorer explorer = null;
690 40435 jjdelcerro
        try {
691 41258 jjdelcerro
            explorer = dataStore.getExplorer();
692 42457 jjdelcerro
            if (explorer == null) {
693 41258 jjdelcerro
                return null;
694 40435 jjdelcerro
            }
695 42879 jjdelcerro
            return explorer.getResourcePath(dataStore, resource);
696 40435 jjdelcerro
        } catch (Exception e) {
697 41258 jjdelcerro
            logger.warn(
698 42457 jjdelcerro
                    "Can't locate a specific legend provided by the explorer "
699
                    + explorer, e);
700 41258 jjdelcerro
            return null;
701
        } finally {
702 42879 jjdelcerro
            DisposeUtils.disposeQuietly(explorer);
703 41258 jjdelcerro
        }
704 40435 jjdelcerro
    }
705
706
    private Map iconLayers = new HashMap(); //  (Map<String storeProviderName, String iconName>)
707 42457 jjdelcerro
708 40435 jjdelcerro
    public void registerIconLayer(String storeProviderName, String iconName) {
709 42457 jjdelcerro
        if (storeProviderName == null || iconName == null) {
710
            logger.info("registerIconLayer, storeProviderName or iconName are null");
711
            return;
712
        }
713 41302 jjdelcerro
        String storeName = storeProviderName.trim().toLowerCase();
714 42457 jjdelcerro
        if (storeName.length() == 0 || iconName.trim().length() == 0) {
715
            logger.info("registerIconLayer, invalid storeProviderName or iconName");
716
            return;
717
        }
718
        iconLayers.put(storeName, iconName);
719
        notifyObservers(REGISTER_ICON_LAYER, storeName, iconName);
720 40435 jjdelcerro
    }
721 42457 jjdelcerro
722 43215 jjdelcerro
        @Override
723 40435 jjdelcerro
    public String getIconLayer(DataStore store) {
724 43574 jjdelcerro
        return this.getIconLayer(store.getProviderName());
725
    }
726
727
    @Override
728
    public String getIconLayer(String providerName) {
729 43215 jjdelcerro
                String name = null;
730
                try {
731 43574 jjdelcerro
                        name = (String) iconLayers.get(providerName.trim().toLowerCase());
732 43215 jjdelcerro
                } catch(Throwable th) {
733
                        // Do nothing
734
                }
735
                if( StringUtils.isEmpty(name) ) {
736
                        name = "layer-icon";
737
                }
738 42457 jjdelcerro
        return name;
739 40435 jjdelcerro
    }
740
741
    /* (non-Javadoc)
742
     * @see org.gvsig.fmap.mapcontext.MapContextManager#getDefaultCRS()
743
     */
744
    public IProjection getDefaultCRS() {
745 41302 jjdelcerro
        IProjection crs = CRSFactory.getCRS("EPSG:4326");
746 42457 jjdelcerro
        return (IProjection) notifyObservers(GET_DEFAULT_CRS, crs).getValue();
747 40435 jjdelcerro
    }
748 40927 jldominguez
749 42457 jjdelcerro
    public Notification notifyLoadMapContext(MapContext mapContext) {
750 41302 jjdelcerro
        return this.observableHelper.notifyObservers(this, LOAD_MAPCONTEXT, mapContext);
751
    }
752 42457 jjdelcerro
753
    public Notification notifyLoadLayer(FLayer layer) {
754 41302 jjdelcerro
        return this.observableHelper.notifyObservers(this, LOAD_LAYER, layer);
755
    }
756 42457 jjdelcerro
757 41302 jjdelcerro
    public void addObserver(Observer o) {
758
        this.observableHelper.addObserver(o);
759
    }
760
761
    public void deleteObserver(Observer o) {
762
        this.observableHelper.deleteObserver(o);
763
    }
764
765
    public void deleteObservers() {
766
        this.observableHelper.deleteObservers();
767
    }
768
769
    protected Notification notifyObservers(String type, Object value) {
770 42457 jjdelcerro
        return this.observableHelper.notifyObservers(this, type, value);
771 41302 jjdelcerro
    }
772
773
    protected Notification notifyObservers(String type, Object value1, Object value2) {
774
        return this.observableHelper.notifyObservers(this, type, value1, value2);
775
    }
776
777
    protected Notification notifyObservers(String type, Object value1, Object value2, Object value3) {
778
        return this.observableHelper.notifyObservers(this, type, value1, value2, value3);
779
    }
780 41347 jjdelcerro
781
    public File getColorTableLibraryFolder() {
782
        if (this.colorTableLibraryFolder == null) {
783
            // Provide a default value to the location for the color
784
            // table library.
785
            String colorTableLibraryPath = System.getProperty("user.home")
786
                    + File.separator
787
                    + "gvSIG"
788
                    + File.separator
789
                    + "colortable";
790
            this.colorTableLibraryFolder = new File(colorTableLibraryPath);
791
        }
792
        return this.colorTableLibraryFolder;
793
    }
794
795
    public void setColorTableLibraryFolder(File colorTableLibraryFolder) {
796
        this.colorTableLibraryFolder = colorTableLibraryFolder;
797
    }
798 42457 jjdelcerro
799 43141 jjdelcerro
    @Override
800
    public LayerInformationBuilder createLayerInformationBuilder() {
801
        return new DefaultLayerInformationBuilder();
802
    }
803
804
805 40435 jjdelcerro
}