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

History | View | Annotate | Download (28.7 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.mapcontext.impl;
25

    
26
import java.awt.Color;
27
import java.awt.Font;
28
import java.io.File;
29
import java.io.FileFilter;
30
import java.io.FileInputStream;
31
import java.io.FileNotFoundException;
32
import java.io.IOException;
33
import java.lang.reflect.InvocationTargetException;
34
import java.lang.reflect.Method;
35
import java.util.ArrayList;
36
import java.util.Collections;
37
import java.util.HashMap;
38
import java.util.Iterator;
39
import java.util.List;
40
import java.util.Map;
41

    
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
import org.gvsig.fmap.mapcontext.MapContext;
51
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
import org.gvsig.fmap.mapcontext.ViewPort;
57
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
58
import org.gvsig.fmap.mapcontext.layers.FLayer;
59
import org.gvsig.fmap.mapcontext.layers.LayerFactory;
60
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer;
61
import org.gvsig.fmap.mapcontext.layers.vectorial.impl.DefaultGraphicLayer;
62
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
63
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
64
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend;
65
import org.gvsig.fmap.mapcontext.rendering.legend.driver.ILegendReader;
66
import org.gvsig.fmap.mapcontext.rendering.legend.driver.ILegendWriter;
67
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
68
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
69
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
70
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol;
71
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolException;
72
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
73
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolPreferences;
74
import org.gvsig.tools.ToolsLocator;
75
import org.gvsig.tools.dynobject.exception.DynMethodException;
76
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException;
77
import org.gvsig.tools.observer.Notification;
78
import org.gvsig.tools.observer.ObservableHelper;
79
import org.gvsig.tools.observer.Observer;
80
import org.gvsig.tools.persistence.PersistenceManager;
81
import org.slf4j.Logger;
82
import org.slf4j.LoggerFactory;
83

    
84
/**
85
 * Default implementation of the {@link MapContextManager}.
86
 *
87
 * @author <a href="mailto:cordinyana@gvsig.org">C?sar Ordi?ana</a>
88
 */
89
public class DefaultMapContextManager implements MapContextManager {
90

    
91
    private static final Logger logger = LoggerFactory
92
        .getLogger(DefaultMapContextManager.class);
93

    
94
        private Class drawerClazz = DefaultMapContextDrawer.class;
95

    
96
        private Map legends = Collections.synchronizedMap(new HashMap());
97

    
98
        private Map legendReaders = Collections.synchronizedMap(new HashMap());
99

    
100
        private Map legendWriters = Collections.synchronizedMap(new HashMap());
101

    
102
        private String defaultVectorLegend;
103

    
104
        private ObservableHelper observableHelper = new ObservableHelper();
105

    
106
        private File colorTableLibraryFolder = null;
107
        
108
        public MapContext createMapContext() {
109
                MapContext mapcontext = new MapContext(new ViewPort());
110
                return (MapContext) notifyObservers(CREATE_MAPCONTEXT, mapcontext).getValue();
111
        }
112
        
113
        public SymbolManager getSymbolManager() {
114
                return MapContextLocator.getSymbolManager();
115
        }
116

    
117
        private SymbolPreferences getSymbolPreferences() {
118
                return getSymbolManager().getSymbolPreferences();
119
        }
120

    
121
        public String getSymbolLibraryPath() {
122
                return getSymbolPreferences().getSymbolLibraryPath();
123
        }
124

    
125
        public void setSymbolLibraryPath(String symbolLibraryPath) {
126
                getSymbolPreferences().setSymbolLibraryPath(symbolLibraryPath);
127
        }
128

    
129
        public void resetSymbolLibraryPath() {
130
                getSymbolPreferences().resetSymbolLibraryPath();
131
        }
132

    
133
        public Color getDefaultSymbolColor() {
134
                return getSymbolPreferences().getDefaultSymbolColor();
135
        }
136

    
137
        public Color getDefaultSymbolFillColor() {
138
                return getSymbolPreferences().getDefaultSymbolFillColor();
139
        }
140

    
141
        public Font getDefaultSymbolFont() {
142
                return getSymbolPreferences().getDefaultSymbolFont();
143
        }
144

    
145
        public String getSymbolFileExtension() {
146
                return getSymbolPreferences().getSymbolFileExtension();
147
        }
148

    
149
        public boolean isDefaultSymbolFillColorAleatory() {
150
                return getSymbolPreferences().isDefaultSymbolFillColorAleatory();
151
        }
152

    
153
        public void resetDefaultSymbolColor() {
154
                getSymbolPreferences().resetDefaultSymbolColor();
155
        }
156

    
157
        public void resetDefaultSymbolFillColor() {
158
                getSymbolPreferences().resetDefaultSymbolFillColor();
159
        }
160

    
161
        public void resetDefaultSymbolFillColorAleatory() {
162
                getSymbolPreferences().resetDefaultSymbolFillColorAleatory();
163
        }
164

    
165
        public void resetDefaultSymbolFont() {
166
                getSymbolPreferences().resetDefaultSymbolFont();
167
        }
168

    
169
        public void setDefaultSymbolColor(Color defaultSymbolColor) {
170
                getSymbolPreferences().setDefaultSymbolColor(defaultSymbolColor);
171
        }
172

    
173
        public void setDefaultSymbolFillColor(Color defaultSymbolFillColor) {
174
                getSymbolPreferences().setDefaultSymbolFillColor(defaultSymbolFillColor);
175
        }
176

    
177
        public void setDefaultSymbolFillColorAleatory(
178
                        boolean defaultSymbolFillColorAleatory) {
179
                getSymbolPreferences().setDefaultSymbolFillColorAleatory(
180
                                defaultSymbolFillColorAleatory);
181
        }
182

    
183
        public void setDefaultSymbolFont(Font defaultSymbolFont) {
184
                getSymbolPreferences().setDefaultSymbolFont(defaultSymbolFont);
185
        }
186

    
187
        public void setSymbolFileExtension(String extension) {
188
                getSymbolPreferences().setSymbolFileExtension(extension);
189
        }
190

    
191
        public int getDefaultCartographicSupportMeasureUnit() {
192
                return getSymbolPreferences().getDefaultCartographicSupportMeasureUnit();
193
        }
194

    
195
        public void setDefaultCartographicSupportMeasureUnit(
196
                        int defaultCartographicSupportMeasureUnit) {
197
                getSymbolPreferences().setDefaultCartographicSupportMeasureUnit(
198
                                defaultCartographicSupportMeasureUnit);
199
        }
200

    
201
        public int getDefaultCartographicSupportReferenceSystem() {
202
                return getSymbolPreferences().getDefaultCartographicSupportReferenceSystem();
203
        }
204

    
205
        public void setDefaultCartographicSupportReferenceSystem(
206
                        int defaultCartographicSupportReferenceSystem) {
207
                getSymbolPreferences().setDefaultCartographicSupportReferenceSystem(
208
                                defaultCartographicSupportReferenceSystem);
209
        }
210

    
211
        public MapContextDrawer createMapContextDrawerInstance(Class drawerClazz)
212
        throws MapContextException {
213
                return createMapContextDrawerInstance(drawerClazz, "NONE");
214
        }
215

    
216
        public MapContextDrawer createDefaultMapContextDrawerInstance()
217
        throws MapContextException {
218

    
219
                return createMapContextDrawerInstance(drawerClazz, "default");
220
        }
221

    
222
        private MapContextDrawer createMapContextDrawerInstance(Class drawerClazz,
223
                        String name) throws RegisteredClassInstantiationException {
224
                try {
225
                         MapContextDrawer drawer = (MapContextDrawer) drawerClazz.newInstance();
226
                         notifyObservers( CREATE_MAPCONTEXT_DRAWER, drawer);
227
                        return drawer;
228
                } catch (Exception ex) {
229
                        throw new RegisteredClassInstantiationException(
230
                                        MapContextDrawer.class, drawerClazz, name, ex);
231
                }
232
        }
233

    
234
        public void setDefaultMapContextDrawer(Class drawerClazz)
235
        throws MapContextException {
236

    
237
                validateMapContextDrawer(drawerClazz);
238
                this.drawerClazz = drawerClazz;
239
                notifyObservers( SET_MAPCONTEXT_DRAWER, drawerClazz);
240
        }
241

    
242
        public void validateMapContextDrawer(Class drawerClazz)
243
        throws MapContextException {
244
                if (!MapContextDrawer.class.isAssignableFrom(drawerClazz)) {
245
                        throw new InvalidRegisteredClassException(MapContextDrawer.class,
246
                                        drawerClazz, "UNKNOWN");
247
                }
248
        }
249

    
250
        public GraphicLayer createGraphicsLayer(IProjection projection) {
251
                DefaultGraphicLayer layer = new DefaultGraphicLayer();
252
                try {
253
                        layer.initialize(projection);        
254
                        layer.setLegend((IVectorLegend)createLegend(IVectorialUniqueValueLegend.LEGEND_NAME));
255
                } catch (Exception e) {
256
                        logger.error("Error initializing the graphics layer", e);
257
                }
258
                return (GraphicLayer) notifyObservers( CREATE_GRAPHICS_LAYER, layer).getValue();
259
        }
260

    
261
        public String getDefaultVectorLegend() {
262
                return defaultVectorLegend;
263
        }
264

    
265
        public void setDefaultVectorLegend(String defaultVectorLegend) {
266
                this.defaultVectorLegend = defaultVectorLegend;
267
        }
268

    
269
        public void registerLegend(String legendName, Class legendClass)
270
        throws MapContextRuntimeException {
271

    
272
                if (legendClass == null || !ILegend.class.isAssignableFrom(legendClass)) {
273
                        throw new InvalidRegisteredClassException(ILegend.class,
274
                                        legendClass, legendName);
275
                }
276

    
277
                legends.put(legendName, legendClass);
278
                notifyObservers( REGISTER_LEGEND, legendName, legendClass);
279
        }
280

    
281
        public ILegend createLegend(String legendName)
282
        throws MapContextRuntimeException {
283
                Class legendClass = (Class) legends.get(legendName);
284

    
285
                if (legendClass != null) {
286
                        try {
287
                                ILegend legend = (ILegend) legendClass.newInstance();
288
                                return (ILegend) notifyObservers( CREATE_LEGEND, legend).getValue();
289
                        } catch (InstantiationException e) {
290
                                throw new RegisteredClassInstantiationException(ILegend.class,
291
                                                legendClass, legendName, e);
292
                        } catch (IllegalAccessException e) {
293
                                throw new RegisteredClassInstantiationException(ILegend.class,
294
                                                legendClass, legendName, e);
295
                        }
296
                }
297
                return null;
298
        }
299

    
300
    public IVectorLegend createDefaultVectorLegend(int shapeType)
301
    throws MapContextRuntimeException {
302
        try {
303
            // Create legend
304
            IVectorLegend legend =
305
                (IVectorLegend) createLegend(getDefaultVectorLegend());
306
            if (legend == null) {
307
                return null;
308
            }
309
            // Set legend values
310
            legend.setShapeType(shapeType);
311
            ISymbol symbol = getSymbolManager().createSymbol(shapeType);
312
            if( symbol == null ) {
313
                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.";
314
                throw new RuntimeException(msg);
315
            }
316
            legend.setDefaultSymbol(symbol);
317
            return legend;
318
        } catch(Exception e) {
319
            throw new MapContextRuntimeException(e);
320
        }
321
    }
322

    
323
    // =============================================================
324
    // Legend reading/writing
325
    
326
        public void registerLegendReader(String format, Class readerClass)
327
        throws MapContextRuntimeException {
328
                if (readerClass == null
329
                                || !ILegendReader.class.isAssignableFrom(readerClass)) {
330
                        throw new InvalidRegisteredClassException(ILegendReader.class,
331
                                        readerClass, format);
332
                }
333

    
334
                legendReaders.put(format, readerClass);
335
                notifyObservers( REGISTER_LEGEND_READER, format, readerClass);
336
        }
337
        
338

    
339
        public ILegendReader createLegendReader(String format)
340
        throws MapContextRuntimeException {
341
                Class legendReaderClazz = (Class) legendReaders.get(format);
342

    
343
                if (legendReaderClazz != null) {
344
                        try {
345
                                ILegendReader reader = (ILegendReader) legendReaderClazz.newInstance();
346
                                return (ILegendReader) notifyObservers( CREATE_LEGEND_READER, reader).getValue();
347
                        } catch (InstantiationException e) {
348
                                throw new RegisteredClassInstantiationException(
349
                                                ILegendReader.class, legendReaderClazz, format, e);
350
                        } catch (IllegalAccessException e) {
351
                                throw new RegisteredClassInstantiationException(
352
                                                ILegendReader.class, legendReaderClazz, format, e);
353
                        }
354
                }
355
                return null;
356
        }
357

    
358
        public void registerLegendWriter(Class legendClass, String format,
359
                        Class writerClass) throws MapContextRuntimeException {
360
                if (writerClass == null
361
                                || !ILegendWriter.class.isAssignableFrom(writerClass)
362
                                || legendClass == null
363
                                || !ILegend.class.isAssignableFrom(legendClass)) {
364
                    
365
                        throw new InvalidRegisteredClassException(ILegendWriter.class,
366
                                        writerClass, format.concat("-").concat(
367
                                            legendClass == null ? "Null" : legendClass.getName()));
368
                }
369

    
370
                Map legendFormatWriters = (Map) legendWriters.get(format);
371

    
372
                synchronized (legendWriters) {
373
                        if (legendFormatWriters == null) {
374
                                legendFormatWriters = Collections
375
                                .synchronizedMap(new HashMap());
376
                                legendWriters.put(format, legendFormatWriters);
377
                        }
378
                }
379
                legendFormatWriters.put(legendClass, writerClass);
380
                notifyObservers( REGISTER_LEGEND_WRITER, format, writerClass);
381
        }
382

    
383
        public ILegendWriter createLegendWriter(Class legendClass, String format)
384
        throws MapContextRuntimeException {
385
            
386
            if (legendClass == null || format == null) {
387
                return null;
388
            }
389
            
390
                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
        Class citem = null;
439
            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
        return null;
456
    }
457

    
458
    private Map getLegendWritersForFormat(String format) {
459
                return (Map) legendWriters.get(format);
460
        }
461
        
462
    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

    
471
    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
        // =============================================================
480

    
481
        public IMultiLayerSymbol createMultiLayerSymbol(int shapeType)
482
        throws MapContextRuntimeException {
483
                IMultiLayerSymbol symbol = getSymbolManager().createMultiLayerSymbol(shapeType);
484
                return (IMultiLayerSymbol) notifyObservers( CREATE_SYMBOL, symbol).getValue();
485
        }
486

    
487
        public IMultiLayerSymbol createMultiLayerSymbol(String symbolName)
488
        throws MapContextRuntimeException {
489
                IMultiLayerSymbol symbol = getSymbolManager().createMultiLayerSymbol(symbolName);
490
                return (IMultiLayerSymbol) notifyObservers( CREATE_SYMBOL, symbol).getValue();
491
        }
492

    
493
        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

    
499
        public ISymbol createSymbol(int shapeType)
500
        throws MapContextRuntimeException {
501
                ISymbol symbol = getSymbolManager().createSymbol(shapeType);
502
                return (ISymbol) notifyObservers( CREATE_SYMBOL, symbol).getValue();
503
        }
504

    
505
        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

    
511
        public ISymbol createSymbol(String symbolName)
512
        throws MapContextRuntimeException {
513
                ISymbol symbol = getSymbolManager().createSymbol(symbolName);
514
                return (ISymbol) notifyObservers( CREATE_SYMBOL, symbol).getValue();
515
        }
516

    
517
        public IWarningSymbol getWarningSymbol(String message, String symbolDesc,
518
                        int symbolDrawExceptionType) throws MapContextRuntimeException {
519
                return getSymbolManager().getWarningSymbol(message, symbolDesc,
520
                                symbolDrawExceptionType);
521
        }
522

    
523
        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

    
529
        public ISymbol[] loadSymbols(File folder) throws SymbolException {
530
                ISymbol[] symbols = getSymbolManager().loadSymbols(folder);
531
                return (ISymbol[]) notifyObservers( LOAD_SYMBOLS, symbols).getValue();
532
        }
533

    
534
        public void registerMultiLayerSymbol(String symbolName, Class symbolClass)
535
        throws MapContextRuntimeException {
536
                getSymbolManager().registerMultiLayerSymbol(symbolName, symbolClass);
537
                notifyObservers( REGISTER_MULTILAYER_SYMBOL, symbolName, symbolClass);
538
        }
539

    
540
        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

    
547
        public void registerSymbol(String symbolName, Class symbolClass)
548
        throws MapContextRuntimeException {
549
                getSymbolManager().registerSymbol(symbolName, symbolClass);
550
                notifyObservers( REGISTER_SYMBOL, symbolName, symbolClass);
551
        }
552

    
553
        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

    
559
        public void saveSymbol(ISymbol symbol, String fileName, File folder,
560
                        boolean overwrite) throws SymbolException {
561
                getSymbolManager().saveSymbol(symbol, fileName, folder, overwrite);
562
        }
563

    
564
        public void saveSymbol(ISymbol symbol, String fileName, File folder)
565
        throws SymbolException {
566
                getSymbolManager().saveSymbol(symbol, fileName, folder);
567
        }
568

    
569
        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

    
575
        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

    
581
    public ILegend getLegend(DataStore dataStore) {
582
        ILegend legend = null;
583

    
584
        File file = getResourcePath(dataStore, SymbolManager.LEGEND_FILE_EXTENSION.substring(1));
585
        try {
586
            if ( (file != null) && (file.exists()) ) {
587
                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
        }
597

    
598
        //If the legend is null, next option is to check if the store has the getLegend method
599
        if ( legend == null ) {
600
            try {
601
                legend = (IVectorLegend) dataStore.invokeDynMethod("getLegend", null);
602
            } catch (DynMethodNotSupportedException e) {
603
                logger.debug("This store {} does not provide a legend.",
604
                        dataStore.getName());
605
            } catch (DynMethodException e) {
606
                logger.error(
607
                        "Can't load the specific legend provided for the store {}.",
608
                        dataStore.getName(), e);
609
            }
610
        }
611

    
612
        //If legend is null, last step is just try to create the legend by default
613
        if ( legend == null ) {
614
            FeatureType featureType;
615
            try {
616
                featureType = (((FeatureStore) dataStore).getDefaultFeatureType());
617
                int indexGeom = featureType.getDefaultGeometryAttributeIndex();
618
                int typeShape = featureType.getAttributeDescriptor(indexGeom).getGeometryType();
619
                legend = createDefaultVectorLegend(typeShape);
620
            } catch (DataException e) {
621
                logger.error("Error getting the default feature type", e);
622
            }
623
        }
624

    
625
        return legend;
626
    }
627

    
628
    public ILabelingStrategy getLabelingStrategy(DataStore dataStore) {
629
        ILabelingStrategy labelingStrategy = null;
630

    
631
        File file = getResourcePath(dataStore, SymbolManager.LABELINGSTRATEGY_FILE_EXTENSION.substring(1));
632
        try{
633
            if ((file != null) && (file.exists())){
634
                PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
635
                FileInputStream is = new FileInputStream(file);
636
                labelingStrategy = (ILabelingStrategy) persistenceManager.getObject(is);
637
                is.close();
638
            }
639
        } catch (FileNotFoundException e){
640
            logger.error("Label strategy not found", e);
641
        } catch (IOException e) {
642
            logger.error("Error reading the labeling strategy", e);
643
        }
644
        
645
        //If the legend is null, next option is to check if the store has the getLegend method
646
        if (labelingStrategy == null){
647
            try {
648
                labelingStrategy =
649
                    (ILabelingStrategy) dataStore.invokeDynMethod("getLabeling",
650
                        null);
651
            } catch (DynMethodNotSupportedException e1) {
652
                labelingStrategy = null;
653
            } catch (DynMethodException e1) {
654
                logger.error("Can't load the specific lebeling strategy provided for the datastore {}.",
655
                    dataStore.getName(),
656
                    e1);
657
            }
658
        }        
659
        
660
        return labelingStrategy;
661
    }
662
    
663
    private Object call(Object instance, String methodName, Class[] signature, Object[] params) {
664
        try {
665
            Method method = instance.getClass().getMethod(methodName, signature);
666
            if( method == null ) {
667
                return null;
668
            }
669
            Object value = method.invoke(instance, params);
670
            return value;
671
        } catch (NoSuchMethodException ex) {
672
            return null;
673
        } catch (SecurityException ex) {
674
            return null;
675
        } catch (IllegalAccessException ex) {
676
            return null;
677
        } catch (IllegalArgumentException ex) {
678
            return null;
679
        } catch (InvocationTargetException ex) {
680
            return null;
681
        }
682
    }
683

    
684
    private File getResourcePath(DataStore dataStore, String resource){
685
        //Loading the file from a store based on file
686
        DataServerExplorer explorer = null;
687
        try {
688
            explorer = dataStore.getExplorer();
689
            if( explorer==null ) {
690
                return null;
691
            }
692
            return (File)call(explorer,"getResourcePath",
693
                    new Class[] {DataStore.class,String.class},
694
                    new Object[] {dataStore,resource});
695
        } catch (Exception e) {
696
            logger.warn(
697
                "Can't locate a specific legend provided by the explorer "
698
                + explorer, e);
699
            return null;
700
        } finally {
701
            if( explorer!=null ) {
702
                explorer.dispose();
703
                explorer = null;
704
            }
705
        }
706
    }
707

    
708
    private Map iconLayers = new HashMap(); //  (Map<String storeProviderName, String iconName>)
709
    
710
    public void registerIconLayer(String storeProviderName, String iconName) {
711
            if( storeProviderName == null || iconName == null ) {
712
                    logger.info("registerIconLayer, storeProviderName or iconName are null");
713
                    return;
714
            }
715
        String storeName = storeProviderName.trim().toLowerCase();
716
            if( storeName.length()==0 || iconName.trim().length()==0 ) {
717
                    logger.info("registerIconLayer, invalid storeProviderName or iconName");
718
                    return;
719
            }
720
            iconLayers.put(storeName, iconName);
721
        notifyObservers( REGISTER_ICON_LAYER, storeName, iconName);
722
    }
723
    
724
    public String getIconLayer(DataStore store) {
725
            String name = (String) iconLayers.get(store.getProviderName().trim().toLowerCase());
726
            if( name == null ) {
727
                    return "layer-icon";
728
            }
729
            return name;
730
    }
731

    
732
    /* (non-Javadoc)
733
     * @see org.gvsig.fmap.mapcontext.MapContextManager#getDefaultCRS()
734
     */
735
    public IProjection getDefaultCRS() {
736
        IProjection crs = CRSFactory.getCRS("EPSG:4326");
737
        return (IProjection) notifyObservers( GET_DEFAULT_CRS, crs).getValue();
738
    }
739

    
740
    public Notification notifyLoadMapContext(MapContext mapContext) {    
741
        return this.observableHelper.notifyObservers(this, LOAD_MAPCONTEXT, mapContext);
742
    }
743
    
744
    public Notification notifyLoadLayer(FLayer layer) {    
745
        return this.observableHelper.notifyObservers(this, LOAD_LAYER, layer);
746
    }
747
    
748
    public void addObserver(Observer o) {
749
        this.observableHelper.addObserver(o);
750
    }
751

    
752
    public void deleteObserver(Observer o) {
753
        this.observableHelper.deleteObserver(o);
754
    }
755

    
756
    public void deleteObservers() {
757
        this.observableHelper.deleteObservers();
758
    }
759

    
760
    protected Notification notifyObservers(String type, Object value) {
761
        return this.observableHelper.notifyObservers(this, type, value);  
762
    }
763

    
764
    protected Notification notifyObservers(String type, Object value1, Object value2) {
765
        return this.observableHelper.notifyObservers(this, type, value1, value2);
766
    }
767

    
768
    protected Notification notifyObservers(String type, Object value1, Object value2, Object value3) {
769
        return this.observableHelper.notifyObservers(this, type, value1, value2, value3);
770
    }
771

    
772
    public File getColorTableLibraryFolder() {
773
        if (this.colorTableLibraryFolder == null) {
774
            // Provide a default value to the location for the color
775
            // table library.
776
            String colorTableLibraryPath = System.getProperty("user.home")
777
                    + File.separator
778
                    + "gvSIG"
779
                    + File.separator
780
                    + "colortable";
781
            this.colorTableLibraryFolder = new File(colorTableLibraryPath);
782
        }
783
        return this.colorTableLibraryFolder;
784
    }
785

    
786
    public void setColorTableLibraryFolder(File colorTableLibraryFolder) {
787
        this.colorTableLibraryFolder = colorTableLibraryFolder;
788
    }
789
    
790
    
791
}