Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / impl / DefaultMapContextManager.java @ 33615

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.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
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.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.exceptions.LoadLayerException;
56
import org.gvsig.fmap.mapcontext.layers.FLayer;
57
import org.gvsig.fmap.mapcontext.layers.LayerFactory;
58
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer;
59
import org.gvsig.fmap.mapcontext.layers.vectorial.impl.DefaultGraphicLayer;
60
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
61
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
62
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend;
63
import org.gvsig.fmap.mapcontext.rendering.legend.driver.ILegendReader;
64
import org.gvsig.fmap.mapcontext.rendering.legend.driver.ILegendWriter;
65
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
66
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
67
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol;
68
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolException;
69
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
70
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolPreferences;
71
import org.gvsig.tools.ToolsLocator;
72
import org.gvsig.tools.dynobject.exception.DynMethodException;
73
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException;
74
import org.gvsig.tools.persistence.PersistenceManager;
75
import org.gvsig.tools.persistence.PersistentState;
76

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

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

    
87
        private Class drawerClazz = DefaultMapContextDrawer.class;
88

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

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

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

    
95
        private String defaultVectorLegend;
96

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
200
        public MapContextDrawer createDefaultMapContextDrawerInstance()
201
        throws MapContextException {
202

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

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

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

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

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

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

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

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

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

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

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

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

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

    
279
        public IVectorLegend createDefaultVectorLegend(int shapeType)
280
        throws MapContextRuntimeException {
281
                try {
282
                        Random rand = new Random();
283

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

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

    
311
                legendReaders.put(format, readerClass);
312
        }
313

    
314
        public ILegendReader createLegendReader(String format)
315
        throws MapContextRuntimeException {
316
                Class legendReaderClazz = (Class) legendReaders.get(format);
317

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

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

    
340
                Map legendFormatWriters = (Map) legendWriters.get(format);
341

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

    
350
                legendFormatWriters.put(legendName, writerClass);
351
        }
352

    
353
        public ILegendWriter createLegendWriter(String legendName, String format)
354
        throws MapContextRuntimeException {
355
                Map legendFormatWriters = getLegendWritersForFormat(format);
356

    
357
                if (legendFormatWriters != null) {
358
                        Class legendWriterClazz = (Class) legendFormatWriters
359
                        .get(legendName);
360

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

    
378
        private Map getLegendWritersForFormat(String format) {
379
                return (Map) legendWriters.get(format);
380
        }
381

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

    
387
        public IMultiLayerSymbol createMultiLayerSymbol(String symbolName)
388
        throws MapContextRuntimeException {
389
                return getSymbolManager().createMultiLayerSymbol(symbolName);
390
        }
391

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

    
397
        public ISymbol createSymbol(int shapeType)
398
        throws MapContextRuntimeException {
399
                return getSymbolManager().createSymbol(shapeType);
400
        }
401

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

    
407
        public ISymbol createSymbol(String symbolName)
408
        throws MapContextRuntimeException {
409
                return getSymbolManager().createSymbol(symbolName);
410
        }
411

    
412
        public IWarningSymbol getWarningSymbol(String message, String symbolDesc,
413
                        int symbolDrawExceptionType) throws MapContextRuntimeException {
414
                return getSymbolManager().getWarningSymbol(message, symbolDesc,
415
                                symbolDrawExceptionType);
416
        }
417

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

    
423
        public ISymbol[] loadSymbols(File folder) throws SymbolException {
424
                return getSymbolManager().loadSymbols(folder);
425
        }
426

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

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

    
438
        public void registerSymbol(String symbolName, Class symbolClass)
439
        throws MapContextRuntimeException {
440
                getSymbolManager().registerSymbol(symbolName, symbolClass);
441
        }
442

    
443
        public void registerSymbol(String symbolName, int[] shapeTypes,
444
                        Class symbolClass) throws MapContextException {
445
                getSymbolManager().registerSymbol(symbolName, shapeTypes, symbolClass);
446
        }
447

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

    
453
        public void saveSymbol(ISymbol symbol, String fileName, File folder)
454
        throws SymbolException {
455
                getSymbolManager().saveSymbol(symbol, fileName, folder);
456
        }
457

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

    
463
        public FLayer createLayer(String layerName, DataStore store)
464
        throws LoadLayerException {
465
                return LayerFactory.getInstance().createLayer(layerName, store);
466
        }
467

    
468
        public ILegend getLegend(DataStore dataStore) {
469
                ILegend legend = null;
470

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

    
502
                } catch (Exception e) {
503
                        LOG.error("Error creating the legend the explorer", e);
504
                } 
505

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

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

    
531
                return legend;
532
        }
533

    
534
}