Revision 18995 trunk/libraries/libTopology/src/org/gvsig/topology/SimpleTopologyErrorContainer.java

View differences:

SimpleTopologyErrorContainer.java
48 48
 */
49 49
package org.gvsig.topology;
50 50

  
51
import java.awt.Color;
52
import java.sql.Types;
51 53
import java.util.ArrayList;
52 54
import java.util.Iterator;
53 55
import java.util.List;
56
import java.util.Random;
54 57

  
55 58
import org.cresques.cts.ICoordTrans;
56 59
import org.cresques.cts.IProjection;
57 60

  
61
import com.hardcode.gdbms.engine.values.ValueFactory;
62
import com.iver.cit.gvsig.drivers.TopologyErrorMemoryDriver;
63
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
64
import com.iver.cit.gvsig.fmap.core.FShape;
65
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
66
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol;
67
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
68
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
69
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
70
import com.iver.cit.gvsig.fmap.layers.FLayerGenericVectorial;
58 71
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
72
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
73
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
59 74
import com.iver.utiles.XMLEntity;
60 75

  
61 76
/**
......
73 88

  
74 89
	private List<TopologyError> topologyErrors;
75 90
	
91
	private Topology topology;
92
	
76 93
	/**
77 94
	 * Number of errors that has been marked as exceptions
78 95
	 */
......
83 100
		topologyErrors = new ArrayList<TopologyError>();
84 101
	}
85 102
	
103
	public void setTopology(Topology topology){
104
		this.topology = topology;
105
	}
106
	
107
	public Topology getTopology(){
108
		return this.topology;
109
	}
110
	
86 111
	public Object clone(){
87 112
		SimpleTopologyErrorContainer newContainer = new SimpleTopologyErrorContainer();
88 113
		for(int i = 0; i < topologyErrors.size(); i++){
89 114
			newContainer.addTopologyError(topologyErrors.get(i));
90 115
		}
116
		newContainer.setTopology(this.topology);
91 117
		return newContainer;
92 118
	}
93 119

  
......
347 373
			int numberOfErrors = xml.getIntProperty("numberOfErrors");
348 374
			for(int i = 0; i < numberOfErrors; i++){
349 375
				XMLEntity errorXML = xml.getChild(i);
350
				TopologyError error = new TopologyError();
376
				TopologyError error = new TopologyError(topology);
351 377
				error.setXMLEntity(errorXML);
352 378
				topologyErrors.add(error);
353 379
			}//for
......
381 407
			}
382 408
		}
383 409
	}
410
	
411
	
412
	private Color getRandomColor(){
413
		//This code is copied from LegendFactory 
414
		Random rand = new Random();
415
		int numreg = rand.nextInt(255/2);
416
		double div = (1-rand.nextDouble()*0.66)*0.9;
417
		return new Color(
418
				((int) (255*div + (numreg * rand.nextDouble()))) % 255,
419
				((int) (255*div + (numreg * rand.nextDouble()))) % 255,
420
				((int) (255*div + (numreg * rand.nextDouble()))) % 255);
421
	}
422

  
423
	/**
424
	 * Returns a representation of the topology errors contained in as a fmap
425
	 * layer.
426
	 * 
427
	 * @param name name of the layer
428
	 * @projection projection of the layer
429
	 */
430
	public FLyrVect getAsFMapLayer(String name, IProjection projection) {
431
		FLayerGenericVectorial solution = new
432
		 	FLayerGenericVectorial();
433
		solution.setName(name);
434
		solution.setProjection(projection);
435
		solution.setDriver(new TopologyErrorMemoryDriver(name, this));
436
		try {
437
			solution.load();
438
			VectorialUniqueValueLegend defaultLegend = 
439
				LegendFactory.createVectorialUniqueValueLegend(FShape.MULTI);
440
			defaultLegend.setClassifyingFieldNames(new String[] {TopologyErrorMemoryDriver.LEGEND_FIELD});
441
			defaultLegend.setClassifyingFieldTypes(new int[]{Types.VARCHAR});
442
			defaultLegend.setDefaultSymbol(SymbologyFactory.
443
					createDefaultSymbolByShapeType(FShape.MULTI, Color.BLACK));
444
			
445
			List<ITopologyRule> rules = topology.getAllRules();
446
			
447
			//Now we are going to set a symbol for each kind of topology rule
448
			ISymbol theSymbol = null;
449
			Color c = null;
450
			
451
			for(int i = 0; i < rules.size(); i++){
452
				ITopologyRule rule = rules.get(i);
453
				c = getRandomColor();
454
				theSymbol =	SymbologyFactory.
455
					createDefaultSymbolByShapeType(FShape.MULTI, c);
456
				theSymbol.setDescription(rule.getName());
457
				
458
				if (theSymbol instanceof IMarkerSymbol) {
459
					((IMarkerSymbol) theSymbol).setSize(1);
460
				}
461

  
462
				if (theSymbol instanceof ILineSymbol) {
463
					((ILineSymbol) theSymbol).setLineWidth(1);
464
				}	
465

  
466
				if (theSymbol instanceof IFillSymbol) {
467
						((IFillSymbol) theSymbol).getOutline().setLineColor(c);
468
						((IFillSymbol) theSymbol).getOutline().setLineWidth(1);
469
						((IFillSymbol) theSymbol).setFillColor(c);
470
				}
471

  
472
				defaultLegend.addSymbol(ValueFactory.createValue(rule.getName()), theSymbol);	
473
			}//for
474
			
475
			solution.setLegend(defaultLegend);
476
			
477
		} catch (LoadLayerException e) {
478
			e.printStackTrace();
479
		}
480
		
481
		
482
		return solution;
483
	}
484

  
384 485
}

Also available in: Unified diff