Statistics
| Revision:

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

History | View | Annotate | Download (14.1 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.util.Collections;
34
import java.util.HashMap;
35
import java.util.Map;
36
import java.util.Random;
37

    
38
import org.cresques.cts.IProjection;
39
import org.gvsig.fmap.mapcontext.MapContextDrawer;
40
import org.gvsig.fmap.mapcontext.MapContextException;
41
import org.gvsig.fmap.mapcontext.MapContextLocator;
42
import org.gvsig.fmap.mapcontext.MapContextManager;
43
import org.gvsig.fmap.mapcontext.MapContextRuntimeException;
44
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer;
45
import org.gvsig.fmap.mapcontext.layers.vectorial.impl.DefaultGraphicLayer;
46
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
47
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
48
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend;
49
import org.gvsig.fmap.mapcontext.rendering.legend.driver.ILegendReader;
50
import org.gvsig.fmap.mapcontext.rendering.legend.driver.ILegendWriter;
51
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
52
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
53
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol;
54
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolException;
55
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
56
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolPreferences;
57
import org.slf4j.Logger;
58
import org.slf4j.LoggerFactory;
59

    
60
/**
61
 * Default implementation of the {@link MapContextManager}.
62
 *
63
 * @author <a href="mailto:cordinyana@gvsig.org">C?sar Ordi?ana</a>
64
 */
65
public class DefaultMapContextManager implements MapContextManager {
66
        private static final Logger logger = LoggerFactory.getLogger(MapContextManager.class);
67
        
68
        private Class drawerClazz = DefaultMapContextDrawer.class;
69

    
70
        private Map legends = Collections.synchronizedMap(new HashMap());
71

    
72
        private Map legendReaders = Collections.synchronizedMap(new HashMap());
73

    
74
        private Map legendWriters = Collections.synchronizedMap(new HashMap());
75

    
76
        private String defaultVectorLegend;
77

    
78
        public SymbolManager getSymbolManager() {
79
                return MapContextLocator.getSymbolManager();
80
        }
81

    
82
        private SymbolPreferences getSymbolPreferences() {
83
                return getSymbolManager().getSymbolPreferences();
84
        }
85

    
86
        public String getSymbolLibraryPath() {
87
                return getSymbolPreferences().getSymbolLibraryPath();
88
        }
89

    
90
        public void setSymbolLibraryPath(String symbolLibraryPath) {
91
                getSymbolPreferences().setSymbolLibraryPath(symbolLibraryPath);
92
        }
93

    
94
        public void resetSymbolLibraryPath() {
95
                getSymbolPreferences().resetSymbolLibraryPath();
96
        }
97

    
98
        public Color getDefaultSymbolColor() {
99
                return getSymbolPreferences().getDefaultSymbolColor();
100
        }
101

    
102
        public Color getDefaultSymbolFillColor() {
103
                return getSymbolPreferences().getDefaultSymbolFillColor();
104
        }
105

    
106
        public Font getDefaultSymbolFont() {
107
                return getSymbolPreferences().getDefaultSymbolFont();
108
        }
109

    
110
        public String getSymbolFileExtension() {
111
                return getSymbolPreferences().getSymbolFileExtension();
112
        }
113

    
114
        public boolean isDefaultSymbolFillColorAleatory() {
115
                return getSymbolPreferences().isDefaultSymbolFillColorAleatory();
116
        }
117

    
118
        public void resetDefaultSymbolColor() {
119
                getSymbolPreferences().resetDefaultSymbolColor();
120
        }
121

    
122
        public void resetDefaultSymbolFillColor() {
123
                getSymbolPreferences().resetDefaultSymbolFillColor();
124
        }
125

    
126
        public void resetDefaultSymbolFillColorAleatory() {
127
                getSymbolPreferences().resetDefaultSymbolFillColorAleatory();
128
        }
129

    
130
        public void resetDefaultSymbolFont() {
131
                getSymbolPreferences().resetDefaultSymbolFont();
132
        }
133

    
134
        public void setDefaultSymbolColor(Color defaultSymbolColor) {
135
                getSymbolPreferences().setDefaultSymbolColor(defaultSymbolColor);
136
        }
137

    
138
        public void setDefaultSymbolFillColor(Color defaultSymbolFillColor) {
139
                getSymbolPreferences().setDefaultSymbolFillColor(defaultSymbolFillColor);
140
        }
141

    
142
        public void setDefaultSymbolFillColorAleatory(
143
                        boolean defaultSymbolFillColorAleatory) {
144
                getSymbolPreferences().setDefaultSymbolFillColorAleatory(
145
                                defaultSymbolFillColorAleatory);
146
        }
147

    
148
        public void setDefaultSymbolFont(Font defaultSymbolFont) {
149
                getSymbolPreferences().setDefaultSymbolFont(defaultSymbolFont);
150
        }
151

    
152
        public void setSymbolFileExtension(String extension) {
153
                getSymbolPreferences().setSymbolFileExtension(extension);
154
        }
155

    
156
        public int getDefaultCartographicSupportMeasureUnit() {
157
                return getSymbolPreferences().getDefaultCartographicSupportMeasureUnit();
158
        }
159

    
160
        public void setDefaultCartographicSupportMeasureUnit(
161
                        int defaultCartographicSupportMeasureUnit) {
162
                getSymbolPreferences().setDefaultCartographicSupportMeasureUnit(
163
                                defaultCartographicSupportMeasureUnit);
164
        }
165

    
166
        public int getDefaultCartographicSupportReferenceSystem() {
167
                return getSymbolPreferences().getDefaultCartographicSupportReferenceSystem();
168
        }
169

    
170
        public void setDefaultCartographicSupportReferenceSystem(
171
                        int defaultCartographicSupportReferenceSystem) {
172
                getSymbolPreferences().setDefaultCartographicSupportReferenceSystem(
173
                                defaultCartographicSupportReferenceSystem);
174
        }
175

    
176
        public MapContextDrawer createMapContextDrawerInstance(Class drawerClazz)
177
                        throws MapContextException {
178
                return createMapContextDrawerInstance(drawerClazz, "NONE");
179
        }
180

    
181
        public MapContextDrawer createDefaultMapContextDrawerInstance()
182
                        throws MapContextException {
183

    
184
                return createMapContextDrawerInstance(drawerClazz, "default");
185
        }
186

    
187
        private MapContextDrawer createMapContextDrawerInstance(Class drawerClazz,
188
                        String name) throws RegisteredClassInstantiationException {
189
                try {
190
                        return (MapContextDrawer) drawerClazz.newInstance();
191
                } catch (Exception ex) {
192
                        throw new RegisteredClassInstantiationException(
193
                                        MapContextDrawer.class, drawerClazz, name, ex);
194
                }
195
        }
196

    
197
        public void setDefaultMapContextDrawer(Class drawerClazz)
198
                        throws MapContextException {
199

    
200
                validateMapContextDrawer(drawerClazz);
201
                this.drawerClazz = drawerClazz;
202
        }
203

    
204
        public void validateMapContextDrawer(Class drawerClazz)
205
                        throws MapContextException {
206
                if (!MapContextDrawer.class.isAssignableFrom(drawerClazz)) {
207
                        throw new InvalidRegisteredClassException(MapContextDrawer.class,
208
                                        drawerClazz, "UNKNOWN");
209
                }
210
        }
211

    
212
        public GraphicLayer createGraphicsLayer(IProjection projection) {
213
                DefaultGraphicLayer layer = new DefaultGraphicLayer();
214
                try {
215
                        layer.initialize(projection);        
216
                        layer.setLegend((IVectorLegend)createLegend(IVectorialUniqueValueLegend.LEGEND_NAME));
217
                } catch (Exception e) {
218
                        logger.error("Error initializing the graphics layer", e);
219
                }
220
                return layer;
221
        }
222

    
223
        public String getDefaultVectorLegend() {
224
                return defaultVectorLegend;
225
        }
226

    
227
        public void setDefaultVectorLegend(String defaultVectorLegend) {
228
                this.defaultVectorLegend = defaultVectorLegend;
229
        }
230

    
231
        public void registerLegend(String legendName, Class legendClass)
232
                        throws MapContextRuntimeException {
233

    
234
                if (legendClass == null || !ILegend.class.isAssignableFrom(legendClass)) {
235
                        throw new InvalidRegisteredClassException(ILegend.class,
236
                                        legendClass, legendName);
237
                }
238

    
239
                legends.put(legendName, legendClass);
240
        }
241

    
242
        public ILegend createLegend(String legendName)
243
                        throws MapContextRuntimeException {
244
                Class legendClass = (Class) legends.get(legendName);
245

    
246
                if (legendClass != null) {
247
                        try {
248
                                return (ILegend) legendClass.newInstance();
249
                        } catch (InstantiationException e) {
250
                                throw new RegisteredClassInstantiationException(ILegend.class,
251
                                                legendClass, legendName, e);
252
                        } catch (IllegalAccessException e) {
253
                                throw new RegisteredClassInstantiationException(ILegend.class,
254
                                                legendClass, legendName, e);
255
                        }
256
                }
257
                return null;
258
        }
259

    
260
        public IVectorLegend createDefaultVectorLegend(int shapeType)
261
                        throws MapContextRuntimeException {
262
                Random rand = new Random();
263

    
264
                int numreg = rand.nextInt(255 / 2);
265
                double div = (1 - rand.nextDouble() * 0.66) * 0.9;
266
                Color randomColor = new Color(((int) (255 * div + (numreg * rand
267
                                .nextDouble()))) % 255, ((int) (255 * div + (numreg * rand
268
                                .nextDouble()))) % 255, ((int) (255 * div + (numreg * rand
269
                                .nextDouble()))) % 255);
270
                IVectorLegend legend = (IVectorLegend) createLegend(getDefaultVectorLegend());
271

    
272
                ISymbol defaultSymbol =
273
                                getSymbolManager().createSymbol(shapeType, randomColor);
274
                legend.setDefaultSymbol(defaultSymbol);
275
                return legend;
276
        }
277

    
278
        public void registerLegendReader(String format, Class readerClass)
279
                        throws MapContextRuntimeException {
280
                if (readerClass == null
281
                                || !ILegendReader.class.isAssignableFrom(readerClass)) {
282
                        throw new InvalidRegisteredClassException(ILegendReader.class,
283
                                        readerClass, format);
284
                }
285

    
286
                legendReaders.put(format, readerClass);
287
        }
288

    
289
        public ILegendReader createLegendReader(String format)
290
                        throws MapContextRuntimeException {
291
                Class legendReaderClazz = (Class) legendReaders.get(format);
292

    
293
                if (legendReaderClazz != null) {
294
                        try {
295
                                return (ILegendReader) legendReaderClazz.newInstance();
296
                        } catch (InstantiationException e) {
297
                                throw new RegisteredClassInstantiationException(
298
                                                ILegendReader.class, legendReaderClazz, format, e);
299
                        } catch (IllegalAccessException e) {
300
                                throw new RegisteredClassInstantiationException(
301
                                                ILegendReader.class, legendReaderClazz, format, e);
302
                        }
303
                }
304
                return null;
305
        }
306

    
307
        public void registerLegendWriter(String legendName, String format,
308
                        Class writerClass) throws MapContextRuntimeException {
309
                if (writerClass == null
310
                                || !ILegendWriter.class.isAssignableFrom(writerClass)) {
311
                        throw new InvalidRegisteredClassException(ILegendWriter.class,
312
                                        writerClass, format.concat("-").concat(legendName));
313
                }
314

    
315
                Map legendFormatWriters = (Map) legendWriters.get(format);
316

    
317
                synchronized (legendWriters) {
318
                        if (legendFormatWriters == null) {
319
                                legendFormatWriters = Collections
320
                                                .synchronizedMap(new HashMap());
321
                                legendWriters.put(format, legendFormatWriters);
322
                        }
323
                }
324

    
325
                legendFormatWriters.put(legendName, writerClass);
326
        }
327

    
328
        public ILegendWriter createLegendWriter(String legendName, String format)
329
                        throws MapContextRuntimeException {
330
                Map legendFormatWriters = getLegendWritersForFormat(format);
331

    
332
                if (legendFormatWriters != null) {
333
                        Class legendWriterClazz = (Class) legendFormatWriters
334
                                        .get(legendName);
335

    
336
                        if (legendWriterClazz != null) {
337
                                try {
338
                                        return (ILegendWriter) legendWriterClazz.newInstance();
339
                                } catch (InstantiationException e) {
340
                                        throw new RegisteredClassInstantiationException(
341
                                                        ILegendWriter.class, legendWriterClazz, format
342
                                                                        .concat("-").concat(legendName), e);
343
                                } catch (IllegalAccessException e) {
344
                                        throw new RegisteredClassInstantiationException(
345
                                                        ILegendWriter.class, legendWriterClazz, format
346
                                                                        .concat("-").concat(legendName), e);
347
                                }
348
                        }
349
                }
350
                return null;
351
        }
352

    
353
        private Map getLegendWritersForFormat(String format) {
354
                return (Map) legendWriters.get(format);
355
        }
356

    
357
        public IMultiLayerSymbol createMultiLayerSymbol(int shapeType)
358
                        throws MapContextRuntimeException {
359
                return getSymbolManager().createMultiLayerSymbol(shapeType);
360
        }
361

    
362
        public IMultiLayerSymbol createMultiLayerSymbol(String symbolName)
363
                        throws MapContextRuntimeException {
364
                return getSymbolManager().createMultiLayerSymbol(symbolName);
365
        }
366

    
367
        public ISymbol createSymbol(int shapeType, Color color)
368
                        throws MapContextRuntimeException {
369
                return getSymbolManager().createSymbol(shapeType, color);
370
        }
371

    
372
        public ISymbol createSymbol(int shapeType)
373
                        throws MapContextRuntimeException {
374
                return getSymbolManager().createSymbol(shapeType);
375
        }
376

    
377
        public ISymbol createSymbol(String symbolName, Color color)
378
                        throws MapContextRuntimeException {
379
                return getSymbolManager().createSymbol(symbolName, color);
380
        }
381

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

    
387
        public IWarningSymbol getWarningSymbol(String message, String symbolDesc,
388
                        int symbolDrawExceptionType) throws MapContextRuntimeException {
389
                return getSymbolManager().getWarningSymbol(message, symbolDesc,
390
                                symbolDrawExceptionType);
391
        }
392

    
393
        public ISymbol[] loadSymbols(File folder, FileFilter filter)
394
                        throws SymbolException {
395
                return getSymbolManager().loadSymbols(folder, filter);
396
        }
397

    
398
        public ISymbol[] loadSymbols(File folder) throws SymbolException {
399
                return getSymbolManager().loadSymbols(folder);
400
        }
401

    
402
        public void registerMultiLayerSymbol(String symbolName, Class symbolClass)
403
                        throws MapContextRuntimeException {
404
                getSymbolManager().registerMultiLayerSymbol(symbolName, symbolClass);
405
        }
406

    
407
        public void registerMultiLayerSymbol(String symbolName, int[] shapeTypes,
408
                        Class symbolClass) throws MapContextRuntimeException {
409
                getSymbolManager().registerMultiLayerSymbol(symbolName, shapeTypes,
410
                                symbolClass);
411
        }
412

    
413
        public void registerSymbol(String symbolName, Class symbolClass)
414
                        throws MapContextRuntimeException {
415
                getSymbolManager().registerSymbol(symbolName, symbolClass);
416
        }
417

    
418
        public void registerSymbol(String symbolName, int[] shapeTypes,
419
                        Class symbolClass) throws MapContextException {
420
                getSymbolManager().registerSymbol(symbolName, shapeTypes, symbolClass);
421
        }
422

    
423
        public void saveSymbol(ISymbol symbol, String fileName, File folder,
424
                        boolean overwrite) throws SymbolException {
425
                getSymbolManager().saveSymbol(symbol, fileName, folder, overwrite);
426
        }
427

    
428
        public void saveSymbol(ISymbol symbol, String fileName, File folder)
429
                        throws SymbolException {
430
                getSymbolManager().saveSymbol(symbol, fileName, folder);
431
        }
432
}