Revision 40927 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
21 21
 * For any additional information, do not hesitate to contact us
22 22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2009 {DiSiD Technologies}  {Create Manager to register MapContextDrawer implementation}
27
 */
28 24
package org.gvsig.fmap.mapcontext.impl;
29 25

  
30 26
import java.awt.Color;
......
35 31
import java.io.FileNotFoundException;
36 32
import java.io.IOException;
37 33
import java.lang.reflect.Method;
34
import java.util.ArrayList;
38 35
import java.util.Collections;
39 36
import java.util.HashMap;
37
import java.util.Iterator;
38
import java.util.List;
40 39
import java.util.Map;
41 40

  
42 41
import org.cresques.cts.IProjection;
......
297 296
        }
298 297
    }
299 298

  
299
    // =============================================================
300
    // Legend reading/writing
301
    
300 302
	public void registerLegendReader(String format, Class readerClass)
301 303
	throws MapContextRuntimeException {
302 304
		if (readerClass == null
......
307 309

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

  
311 314
	public ILegendReader createLegendReader(String format)
312 315
	throws MapContextRuntimeException {
......
326 329
		return null;
327 330
	}
328 331

  
329
	public void registerLegendWriter(String legendName, String format,
332
	public void registerLegendWriter(Class legendClass, String format,
330 333
			Class writerClass) throws MapContextRuntimeException {
331 334
		if (writerClass == null
332
				|| !ILegendWriter.class.isAssignableFrom(writerClass)) {
335
				|| !ILegendWriter.class.isAssignableFrom(writerClass)
336
				|| legendClass == null
337
				|| !ILegend.class.isAssignableFrom(legendClass)) {
338
		    
333 339
			throw new InvalidRegisteredClassException(ILegendWriter.class,
334
					writerClass, format.concat("-").concat(legendName));
340
					writerClass, format.concat("-").concat(
341
					    legendClass == null ? "Null" : legendClass.getName()));
335 342
		}
336 343

  
337 344
		Map legendFormatWriters = (Map) legendWriters.get(format);
......
343 350
				legendWriters.put(format, legendFormatWriters);
344 351
			}
345 352
		}
346

  
347
		legendFormatWriters.put(legendName, writerClass);
353
		legendFormatWriters.put(legendClass, writerClass);
348 354
	}
349 355

  
350
	public ILegendWriter createLegendWriter(String legendName, String format)
356
	public ILegendWriter createLegendWriter(Class legendClass, String format)
351 357
	throws MapContextRuntimeException {
358
	    
359
	    if (legendClass == null || format == null) {
360
	        return null;
361
	    }
362
	    
352 363
		Map legendFormatWriters = getLegendWritersForFormat(format);
353 364

  
354 365
		if (legendFormatWriters != null) {
355 366
			Class legendWriterClazz = (Class) legendFormatWriters
356
			.get(legendName);
367
			.get(legendClass);
357 368

  
358 369
			if (legendWriterClazz != null) {
370
			    /*
371
			     * Found exact match
372
			     */
359 373
				try {
360 374
					return (ILegendWriter) legendWriterClazz.newInstance();
361 375
				} catch (InstantiationException e) {
362 376
					throw new RegisteredClassInstantiationException(
363 377
							ILegendWriter.class, legendWriterClazz, format
364
							.concat("-").concat(legendName), e);
378
							.concat("-").concat(
379
							    legendClass == null ? "Null" : legendClass.getName()), e);
365 380
				} catch (IllegalAccessException e) {
366 381
					throw new RegisteredClassInstantiationException(
367 382
							ILegendWriter.class, legendWriterClazz, format
368
							.concat("-").concat(legendName), e);
383
							.concat("-").concat(
384
							    legendClass == null ? "Null" : legendClass.getName()), e);
369 385
				}
386
			} else {
387
			    /*
388
			     * Trying to find superclass/superinterface of parameter
389
			     */
390
			    try {
391
			        return getSuperClassLegendWriter(legendFormatWriters, legendClass);
392
			    } catch (Exception exc) {
393
			        throw new MapContextRuntimeException(exc);
394
			    }
370 395
			}
371 396
		}
372 397
		return null;
373 398
	}
374 399

  
375
	private Map getLegendWritersForFormat(String format) {
400
	private ILegendWriter getSuperClassLegendWriter(Map clsToWtr, Class legclass)
401
	throws Exception {
402
	    
403
	    if (!ILegend.class.isAssignableFrom(legclass)) {
404
	        // Class is not a legend
405
	        return null;
406
	    }
407
	    
408
	    Iterator kiter = clsToWtr.keySet().iterator();
409
	    Object oitem = null;
410
        Class citem = null;
411
	    while (kiter.hasNext()) {
412
	        oitem = kiter.next();
413
	        if (oitem instanceof Class) {
414
	            citem = (Class) oitem; 
415
	            if (citem.isAssignableFrom(legclass)) {
416
	                /*
417
	                 * Found superclass/superinterface
418
	                 */
419
	                citem = (Class) clsToWtr.get(oitem);
420
	                return (ILegendWriter) citem.newInstance();
421
	            }
422
	        }
423
	    }
424
	    /*
425
	     * No superclass/superinterface found
426
	     */
427
        return null;
428
    }
429

  
430
    private Map getLegendWritersForFormat(String format) {
376 431
		return (Map) legendWriters.get(format);
377 432
	}
433
	
434
    public List getLegendReadingFormats() {
435
        List resp = new ArrayList();
436
        Iterator iter = legendReaders.keySet().iterator();
437
        while (iter.hasNext()) {
438
            resp.add(iter.next());
439
        }
440
        return resp;
441
    }
378 442

  
443
    public List getLegendWritingFormats() {
444
        List resp = new ArrayList();
445
        Iterator iter = legendWriters.keySet().iterator();
446
        while (iter.hasNext()) {
447
            resp.add(iter.next());
448
        }
449
        return resp;
450
    }
451
	// =============================================================
452

  
379 453
	public IMultiLayerSymbol createMultiLayerSymbol(int shapeType)
380 454
	throws MapContextRuntimeException {
381 455
		return getSymbolManager().createMultiLayerSymbol(shapeType);
......
596 670
    public IProjection getDefaultCRS() {
597 671
        return CRSFactory.getCRS("EPSG:4326");
598 672
    }
673

  
599 674
}

Also available in: Unified diff