Revision 33339 branches/v2_0_0_prep/libraries/libFMap_mapcontext/src/org/gvsig/fmap/mapcontext/impl/DefaultMapContextManager.java
DefaultMapContextManager.java | ||
---|---|---|
30 | 30 |
import java.awt.Font; |
31 | 31 |
import java.io.File; |
32 | 32 |
import java.io.FileFilter; |
33 |
import java.io.FileInputStream; |
|
34 |
import java.lang.reflect.Method; |
|
33 | 35 |
import java.util.Collections; |
34 | 36 |
import java.util.HashMap; |
35 | 37 |
import java.util.Map; |
36 | 38 |
import java.util.Random; |
37 | 39 |
|
38 | 40 |
import org.cresques.cts.IProjection; |
41 |
import org.gvsig.fmap.dal.DataServerExplorer; |
|
39 | 42 |
import org.gvsig.fmap.dal.DataStore; |
40 | 43 |
import org.gvsig.fmap.dal.DataStoreParameters; |
44 |
import org.gvsig.fmap.dal.exception.DataException; |
|
45 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
46 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
41 | 47 |
import org.gvsig.fmap.mapcontext.MapContextDrawer; |
42 | 48 |
import org.gvsig.fmap.mapcontext.MapContextException; |
43 | 49 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
... | ... | |
59 | 65 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolException; |
60 | 66 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
61 | 67 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolPreferences; |
68 |
import org.gvsig.tools.ToolsLocator; |
|
69 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
|
70 |
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException; |
|
71 |
import org.gvsig.tools.persistence.PersistenceManager; |
|
72 |
import org.gvsig.tools.persistence.PersistentState; |
|
62 | 73 |
import org.slf4j.Logger; |
63 | 74 |
import org.slf4j.LoggerFactory; |
64 | 75 |
|
... | ... | |
69 | 80 |
*/ |
70 | 81 |
public class DefaultMapContextManager implements MapContextManager { |
71 | 82 |
private static final Logger LOG = LoggerFactory.getLogger(MapContextManager.class); |
72 |
|
|
83 |
|
|
73 | 84 |
private Class drawerClazz = DefaultMapContextDrawer.class; |
74 | 85 |
|
75 | 86 |
private Map legends = Collections.synchronizedMap(new HashMap()); |
... | ... | |
179 | 190 |
} |
180 | 191 |
|
181 | 192 |
public MapContextDrawer createMapContextDrawerInstance(Class drawerClazz) |
182 |
throws MapContextException {
|
|
193 |
throws MapContextException { |
|
183 | 194 |
return createMapContextDrawerInstance(drawerClazz, "NONE"); |
184 | 195 |
} |
185 | 196 |
|
186 | 197 |
public MapContextDrawer createDefaultMapContextDrawerInstance() |
187 |
throws MapContextException {
|
|
198 |
throws MapContextException { |
|
188 | 199 |
|
189 | 200 |
return createMapContextDrawerInstance(drawerClazz, "default"); |
190 | 201 |
} |
... | ... | |
200 | 211 |
} |
201 | 212 |
|
202 | 213 |
public void setDefaultMapContextDrawer(Class drawerClazz) |
203 |
throws MapContextException {
|
|
214 |
throws MapContextException { |
|
204 | 215 |
|
205 | 216 |
validateMapContextDrawer(drawerClazz); |
206 | 217 |
this.drawerClazz = drawerClazz; |
207 | 218 |
} |
208 | 219 |
|
209 | 220 |
public void validateMapContextDrawer(Class drawerClazz) |
210 |
throws MapContextException {
|
|
221 |
throws MapContextException { |
|
211 | 222 |
if (!MapContextDrawer.class.isAssignableFrom(drawerClazz)) { |
212 | 223 |
throw new InvalidRegisteredClassException(MapContextDrawer.class, |
213 | 224 |
drawerClazz, "UNKNOWN"); |
... | ... | |
234 | 245 |
} |
235 | 246 |
|
236 | 247 |
public void registerLegend(String legendName, Class legendClass) |
237 |
throws MapContextRuntimeException {
|
|
248 |
throws MapContextRuntimeException { |
|
238 | 249 |
|
239 | 250 |
if (legendClass == null || !ILegend.class.isAssignableFrom(legendClass)) { |
240 | 251 |
throw new InvalidRegisteredClassException(ILegend.class, |
... | ... | |
245 | 256 |
} |
246 | 257 |
|
247 | 258 |
public ILegend createLegend(String legendName) |
248 |
throws MapContextRuntimeException {
|
|
259 |
throws MapContextRuntimeException { |
|
249 | 260 |
Class legendClass = (Class) legends.get(legendName); |
250 | 261 |
|
251 | 262 |
if (legendClass != null) { |
... | ... | |
263 | 274 |
} |
264 | 275 |
|
265 | 276 |
public IVectorLegend createDefaultVectorLegend(int shapeType) |
266 |
throws MapContextRuntimeException {
|
|
277 |
throws MapContextRuntimeException { |
|
267 | 278 |
try { |
268 | 279 |
Random rand = new Random(); |
269 |
|
|
280 |
|
|
270 | 281 |
int numreg = rand.nextInt(255 / 2); |
271 | 282 |
double div = (1 - rand.nextDouble() * 0.66) * 0.9; |
272 | 283 |
Color randomColor = new Color(((int) (255 * div + (numreg * rand |
273 | 284 |
.nextDouble()))) % 255, ((int) (255 * div + (numreg * rand |
274 |
.nextDouble()))) % 255, ((int) (255 * div + (numreg * rand |
|
275 |
.nextDouble()))) % 255); |
|
285 |
.nextDouble()))) % 255, ((int) (255 * div + (numreg * rand
|
|
286 |
.nextDouble()))) % 255);
|
|
276 | 287 |
IVectorLegend legend = (IVectorLegend) createLegend(getDefaultVectorLegend()); |
277 | 288 |
if( legend == null ) { |
278 | 289 |
return null; |
279 | 290 |
} |
280 | 291 |
ISymbol defaultSymbol = |
281 |
getSymbolManager().createSymbol(shapeType, randomColor);
|
|
292 |
getSymbolManager().createSymbol(shapeType, randomColor); |
|
282 | 293 |
legend.setDefaultSymbol(defaultSymbol); |
283 | 294 |
return legend; |
284 | 295 |
} catch(Exception e) { |
... | ... | |
287 | 298 |
} |
288 | 299 |
|
289 | 300 |
public void registerLegendReader(String format, Class readerClass) |
290 |
throws MapContextRuntimeException {
|
|
301 |
throws MapContextRuntimeException { |
|
291 | 302 |
if (readerClass == null |
292 | 303 |
|| !ILegendReader.class.isAssignableFrom(readerClass)) { |
293 | 304 |
throw new InvalidRegisteredClassException(ILegendReader.class, |
... | ... | |
298 | 309 |
} |
299 | 310 |
|
300 | 311 |
public ILegendReader createLegendReader(String format) |
301 |
throws MapContextRuntimeException {
|
|
312 |
throws MapContextRuntimeException { |
|
302 | 313 |
Class legendReaderClazz = (Class) legendReaders.get(format); |
303 | 314 |
|
304 | 315 |
if (legendReaderClazz != null) { |
... | ... | |
328 | 339 |
synchronized (legendWriters) { |
329 | 340 |
if (legendFormatWriters == null) { |
330 | 341 |
legendFormatWriters = Collections |
331 |
.synchronizedMap(new HashMap());
|
|
342 |
.synchronizedMap(new HashMap()); |
|
332 | 343 |
legendWriters.put(format, legendFormatWriters); |
333 | 344 |
} |
334 | 345 |
} |
... | ... | |
337 | 348 |
} |
338 | 349 |
|
339 | 350 |
public ILegendWriter createLegendWriter(String legendName, String format) |
340 |
throws MapContextRuntimeException {
|
|
351 |
throws MapContextRuntimeException { |
|
341 | 352 |
Map legendFormatWriters = getLegendWritersForFormat(format); |
342 | 353 |
|
343 | 354 |
if (legendFormatWriters != null) { |
344 | 355 |
Class legendWriterClazz = (Class) legendFormatWriters |
345 |
.get(legendName);
|
|
356 |
.get(legendName); |
|
346 | 357 |
|
347 | 358 |
if (legendWriterClazz != null) { |
348 | 359 |
try { |
... | ... | |
350 | 361 |
} catch (InstantiationException e) { |
351 | 362 |
throw new RegisteredClassInstantiationException( |
352 | 363 |
ILegendWriter.class, legendWriterClazz, format |
353 |
.concat("-").concat(legendName), e);
|
|
364 |
.concat("-").concat(legendName), e); |
|
354 | 365 |
} catch (IllegalAccessException e) { |
355 | 366 |
throw new RegisteredClassInstantiationException( |
356 | 367 |
ILegendWriter.class, legendWriterClazz, format |
357 |
.concat("-").concat(legendName), e);
|
|
368 |
.concat("-").concat(legendName), e); |
|
358 | 369 |
} |
359 | 370 |
} |
360 | 371 |
} |
... | ... | |
366 | 377 |
} |
367 | 378 |
|
368 | 379 |
public IMultiLayerSymbol createMultiLayerSymbol(int shapeType) |
369 |
throws MapContextRuntimeException {
|
|
380 |
throws MapContextRuntimeException { |
|
370 | 381 |
return getSymbolManager().createMultiLayerSymbol(shapeType); |
371 | 382 |
} |
372 | 383 |
|
373 | 384 |
public IMultiLayerSymbol createMultiLayerSymbol(String symbolName) |
374 |
throws MapContextRuntimeException {
|
|
385 |
throws MapContextRuntimeException { |
|
375 | 386 |
return getSymbolManager().createMultiLayerSymbol(symbolName); |
376 | 387 |
} |
377 | 388 |
|
378 | 389 |
public ISymbol createSymbol(int shapeType, Color color) |
379 |
throws MapContextRuntimeException {
|
|
390 |
throws MapContextRuntimeException { |
|
380 | 391 |
return getSymbolManager().createSymbol(shapeType, color); |
381 | 392 |
} |
382 | 393 |
|
383 | 394 |
public ISymbol createSymbol(int shapeType) |
384 |
throws MapContextRuntimeException {
|
|
395 |
throws MapContextRuntimeException { |
|
385 | 396 |
return getSymbolManager().createSymbol(shapeType); |
386 | 397 |
} |
387 | 398 |
|
388 | 399 |
public ISymbol createSymbol(String symbolName, Color color) |
389 |
throws MapContextRuntimeException {
|
|
400 |
throws MapContextRuntimeException { |
|
390 | 401 |
return getSymbolManager().createSymbol(symbolName, color); |
391 | 402 |
} |
392 | 403 |
|
393 | 404 |
public ISymbol createSymbol(String symbolName) |
394 |
throws MapContextRuntimeException {
|
|
405 |
throws MapContextRuntimeException { |
|
395 | 406 |
return getSymbolManager().createSymbol(symbolName); |
396 | 407 |
} |
397 | 408 |
|
... | ... | |
402 | 413 |
} |
403 | 414 |
|
404 | 415 |
public ISymbol[] loadSymbols(File folder, FileFilter filter) |
405 |
throws SymbolException {
|
|
416 |
throws SymbolException { |
|
406 | 417 |
return getSymbolManager().loadSymbols(folder, filter); |
407 | 418 |
} |
408 | 419 |
|
... | ... | |
411 | 422 |
} |
412 | 423 |
|
413 | 424 |
public void registerMultiLayerSymbol(String symbolName, Class symbolClass) |
414 |
throws MapContextRuntimeException {
|
|
425 |
throws MapContextRuntimeException { |
|
415 | 426 |
getSymbolManager().registerMultiLayerSymbol(symbolName, symbolClass); |
416 | 427 |
} |
417 | 428 |
|
... | ... | |
422 | 433 |
} |
423 | 434 |
|
424 | 435 |
public void registerSymbol(String symbolName, Class symbolClass) |
425 |
throws MapContextRuntimeException {
|
|
436 |
throws MapContextRuntimeException { |
|
426 | 437 |
getSymbolManager().registerSymbol(symbolName, symbolClass); |
427 | 438 |
} |
428 | 439 |
|
... | ... | |
437 | 448 |
} |
438 | 449 |
|
439 | 450 |
public void saveSymbol(ISymbol symbol, String fileName, File folder) |
440 |
throws SymbolException {
|
|
451 |
throws SymbolException { |
|
441 | 452 |
getSymbolManager().saveSymbol(symbol, fileName, folder); |
442 | 453 |
} |
443 | 454 |
|
444 | 455 |
public FLayer createLayer(String layerName, DataStoreParameters parameters) |
445 |
throws LoadLayerException {
|
|
456 |
throws LoadLayerException { |
|
446 | 457 |
return LayerFactory.getInstance().createLayer(layerName, parameters); |
447 | 458 |
} |
448 | 459 |
|
449 | 460 |
public FLayer createLayer(String layerName, DataStore store) |
450 |
throws LoadLayerException {
|
|
461 |
throws LoadLayerException { |
|
451 | 462 |
return LayerFactory.getInstance().createLayer(layerName, store); |
452 | 463 |
} |
464 |
|
|
465 |
public ILegend getLegend(DataStore dataStore) { |
|
466 |
ILegend legend = null; |
|
467 |
|
|
468 |
//Loading the legend from a store based on file |
|
469 |
try { |
|
470 |
// This access by reflection is done because the libFMap_dalfile project |
|
471 |
// has to be divided in several projects. The commented code is the fine |
|
472 |
// code whereas the used code has to be deleted. |
|
473 |
// |
|
474 |
// if (dataStore.getExplorer() instanceof FilesystemServerExplorer){ |
|
475 |
// File file = ((FilesystemServerExplorer)dataStore.getExplorer()).getResourcePath(dataStore, "gvl"); |
|
476 |
// if ((file != null) && (file.exists())){ |
|
477 |
// PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager(); |
|
478 |
// PersistentState persistentState = persistenceManager.loadState(new FileInputStream(file)); |
|
479 |
// legend = (ILegend)persistenceManager.create(persistentState); |
|
480 |
// } |
|
481 |
// } |
|
482 |
DataServerExplorer dataServerExplorer = dataStore.getExplorer(); |
|
483 |
Class[] args = new Class[2]; |
|
484 |
args[0] = DataStore.class; |
|
485 |
args[1] = String.class; |
|
486 |
Method method = dataStore.getExplorer().getClass().getMethod("getResourcePath", args); |
|
487 |
if (method != null){ |
|
488 |
Object[] params = new Object[2]; |
|
489 |
params[0] = dataStore; |
|
490 |
params[1] = "gvl"; |
|
491 |
File file = (File)method.invoke(dataServerExplorer, params); |
|
492 |
if ((file != null) && (file.exists())){ |
|
493 |
PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager(); |
|
494 |
PersistentState persistentState = persistenceManager.loadState(new FileInputStream(file)); |
|
495 |
legend = (ILegend)persistenceManager.create(persistentState); |
|
496 |
} |
|
497 |
} |
|
498 |
|
|
499 |
} catch (Exception e) { |
|
500 |
LOG.error("Error creating the legend the explorer", e); |
|
501 |
} |
|
502 |
|
|
503 |
//If the legend is null, next option is to check if the store has the getLegend method |
|
504 |
if (legend == null){ |
|
505 |
try { |
|
506 |
legend = (IVectorLegend) dataStore.invokeDynMethod( |
|
507 |
"getLegend", null); |
|
508 |
} catch (DynMethodNotSupportedException e) { |
|
509 |
// Do nothing |
|
510 |
} catch (DynMethodException e) { |
|
511 |
LOG.error("Can't load the specific legend provided for the store {}.", dataStore.getName(), e); |
|
512 |
} |
|
513 |
} |
|
514 |
|
|
515 |
//If legend is null, last step is just try to create the legend by default |
|
516 |
if( legend == null ) { |
|
517 |
FeatureType featureType; |
|
518 |
try { |
|
519 |
featureType = (((FeatureStore)dataStore).getDefaultFeatureType()); |
|
520 |
int indexGeom = featureType.getDefaultGeometryAttributeIndex(); |
|
521 |
int typeShape = featureType.getAttributeDescriptor(indexGeom).getGeometryType(); |
|
522 |
legend = MapContextLocator.getMapContextManager().createDefaultVectorLegend(typeShape); |
|
523 |
} catch (DataException e) { |
|
524 |
LOG.error("Error getting the default feature type", e); |
|
525 |
} |
|
526 |
} |
|
527 |
|
|
528 |
return legend; |
|
529 |
} |
|
530 |
|
|
453 | 531 |
} |
Also available in: Unified diff