Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2011 / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / impl / DefaultMapContextManager.java @ 33562

History | View | Annotate | Download (17.9 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.lang.reflect.Method;
35
import java.util.Collections;
36
import java.util.HashMap;
37
import java.util.Map;
38
import java.util.Random;
39

    
40
import org.cresques.cts.IProjection;
41
import org.gvsig.fmap.dal.DataServerExplorer;
42
import org.gvsig.fmap.dal.DataStore;
43
import org.gvsig.fmap.dal.DataStoreParameters;
44
import org.gvsig.fmap.dal.exception.DataException;
45
import org.gvsig.fmap.dal.feature.FeatureStore;
46
import org.gvsig.fmap.dal.feature.FeatureType;
47
import org.gvsig.fmap.mapcontext.MapContextDrawer;
48
import org.gvsig.fmap.mapcontext.MapContextException;
49
import org.gvsig.fmap.mapcontext.MapContextLocator;
50
import org.gvsig.fmap.mapcontext.MapContextManager;
51
import org.gvsig.fmap.mapcontext.MapContextRuntimeException;
52
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
53
import org.gvsig.fmap.mapcontext.layers.FLayer;
54
import org.gvsig.fmap.mapcontext.layers.LayerFactory;
55
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer;
56
import org.gvsig.fmap.mapcontext.layers.vectorial.impl.DefaultGraphicLayer;
57
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
58
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
59
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend;
60
import org.gvsig.fmap.mapcontext.rendering.legend.driver.ILegendReader;
61
import org.gvsig.fmap.mapcontext.rendering.legend.driver.ILegendWriter;
62
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
63
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
64
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol;
65
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolException;
66
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
67
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolPreferences;
68
import org.gvsig.tools.ToolsLocator;
69
import org.gvsig.tools.dynobject.exception.DynMethodException;
70
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException;
71
import org.gvsig.tools.persistence.PersistenceManager;
72
import org.gvsig.tools.persistence.PersistentState;
73
import org.slf4j.Logger;
74
import org.slf4j.LoggerFactory;
75

    
76
/**
77
 * Default implementation of the {@link MapContextManager}.
78
 *
79
 * @author <a href="mailto:cordinyana@gvsig.org">C?sar Ordi?ana</a>
80
 */
81
public class DefaultMapContextManager implements MapContextManager {
82
        private static final Logger LOG = LoggerFactory.getLogger(MapContextManager.class);
83

    
84
        private Class drawerClazz = DefaultMapContextDrawer.class;
85

    
86
        private Map legends = Collections.synchronizedMap(new HashMap());
87

    
88
        private Map legendReaders = Collections.synchronizedMap(new HashMap());
89

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

    
92
        private String defaultVectorLegend;
93

    
94
        public SymbolManager getSymbolManager() {
95
                return MapContextLocator.getSymbolManager();
96
        }
97

    
98
        private SymbolPreferences getSymbolPreferences() {
99
                return getSymbolManager().getSymbolPreferences();
100
        }
101

    
102
        public String getSymbolLibraryPath() {
103
                return getSymbolPreferences().getSymbolLibraryPath();
104
        }
105

    
106
        public void setSymbolLibraryPath(String symbolLibraryPath) {
107
                getSymbolPreferences().setSymbolLibraryPath(symbolLibraryPath);
108
        }
109

    
110
        public void resetSymbolLibraryPath() {
111
                getSymbolPreferences().resetSymbolLibraryPath();
112
        }
113

    
114
        public Color getDefaultSymbolColor() {
115
                return getSymbolPreferences().getDefaultSymbolColor();
116
        }
117

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

    
122
        public Font getDefaultSymbolFont() {
123
                return getSymbolPreferences().getDefaultSymbolFont();
124
        }
125

    
126
        public String getSymbolFileExtension() {
127
                return getSymbolPreferences().getSymbolFileExtension();
128
        }
129

    
130
        public boolean isDefaultSymbolFillColorAleatory() {
131
                return getSymbolPreferences().isDefaultSymbolFillColorAleatory();
132
        }
133

    
134
        public void resetDefaultSymbolColor() {
135
                getSymbolPreferences().resetDefaultSymbolColor();
136
        }
137

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

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

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

    
150
        public void setDefaultSymbolColor(Color defaultSymbolColor) {
151
                getSymbolPreferences().setDefaultSymbolColor(defaultSymbolColor);
152
        }
153

    
154
        public void setDefaultSymbolFillColor(Color defaultSymbolFillColor) {
155
                getSymbolPreferences().setDefaultSymbolFillColor(defaultSymbolFillColor);
156
        }
157

    
158
        public void setDefaultSymbolFillColorAleatory(
159
                        boolean defaultSymbolFillColorAleatory) {
160
                getSymbolPreferences().setDefaultSymbolFillColorAleatory(
161
                                defaultSymbolFillColorAleatory);
162
        }
163

    
164
        public void setDefaultSymbolFont(Font defaultSymbolFont) {
165
                getSymbolPreferences().setDefaultSymbolFont(defaultSymbolFont);
166
        }
167

    
168
        public void setSymbolFileExtension(String extension) {
169
                getSymbolPreferences().setSymbolFileExtension(extension);
170
        }
171

    
172
        public int getDefaultCartographicSupportMeasureUnit() {
173
                return getSymbolPreferences().getDefaultCartographicSupportMeasureUnit();
174
        }
175

    
176
        public void setDefaultCartographicSupportMeasureUnit(
177
                        int defaultCartographicSupportMeasureUnit) {
178
                getSymbolPreferences().setDefaultCartographicSupportMeasureUnit(
179
                                defaultCartographicSupportMeasureUnit);
180
        }
181

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

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

    
192
        public MapContextDrawer createMapContextDrawerInstance(Class drawerClazz)
193
        throws MapContextException {
194
                return createMapContextDrawerInstance(drawerClazz, "NONE");
195
        }
196

    
197
        public MapContextDrawer createDefaultMapContextDrawerInstance()
198
        throws MapContextException {
199

    
200
                return createMapContextDrawerInstance(drawerClazz, "default");
201
        }
202

    
203
        private MapContextDrawer createMapContextDrawerInstance(Class drawerClazz,
204
                        String name) throws RegisteredClassInstantiationException {
205
                try {
206
                        return (MapContextDrawer) drawerClazz.newInstance();
207
                } catch (Exception ex) {
208
                        throw new RegisteredClassInstantiationException(
209
                                        MapContextDrawer.class, drawerClazz, name, ex);
210
                }
211
        }
212

    
213
        public void setDefaultMapContextDrawer(Class drawerClazz)
214
        throws MapContextException {
215

    
216
                validateMapContextDrawer(drawerClazz);
217
                this.drawerClazz = drawerClazz;
218
        }
219

    
220
        public void validateMapContextDrawer(Class drawerClazz)
221
        throws MapContextException {
222
                if (!MapContextDrawer.class.isAssignableFrom(drawerClazz)) {
223
                        throw new InvalidRegisteredClassException(MapContextDrawer.class,
224
                                        drawerClazz, "UNKNOWN");
225
                }
226
        }
227

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

    
239
        public String getDefaultVectorLegend() {
240
                return defaultVectorLegend;
241
        }
242

    
243
        public void setDefaultVectorLegend(String defaultVectorLegend) {
244
                this.defaultVectorLegend = defaultVectorLegend;
245
        }
246

    
247
        public void registerLegend(String legendName, Class legendClass)
248
        throws MapContextRuntimeException {
249

    
250
                if (legendClass == null || !ILegend.class.isAssignableFrom(legendClass)) {
251
                        throw new InvalidRegisteredClassException(ILegend.class,
252
                                        legendClass, legendName);
253
                }
254

    
255
                legends.put(legendName, legendClass);
256
        }
257

    
258
        public ILegend createLegend(String legendName)
259
        throws MapContextRuntimeException {
260
                Class legendClass = (Class) legends.get(legendName);
261

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

    
276
        public IVectorLegend createDefaultVectorLegend(int shapeType)
277
        throws MapContextRuntimeException {
278
                try {
279
                        Random rand = new Random();
280

    
281
                        int numreg = rand.nextInt(255 / 2);
282
                        double div = (1 - rand.nextDouble() * 0.66) * 0.9;
283
                        Color randomColor = new Color(((int) (255 * div + (numreg * rand
284
                                        .nextDouble()))) % 255, ((int) (255 * div + (numreg * rand
285
                                                        .nextDouble()))) % 255, ((int) (255 * div + (numreg * rand
286
                                                                        .nextDouble()))) % 255);
287
                        IVectorLegend legend = (IVectorLegend) createLegend(getDefaultVectorLegend());
288
                        if( legend == null ) {
289
                                return null;
290
                        }
291
                        ISymbol defaultSymbol =
292
                                getSymbolManager().createSymbol(shapeType, randomColor);
293
                        legend.setDefaultSymbol(defaultSymbol);
294
                        return legend;
295
                } catch(Exception e) {
296
                        throw new MapContextRuntimeException(e);
297
                }
298
        }
299

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

    
308
                legendReaders.put(format, readerClass);
309
        }
310

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

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

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

    
337
                Map legendFormatWriters = (Map) legendWriters.get(format);
338

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

    
347
                legendFormatWriters.put(legendName, writerClass);
348
        }
349

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
465
        public ILegend getLegend(DataStore dataStore) {
466
                ILegend legend = null;
467

    
468
                //Loading the legend from a store based on file
469
                try {
470
//                        This access by reflection is done because the libFMap_dalfile project
471
//                        has to be divided in several projects. The commented code is the fine
472
//                        code whereas the used code has to be deleted.                
473
//                        
474
//                        if (dataStore.getExplorer() instanceof FilesystemServerExplorer){
475
//                                File file = ((FilesystemServerExplorer)dataStore.getExplorer()).getResourcePath(dataStore, "gvl");
476
//                                if ((file != null) && (file.exists())){
477
//                                        PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
478
//                                        PersistentState persistentState = persistenceManager.loadState(new FileInputStream(file));
479
//                                        legend = (ILegend)persistenceManager.create(persistentState);
480
//                                }
481
//                        }
482
                        DataServerExplorer dataServerExplorer = dataStore.getExplorer();
483
                        Class[] args = new Class[2];
484
                        args[0] = DataStore.class;
485
                        args[1] = String.class;
486
                        Method method = dataStore.getExplorer().getClass().getMethod("getResourcePath", args);
487
                        if (method != null){
488
                                Object[] params = new Object[2];
489
                                params[0] = dataStore;
490
                                params[1] = "gvl";
491
                                File file = (File)method.invoke(dataServerExplorer, params);
492
                                if ((file != null) && (file.exists())){
493
                                        PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
494
                                        PersistentState persistentState = persistenceManager.loadState(new FileInputStream(file));
495
                                        legend = (ILegend)persistenceManager.create(persistentState);
496
                                }
497
                        }        
498

    
499
                } catch (Exception e) {
500
                        LOG.error("Error creating the legend the explorer", e);
501
                } 
502

    
503
                //If the legend is null, next option is to check if the store has the getLegend method
504
                if (legend == null){
505
                        try {
506
                                legend = (IVectorLegend) dataStore.invokeDynMethod(
507
                                                "getLegend", null);
508
                        } catch (DynMethodNotSupportedException e) {
509
                                // Do nothing
510
                        } catch (DynMethodException e) {
511
                                LOG.error("Can't load the specific legend provided for the store {}.", dataStore.getName(), e);
512
                        }
513
                }
514

    
515
                //If legend is null, last step is just try to create the legend by default
516
                if( legend == null ) {
517
                        FeatureType featureType;
518
                        try {
519
                                featureType = (((FeatureStore)dataStore).getDefaultFeatureType());
520
                                int indexGeom = featureType.getDefaultGeometryAttributeIndex();
521
                                int typeShape = featureType.getAttributeDescriptor(indexGeom).getGeometryType();
522
                                legend = MapContextLocator.getMapContextManager().createDefaultVectorLegend(typeShape);
523
                        } catch (DataException e) {
524
                                LOG.error("Error getting the default feature type", e);
525
                        }                        
526
                }
527

    
528
                return legend;
529
        }
530

    
531
}