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

History | View | Annotate | Download (23 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.Method;
34
import java.util.ArrayList;
35
import java.util.Collections;
36
import java.util.HashMap;
37
import java.util.Iterator;
38
import java.util.List;
39
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
import org.gvsig.fmap.mapcontext.MapContext;
50
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
import org.gvsig.fmap.mapcontext.ViewPort;
56
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
import org.gvsig.tools.persistence.PersistenceManager;
77
import org.slf4j.Logger;
78
import org.slf4j.LoggerFactory;
79

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

    
87
    private static final Logger logger = LoggerFactory
88
        .getLogger(DefaultMapContextManager.class);
89

    
90
        private Class drawerClazz = DefaultMapContextDrawer.class;
91

    
92
        private Map legends = Collections.synchronizedMap(new HashMap());
93

    
94
        private Map legendReaders = Collections.synchronizedMap(new HashMap());
95

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

    
98
        private String defaultVectorLegend;
99

    
100
        public MapContext createMapContext() {
101
                return new MapContext(new ViewPort());
102
        }
103
        
104
        public SymbolManager getSymbolManager() {
105
                return MapContextLocator.getSymbolManager();
106
        }
107

    
108
        private SymbolPreferences getSymbolPreferences() {
109
                return getSymbolManager().getSymbolPreferences();
110
        }
111

    
112
        public String getSymbolLibraryPath() {
113
                return getSymbolPreferences().getSymbolLibraryPath();
114
        }
115

    
116
        public void setSymbolLibraryPath(String symbolLibraryPath) {
117
                getSymbolPreferences().setSymbolLibraryPath(symbolLibraryPath);
118
        }
119

    
120
        public void resetSymbolLibraryPath() {
121
                getSymbolPreferences().resetSymbolLibraryPath();
122
        }
123

    
124
        public Color getDefaultSymbolColor() {
125
                return getSymbolPreferences().getDefaultSymbolColor();
126
        }
127

    
128
        public Color getDefaultSymbolFillColor() {
129
                return getSymbolPreferences().getDefaultSymbolFillColor();
130
        }
131

    
132
        public Font getDefaultSymbolFont() {
133
                return getSymbolPreferences().getDefaultSymbolFont();
134
        }
135

    
136
        public String getSymbolFileExtension() {
137
                return getSymbolPreferences().getSymbolFileExtension();
138
        }
139

    
140
        public boolean isDefaultSymbolFillColorAleatory() {
141
                return getSymbolPreferences().isDefaultSymbolFillColorAleatory();
142
        }
143

    
144
        public void resetDefaultSymbolColor() {
145
                getSymbolPreferences().resetDefaultSymbolColor();
146
        }
147

    
148
        public void resetDefaultSymbolFillColor() {
149
                getSymbolPreferences().resetDefaultSymbolFillColor();
150
        }
151

    
152
        public void resetDefaultSymbolFillColorAleatory() {
153
                getSymbolPreferences().resetDefaultSymbolFillColorAleatory();
154
        }
155

    
156
        public void resetDefaultSymbolFont() {
157
                getSymbolPreferences().resetDefaultSymbolFont();
158
        }
159

    
160
        public void setDefaultSymbolColor(Color defaultSymbolColor) {
161
                getSymbolPreferences().setDefaultSymbolColor(defaultSymbolColor);
162
        }
163

    
164
        public void setDefaultSymbolFillColor(Color defaultSymbolFillColor) {
165
                getSymbolPreferences().setDefaultSymbolFillColor(defaultSymbolFillColor);
166
        }
167

    
168
        public void setDefaultSymbolFillColorAleatory(
169
                        boolean defaultSymbolFillColorAleatory) {
170
                getSymbolPreferences().setDefaultSymbolFillColorAleatory(
171
                                defaultSymbolFillColorAleatory);
172
        }
173

    
174
        public void setDefaultSymbolFont(Font defaultSymbolFont) {
175
                getSymbolPreferences().setDefaultSymbolFont(defaultSymbolFont);
176
        }
177

    
178
        public void setSymbolFileExtension(String extension) {
179
                getSymbolPreferences().setSymbolFileExtension(extension);
180
        }
181

    
182
        public int getDefaultCartographicSupportMeasureUnit() {
183
                return getSymbolPreferences().getDefaultCartographicSupportMeasureUnit();
184
        }
185

    
186
        public void setDefaultCartographicSupportMeasureUnit(
187
                        int defaultCartographicSupportMeasureUnit) {
188
                getSymbolPreferences().setDefaultCartographicSupportMeasureUnit(
189
                                defaultCartographicSupportMeasureUnit);
190
        }
191

    
192
        public int getDefaultCartographicSupportReferenceSystem() {
193
                return getSymbolPreferences().getDefaultCartographicSupportReferenceSystem();
194
        }
195

    
196
        public void setDefaultCartographicSupportReferenceSystem(
197
                        int defaultCartographicSupportReferenceSystem) {
198
                getSymbolPreferences().setDefaultCartographicSupportReferenceSystem(
199
                                defaultCartographicSupportReferenceSystem);
200
        }
201

    
202
        public MapContextDrawer createMapContextDrawerInstance(Class drawerClazz)
203
        throws MapContextException {
204
                return createMapContextDrawerInstance(drawerClazz, "NONE");
205
        }
206

    
207
        public MapContextDrawer createDefaultMapContextDrawerInstance()
208
        throws MapContextException {
209

    
210
                return createMapContextDrawerInstance(drawerClazz, "default");
211
        }
212

    
213
        private MapContextDrawer createMapContextDrawerInstance(Class drawerClazz,
214
                        String name) throws RegisteredClassInstantiationException {
215
                try {
216
                        return (MapContextDrawer) drawerClazz.newInstance();
217
                } catch (Exception ex) {
218
                        throw new RegisteredClassInstantiationException(
219
                                        MapContextDrawer.class, drawerClazz, name, ex);
220
                }
221
        }
222

    
223
        public void setDefaultMapContextDrawer(Class drawerClazz)
224
        throws MapContextException {
225

    
226
                validateMapContextDrawer(drawerClazz);
227
                this.drawerClazz = drawerClazz;
228
        }
229

    
230
        public void validateMapContextDrawer(Class drawerClazz)
231
        throws MapContextException {
232
                if (!MapContextDrawer.class.isAssignableFrom(drawerClazz)) {
233
                        throw new InvalidRegisteredClassException(MapContextDrawer.class,
234
                                        drawerClazz, "UNKNOWN");
235
                }
236
        }
237

    
238
        public GraphicLayer createGraphicsLayer(IProjection projection) {
239
                DefaultGraphicLayer layer = new DefaultGraphicLayer();
240
                try {
241
                        layer.initialize(projection);        
242
                        layer.setLegend((IVectorLegend)createLegend(IVectorialUniqueValueLegend.LEGEND_NAME));
243
                } catch (Exception e) {
244
                        logger.error("Error initializing the graphics layer", e);
245
                }
246
                return layer;
247
        }
248

    
249
        public String getDefaultVectorLegend() {
250
                return defaultVectorLegend;
251
        }
252

    
253
        public void setDefaultVectorLegend(String defaultVectorLegend) {
254
                this.defaultVectorLegend = defaultVectorLegend;
255
        }
256

    
257
        public void registerLegend(String legendName, Class legendClass)
258
        throws MapContextRuntimeException {
259

    
260
                if (legendClass == null || !ILegend.class.isAssignableFrom(legendClass)) {
261
                        throw new InvalidRegisteredClassException(ILegend.class,
262
                                        legendClass, legendName);
263
                }
264

    
265
                legends.put(legendName, legendClass);
266
        }
267

    
268
        public ILegend createLegend(String legendName)
269
        throws MapContextRuntimeException {
270
                Class legendClass = (Class) legends.get(legendName);
271

    
272
                if (legendClass != null) {
273
                        try {
274
                                return (ILegend) legendClass.newInstance();
275
                        } catch (InstantiationException e) {
276
                                throw new RegisteredClassInstantiationException(ILegend.class,
277
                                                legendClass, legendName, e);
278
                        } catch (IllegalAccessException e) {
279
                                throw new RegisteredClassInstantiationException(ILegend.class,
280
                                                legendClass, legendName, e);
281
                        }
282
                }
283
                return null;
284
        }
285

    
286
    public IVectorLegend createDefaultVectorLegend(int shapeType)
287
    throws MapContextRuntimeException {
288
        try {
289
            // Create legend
290
            IVectorLegend legend =
291
                (IVectorLegend) createLegend(getDefaultVectorLegend());
292
            if (legend == null) {
293
                return null;
294
            }
295
            // Set legend values
296
            legend.setShapeType(shapeType);
297
            ISymbol symbol = getSymbolManager().createSymbol(shapeType);
298
            if( symbol == null ) {
299
                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.";
300
                throw new RuntimeException(msg);
301
            }
302
            legend.setDefaultSymbol(symbol);
303
            return legend;
304
        } catch(Exception e) {
305
            throw new MapContextRuntimeException(e);
306
        }
307
    }
308

    
309
    // =============================================================
310
    // Legend reading/writing
311
    
312
        public void registerLegendReader(String format, Class readerClass)
313
        throws MapContextRuntimeException {
314
                if (readerClass == null
315
                                || !ILegendReader.class.isAssignableFrom(readerClass)) {
316
                        throw new InvalidRegisteredClassException(ILegendReader.class,
317
                                        readerClass, format);
318
                }
319

    
320
                legendReaders.put(format, readerClass);
321
        }
322
        
323

    
324
        public ILegendReader createLegendReader(String format)
325
        throws MapContextRuntimeException {
326
                Class legendReaderClazz = (Class) legendReaders.get(format);
327

    
328
                if (legendReaderClazz != null) {
329
                        try {
330
                                return (ILegendReader) legendReaderClazz.newInstance();
331
                        } catch (InstantiationException e) {
332
                                throw new RegisteredClassInstantiationException(
333
                                                ILegendReader.class, legendReaderClazz, format, e);
334
                        } catch (IllegalAccessException e) {
335
                                throw new RegisteredClassInstantiationException(
336
                                                ILegendReader.class, legendReaderClazz, format, e);
337
                        }
338
                }
339
                return null;
340
        }
341

    
342
        public void registerLegendWriter(Class legendClass, String format,
343
                        Class writerClass) throws MapContextRuntimeException {
344
                if (writerClass == null
345
                                || !ILegendWriter.class.isAssignableFrom(writerClass)
346
                                || legendClass == null
347
                                || !ILegend.class.isAssignableFrom(legendClass)) {
348
                    
349
                        throw new InvalidRegisteredClassException(ILegendWriter.class,
350
                                        writerClass, format.concat("-").concat(
351
                                            legendClass == null ? "Null" : legendClass.getName()));
352
                }
353

    
354
                Map legendFormatWriters = (Map) legendWriters.get(format);
355

    
356
                synchronized (legendWriters) {
357
                        if (legendFormatWriters == null) {
358
                                legendFormatWriters = Collections
359
                                .synchronizedMap(new HashMap());
360
                                legendWriters.put(format, legendFormatWriters);
361
                        }
362
                }
363
                legendFormatWriters.put(legendClass, writerClass);
364
        }
365

    
366
        public ILegendWriter createLegendWriter(Class legendClass, String format)
367
        throws MapContextRuntimeException {
368
            
369
            if (legendClass == null || format == null) {
370
                return null;
371
            }
372
            
373
                Map legendFormatWriters = getLegendWritersForFormat(format);
374

    
375
                if (legendFormatWriters != null) {
376
                        Class legendWriterClazz = (Class) legendFormatWriters
377
                        .get(legendClass);
378

    
379
                        if (legendWriterClazz != null) {
380
                            /*
381
                             * Found exact match
382
                             */
383
                                try {
384
                                        return (ILegendWriter) legendWriterClazz.newInstance();
385
                                } catch (InstantiationException e) {
386
                                        throw new RegisteredClassInstantiationException(
387
                                                        ILegendWriter.class, legendWriterClazz, format
388
                                                        .concat("-").concat(
389
                                                            legendClass == null ? "Null" : legendClass.getName()), e);
390
                                } catch (IllegalAccessException e) {
391
                                        throw new RegisteredClassInstantiationException(
392
                                                        ILegendWriter.class, legendWriterClazz, format
393
                                                        .concat("-").concat(
394
                                                            legendClass == null ? "Null" : legendClass.getName()), e);
395
                                }
396
                        } else {
397
                            /*
398
                             * Trying to find superclass/superinterface of parameter
399
                             */
400
                            try {
401
                                return getSuperClassLegendWriter(legendFormatWriters, legendClass);
402
                            } catch (Exception exc) {
403
                                throw new MapContextRuntimeException(exc);
404
                            }
405
                        }
406
                }
407
                return null;
408
        }
409

    
410
        private ILegendWriter getSuperClassLegendWriter(Map clsToWtr, Class legclass)
411
        throws Exception {
412
            
413
            if (!ILegend.class.isAssignableFrom(legclass)) {
414
                // Class is not a legend
415
                return null;
416
            }
417
            
418
            Iterator kiter = clsToWtr.keySet().iterator();
419
            Object oitem = null;
420
        Class citem = null;
421
            while (kiter.hasNext()) {
422
                oitem = kiter.next();
423
                if (oitem instanceof Class) {
424
                    citem = (Class) oitem; 
425
                    if (citem.isAssignableFrom(legclass)) {
426
                        /*
427
                         * Found superclass/superinterface
428
                         */
429
                        citem = (Class) clsToWtr.get(oitem);
430
                        return (ILegendWriter) citem.newInstance();
431
                    }
432
                }
433
            }
434
            /*
435
             * No superclass/superinterface found
436
             */
437
        return null;
438
    }
439

    
440
    private Map getLegendWritersForFormat(String format) {
441
                return (Map) legendWriters.get(format);
442
        }
443
        
444
    public List getLegendReadingFormats() {
445
        List resp = new ArrayList();
446
        Iterator iter = legendReaders.keySet().iterator();
447
        while (iter.hasNext()) {
448
            resp.add(iter.next());
449
        }
450
        return resp;
451
    }
452

    
453
    public List getLegendWritingFormats() {
454
        List resp = new ArrayList();
455
        Iterator iter = legendWriters.keySet().iterator();
456
        while (iter.hasNext()) {
457
            resp.add(iter.next());
458
        }
459
        return resp;
460
    }
461
        // =============================================================
462

    
463
        public IMultiLayerSymbol createMultiLayerSymbol(int shapeType)
464
        throws MapContextRuntimeException {
465
                return getSymbolManager().createMultiLayerSymbol(shapeType);
466
        }
467

    
468
        public IMultiLayerSymbol createMultiLayerSymbol(String symbolName)
469
        throws MapContextRuntimeException {
470
                return getSymbolManager().createMultiLayerSymbol(symbolName);
471
        }
472

    
473
        public ISymbol createSymbol(int shapeType, Color color)
474
        throws MapContextRuntimeException {
475
                return getSymbolManager().createSymbol(shapeType, color);
476
        }
477

    
478
        public ISymbol createSymbol(int shapeType)
479
        throws MapContextRuntimeException {
480
                return getSymbolManager().createSymbol(shapeType);
481
        }
482

    
483
        public ISymbol createSymbol(String symbolName, Color color)
484
        throws MapContextRuntimeException {
485
                return getSymbolManager().createSymbol(symbolName, color);
486
        }
487

    
488
        public ISymbol createSymbol(String symbolName)
489
        throws MapContextRuntimeException {
490
                return getSymbolManager().createSymbol(symbolName);
491
        }
492

    
493
        public IWarningSymbol getWarningSymbol(String message, String symbolDesc,
494
                        int symbolDrawExceptionType) throws MapContextRuntimeException {
495
                return getSymbolManager().getWarningSymbol(message, symbolDesc,
496
                                symbolDrawExceptionType);
497
        }
498

    
499
        public ISymbol[] loadSymbols(File folder, FileFilter filter)
500
        throws SymbolException {
501
                return getSymbolManager().loadSymbols(folder, filter);
502
        }
503

    
504
        public ISymbol[] loadSymbols(File folder) throws SymbolException {
505
                return getSymbolManager().loadSymbols(folder);
506
        }
507

    
508
        public void registerMultiLayerSymbol(String symbolName, Class symbolClass)
509
        throws MapContextRuntimeException {
510
                getSymbolManager().registerMultiLayerSymbol(symbolName, symbolClass);
511
        }
512

    
513
        public void registerMultiLayerSymbol(String symbolName, int[] shapeTypes,
514
                        Class symbolClass) throws MapContextRuntimeException {
515
                getSymbolManager().registerMultiLayerSymbol(symbolName, shapeTypes,
516
                                symbolClass);
517
        }
518

    
519
        public void registerSymbol(String symbolName, Class symbolClass)
520
        throws MapContextRuntimeException {
521
                getSymbolManager().registerSymbol(symbolName, symbolClass);
522
        }
523

    
524
        public void registerSymbol(String symbolName, int[] shapeTypes,
525
                        Class symbolClass) throws MapContextException {
526
                getSymbolManager().registerSymbol(symbolName, shapeTypes, symbolClass);
527
        }
528

    
529
        public void saveSymbol(ISymbol symbol, String fileName, File folder,
530
                        boolean overwrite) throws SymbolException {
531
                getSymbolManager().saveSymbol(symbol, fileName, folder, overwrite);
532
        }
533

    
534
        public void saveSymbol(ISymbol symbol, String fileName, File folder)
535
        throws SymbolException {
536
                getSymbolManager().saveSymbol(symbol, fileName, folder);
537
        }
538

    
539
        public FLayer createLayer(String layerName, DataStoreParameters parameters)
540
        throws LoadLayerException {
541
                return LayerFactory.getInstance().createLayer(layerName, parameters);
542
        }
543

    
544
        public FLayer createLayer(String layerName, DataStore store)
545
        throws LoadLayerException {
546
                return LayerFactory.getInstance().createLayer(layerName, store);
547
        }
548

    
549
        public ILegend getLegend(DataStore dataStore) {
550
                ILegend legend = null;
551

    
552
                File file = getResourcePathByReflection(dataStore, SymbolManager.LEGEND_FILE_EXTENSION.substring(1));
553
                try{
554
                if ((file != null) && (file.exists())){
555
                        PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
556
                        FileInputStream is = new FileInputStream(file);
557
                        legend = (ILegend) persistenceManager.getObject(is);
558
                        is.close();
559
                }
560
                } catch (FileNotFoundException e){
561
                    logger.error("Legend not found", e);
562
                } catch (IOException e) {
563
                    logger.error("Error reading the legend", e);
564
        }
565
                
566
                //If the legend is null, next option is to check if the store has the getLegend method
567
                if (legend == null){
568
                        try {
569
                                legend = (IVectorLegend) dataStore.invokeDynMethod(
570
                                                "getLegend", null);
571
                        } catch (DynMethodNotSupportedException e) {
572
                logger.debug("This store {} does not provide a legend.",
573
                    dataStore.getName());
574
                        } catch (DynMethodException e) {
575
                logger.error(
576
                    "Can't load the specific legend provided for the store {}.",
577
                    dataStore.getName(), e);
578
                        }
579
                }
580

    
581
                //If legend is null, last step is just try to create the legend by default
582
                if( legend == null ) {
583
                        FeatureType featureType;
584
                        try {
585
                                featureType = (((FeatureStore)dataStore).getDefaultFeatureType());
586
                                int indexGeom = featureType.getDefaultGeometryAttributeIndex();
587
                                int typeShape = featureType.getAttributeDescriptor(indexGeom).getGeometryType();
588
                                legend = createDefaultVectorLegend(typeShape);
589
                        } catch (DataException e) {
590
                                logger.error("Error getting the default feature type", e);
591
                        }                        
592
                }
593

    
594
                return legend;
595
        }
596

    
597
    public ILabelingStrategy getLabelingStrategy(DataStore dataStore) {
598
        ILabelingStrategy labelingStrategy = null;
599

    
600
        File file = getResourcePathByReflection(dataStore, SymbolManager.LABELINGSTRATEGY_FILE_EXTENSION.substring(1));
601
        try{
602
            if ((file != null) && (file.exists())){
603
                PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
604
                FileInputStream is = new FileInputStream(file);
605
                labelingStrategy = (ILabelingStrategy) persistenceManager.getObject(is);
606
                is.close();
607
            }
608
        } catch (FileNotFoundException e){
609
            logger.error("Label strategy not found", e);
610
        } catch (IOException e) {
611
            logger.error("Error reading the labeling strategy", e);
612
        }
613
        
614
        //If the legend is null, next option is to check if the store has the getLegend method
615
        if (labelingStrategy == null){
616
            try {
617
                labelingStrategy =
618
                    (ILabelingStrategy) dataStore.invokeDynMethod("getLabeling",
619
                        null);
620
            } catch (DynMethodNotSupportedException e1) {
621
                labelingStrategy = null;
622
            } catch (DynMethodException e1) {
623
                logger.error("Can't load the specific lebeling strategy provided for the datastore {}.",
624
                    dataStore.getName(),
625
                    e1);
626
            }
627
        }        
628
        
629
        return labelingStrategy;
630
    }
631
    
632
    private File getResourcePathByReflection(DataStore dataStore, String resource){
633
        //Loading the file from a store based on file
634
        DataServerExplorer dataServerExplorer = null;
635
        try {
636
            dataServerExplorer = dataStore.getExplorer();
637
            Class[] args = new Class[2];
638
            args[0] = DataStore.class;
639
            args[1] = String.class;
640
            Method method = dataStore.getExplorer().getClass().getMethod("getResourcePath", args);
641
            if (method != null){
642
                Object[] params = new Object[2];
643
                params[0] = dataStore;
644
                params[1] = resource;
645
                return (File)method.invoke(dataServerExplorer, params);
646
            }
647
        } catch (Exception e) {
648
            logger.debug(
649
                "Can't load the specific legend provided by the explorer "
650
                + dataServerExplorer, e);
651
        } 
652
        return null;
653
    }
654

    
655
    private Map iconLayers = new HashMap(); //  (Map<String storeProviderName, String iconName>)
656
    
657
    public void registerIconLayer(String storeProviderName, String iconName) {
658
            if( storeProviderName == null || iconName == null ) {
659
                    logger.info("registerIconLayer, storeProviderName or iconName are null");
660
                    return;
661
            }
662
            if( storeProviderName.trim().length()==0 || iconName.trim().length()==0 ) {
663
                    logger.info("registerIconLayer, invalid storeProviderName or iconName");
664
                    return;
665
            }
666
            iconLayers.put(storeProviderName.trim().toLowerCase(), iconName);
667
    }
668
    
669
    public String getIconLayer(DataStore store) {
670
            String name = (String) iconLayers.get(store.getProviderName().trim().toLowerCase());
671
            if( name == null ) {
672
                    return "layer-icon";
673
            }
674
            return name;
675
    }
676

    
677
    /* (non-Javadoc)
678
     * @see org.gvsig.fmap.mapcontext.MapContextManager#getDefaultCRS()
679
     */
680
    public IProjection getDefaultCRS() {
681
        return CRSFactory.getCRS("EPSG:4326");
682
    }
683

    
684
}