Revision 44984 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.symbology/org.gvsig.symbology.lib/org.gvsig.symbology.lib.impl/src/main/java/org/gvsig/symbology/fmap/mapcontext/rendering/symbol/line/impl/SimpleLineSymbol.java

View differences:

SimpleLineSymbol.java
30 30
import org.gvsig.compat.print.PrintAttributes;
31 31
import org.gvsig.fmap.dal.feature.Feature;
32 32
import org.gvsig.fmap.geom.Geometry;
33
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
34 33
import org.gvsig.fmap.geom.GeometryLocator;
35 34
import org.gvsig.fmap.geom.GeometryManager;
36 35
import org.gvsig.fmap.geom.exception.CreateGeometryException;
......
44 43
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
45 44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol;
46 45
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ArrowDecoratorStyle;
47
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.Line2DOffset;
48 46
import org.gvsig.tools.ToolsLocator;
49 47
import org.gvsig.tools.dynobject.DynStruct;
50 48
import org.gvsig.tools.persistence.PersistenceManager;
......
55 53
import org.slf4j.Logger;
56 54
import org.slf4j.LoggerFactory;
57 55

  
58

  
59 56
/**
60
 * SimpleLineSymbol is the most basic symbol for the representation of line objects.
61
 * Allows to define the width of the line, the color and the drawn pattern.
57
 * SimpleLineSymbol is the most basic symbol for the representation of line
58
 * objects. Allows to define the width of the line, the color and the drawn
59
 * pattern.
62 60
 *
63 61
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
64 62
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
65 63
 */
66 64
public class SimpleLineSymbol extends AbstractLineSymbol implements ISimpleLineSymbol {
67 65

  
68
	private static final Logger LOG = LoggerFactory.getLogger(SimpleLineSymbol.class);
66
    private static final Logger LOG = LoggerFactory.getLogger(SimpleLineSymbol.class);
69 67
    public static final String SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleLineSymbol";
70 68

  
71 69
    private static final String SELECTION_SYMBOL = "symbolForSelection";
72 70

  
73
	SimpleLineSymbol symbolForSelection;
74
	private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
71
    SimpleLineSymbol symbolForSelection;
72
    private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
75 73

  
76
	public SimpleLineSymbol() {
77
		super();
78
		setLineWidth(1d);
79
	}
74
    public SimpleLineSymbol() {
75
        super();
76
        setLineWidth(1d);
77
    }
80 78

  
81
	public ISymbol getSymbolForSelection() {
82
		if (symbolForSelection == null) {
83
			symbolForSelection = (SimpleLineSymbol) cloneForSelection();
84
		}else{
85
		    symbolForSelection.setColor(MapContext.getSelectionColor());
86
		}
87
		return symbolForSelection;
88
	}
79
    public ISymbol getSymbolForSelection() {
80
        if (symbolForSelection == null) {
81
            symbolForSelection = (SimpleLineSymbol) cloneForSelection();
82
        } else {
83
            symbolForSelection.setColor(MapContext.getSelectionColor());
84
        }
85
        return symbolForSelection;
86
    }
89 87

  
90
	public void draw(Graphics2D g, AffineTransform affineTransform,
91
			Geometry geom, Feature feature, Cancellable cancel) {
92
        
93
        if( true ) { 
88
    public void draw(Graphics2D g, AffineTransform affineTransform,
89
            Geometry geom, Feature feature, Cancellable cancel) {
90

  
91
        if (true) {
94 92
            // Esto deberia ser para optimiza el pintado de 
95 93
            // geometrias grandes.
96 94
            try {
97 95
                Geometry env = geom.getEnvelope().getGeometry();
98 96
                env.transform(affineTransform);
99 97
                Envelope env2 = env.getEnvelope();
100
                if( env2.getLength(0)<1.5 && env2.getLength(1)<1.5 ) {
98
                if (env2.getLength(0) < 1.5 && env2.getLength(1) < 1.5) {
101 99
                    g.setColor(getColor());
102 100
                    Point upperCorner = env2.getUpperCorner();
103 101
                    int x = (int) upperCorner.getX();
......
105 103
                    g.drawLine(x, y, x, y);
106 104
                    return;
107 105
                }
108
            } catch(Exception ex) {
109
				LOG.warn("Error optimizing the drawing of the geometry. Continues with normal drawing.", ex);
106
            } catch (Exception ex) {
107
                LOG.warn("Error optimizing the drawing of the geometry. Continues with normal drawing.", ex);
110 108
                // Do nothing, continue with the draw of the original geometry
111 109
            }
112 110
        }
113
        
114
		Geometry geomToDraw = geom;
115
		g.setStroke(getLineStyle().getStroke());
116 111

  
117
		if (getLineStyle().getOffset() != 0) {
118
			double offset = getLineStyle().getOffset();
119
			try {
120
				geomToDraw =
121
						geomManager.createSurface(Line2DOffset.offsetLine(
122
								geomToDraw.getShape(), offset), SUBTYPES.GEOM2D);
123
			} catch (CreateGeometryException e) {
124
				LOG.warn("Error creating a polygon with an offset", e);
125
			}
126
		}
127
		g.setColor(getColor());
128
                g.draw(geomToDraw.getShape(affineTransform));
112
        Geometry geomToDraw = geom;
113
        g.setStroke(getLineStyle().getStroke());
129 114

  
130
		ArrowDecoratorStyle arrowDecorator = (ArrowDecoratorStyle) getLineStyle().getArrowDecorator();
115
        if (getLineStyle()!=null && getLineStyle().getOffset() != 0) {
116
            double offset = getLineStyle().getOffset();
117
            try {
118
                geomToDraw = geomToDraw.offset(offset);
119
            } catch (Exception e) {
120
                LOG.warn("Error creating a polygon with an offset", e);
121
            }
122
        }
123
        g.setColor(getColor());
124
        g.draw(geomToDraw.getShape(affineTransform));
131 125

  
132
		if (arrowDecorator != null) {
133
			try {
134
				arrowDecorator.draw(g, affineTransform, geomToDraw, feature);
135
			} catch (CreateGeometryException e) {
136
				LOG.warn("Error drawing geometry.", e);
137
			}
138
		}
139
	}
126
        ArrowDecoratorStyle arrowDecorator = (ArrowDecoratorStyle) getLineStyle().getArrowDecorator();
140 127

  
141
	public int getOnePointRgb() {
142
		return getColor().getRGB();
143
	}
128
        if (arrowDecorator != null) {
129
            try {
130
                arrowDecorator.draw(g, affineTransform, geomToDraw, feature);
131
            } catch (CreateGeometryException e) {
132
                LOG.warn("Error drawing geometry.", e);
133
            }
134
        }
135
    }
144 136

  
145
	public void drawInsideRectangle(Graphics2D g,
146
			AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
147
		g.setColor(getColor());
148
		g.setStroke(getLineStyle().getStroke());
149
		super.drawInsideRectangle(g, scaleInstance, r, properties);
150
	}
137
    public int getOnePointRgb() {
138
        return getColor().getRGB();
139
    }
151 140

  
152
	public void setLineWidth(double width) {
153
		getLineStyle().setLineWidth((float) width);
154
	}
141
    public void drawInsideRectangle(Graphics2D g,
142
            AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
143
        g.setColor(getColor());
144
        g.setStroke(getLineStyle().getStroke());
145
        super.drawInsideRectangle(g, scaleInstance, r, properties);
146
    }
155 147

  
156
	public double getLineWidth() {
157
		return getLineStyle().getLineWidth();
158
	}
148
    public void setLineWidth(double width) {
149
        getLineStyle().setLineWidth((float) width);
150
    }
159 151

  
160
	public Object clone() throws CloneNotSupportedException {
161
		SimpleLineSymbol copy = (SimpleLineSymbol) super.clone();
152
    public double getLineWidth() {
153
        return getLineStyle().getLineWidth();
154
    }
162 155

  
163
		if (symbolForSelection != null) {
164
			copy.symbolForSelection = (SimpleLineSymbol) symbolForSelection
165
					.clone();
166
		}
156
    public Object clone() throws CloneNotSupportedException {
157
        SimpleLineSymbol copy = (SimpleLineSymbol) super.clone();
167 158

  
168
		return copy;
169
	}
159
        if (symbolForSelection != null) {
160
            copy.symbolForSelection = (SimpleLineSymbol) symbolForSelection
161
                    .clone();
162
        }
170 163

  
164
        return copy;
165
    }
166

  
171 167
    public void loadFromState(PersistentState state) throws PersistenceException {
172 168
        // Set parent style properties
173 169
        super.loadFromState(state);
......
180 176
        super.saveToState(state);
181 177

  
182 178
        // Save own properties
183
        if (this.symbolForSelection != null){
179
        if (this.symbolForSelection != null) {
184 180
            state.set(SELECTION_SYMBOL, this.getSymbolForSelection());
185 181
        }
186 182
    }
187 183

  
184
    public static class RegisterPersistence implements Callable {
188 185

  
186
        public Object call() throws Exception {
187
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
188
            if (manager.getDefinition(SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) {
189
                DynStruct definition = manager.addDefinition(
190
                        SimpleLineSymbol.class,
191
                        SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME,
192
                        SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
193
                        null,
194
                        null
195
                );
196
                // Extend the LineSymbol base definition
197
                definition.extend(manager.getDefinition(LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME));
189 198

  
190
	public static class RegisterPersistence implements Callable {
191

  
192
		public Object call() throws Exception {
193
			PersistenceManager manager = ToolsLocator.getPersistenceManager();
194
			if( manager.getDefinition(SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
195
				DynStruct definition = manager.addDefinition(
196
						SimpleLineSymbol.class,
197
						SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME,
198
						SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
199
						null,
200
						null
201
				);
202
				// Extend the LineSymbol base definition
203
				definition.extend(manager.getDefinition(LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME));
204

  
205 199
                definition.addDynFieldObject(SELECTION_SYMBOL).setClassOfValue(SimpleLineSymbol.class);
206 200

  
207
			}
208
			return Boolean.TRUE;
209
		}
201
            }
202
            return Boolean.TRUE;
203
        }
210 204

  
211
	}
205
    }
212 206

  
213
	public static class RegisterSymbol implements Callable {
207
    public static class RegisterSymbol implements Callable {
214 208

  
215
		public Object call() throws Exception {
216
			int[] shapeTypes;
217
			SymbolManager manager = MapContextLocator.getSymbolManager();
209
        public Object call() throws Exception {
210
            int[] shapeTypes;
211
            SymbolManager manager = MapContextLocator.getSymbolManager();
218 212

  
219
	        shapeTypes = new int[] { Geometry.TYPES.CURVE, Geometry.TYPES.ARC,
220
	                Geometry.TYPES.MULTICURVE, Geometry.TYPES.CIRCUMFERENCE,
221
	                Geometry.TYPES.PERIELLIPSE, Geometry.TYPES.SPLINE,
222
	                Geometry.TYPES.LINE, Geometry.TYPES.MULTILINE};
223
	        manager.registerSymbol(ILineSymbol.SYMBOL_NAME,
224
	            shapeTypes,
225
	            SimpleLineSymbol.class);
213
            shapeTypes = new int[]{Geometry.TYPES.CURVE, Geometry.TYPES.ARC,
214
                Geometry.TYPES.MULTICURVE, Geometry.TYPES.CIRCUMFERENCE,
215
                Geometry.TYPES.PERIELLIPSE, Geometry.TYPES.SPLINE,
216
                Geometry.TYPES.LINE, Geometry.TYPES.MULTILINE};
217
            manager.registerSymbol(ILineSymbol.SYMBOL_NAME,
218
                    shapeTypes,
219
                    SimpleLineSymbol.class);
226 220

  
227
			return Boolean.TRUE;
228
		}
221
            return Boolean.TRUE;
222
        }
229 223

  
230
	}
231
        
232
}
224
    }
225

  
226
}

Also available in: Unified diff