Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / impl / DefaultMapContextManager.java @ 38443

History | View | Annotate | Download (19.2 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2009 {DiSiD Technologies}  {Create Manager to register MapContextDrawer implementation}
26
 */
27
package org.gvsig.fmap.mapcontext.impl;
28

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

    
41
import org.cresques.cts.IProjection;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44

    
45
import org.gvsig.fmap.dal.DataServerExplorer;
46
import org.gvsig.fmap.dal.DataStore;
47
import org.gvsig.fmap.dal.DataStoreParameters;
48
import org.gvsig.fmap.dal.exception.DataException;
49
import org.gvsig.fmap.dal.feature.FeatureStore;
50
import org.gvsig.fmap.dal.feature.FeatureType;
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.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

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

    
85
    private static final Logger LOG = LoggerFactory
86
        .getLogger(DefaultMapContextManager.class);
87

    
88
        private Class drawerClazz = DefaultMapContextDrawer.class;
89

    
90
        private Map legends = Collections.synchronizedMap(new HashMap());
91

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

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

    
96
        private String defaultVectorLegend;
97

    
98
        public SymbolManager getSymbolManager() {
99
                return MapContextLocator.getSymbolManager();
100
        }
101

    
102
        private SymbolPreferences getSymbolPreferences() {
103
                return getSymbolManager().getSymbolPreferences();
104
        }
105

    
106
        public String getSymbolLibraryPath() {
107
                return getSymbolPreferences().getSymbolLibraryPath();
108
        }
109

    
110
        public void setSymbolLibraryPath(String symbolLibraryPath) {
111
                getSymbolPreferences().setSymbolLibraryPath(symbolLibraryPath);
112
        }
113

    
114
        public void resetSymbolLibraryPath() {
115
                getSymbolPreferences().resetSymbolLibraryPath();
116
        }
117

    
118
        public Color getDefaultSymbolColor() {
119
                return getSymbolPreferences().getDefaultSymbolColor();
120
        }
121

    
122
        public Color getDefaultSymbolFillColor() {
123
                return getSymbolPreferences().getDefaultSymbolFillColor();
124
        }
125

    
126
        public Font getDefaultSymbolFont() {
127
                return getSymbolPreferences().getDefaultSymbolFont();
128
        }
129

    
130
        public String getSymbolFileExtension() {
131
                return getSymbolPreferences().getSymbolFileExtension();
132
        }
133

    
134
        public boolean isDefaultSymbolFillColorAleatory() {
135
                return getSymbolPreferences().isDefaultSymbolFillColorAleatory();
136
        }
137

    
138
        public void resetDefaultSymbolColor() {
139
                getSymbolPreferences().resetDefaultSymbolColor();
140
        }
141

    
142
        public void resetDefaultSymbolFillColor() {
143
                getSymbolPreferences().resetDefaultSymbolFillColor();
144
        }
145

    
146
        public void resetDefaultSymbolFillColorAleatory() {
147
                getSymbolPreferences().resetDefaultSymbolFillColorAleatory();
148
        }
149

    
150
        public void resetDefaultSymbolFont() {
151
                getSymbolPreferences().resetDefaultSymbolFont();
152
        }
153

    
154
        public void setDefaultSymbolColor(Color defaultSymbolColor) {
155
                getSymbolPreferences().setDefaultSymbolColor(defaultSymbolColor);
156
        }
157

    
158
        public void setDefaultSymbolFillColor(Color defaultSymbolFillColor) {
159
                getSymbolPreferences().setDefaultSymbolFillColor(defaultSymbolFillColor);
160
        }
161

    
162
        public void setDefaultSymbolFillColorAleatory(
163
                        boolean defaultSymbolFillColorAleatory) {
164
                getSymbolPreferences().setDefaultSymbolFillColorAleatory(
165
                                defaultSymbolFillColorAleatory);
166
        }
167

    
168
        public void setDefaultSymbolFont(Font defaultSymbolFont) {
169
                getSymbolPreferences().setDefaultSymbolFont(defaultSymbolFont);
170
        }
171

    
172
        public void setSymbolFileExtension(String extension) {
173
                getSymbolPreferences().setSymbolFileExtension(extension);
174
        }
175

    
176
        public int getDefaultCartographicSupportMeasureUnit() {
177
                return getSymbolPreferences().getDefaultCartographicSupportMeasureUnit();
178
        }
179

    
180
        public void setDefaultCartographicSupportMeasureUnit(
181
                        int defaultCartographicSupportMeasureUnit) {
182
                getSymbolPreferences().setDefaultCartographicSupportMeasureUnit(
183
                                defaultCartographicSupportMeasureUnit);
184
        }
185

    
186
        public int getDefaultCartographicSupportReferenceSystem() {
187
                return getSymbolPreferences().getDefaultCartographicSupportReferenceSystem();
188
        }
189

    
190
        public void setDefaultCartographicSupportReferenceSystem(
191
                        int defaultCartographicSupportReferenceSystem) {
192
                getSymbolPreferences().setDefaultCartographicSupportReferenceSystem(
193
                                defaultCartographicSupportReferenceSystem);
194
        }
195

    
196
        public MapContextDrawer createMapContextDrawerInstance(Class drawerClazz)
197
        throws MapContextException {
198
                return createMapContextDrawerInstance(drawerClazz, "NONE");
199
        }
200

    
201
        public MapContextDrawer createDefaultMapContextDrawerInstance()
202
        throws MapContextException {
203

    
204
                return createMapContextDrawerInstance(drawerClazz, "default");
205
        }
206

    
207
        private MapContextDrawer createMapContextDrawerInstance(Class drawerClazz,
208
                        String name) throws RegisteredClassInstantiationException {
209
                try {
210
                        return (MapContextDrawer) drawerClazz.newInstance();
211
                } catch (Exception ex) {
212
                        throw new RegisteredClassInstantiationException(
213
                                        MapContextDrawer.class, drawerClazz, name, ex);
214
                }
215
        }
216

    
217
        public void setDefaultMapContextDrawer(Class drawerClazz)
218
        throws MapContextException {
219

    
220
                validateMapContextDrawer(drawerClazz);
221
                this.drawerClazz = drawerClazz;
222
        }
223

    
224
        public void validateMapContextDrawer(Class drawerClazz)
225
        throws MapContextException {
226
                if (!MapContextDrawer.class.isAssignableFrom(drawerClazz)) {
227
                        throw new InvalidRegisteredClassException(MapContextDrawer.class,
228
                                        drawerClazz, "UNKNOWN");
229
                }
230
        }
231

    
232
        public GraphicLayer createGraphicsLayer(IProjection projection) {
233
                DefaultGraphicLayer layer = new DefaultGraphicLayer();
234
                try {
235
                        layer.initialize(projection);        
236
                        layer.setLegend((IVectorLegend)createLegend(IVectorialUniqueValueLegend.LEGEND_NAME));
237
                } catch (Exception e) {
238
                        LOG.error("Error initializing the graphics layer", e);
239
                }
240
                return layer;
241
        }
242

    
243
        public String getDefaultVectorLegend() {
244
                return defaultVectorLegend;
245
        }
246

    
247
        public void setDefaultVectorLegend(String defaultVectorLegend) {
248
                this.defaultVectorLegend = defaultVectorLegend;
249
        }
250

    
251
        public void registerLegend(String legendName, Class legendClass)
252
        throws MapContextRuntimeException {
253

    
254
                if (legendClass == null || !ILegend.class.isAssignableFrom(legendClass)) {
255
                        throw new InvalidRegisteredClassException(ILegend.class,
256
                                        legendClass, legendName);
257
                }
258

    
259
                legends.put(legendName, legendClass);
260
        }
261

    
262
        public ILegend createLegend(String legendName)
263
        throws MapContextRuntimeException {
264
                Class legendClass = (Class) legends.get(legendName);
265

    
266
                if (legendClass != null) {
267
                        try {
268
                                return (ILegend) legendClass.newInstance();
269
                        } catch (InstantiationException e) {
270
                                throw new RegisteredClassInstantiationException(ILegend.class,
271
                                                legendClass, legendName, e);
272
                        } catch (IllegalAccessException e) {
273
                                throw new RegisteredClassInstantiationException(ILegend.class,
274
                                                legendClass, legendName, e);
275
                        }
276
                }
277
                return null;
278
        }
279

    
280
    public IVectorLegend createDefaultVectorLegend(int shapeType)
281
    throws MapContextRuntimeException {
282
        try {
283
            // Create legend
284
            IVectorLegend legend =
285
                (IVectorLegend) createLegend(getDefaultVectorLegend());
286
            if (legend == null) {
287
                return null;
288
            }
289
            // Set legend values
290
            legend.setShapeType(shapeType);
291
            legend.setDefaultSymbol(getSymbolManager().createSymbol(shapeType));
292
            return legend;
293
        } catch(Exception e) {
294
            throw new MapContextRuntimeException(e);
295
        }
296
    }
297

    
298
        public void registerLegendReader(String format, Class readerClass)
299
        throws MapContextRuntimeException {
300
                if (readerClass == null
301
                                || !ILegendReader.class.isAssignableFrom(readerClass)) {
302
                        throw new InvalidRegisteredClassException(ILegendReader.class,
303
                                        readerClass, format);
304
                }
305

    
306
                legendReaders.put(format, readerClass);
307
        }
308

    
309
        public ILegendReader createLegendReader(String format)
310
        throws MapContextRuntimeException {
311
                Class legendReaderClazz = (Class) legendReaders.get(format);
312

    
313
                if (legendReaderClazz != null) {
314
                        try {
315
                                return (ILegendReader) legendReaderClazz.newInstance();
316
                        } catch (InstantiationException e) {
317
                                throw new RegisteredClassInstantiationException(
318
                                                ILegendReader.class, legendReaderClazz, format, e);
319
                        } catch (IllegalAccessException e) {
320
                                throw new RegisteredClassInstantiationException(
321
                                                ILegendReader.class, legendReaderClazz, format, e);
322
                        }
323
                }
324
                return null;
325
        }
326

    
327
        public void registerLegendWriter(String legendName, String format,
328
                        Class writerClass) throws MapContextRuntimeException {
329
                if (writerClass == null
330
                                || !ILegendWriter.class.isAssignableFrom(writerClass)) {
331
                        throw new InvalidRegisteredClassException(ILegendWriter.class,
332
                                        writerClass, format.concat("-").concat(legendName));
333
                }
334

    
335
                Map legendFormatWriters = (Map) legendWriters.get(format);
336

    
337
                synchronized (legendWriters) {
338
                        if (legendFormatWriters == null) {
339
                                legendFormatWriters = Collections
340
                                .synchronizedMap(new HashMap());
341
                                legendWriters.put(format, legendFormatWriters);
342
                        }
343
                }
344

    
345
                legendFormatWriters.put(legendName, writerClass);
346
        }
347

    
348
        public ILegendWriter createLegendWriter(String legendName, String format)
349
        throws MapContextRuntimeException {
350
                Map legendFormatWriters = getLegendWritersForFormat(format);
351

    
352
                if (legendFormatWriters != null) {
353
                        Class legendWriterClazz = (Class) legendFormatWriters
354
                        .get(legendName);
355

    
356
                        if (legendWriterClazz != null) {
357
                                try {
358
                                        return (ILegendWriter) legendWriterClazz.newInstance();
359
                                } catch (InstantiationException e) {
360
                                        throw new RegisteredClassInstantiationException(
361
                                                        ILegendWriter.class, legendWriterClazz, format
362
                                                        .concat("-").concat(legendName), e);
363
                                } catch (IllegalAccessException e) {
364
                                        throw new RegisteredClassInstantiationException(
365
                                                        ILegendWriter.class, legendWriterClazz, format
366
                                                        .concat("-").concat(legendName), e);
367
                                }
368
                        }
369
                }
370
                return null;
371
        }
372

    
373
        private Map getLegendWritersForFormat(String format) {
374
                return (Map) legendWriters.get(format);
375
        }
376

    
377
        public IMultiLayerSymbol createMultiLayerSymbol(int shapeType)
378
        throws MapContextRuntimeException {
379
                return getSymbolManager().createMultiLayerSymbol(shapeType);
380
        }
381

    
382
        public IMultiLayerSymbol createMultiLayerSymbol(String symbolName)
383
        throws MapContextRuntimeException {
384
                return getSymbolManager().createMultiLayerSymbol(symbolName);
385
        }
386

    
387
        public ISymbol createSymbol(int shapeType, Color color)
388
        throws MapContextRuntimeException {
389
                return getSymbolManager().createSymbol(shapeType, color);
390
        }
391

    
392
        public ISymbol createSymbol(int shapeType)
393
        throws MapContextRuntimeException {
394
                return getSymbolManager().createSymbol(shapeType);
395
        }
396

    
397
        public ISymbol createSymbol(String symbolName, Color color)
398
        throws MapContextRuntimeException {
399
                return getSymbolManager().createSymbol(symbolName, color);
400
        }
401

    
402
        public ISymbol createSymbol(String symbolName)
403
        throws MapContextRuntimeException {
404
                return getSymbolManager().createSymbol(symbolName);
405
        }
406

    
407
        public IWarningSymbol getWarningSymbol(String message, String symbolDesc,
408
                        int symbolDrawExceptionType) throws MapContextRuntimeException {
409
                return getSymbolManager().getWarningSymbol(message, symbolDesc,
410
                                symbolDrawExceptionType);
411
        }
412

    
413
        public ISymbol[] loadSymbols(File folder, FileFilter filter)
414
        throws SymbolException {
415
                return getSymbolManager().loadSymbols(folder, filter);
416
        }
417

    
418
        public ISymbol[] loadSymbols(File folder) throws SymbolException {
419
                return getSymbolManager().loadSymbols(folder);
420
        }
421

    
422
        public void registerMultiLayerSymbol(String symbolName, Class symbolClass)
423
        throws MapContextRuntimeException {
424
                getSymbolManager().registerMultiLayerSymbol(symbolName, symbolClass);
425
        }
426

    
427
        public void registerMultiLayerSymbol(String symbolName, int[] shapeTypes,
428
                        Class symbolClass) throws MapContextRuntimeException {
429
                getSymbolManager().registerMultiLayerSymbol(symbolName, shapeTypes,
430
                                symbolClass);
431
        }
432

    
433
        public void registerSymbol(String symbolName, Class symbolClass)
434
        throws MapContextRuntimeException {
435
                getSymbolManager().registerSymbol(symbolName, symbolClass);
436
        }
437

    
438
        public void registerSymbol(String symbolName, int[] shapeTypes,
439
                        Class symbolClass) throws MapContextException {
440
                getSymbolManager().registerSymbol(symbolName, shapeTypes, symbolClass);
441
        }
442

    
443
        public void saveSymbol(ISymbol symbol, String fileName, File folder,
444
                        boolean overwrite) throws SymbolException {
445
                getSymbolManager().saveSymbol(symbol, fileName, folder, overwrite);
446
        }
447

    
448
        public void saveSymbol(ISymbol symbol, String fileName, File folder)
449
        throws SymbolException {
450
                getSymbolManager().saveSymbol(symbol, fileName, folder);
451
        }
452

    
453
        public FLayer createLayer(String layerName, DataStoreParameters parameters)
454
        throws LoadLayerException {
455
                return LayerFactory.getInstance().createLayer(layerName, parameters);
456
        }
457

    
458
        public FLayer createLayer(String layerName, DataStore store)
459
        throws LoadLayerException {
460
                return LayerFactory.getInstance().createLayer(layerName, store);
461
        }
462

    
463
        public ILegend getLegend(DataStore dataStore) {
464
                ILegend legend = null;
465

    
466
                File file = getResourcePathByReflection(dataStore, SymbolManager.LEGEND_FILE_EXTENSION.substring(1));
467
                try{
468
                if ((file != null) && (file.exists())){
469
                        PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
470
                        FileInputStream is = new FileInputStream(file);
471
                        legend = (ILegend) persistenceManager.getObject(is);
472
                        is.close();
473
                }
474
                } catch (FileNotFoundException e){
475
                    LOG.error("Legend not found", e);
476
                } catch (IOException e) {
477
                    LOG.error("Error reading the legend", e);
478
        }
479
                
480
                //If the legend is null, next option is to check if the store has the getLegend method
481
                if (legend == null){
482
                        try {
483
                                legend = (IVectorLegend) dataStore.invokeDynMethod(
484
                                                "getLegend", null);
485
                        } catch (DynMethodNotSupportedException e) {
486
                LOG.debug("This store {} does not provide a legend.",
487
                    dataStore.getName());
488
                        } catch (DynMethodException e) {
489
                LOG.error(
490
                    "Can't load the specific legend provided for the store {}.",
491
                    dataStore.getName(), e);
492
                        }
493
                }
494

    
495
                //If legend is null, last step is just try to create the legend by default
496
                if( legend == null ) {
497
                        FeatureType featureType;
498
                        try {
499
                                featureType = (((FeatureStore)dataStore).getDefaultFeatureType());
500
                                int indexGeom = featureType.getDefaultGeometryAttributeIndex();
501
                                int typeShape = featureType.getAttributeDescriptor(indexGeom).getGeometryType();
502
                                legend = createDefaultVectorLegend(typeShape);
503
                        } catch (DataException e) {
504
                                LOG.error("Error getting the default feature type", e);
505
                        }                        
506
                }
507

    
508
                return legend;
509
        }
510

    
511
    public ILabelingStrategy getLabelingStrategy(DataStore dataStore) {
512
        ILabelingStrategy labelingStrategy = null;
513

    
514
        File file = getResourcePathByReflection(dataStore, SymbolManager.LABELINGSTRATEGY_FILE_EXTENSION.substring(1));
515
        try{
516
            if ((file != null) && (file.exists())){
517
                PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
518
                FileInputStream is = new FileInputStream(file);
519
                labelingStrategy = (ILabelingStrategy) persistenceManager.getObject(is);
520
                is.close();
521
            }
522
        } catch (FileNotFoundException e){
523
            LOG.error("Label strategy not found", e);
524
        } catch (IOException e) {
525
            LOG.error("Error reading the labeling strategy", e);
526
        }
527
        
528
        //If the legend is null, next option is to check if the store has the getLegend method
529
        if (labelingStrategy == null){
530
            try {
531
                labelingStrategy =
532
                    (ILabelingStrategy) dataStore.invokeDynMethod("getLabeling",
533
                        null);
534
            } catch (DynMethodNotSupportedException e1) {
535
                labelingStrategy = null;
536
            } catch (DynMethodException e1) {
537
                LOG.error("Can't load the specific lebeling strategy provided for the datastore {}.",
538
                    dataStore.getName(),
539
                    e1);
540
            }
541
        }        
542
        
543
        return labelingStrategy;
544
    }
545
    
546
    private File getResourcePathByReflection(DataStore dataStore, String resource){
547
        //Loading the file from a store based on file
548
        DataServerExplorer dataServerExplorer = null;
549
        try {
550
            dataServerExplorer = dataStore.getExplorer();
551
            Class[] args = new Class[2];
552
            args[0] = DataStore.class;
553
            args[1] = String.class;
554
            Method method = dataStore.getExplorer().getClass().getMethod("getResourcePath", args);
555
            if (method != null){
556
                Object[] params = new Object[2];
557
                params[0] = dataStore;
558
                params[1] = resource;
559
                return (File)method.invoke(dataServerExplorer, params);
560
            }
561
        } catch (Exception e) {
562
            LOG.debug(
563
                "Can't load the specific legend provided by the explorer "
564
                + dataServerExplorer, e);
565
        } 
566
        return null;
567
    }
568
}