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

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