Revision 42457 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.mapcontext/org.gvsig.fmap.mapcontext.api/src/main/java/org/gvsig/fmap/mapcontext/impl/DefaultMapContextManager.java

View differences:

DefaultMapContextManager.java
3 3
 *
4 4
 * Copyright (C) 2007-2013 gvSIG Association.
5 5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10 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.
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15 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.
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 19
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
23 22
 */
24 23
package org.gvsig.fmap.mapcontext.impl;
25 24

  
......
89 88
public class DefaultMapContextManager implements MapContextManager {
90 89

  
91 90
    private static final Logger logger = LoggerFactory
92
        .getLogger(DefaultMapContextManager.class);
91
            .getLogger(DefaultMapContextManager.class);
93 92

  
94
	private Class drawerClazz = DefaultMapContextDrawer.class;
93
    private Class drawerClazz = DefaultMapContextDrawer.class;
95 94

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

  
98
	private Map legendReaders = Collections.synchronizedMap(new HashMap());
97
    private Map legendReaders = Collections.synchronizedMap(new HashMap());
99 98

  
100
	private Map legendWriters = Collections.synchronizedMap(new HashMap());
99
    private Map legendWriters = Collections.synchronizedMap(new HashMap());
101 100

  
102
	private String defaultVectorLegend;
101
    private String defaultVectorLegend;
103 102

  
104
        private ObservableHelper observableHelper = new ObservableHelper();
103
    private ObservableHelper observableHelper = new ObservableHelper();
105 104

  
106
        private File colorTableLibraryFolder = null;
107
        
108
	public MapContext createMapContext() {
109
                MapContext mapcontext = new MapContext(new ViewPort());
110
		return (MapContext) notifyObservers(CREATE_MAPCONTEXT, mapcontext).getValue();
111
	}
112
	
113
	public SymbolManager getSymbolManager() {
114
		return MapContextLocator.getSymbolManager();
115
	}
105
    private File colorTableLibraryFolder = null;
116 106

  
117
	private SymbolPreferences getSymbolPreferences() {
118
		return getSymbolManager().getSymbolPreferences();
119
	}
107
    public MapContext createMapContext() {
108
        MapContext mapcontext = new MapContext(new ViewPort());
109
        return (MapContext) notifyObservers(CREATE_MAPCONTEXT, mapcontext).getValue();
110
    }
120 111

  
121
	public String getSymbolLibraryPath() {
122
		return getSymbolPreferences().getSymbolLibraryPath();
123
	}
112
    public SymbolManager getSymbolManager() {
113
        return MapContextLocator.getSymbolManager();
114
    }
124 115

  
125
	public void setSymbolLibraryPath(String symbolLibraryPath) {
126
		getSymbolPreferences().setSymbolLibraryPath(symbolLibraryPath);
127
	}
116
    private SymbolPreferences getSymbolPreferences() {
117
        return getSymbolManager().getSymbolPreferences();
118
    }
128 119

  
129
	public void resetSymbolLibraryPath() {
130
		getSymbolPreferences().resetSymbolLibraryPath();
131
	}
120
    public String getSymbolLibraryPath() {
121
        return getSymbolPreferences().getSymbolLibraryPath();
122
    }
132 123

  
133
	public Color getDefaultSymbolColor() {
134
		return getSymbolPreferences().getDefaultSymbolColor();
135
	}
124
    public void setSymbolLibraryPath(String symbolLibraryPath) {
125
        getSymbolPreferences().setSymbolLibraryPath(symbolLibraryPath);
126
    }
136 127

  
137
	public Color getDefaultSymbolFillColor() {
138
		return getSymbolPreferences().getDefaultSymbolFillColor();
139
	}
128
    public void resetSymbolLibraryPath() {
129
        getSymbolPreferences().resetSymbolLibraryPath();
130
    }
140 131

  
141
	public Font getDefaultSymbolFont() {
142
		return getSymbolPreferences().getDefaultSymbolFont();
143
	}
132
    public Color getDefaultSymbolColor() {
133
        return getSymbolPreferences().getDefaultSymbolColor();
134
    }
144 135

  
145
	public String getSymbolFileExtension() {
146
		return getSymbolPreferences().getSymbolFileExtension();
147
	}
136
    public Color getDefaultSymbolFillColor() {
137
        return getSymbolPreferences().getDefaultSymbolFillColor();
138
    }
148 139

  
149
	public boolean isDefaultSymbolFillColorAleatory() {
150
		return getSymbolPreferences().isDefaultSymbolFillColorAleatory();
151
	}
140
    public Font getDefaultSymbolFont() {
141
        return getSymbolPreferences().getDefaultSymbolFont();
142
    }
152 143

  
153
	public void resetDefaultSymbolColor() {
154
		getSymbolPreferences().resetDefaultSymbolColor();
155
	}
144
    public String getSymbolFileExtension() {
145
        return getSymbolPreferences().getSymbolFileExtension();
146
    }
156 147

  
157
	public void resetDefaultSymbolFillColor() {
158
		getSymbolPreferences().resetDefaultSymbolFillColor();
159
	}
148
    public boolean isDefaultSymbolFillColorAleatory() {
149
        return getSymbolPreferences().isDefaultSymbolFillColorAleatory();
150
    }
160 151

  
161
	public void resetDefaultSymbolFillColorAleatory() {
162
		getSymbolPreferences().resetDefaultSymbolFillColorAleatory();
163
	}
152
    public void resetDefaultSymbolColor() {
153
        getSymbolPreferences().resetDefaultSymbolColor();
154
    }
164 155

  
165
	public void resetDefaultSymbolFont() {
166
		getSymbolPreferences().resetDefaultSymbolFont();
167
	}
156
    public void resetDefaultSymbolFillColor() {
157
        getSymbolPreferences().resetDefaultSymbolFillColor();
158
    }
168 159

  
169
	public void setDefaultSymbolColor(Color defaultSymbolColor) {
170
		getSymbolPreferences().setDefaultSymbolColor(defaultSymbolColor);
171
	}
160
    public void resetDefaultSymbolFillColorAleatory() {
161
        getSymbolPreferences().resetDefaultSymbolFillColorAleatory();
162
    }
172 163

  
173
	public void setDefaultSymbolFillColor(Color defaultSymbolFillColor) {
174
		getSymbolPreferences().setDefaultSymbolFillColor(defaultSymbolFillColor);
175
	}
164
    public void resetDefaultSymbolFont() {
165
        getSymbolPreferences().resetDefaultSymbolFont();
166
    }
176 167

  
177
	public void setDefaultSymbolFillColorAleatory(
178
			boolean defaultSymbolFillColorAleatory) {
179
		getSymbolPreferences().setDefaultSymbolFillColorAleatory(
180
				defaultSymbolFillColorAleatory);
181
	}
168
    public void setDefaultSymbolColor(Color defaultSymbolColor) {
169
        getSymbolPreferences().setDefaultSymbolColor(defaultSymbolColor);
170
    }
182 171

  
183
	public void setDefaultSymbolFont(Font defaultSymbolFont) {
184
		getSymbolPreferences().setDefaultSymbolFont(defaultSymbolFont);
185
	}
172
    public void setDefaultSymbolFillColor(Color defaultSymbolFillColor) {
173
        getSymbolPreferences().setDefaultSymbolFillColor(defaultSymbolFillColor);
174
    }
186 175

  
187
	public void setSymbolFileExtension(String extension) {
188
		getSymbolPreferences().setSymbolFileExtension(extension);
189
	}
176
    public void setDefaultSymbolFillColorAleatory(
177
            boolean defaultSymbolFillColorAleatory) {
178
        getSymbolPreferences().setDefaultSymbolFillColorAleatory(
179
                defaultSymbolFillColorAleatory);
180
    }
190 181

  
191
	public int getDefaultCartographicSupportMeasureUnit() {
192
		return getSymbolPreferences().getDefaultCartographicSupportMeasureUnit();
193
	}
182
    public void setDefaultSymbolFont(Font defaultSymbolFont) {
183
        getSymbolPreferences().setDefaultSymbolFont(defaultSymbolFont);
184
    }
194 185

  
195
	public void setDefaultCartographicSupportMeasureUnit(
196
			int defaultCartographicSupportMeasureUnit) {
197
		getSymbolPreferences().setDefaultCartographicSupportMeasureUnit(
198
				defaultCartographicSupportMeasureUnit);
199
	}
186
    public void setSymbolFileExtension(String extension) {
187
        getSymbolPreferences().setSymbolFileExtension(extension);
188
    }
200 189

  
201
	public int getDefaultCartographicSupportReferenceSystem() {
202
		return getSymbolPreferences().getDefaultCartographicSupportReferenceSystem();
203
	}
190
    public int getDefaultCartographicSupportMeasureUnit() {
191
        return getSymbolPreferences().getDefaultCartographicSupportMeasureUnit();
192
    }
204 193

  
205
	public void setDefaultCartographicSupportReferenceSystem(
206
			int defaultCartographicSupportReferenceSystem) {
207
		getSymbolPreferences().setDefaultCartographicSupportReferenceSystem(
208
				defaultCartographicSupportReferenceSystem);
209
	}
194
    public void setDefaultCartographicSupportMeasureUnit(
195
            int defaultCartographicSupportMeasureUnit) {
196
        getSymbolPreferences().setDefaultCartographicSupportMeasureUnit(
197
                defaultCartographicSupportMeasureUnit);
198
    }
210 199

  
211
	public MapContextDrawer createMapContextDrawerInstance(Class drawerClazz)
212
	throws MapContextException {
213
		return createMapContextDrawerInstance(drawerClazz, "NONE");
214
	}
200
    public int getDefaultCartographicSupportReferenceSystem() {
201
        return getSymbolPreferences().getDefaultCartographicSupportReferenceSystem();
202
    }
215 203

  
216
	public MapContextDrawer createDefaultMapContextDrawerInstance()
217
	throws MapContextException {
204
    public void setDefaultCartographicSupportReferenceSystem(
205
            int defaultCartographicSupportReferenceSystem) {
206
        getSymbolPreferences().setDefaultCartographicSupportReferenceSystem(
207
                defaultCartographicSupportReferenceSystem);
208
    }
218 209

  
219
		return createMapContextDrawerInstance(drawerClazz, "default");
220
	}
210
    public MapContextDrawer createMapContextDrawerInstance(Class drawerClazz)
211
            throws MapContextException {
212
        return createMapContextDrawerInstance(drawerClazz, "NONE");
213
    }
221 214

  
222
	private MapContextDrawer createMapContextDrawerInstance(Class drawerClazz,
223
			String name) throws RegisteredClassInstantiationException {
224
		try {
225
                         MapContextDrawer drawer = (MapContextDrawer) drawerClazz.newInstance();
226
                         notifyObservers( CREATE_MAPCONTEXT_DRAWER, drawer);
227
			return drawer;
228
		} catch (Exception ex) {
229
			throw new RegisteredClassInstantiationException(
230
					MapContextDrawer.class, drawerClazz, name, ex);
231
		}
232
	}
215
    public MapContextDrawer createDefaultMapContextDrawerInstance()
216
            throws MapContextException {
233 217

  
234
	public void setDefaultMapContextDrawer(Class drawerClazz)
235
	throws MapContextException {
218
        return createMapContextDrawerInstance(drawerClazz, "default");
219
    }
236 220

  
237
		validateMapContextDrawer(drawerClazz);
238
		this.drawerClazz = drawerClazz;
239
                notifyObservers( SET_MAPCONTEXT_DRAWER, drawerClazz);
240
	}
221
    private MapContextDrawer createMapContextDrawerInstance(Class drawerClazz,
222
            String name) throws RegisteredClassInstantiationException {
223
        try {
224
            MapContextDrawer drawer = (MapContextDrawer) drawerClazz.newInstance();
225
            notifyObservers(CREATE_MAPCONTEXT_DRAWER, drawer);
226
            return drawer;
227
        } catch (Exception ex) {
228
            throw new RegisteredClassInstantiationException(
229
                    MapContextDrawer.class, drawerClazz, name, ex);
230
        }
231
    }
241 232

  
242
	public void validateMapContextDrawer(Class drawerClazz)
243
	throws MapContextException {
244
		if (!MapContextDrawer.class.isAssignableFrom(drawerClazz)) {
245
			throw new InvalidRegisteredClassException(MapContextDrawer.class,
246
					drawerClazz, "UNKNOWN");
247
		}
248
	}
233
    public void setDefaultMapContextDrawer(Class drawerClazz)
234
            throws MapContextException {
249 235

  
250
	public GraphicLayer createGraphicsLayer(IProjection projection) {
251
		DefaultGraphicLayer layer = new DefaultGraphicLayer();
252
		try {
253
			layer.initialize(projection);	
254
			layer.setLegend((IVectorLegend)createLegend(IVectorialUniqueValueLegend.LEGEND_NAME));
255
		} catch (Exception e) {
256
			logger.error("Error initializing the graphics layer", e);
257
		}
258
                return (GraphicLayer) notifyObservers( CREATE_GRAPHICS_LAYER, layer).getValue();
259
	}
236
        validateMapContextDrawer(drawerClazz);
237
        this.drawerClazz = drawerClazz;
238
        notifyObservers(SET_MAPCONTEXT_DRAWER, drawerClazz);
239
    }
260 240

  
261
	public String getDefaultVectorLegend() {
262
		return defaultVectorLegend;
263
	}
241
    public void validateMapContextDrawer(Class drawerClazz)
242
            throws MapContextException {
243
        if (!MapContextDrawer.class.isAssignableFrom(drawerClazz)) {
244
            throw new InvalidRegisteredClassException(MapContextDrawer.class,
245
                    drawerClazz, "UNKNOWN");
246
        }
247
    }
264 248

  
265
	public void setDefaultVectorLegend(String defaultVectorLegend) {
266
		this.defaultVectorLegend = defaultVectorLegend;
267
	}
249
    public GraphicLayer createGraphicsLayer(IProjection projection) {
250
        DefaultGraphicLayer layer = new DefaultGraphicLayer();
251
        try {
252
            layer.initialize(projection);
253
            layer.setLegend((IVectorLegend) createLegend(IVectorialUniqueValueLegend.LEGEND_NAME));
254
        } catch (Exception e) {
255
            logger.error("Error initializing the graphics layer", e);
256
        }
257
        return (GraphicLayer) notifyObservers(CREATE_GRAPHICS_LAYER, layer).getValue();
258
    }
268 259

  
269
	public void registerLegend(String legendName, Class legendClass)
270
	throws MapContextRuntimeException {
260
    public String getDefaultVectorLegend() {
261
        return defaultVectorLegend;
262
    }
271 263

  
272
		if (legendClass == null || !ILegend.class.isAssignableFrom(legendClass)) {
273
			throw new InvalidRegisteredClassException(ILegend.class,
274
					legendClass, legendName);
275
		}
264
    public void setDefaultVectorLegend(String defaultVectorLegend) {
265
        this.defaultVectorLegend = defaultVectorLegend;
266
    }
276 267

  
277
		legends.put(legendName, legendClass);
278
                notifyObservers( REGISTER_LEGEND, legendName, legendClass);
279
	}
268
    public void registerLegend(String legendName, Class legendClass)
269
            throws MapContextRuntimeException {
280 270

  
281
	public ILegend createLegend(String legendName)
282
	throws MapContextRuntimeException {
283
		Class legendClass = (Class) legends.get(legendName);
271
        if (legendClass == null || !ILegend.class.isAssignableFrom(legendClass)) {
272
            throw new InvalidRegisteredClassException(ILegend.class,
273
                    legendClass, legendName);
274
        }
284 275

  
285
		if (legendClass != null) {
286
			try {
287
                                ILegend legend = (ILegend) legendClass.newInstance();
288
                                return (ILegend) notifyObservers( CREATE_LEGEND, legend).getValue();
289
			} catch (InstantiationException e) {
290
				throw new RegisteredClassInstantiationException(ILegend.class,
291
						legendClass, legendName, e);
292
			} catch (IllegalAccessException e) {
293
				throw new RegisteredClassInstantiationException(ILegend.class,
294
						legendClass, legendName, e);
295
			}
296
		}
297
		return null;
298
	}
276
        legends.put(legendName, legendClass);
277
        notifyObservers(REGISTER_LEGEND, legendName, legendClass);
278
    }
299 279

  
280
    public ILegend createLegend(String legendName)
281
            throws MapContextRuntimeException {
282
        Class legendClass = (Class) legends.get(legendName);
283

  
284
        if (legendClass != null) {
285
            try {
286
                ILegend legend = (ILegend) legendClass.newInstance();
287
                return (ILegend) notifyObservers(CREATE_LEGEND, legend).getValue();
288
            } catch (InstantiationException e) {
289
                throw new RegisteredClassInstantiationException(ILegend.class,
290
                        legendClass, legendName, e);
291
            } catch (IllegalAccessException e) {
292
                throw new RegisteredClassInstantiationException(ILegend.class,
293
                        legendClass, legendName, e);
294
            }
295
        }
296
        return null;
297
    }
298

  
300 299
    public IVectorLegend createDefaultVectorLegend(int shapeType)
301
    throws MapContextRuntimeException {
300
            throws MapContextRuntimeException {
302 301
        try {
303 302
            // Create legend
304
            IVectorLegend legend =
305
                (IVectorLegend) createLegend(getDefaultVectorLegend());
303
            IVectorLegend legend
304
                    = (IVectorLegend) createLegend(getDefaultVectorLegend());
306 305
            if (legend == null) {
307 306
                return null;
308 307
            }
309 308
            // Set legend values
310 309
            legend.setShapeType(shapeType);
311 310
            ISymbol symbol = getSymbolManager().createSymbol(shapeType);
312
            if( symbol == null ) {
313
                String msg = "Can't create a legend for the shape type "+shapeType+". The type can be incorrect or there is not registered a symbol by default for that value. If this a was obtained from the store settings, review your FeatureType have correctly configured this value.";
311
            if (symbol == null) {
312
                String msg = "Can't create a legend for the shape type " + shapeType + ". The type can be incorrect or there is not registered a symbol by default for that value. If this a was obtained from the store settings, review your FeatureType have correctly configured this value.";
314 313
                throw new RuntimeException(msg);
315 314
            }
316 315
            legend.setDefaultSymbol(symbol);
317 316
            return legend;
318
        } catch(Exception e) {
317
        } catch (Exception e) {
319 318
            throw new MapContextRuntimeException(e);
320 319
        }
321 320
    }
322 321

  
323 322
    // =============================================================
324 323
    // Legend reading/writing
325
    
326
	public void registerLegendReader(String format, Class readerClass)
327
	throws MapContextRuntimeException {
328
		if (readerClass == null
329
				|| !ILegendReader.class.isAssignableFrom(readerClass)) {
330
			throw new InvalidRegisteredClassException(ILegendReader.class,
331
					readerClass, format);
332
		}
324
    public void registerLegendReader(String format, Class readerClass)
325
            throws MapContextRuntimeException {
326
        if (readerClass == null
327
                || !ILegendReader.class.isAssignableFrom(readerClass)) {
328
            throw new InvalidRegisteredClassException(ILegendReader.class,
329
                    readerClass, format);
330
        }
333 331

  
334
		legendReaders.put(format, readerClass);
335
                notifyObservers( REGISTER_LEGEND_READER, format, readerClass);
336
	}
337
	
332
        legendReaders.put(format, readerClass);
333
        notifyObservers(REGISTER_LEGEND_READER, format, readerClass);
334
    }
338 335

  
339
	public ILegendReader createLegendReader(String format)
340
	throws MapContextRuntimeException {
341
		Class legendReaderClazz = (Class) legendReaders.get(format);
336
    public ILegendReader createLegendReader(String format)
337
            throws MapContextRuntimeException {
338
        Class legendReaderClazz = (Class) legendReaders.get(format);
342 339

  
343
		if (legendReaderClazz != null) {
344
			try {
345
                                ILegendReader reader = (ILegendReader) legendReaderClazz.newInstance();
346
                                return (ILegendReader) notifyObservers( CREATE_LEGEND_READER, reader).getValue();
347
			} catch (InstantiationException e) {
348
				throw new RegisteredClassInstantiationException(
349
						ILegendReader.class, legendReaderClazz, format, e);
350
			} catch (IllegalAccessException e) {
351
				throw new RegisteredClassInstantiationException(
352
						ILegendReader.class, legendReaderClazz, format, e);
353
			}
354
		}
355
		return null;
356
	}
340
        if (legendReaderClazz != null) {
341
            try {
342
                ILegendReader reader = (ILegendReader) legendReaderClazz.newInstance();
343
                return (ILegendReader) notifyObservers(CREATE_LEGEND_READER, reader).getValue();
344
            } catch (InstantiationException e) {
345
                throw new RegisteredClassInstantiationException(
346
                        ILegendReader.class, legendReaderClazz, format, e);
347
            } catch (IllegalAccessException e) {
348
                throw new RegisteredClassInstantiationException(
349
                        ILegendReader.class, legendReaderClazz, format, e);
350
            }
351
        }
352
        return null;
353
    }
357 354

  
358
	public void registerLegendWriter(Class legendClass, String format,
359
			Class writerClass) throws MapContextRuntimeException {
360
		if (writerClass == null
361
				|| !ILegendWriter.class.isAssignableFrom(writerClass)
362
				|| legendClass == null
363
				|| !ILegend.class.isAssignableFrom(legendClass)) {
364
		    
365
			throw new InvalidRegisteredClassException(ILegendWriter.class,
366
					writerClass, format.concat("-").concat(
367
					    legendClass == null ? "Null" : legendClass.getName()));
368
		}
355
    public void registerLegendWriter(Class legendClass, String format,
356
            Class writerClass) throws MapContextRuntimeException {
357
        if (writerClass == null
358
                || !ILegendWriter.class.isAssignableFrom(writerClass)
359
                || legendClass == null
360
                || !ILegend.class.isAssignableFrom(legendClass)) {
369 361

  
370
		Map legendFormatWriters = (Map) legendWriters.get(format);
362
            throw new InvalidRegisteredClassException(ILegendWriter.class,
363
                    writerClass, format.concat("-").concat(
364
                            legendClass == null ? "Null" : legendClass.getName()));
365
        }
371 366

  
372
		synchronized (legendWriters) {
373
			if (legendFormatWriters == null) {
374
				legendFormatWriters = Collections
375
				.synchronizedMap(new HashMap());
376
				legendWriters.put(format, legendFormatWriters);
377
			}
378
		}
379
		legendFormatWriters.put(legendClass, writerClass);
380
                notifyObservers( REGISTER_LEGEND_WRITER, format, writerClass);
381
	}
367
        Map legendFormatWriters = (Map) legendWriters.get(format);
382 368

  
383
	public ILegendWriter createLegendWriter(Class legendClass, String format)
384
	throws MapContextRuntimeException {
385
	    
386
	    if (legendClass == null || format == null) {
387
	        return null;
388
	    }
389
	    
390
		Map legendFormatWriters = getLegendWritersForFormat(format);
369
        synchronized (legendWriters) {
370
            if (legendFormatWriters == null) {
371
                legendFormatWriters = Collections.synchronizedMap(new HashMap());
372
                legendWriters.put(format, legendFormatWriters);
373
            }
374
        }
375
        legendFormatWriters.put(legendClass, writerClass);
376
        notifyObservers(REGISTER_LEGEND_WRITER, format, writerClass);
377
    }
391 378

  
392
		if (legendFormatWriters != null) {
393
			Class legendWriterClazz = (Class) legendFormatWriters
394
			.get(legendClass);
379
    public ILegendWriter createLegendWriter(Class legendClass, String format)
380
            throws MapContextRuntimeException {
395 381

  
396
			if (legendWriterClazz != null) {
397
			    /*
398
			     * Found exact match
399
			     */
400
				try {
401
    					ILegendWriter writer = (ILegendWriter) legendWriterClazz.newInstance();
402
                                        return (ILegendWriter) notifyObservers( CREATE_LEGEND_READER, writer).getValue();
403
				} catch (InstantiationException e) {
404
					throw new RegisteredClassInstantiationException(
405
							ILegendWriter.class, legendWriterClazz, format
406
							.concat("-").concat(
407
							    legendClass == null ? "Null" : legendClass.getName()), e);
408
				} catch (IllegalAccessException e) {
409
					throw new RegisteredClassInstantiationException(
410
							ILegendWriter.class, legendWriterClazz, format
411
							.concat("-").concat(
412
							    legendClass == null ? "Null" : legendClass.getName()), e);
413
				}
414
			} else {
415
			    /*
416
			     * Trying to find superclass/superinterface of parameter
417
			     */
418
			    try {
419
			        return getSuperClassLegendWriter(legendFormatWriters, legendClass);
420
			    } catch (Exception exc) {
421
			        throw new MapContextRuntimeException(exc);
422
			    }
423
			}
424
		}
425
		return null;
426
	}
382
        if (legendClass == null || format == null) {
383
            return null;
384
        }
427 385

  
428
	private ILegendWriter getSuperClassLegendWriter(Map clsToWtr, Class legclass)
429
	throws Exception {
430
	    
431
	    if (!ILegend.class.isAssignableFrom(legclass)) {
432
	        // Class is not a legend
433
	        return null;
434
	    }
435
	    
436
	    Iterator kiter = clsToWtr.keySet().iterator();
437
	    Object oitem = null;
386
        Map legendFormatWriters = getLegendWritersForFormat(format);
387

  
388
        if (legendFormatWriters != null) {
389
            Class legendWriterClazz = (Class) legendFormatWriters
390
                    .get(legendClass);
391

  
392
            if (legendWriterClazz != null) {
393
                /*
394
                 * Found exact match
395
                 */
396
                try {
397
                    ILegendWriter writer = (ILegendWriter) legendWriterClazz.newInstance();
398
                    return (ILegendWriter) notifyObservers(CREATE_LEGEND_READER, writer).getValue();
399
                } catch (InstantiationException e) {
400
                    throw new RegisteredClassInstantiationException(
401
                            ILegendWriter.class, legendWriterClazz, format
402
                            .concat("-").concat(
403
                                    legendClass == null ? "Null" : legendClass.getName()), e);
404
                } catch (IllegalAccessException e) {
405
                    throw new RegisteredClassInstantiationException(
406
                            ILegendWriter.class, legendWriterClazz, format
407
                            .concat("-").concat(
408
                                    legendClass == null ? "Null" : legendClass.getName()), e);
409
                }
410
            } else {
411
                /*
412
                 * Trying to find superclass/superinterface of parameter
413
                 */
414
                try {
415
                    return getSuperClassLegendWriter(legendFormatWriters, legendClass);
416
                } catch (Exception exc) {
417
                    throw new MapContextRuntimeException(exc);
418
                }
419
            }
420
        }
421
        return null;
422
    }
423

  
424
    private ILegendWriter getSuperClassLegendWriter(Map clsToWtr, Class legclass)
425
            throws Exception {
426

  
427
        if (!ILegend.class.isAssignableFrom(legclass)) {
428
            // Class is not a legend
429
            return null;
430
        }
431

  
432
        Iterator kiter = clsToWtr.keySet().iterator();
433
        Object oitem = null;
438 434
        Class citem = null;
439
	    while (kiter.hasNext()) {
440
	        oitem = kiter.next();
441
	        if (oitem instanceof Class) {
442
	            citem = (Class) oitem; 
443
	            if (citem.isAssignableFrom(legclass)) {
444
	                /*
445
	                 * Found superclass/superinterface
446
	                 */
447
	                citem = (Class) clsToWtr.get(oitem);
448
	                return (ILegendWriter) citem.newInstance();
449
	            }
450
	        }
451
	    }
452
	    /*
453
	     * No superclass/superinterface found
454
	     */
435
        while (kiter.hasNext()) {
436
            oitem = kiter.next();
437
            if (oitem instanceof Class) {
438
                citem = (Class) oitem;
439
                if (citem.isAssignableFrom(legclass)) {
440
                    /*
441
                     * Found superclass/superinterface
442
                     */
443
                    citem = (Class) clsToWtr.get(oitem);
444
                    return (ILegendWriter) citem.newInstance();
445
                }
446
            }
447
        }
448
        /*
449
         * No superclass/superinterface found
450
         */
455 451
        return null;
456 452
    }
457 453

  
458 454
    private Map getLegendWritersForFormat(String format) {
459
		return (Map) legendWriters.get(format);
460
	}
461
	
455
        return (Map) legendWriters.get(format);
456
    }
457

  
462 458
    public List getLegendReadingFormats() {
463 459
        List resp = new ArrayList();
464 460
        Iterator iter = legendReaders.keySet().iterator();
......
476 472
        }
477 473
        return resp;
478 474
    }
479
	// =============================================================
475
    // =============================================================
480 476

  
481
	public IMultiLayerSymbol createMultiLayerSymbol(int shapeType)
482
	throws MapContextRuntimeException {
483
                IMultiLayerSymbol symbol = getSymbolManager().createMultiLayerSymbol(shapeType);
484
                return (IMultiLayerSymbol) notifyObservers( CREATE_SYMBOL, symbol).getValue();
485
	}
477
    public IMultiLayerSymbol createMultiLayerSymbol(int shapeType)
478
            throws MapContextRuntimeException {
479
        IMultiLayerSymbol symbol = getSymbolManager().createMultiLayerSymbol(shapeType);
480
        return (IMultiLayerSymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
481
    }
486 482

  
487
	public IMultiLayerSymbol createMultiLayerSymbol(String symbolName)
488
	throws MapContextRuntimeException {
489
                IMultiLayerSymbol symbol = getSymbolManager().createMultiLayerSymbol(symbolName);
490
                return (IMultiLayerSymbol) notifyObservers( CREATE_SYMBOL, symbol).getValue();
491
	}
483
    public IMultiLayerSymbol createMultiLayerSymbol(String symbolName)
484
            throws MapContextRuntimeException {
485
        IMultiLayerSymbol symbol = getSymbolManager().createMultiLayerSymbol(symbolName);
486
        return (IMultiLayerSymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
487
    }
492 488

  
493
	public ISymbol createSymbol(int shapeType, Color color)
494
	throws MapContextRuntimeException {
495
                ISymbol symbol = getSymbolManager().createSymbol(shapeType, color);
496
                return (ISymbol) notifyObservers( CREATE_SYMBOL, symbol).getValue();
497
	}
489
    public ISymbol createSymbol(int shapeType, Color color)
490
            throws MapContextRuntimeException {
491
        ISymbol symbol = getSymbolManager().createSymbol(shapeType, color);
492
        return (ISymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
493
    }
498 494

  
499
	public ISymbol createSymbol(int shapeType)
500
	throws MapContextRuntimeException {
501
                ISymbol symbol = getSymbolManager().createSymbol(shapeType);
502
                return (ISymbol) notifyObservers( CREATE_SYMBOL, symbol).getValue();
503
	}
495
    public ISymbol createSymbol(int shapeType)
496
            throws MapContextRuntimeException {
497
        ISymbol symbol = getSymbolManager().createSymbol(shapeType);
498
        return (ISymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
499
    }
504 500

  
505
	public ISymbol createSymbol(String symbolName, Color color)
506
	throws MapContextRuntimeException {
507
                ISymbol symbol = getSymbolManager().createSymbol(symbolName, color);
508
                return (ISymbol) notifyObservers( CREATE_SYMBOL, symbol).getValue();
509
	}
501
    public ISymbol createSymbol(String symbolName, Color color)
502
            throws MapContextRuntimeException {
503
        ISymbol symbol = getSymbolManager().createSymbol(symbolName, color);
504
        return (ISymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
505
    }
510 506

  
511
	public ISymbol createSymbol(String symbolName)
512
	throws MapContextRuntimeException {
513
                ISymbol symbol = getSymbolManager().createSymbol(symbolName);
514
                return (ISymbol) notifyObservers( CREATE_SYMBOL, symbol).getValue();
515
	}
507
    public ISymbol createSymbol(String symbolName)
508
            throws MapContextRuntimeException {
509
        ISymbol symbol = getSymbolManager().createSymbol(symbolName);
510
        return (ISymbol) notifyObservers(CREATE_SYMBOL, symbol).getValue();
511
    }
516 512

  
517
	public IWarningSymbol getWarningSymbol(String message, String symbolDesc,
518
			int symbolDrawExceptionType) throws MapContextRuntimeException {
519
		return getSymbolManager().getWarningSymbol(message, symbolDesc,
520
				symbolDrawExceptionType);
521
	}
513
    public IWarningSymbol getWarningSymbol(String message, String symbolDesc,
514
            int symbolDrawExceptionType) throws MapContextRuntimeException {
515
        return getSymbolManager().getWarningSymbol(message, symbolDesc,
516
                symbolDrawExceptionType);
517
    }
522 518

  
523
	public ISymbol[] loadSymbols(File folder, FileFilter filter)
524
	throws SymbolException {
525
                ISymbol[] symbols = getSymbolManager().loadSymbols(folder, filter);
526
                return (ISymbol[]) notifyObservers( LOAD_SYMBOLS, symbols).getValue();
527
	}
519
    public ISymbol[] loadSymbols(File folder, FileFilter filter)
520
            throws SymbolException {
521
        ISymbol[] symbols = getSymbolManager().loadSymbols(folder, filter);
522
        return (ISymbol[]) notifyObservers(LOAD_SYMBOLS, symbols).getValue();
523
    }
528 524

  
529
	public ISymbol[] loadSymbols(File folder) throws SymbolException {
530
                ISymbol[] symbols = getSymbolManager().loadSymbols(folder);
531
                return (ISymbol[]) notifyObservers( LOAD_SYMBOLS, symbols).getValue();
532
	}
525
    public ISymbol[] loadSymbols(File folder) throws SymbolException {
526
        ISymbol[] symbols = getSymbolManager().loadSymbols(folder);
527
        return (ISymbol[]) notifyObservers(LOAD_SYMBOLS, symbols).getValue();
528
    }
533 529

  
534
	public void registerMultiLayerSymbol(String symbolName, Class symbolClass)
535
	throws MapContextRuntimeException {
536
		getSymbolManager().registerMultiLayerSymbol(symbolName, symbolClass);
537
                notifyObservers( REGISTER_MULTILAYER_SYMBOL, symbolName, symbolClass);
538
	}
530
    public void registerMultiLayerSymbol(String symbolName, Class symbolClass)
531
            throws MapContextRuntimeException {
532
        getSymbolManager().registerMultiLayerSymbol(symbolName, symbolClass);
533
        notifyObservers(REGISTER_MULTILAYER_SYMBOL, symbolName, symbolClass);
534
    }
539 535

  
540
	public void registerMultiLayerSymbol(String symbolName, int[] shapeTypes,
541
			Class symbolClass) throws MapContextRuntimeException {
542
		getSymbolManager().registerMultiLayerSymbol(symbolName, shapeTypes,
543
				symbolClass);
544
                notifyObservers( REGISTER_MULTILAYER_SYMBOL, symbolName, symbolClass, shapeTypes);
545
	}
536
    public void registerMultiLayerSymbol(String symbolName, int[] shapeTypes,
537
            Class symbolClass) throws MapContextRuntimeException {
538
        getSymbolManager().registerMultiLayerSymbol(symbolName, shapeTypes,
539
                symbolClass);
540
        notifyObservers(REGISTER_MULTILAYER_SYMBOL, symbolName, symbolClass, shapeTypes);
541
    }
546 542

  
547
	public void registerSymbol(String symbolName, Class symbolClass)
548
	throws MapContextRuntimeException {
549
		getSymbolManager().registerSymbol(symbolName, symbolClass);
550
                notifyObservers( REGISTER_SYMBOL, symbolName, symbolClass);
551
	}
543
    public void registerSymbol(String symbolName, Class symbolClass)
544
            throws MapContextRuntimeException {
545
        getSymbolManager().registerSymbol(symbolName, symbolClass);
546
        notifyObservers(REGISTER_SYMBOL, symbolName, symbolClass);
547
    }
552 548

  
553
	public void registerSymbol(String symbolName, int[] shapeTypes,
554
			Class symbolClass) throws MapContextException {
555
		getSymbolManager().registerSymbol(symbolName, shapeTypes, symbolClass);
556
                notifyObservers( REGISTER_SYMBOL, symbolName, symbolClass, shapeTypes);
557
	}
549
    public void registerSymbol(String symbolName, int[] shapeTypes,
550
            Class symbolClass) throws MapContextException {
551
        getSymbolManager().registerSymbol(symbolName, shapeTypes, symbolClass);
552
        notifyObservers(REGISTER_SYMBOL, symbolName, symbolClass, shapeTypes);
553
    }
558 554

  
559
	public void saveSymbol(ISymbol symbol, String fileName, File folder,
560
			boolean overwrite) throws SymbolException {
561
		getSymbolManager().saveSymbol(symbol, fileName, folder, overwrite);
562
	}
555
    public void saveSymbol(ISymbol symbol, String fileName, File folder,
556
            boolean overwrite) throws SymbolException {
557
        getSymbolManager().saveSymbol(symbol, fileName, folder, overwrite);
558
    }
563 559

  
564
	public void saveSymbol(ISymbol symbol, String fileName, File folder)
565
	throws SymbolException {
566
		getSymbolManager().saveSymbol(symbol, fileName, folder);
567
	}
560
    public void saveSymbol(ISymbol symbol, String fileName, File folder)
561
            throws SymbolException {
562
        getSymbolManager().saveSymbol(symbol, fileName, folder);
563
    }
568 564

  
569
	public FLayer createLayer(String layerName, DataStoreParameters parameters)
570
	throws LoadLayerException {
571
                FLayer layer = LayerFactory.getInstance().createLayer(layerName, parameters);
572
                return (FLayer) notifyObservers( CREATE_LAYER, layer).getValue();
573
	}
565
    public FLayer createLayer(String layerName, DataStoreParameters parameters)
566
            throws LoadLayerException {
567
        FLayer layer = LayerFactory.getInstance().createLayer(layerName, parameters);
568
        return (FLayer) notifyObservers(CREATE_LAYER, layer).getValue();
569
    }
574 570

  
575
	public FLayer createLayer(String layerName, DataStore store)
576
	throws LoadLayerException {
577
                FLayer layer = LayerFactory.getInstance().createLayer(layerName, store);
578
                return (FLayer) notifyObservers( CREATE_LAYER, layer).getValue();
579
	}
571
    public FLayer createLayer(String layerName, DataStore store)
572
            throws LoadLayerException {
573
        FLayer layer = LayerFactory.getInstance().createLayer(layerName, store);
574
        return (FLayer) notifyObservers(CREATE_LAYER, layer).getValue();
575
    }
580 576

  
581 577
    public ILegend getLegend(DataStore dataStore) {
582 578
        ILegend legend = null;
583 579

  
584 580
        File file = getResourcePath(dataStore, SymbolManager.LEGEND_FILE_EXTENSION.substring(1));
585 581
        try {
586
            if ( (file != null) && (file.exists()) ) {
582
            if ((file != null) && (file.exists())) {
587 583
                PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
588 584
                FileInputStream is = new FileInputStream(file);
589 585
                legend = (ILegend) persistenceManager.getObject(is);
......
596 592
        }
597 593

  
598 594
        //If the legend is null, next option is to check if the store has the getLegend method
599
        if ( legend == null ) {
595
        if (legend == null) {
600 596
            try {
601 597
                legend = (IVectorLegend) dataStore.invokeDynMethod("getLegend", null);
602 598
            } catch (DynMethodNotSupportedException e) {
......
610 606
        }
611 607

  
612 608
        //If legend is null, last step is just try to create the legend by default
613
        if ( legend == null ) {
609
        if (legend == null) {
614 610
            FeatureType featureType;
615 611
            try {
616 612
                featureType = (((FeatureStore) dataStore).getDefaultFeatureType());
617 613
                int indexGeom = featureType.getDefaultGeometryAttributeIndex();
618
                if( indexGeom <0 ) {
614
                if (indexGeom < 0) {
619 615
                    throw new IllegalArgumentException("The layer don't has a geometry column.");
620 616
                }
621 617
                int typeShape = featureType.getAttributeDescriptor(indexGeom).getGeometryType();
......
632 628
        ILabelingStrategy labelingStrategy = null;
633 629

  
634 630
        File file = getResourcePath(dataStore, SymbolManager.LABELINGSTRATEGY_FILE_EXTENSION.substring(1));
635
        try{
636
            if ((file != null) && (file.exists())){
631
        try {
632
            if ((file != null) && (file.exists())) {
637 633
                PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
638 634
                FileInputStream is = new FileInputStream(file);
639 635
                labelingStrategy = (ILabelingStrategy) persistenceManager.getObject(is);
640 636
                is.close();
641 637
            }
642
        } catch (FileNotFoundException e){
638
        } catch (FileNotFoundException e) {
643 639
            logger.error("Label strategy not found", e);
644 640
        } catch (IOException e) {
645 641
            logger.error("Error reading the labeling strategy", e);
646 642
        }
647
        
643

  
648 644
        //If the legend is null, next option is to check if the store has the getLegend method
649
        if (labelingStrategy == null){
645
        if (labelingStrategy == null) {
650 646
            try {
651
                labelingStrategy =
652
                    (ILabelingStrategy) dataStore.invokeDynMethod("getLabeling",
653
                        null);
647
                labelingStrategy
648
                        = (ILabelingStrategy) dataStore.invokeDynMethod("getLabeling",
649
                                null);
654 650
            } catch (DynMethodNotSupportedException e1) {
655 651
                labelingStrategy = null;
656 652
            } catch (DynMethodException e1) {
657 653
                logger.error("Can't load the specific lebeling strategy provided for the datastore {}.",
658
                    dataStore.getName(),
659
                    e1);
654
                        dataStore.getName(),
655
                        e1);
660 656
            }
661
        }        
662
        
657
        }
658

  
663 659
        return labelingStrategy;
664 660
    }
665
    
661

  
666 662
    private Object call(Object instance, String methodName, Class[] signature, Object[] params) {
667 663
        try {
668 664
            Method method = instance.getClass().getMethod(methodName, signature);
669
            if( method == null ) {
665
            if (method == null) {
670 666
                return null;
671 667
            }
672 668
            Object value = method.invoke(instance, params);
......
684 680
        }
685 681
    }
686 682

  
687
    private File getResourcePath(DataStore dataStore, String resource){
683
    private File getResourcePath(DataStore dataStore, String resource) {
688 684
        //Loading the file from a store based on file
689 685
        DataServerExplorer explorer = null;
690 686
        try {
691 687
            explorer = dataStore.getExplorer();
692
            if( explorer==null ) {
688
            if (explorer == null) {
693 689
                return null;
694 690
            }
695
            return (File)call(explorer,"getResourcePath",
696
                    new Class[] {DataStore.class,String.class},
697
                    new Object[] {dataStore,resource});
691
            return (File) call(explorer, "getResourcePath",
692
                    new Class[]{DataStore.class, String.class},
693
                    new Object[]{dataStore, resource});
698 694
        } catch (Exception e) {
699 695
            logger.warn(
700
                "Can't locate a specific legend provided by the explorer "
701
                + explorer, e);
696
                    "Can't locate a specific legend provided by the explorer "
697
                    + explorer, e);
702 698
            return null;
703 699
        } finally {
704
            if( explorer!=null ) {
700
            if (explorer != null) {
705 701
                explorer.dispose();
706 702
                explorer = null;
707 703
            }
......
709 705
    }
710 706

  
711 707
    private Map iconLayers = new HashMap(); //  (Map<String storeProviderName, String iconName>)
712
    
708

  
713 709
    public void registerIconLayer(String storeProviderName, String iconName) {
714
    	if( storeProviderName == null || iconName == null ) {
715
    		logger.info("registerIconLayer, storeProviderName or iconName are null");
716
    		return;
717
    	}
710
        if (storeProviderName == null || iconName == null) {
711
            logger.info("registerIconLayer, storeProviderName or iconName are null");
712
            return;
713
        }
718 714
        String storeName = storeProviderName.trim().toLowerCase();
719
    	if( storeName.length()==0 || iconName.trim().length()==0 ) {
720
    		logger.info("registerIconLayer, invalid storeProviderName or iconName");
721
    		return;
722
    	}
723
    	iconLayers.put(storeName, iconName);
724
        notifyObservers( REGISTER_ICON_LAYER, storeName, iconName);
715
        if (storeName.length() == 0 || iconName.trim().length() == 0) {
716
            logger.info("registerIconLayer, invalid storeProviderName or iconName");
717
            return;
718
        }
719
        iconLayers.put(storeName, iconName);
720
        notifyObservers(REGISTER_ICON_LAYER, storeName, iconName);
725 721
    }
726
    
722

  
727 723
    public String getIconLayer(DataStore store) {
728
    	String name = (String) iconLayers.get(store.getProviderName().trim().toLowerCase());
729
    	if( name == null ) {
730
    		return "layer-icon";
731
    	}
732
    	return name;
724
        String name = (String) iconLayers.get(store.getProviderName().trim().toLowerCase());
725
        if (name == null) {
726
            return "layer-icon";
727
        }
728
        return name;
733 729
    }
734 730

  
735 731
    /* (non-Javadoc)
......
737 733
     */
738 734
    public IProjection getDefaultCRS() {
739 735
        IProjection crs = CRSFactory.getCRS("EPSG:4326");
740
        return (IProjection) notifyObservers( GET_DEFAULT_CRS, crs).getValue();
736
        return (IProjection) notifyObservers(GET_DEFAULT_CRS, crs).getValue();
741 737
    }
742 738

  
743
    public Notification notifyLoadMapContext(MapContext mapContext) {    
739
    public Notification notifyLoadMapContext(MapContext mapContext) {
744 740
        return this.observableHelper.notifyObservers(this, LOAD_MAPCONTEXT, mapContext);
745 741
    }
746
    
747
    public Notification notifyLoadLayer(FLayer layer) {    
742

  
743
    public Notification notifyLoadLayer(FLayer layer) {
748 744
        return this.observableHelper.notifyObservers(this, LOAD_LAYER, layer);
749 745
    }
750
    
746

  
751 747
    public void addObserver(Observer o) {
752 748
        this.observableHelper.addObserver(o);
753 749
    }
......
761 757
    }
762 758

  
763 759
    protected Notification notifyObservers(String type, Object value) {
764
        return this.observableHelper.notifyObservers(this, type, value);  
760
        return this.observableHelper.notifyObservers(this, type, value);
765 761
    }
766 762

  
767 763
    protected Notification notifyObservers(String type, Object value1, Object value2) {
......
789 785
    public void setColorTableLibraryFolder(File colorTableLibraryFolder) {
790 786
        this.colorTableLibraryFolder = colorTableLibraryFolder;
791 787
    }
792
    
793
    
788

  
794 789
}

Also available in: Unified diff