svn-gvsig-desktop / 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 @ 47790
History | View | Annotate | Download (12.4 KB)
1 | 40560 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40560 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | *
|
||
6 | 40435 | jjdelcerro | * This program is free software; you can redistribute it and/or
|
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40560 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | 40560 | jjdelcerro | *
|
11 | 40435 | jjdelcerro | * 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.
|
||
15 | 40560 | jjdelcerro | *
|
16 | 40435 | jjdelcerro | * 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 | 40560 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | 40435 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 40560 | jjdelcerro | *
|
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.impl; |
||
25 | |||
26 | 45743 | fdiaz | import java.awt.Color; |
27 | 40435 | jjdelcerro | import java.awt.Graphics2D; |
28 | import java.awt.Rectangle; |
||
29 | 45749 | fdiaz | import java.awt.Shape; |
30 | 40435 | jjdelcerro | import java.awt.geom.AffineTransform; |
31 | import org.gvsig.fmap.dal.feature.Feature; |
||
32 | import org.gvsig.fmap.geom.Geometry; |
||
33 | import org.gvsig.fmap.geom.GeometryLocator; |
||
34 | import org.gvsig.fmap.geom.GeometryManager; |
||
35 | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
||
36 | 43377 | jjdelcerro | import org.gvsig.fmap.geom.primitive.Envelope; |
37 | 44545 | omartinez | import org.gvsig.fmap.geom.primitive.Point; |
38 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.MapContextLocator; |
39 | 47476 | fdiaz | import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport; |
40 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
41 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
||
42 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol; |
||
43 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol; |
||
44 | 47476 | fdiaz | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol; |
45 | 40435 | jjdelcerro | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ArrowDecoratorStyle; |
46 | 47476 | fdiaz | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.IArrowDecoratorStyle; |
47 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ILineStyle; |
||
48 | 40435 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
49 | import org.gvsig.tools.dynobject.DynStruct; |
||
50 | import org.gvsig.tools.persistence.PersistenceManager; |
||
51 | import org.gvsig.tools.persistence.PersistentState; |
||
52 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
53 | 45743 | fdiaz | import org.gvsig.tools.swing.api.TransparencySupport; |
54 | 40435 | jjdelcerro | import org.gvsig.tools.task.Cancellable; |
55 | import org.gvsig.tools.util.Callable; |
||
56 | import org.slf4j.Logger; |
||
57 | import org.slf4j.LoggerFactory; |
||
58 | |||
59 | /**
|
||
60 | 44984 | fdiaz | * SimpleLineSymbol is the most basic symbol for the representation of line
|
61 | * objects. Allows to define the width of the line, the color and the drawn
|
||
62 | * pattern.
|
||
63 | 40435 | jjdelcerro | *
|
64 | * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
|
||
65 | * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
||
66 | */
|
||
67 | 45743 | fdiaz | public class SimpleLineSymbol extends AbstractLineSymbol implements ISimpleLineSymbol, TransparencySupport { |
68 | 40435 | jjdelcerro | |
69 | 44984 | fdiaz | private static final Logger LOG = LoggerFactory.getLogger(SimpleLineSymbol.class); |
70 | 40435 | jjdelcerro | public static final String SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleLineSymbol"; |
71 | 42352 | fdiaz | |
72 | 40435 | jjdelcerro | private static final String SELECTION_SYMBOL = "symbolForSelection"; |
73 | |||
74 | 44984 | fdiaz | SimpleLineSymbol symbolForSelection; |
75 | private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
||
76 | 40435 | jjdelcerro | |
77 | 44984 | fdiaz | public SimpleLineSymbol() {
|
78 | super();
|
||
79 | setLineWidth(1d);
|
||
80 | 47470 | fdiaz | } |
81 | 42352 | fdiaz | |
82 | 45743 | fdiaz | @Override
|
83 | 47790 | fdiaz | public ISymbol getSymbolForSelection(Color selectionColor) { |
84 | 44984 | fdiaz | if (symbolForSelection == null) { |
85 | 47790 | fdiaz | symbolForSelection = (SimpleLineSymbol) cloneForSelection(selectionColor); |
86 | 44984 | fdiaz | } else {
|
87 | 47790 | fdiaz | symbolForSelection.setColor(selectionColor); |
88 | 44984 | fdiaz | } |
89 | 45743 | fdiaz | if (symbolForSelection instanceof TransparencySupport) { |
90 | 47470 | fdiaz | symbolForSelection.setTransparency(this.getTransparency());
|
91 | 45743 | fdiaz | } |
92 | 44984 | fdiaz | return symbolForSelection;
|
93 | } |
||
94 | 40435 | jjdelcerro | |
95 | 47470 | fdiaz | @Override
|
96 | 47476 | fdiaz | public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel, Rectangle r) { |
97 | if(r != null){ |
||
98 | geom = this.getSampleGeometry(r);
|
||
99 | } |
||
100 | 44984 | fdiaz | |
101 | 45743 | fdiaz | Color c = getColor();
|
102 | 47470 | fdiaz | |
103 | 44984 | fdiaz | if (true) { |
104 | 47476 | fdiaz | // Esto deberia ser para optimizar el pintado de geometrias grandes.
|
105 | 43407 | jjdelcerro | try {
|
106 | Geometry env = geom.getEnvelope().getGeometry(); |
||
107 | env.transform(affineTransform); |
||
108 | Envelope env2 = env.getEnvelope(); |
||
109 | 44984 | fdiaz | if (env2.getLength(0) < 1.5 && env2.getLength(1) < 1.5) { |
110 | 45743 | fdiaz | g.setColor(c); |
111 | 44545 | omartinez | Point upperCorner = env2.getUpperCorner();
|
112 | int x = (int) upperCorner.getX(); |
||
113 | int y = (int) upperCorner.getY(); |
||
114 | g.drawLine(x, y, x, y); |
||
115 | return;
|
||
116 | } |
||
117 | 44984 | fdiaz | } catch (Exception ex) { |
118 | LOG.warn("Error optimizing the drawing of the geometry. Continues with normal drawing.", ex);
|
||
119 | 43407 | jjdelcerro | // Do nothing, continue with the draw of the original geometry
|
120 | } |
||
121 | 43377 | jjdelcerro | } |
122 | 40435 | jjdelcerro | |
123 | 47476 | fdiaz | Geometry geomToDraw; |
124 | // try {
|
||
125 | // geomToDraw = geom.toLines();
|
||
126 | // } catch (Exception ex) {
|
||
127 | geomToDraw = geom.cloneGeometry(); |
||
128 | // }
|
||
129 | ILineStyle lineStyle = getLineStyle(); |
||
130 | |||
131 | if (lineStyle != null) { |
||
132 | if(lineStyle instanceof CartographicSupport && r == null){ |
||
133 | ((CartographicSupport)lineStyle).setCartographicContext(this.getCartographicContext());
|
||
134 | g.setStroke(lineStyle.getCartographicStroke()); |
||
135 | } else {
|
||
136 | g.setStroke(lineStyle.getStroke()); |
||
137 | } |
||
138 | 40435 | jjdelcerro | |
139 | 44984 | fdiaz | } |
140 | 45749 | fdiaz | if(geomToDraw != null) { |
141 | g.setColor(c); |
||
142 | 47476 | fdiaz | Shape shape = null; |
143 | geomToDraw.transform(affineTransform); |
||
144 | double offset = lineStyle.getOffset();
|
||
145 | if(offset != 0) { |
||
146 | geomToDraw = applyOffset(this.toCartographicUnits(offset), r, geomToDraw);
|
||
147 | } |
||
148 | if(geomToDraw != null){ |
||
149 | shape = geomToDraw.getShape(); |
||
150 | 40435 | jjdelcerro | |
151 | 47476 | fdiaz | if (shape != null) { |
152 | g.draw(shape); |
||
153 | if (lineStyle != null) { |
||
154 | ArrowDecoratorStyle arrowDecorator = (ArrowDecoratorStyle) lineStyle.getArrowDecorator(); |
||
155 | |||
156 | if (arrowDecorator != null) { |
||
157 | try {
|
||
158 | if(arrowDecorator instanceof CartographicSupport){ |
||
159 | if(r == null){ |
||
160 | arrowDecorator.setCartographicContext(this.getCartographicContext());
|
||
161 | } else {
|
||
162 | arrowDecorator.setCartographicContext((CartographicContext)null);
|
||
163 | } |
||
164 | } |
||
165 | //Pasamos una transformaci?n afin identidad porque la geometr?a ya viene transformada
|
||
166 | arrowDecorator.draw(g, null, geomToDraw, f);
|
||
167 | } catch (CreateGeometryException e) {
|
||
168 | LOG.warn("Error drawing geometry.", e);
|
||
169 | } |
||
170 | 45749 | fdiaz | } |
171 | } |
||
172 | } |
||
173 | 44984 | fdiaz | } |
174 | } |
||
175 | } |
||
176 | 40435 | jjdelcerro | |
177 | 47476 | fdiaz | private Geometry applyOffset(double offset, Rectangle r, Geometry geomToDraw) { |
178 | if (offset != 0) { |
||
179 | try {
|
||
180 | geomToDraw = geomToDraw.offset(offset); |
||
181 | // if(offset > 0 ){
|
||
182 | // geomToDraw.flip();
|
||
183 | // }
|
||
184 | } catch (Exception e) { |
||
185 | LOG.warn("Error creating a polygon with an offset", e);
|
||
186 | } |
||
187 | } |
||
188 | return geomToDraw;
|
||
189 | 44984 | fdiaz | } |
190 | 40435 | jjdelcerro | |
191 | 47476 | fdiaz | // @Override
|
192 | // public int getOnePointRgb() {
|
||
193 | // return getColor().getRGB();
|
||
194 | // }
|
||
195 | 40435 | jjdelcerro | |
196 | 47470 | fdiaz | @Override
|
197 | 44984 | fdiaz | public void setLineWidth(double width) { |
198 | getLineStyle().setLineWidth((float) width);
|
||
199 | } |
||
200 | 40435 | jjdelcerro | |
201 | 47470 | fdiaz | @Override
|
202 | 44984 | fdiaz | public double getLineWidth() { |
203 | return getLineStyle().getLineWidth();
|
||
204 | } |
||
205 | 40435 | jjdelcerro | |
206 | 47470 | fdiaz | @Override
|
207 | 44984 | fdiaz | public Object clone() throws CloneNotSupportedException { |
208 | SimpleLineSymbol copy = (SimpleLineSymbol) super.clone();
|
||
209 | 40435 | jjdelcerro | |
210 | 44984 | fdiaz | if (symbolForSelection != null) { |
211 | copy.symbolForSelection = (SimpleLineSymbol) symbolForSelection |
||
212 | .clone(); |
||
213 | } |
||
214 | 42352 | fdiaz | |
215 | 44984 | fdiaz | return copy;
|
216 | } |
||
217 | |||
218 | 47470 | fdiaz | @Override
|
219 | 40435 | jjdelcerro | public void loadFromState(PersistentState state) throws PersistenceException { |
220 | // Set parent style properties
|
||
221 | super.loadFromState(state);
|
||
222 | |||
223 | this.symbolForSelection = (SimpleLineSymbol) state.get(SELECTION_SYMBOL);
|
||
224 | } |
||
225 | |||
226 | 47470 | fdiaz | @Override
|
227 | 40435 | jjdelcerro | public void saveToState(PersistentState state) throws PersistenceException { |
228 | // Save parent fill symbol properties
|
||
229 | super.saveToState(state);
|
||
230 | |||
231 | // Save own properties
|
||
232 | 44984 | fdiaz | if (this.symbolForSelection != null) { |
233 | 40435 | jjdelcerro | state.set(SELECTION_SYMBOL, this.getSymbolForSelection());
|
234 | } |
||
235 | } |
||
236 | |||
237 | 45743 | fdiaz | @Override
|
238 | public void setTransparency(double transparency) { |
||
239 | 47470 | fdiaz | Color c = getColor();
|
240 | c = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (transparency * 255)); |
||
241 | setColor(c); |
||
242 | |||
243 | 45743 | fdiaz | ISymbol selectionSymbol = this.symbolForSelection;
|
244 | if (selectionSymbol != null && selectionSymbol instanceof TransparencySupport) { |
||
245 | ((TransparencySupport) selectionSymbol).setTransparency(transparency); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | @Override
|
||
250 | public double getTransparency() { |
||
251 | 47470 | fdiaz | return ((double)this.getAlpha())/255; |
252 | 45743 | fdiaz | } |
253 | |||
254 | 44984 | fdiaz | public static class RegisterPersistence implements Callable { |
255 | 40435 | jjdelcerro | |
256 | 47470 | fdiaz | @Override
|
257 | 44984 | fdiaz | public Object call() throws Exception { |
258 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
259 | if (manager.getDefinition(SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) { |
||
260 | DynStruct definition = manager.addDefinition( |
||
261 | SimpleLineSymbol.class, |
||
262 | SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME, |
||
263 | SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
|
||
264 | null,
|
||
265 | null
|
||
266 | ); |
||
267 | // Extend the LineSymbol base definition
|
||
268 | definition.extend(manager.getDefinition(LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME)); |
||
269 | 40435 | jjdelcerro | |
270 | definition.addDynFieldObject(SELECTION_SYMBOL).setClassOfValue(SimpleLineSymbol.class); |
||
271 | |||
272 | 44984 | fdiaz | } |
273 | return Boolean.TRUE; |
||
274 | } |
||
275 | 42352 | fdiaz | |
276 | 44984 | fdiaz | } |
277 | 40435 | jjdelcerro | |
278 | 44984 | fdiaz | public static class RegisterSymbol implements Callable { |
279 | 40435 | jjdelcerro | |
280 | 47470 | fdiaz | @Override
|
281 | 44984 | fdiaz | public Object call() throws Exception { |
282 | int[] shapeTypes; |
||
283 | SymbolManager manager = MapContextLocator.getSymbolManager(); |
||
284 | 40435 | jjdelcerro | |
285 | 44984 | fdiaz | shapeTypes = new int[]{Geometry.TYPES.CURVE, Geometry.TYPES.ARC, |
286 | Geometry.TYPES.MULTICURVE, Geometry.TYPES.CIRCUMFERENCE, |
||
287 | Geometry.TYPES.PERIELLIPSE, Geometry.TYPES.SPLINE, |
||
288 | Geometry.TYPES.LINE, Geometry.TYPES.MULTILINE}; |
||
289 | manager.registerSymbol(ILineSymbol.SYMBOL_NAME, |
||
290 | shapeTypes, |
||
291 | SimpleLineSymbol.class); |
||
292 | 40435 | jjdelcerro | |
293 | 44984 | fdiaz | return Boolean.TRUE; |
294 | } |
||
295 | 42352 | fdiaz | |
296 | 44984 | fdiaz | } |
297 | |||
298 | 47476 | fdiaz | @Override
|
299 | public void setCartographicContext(CartographicContext ctx) { |
||
300 | super.setCartographicContext(ctx);
|
||
301 | if (getLineStyle() != null |
||
302 | && getLineStyle().getArrowDecorator() != null) {
|
||
303 | IArrowDecoratorStyle arrowDecorator = getLineStyle().getArrowDecorator(); |
||
304 | if(arrowDecorator != null){ |
||
305 | if (arrowDecorator instanceof CartographicSupport) { |
||
306 | ((CartographicSupport) arrowDecorator).setCartographicContext(ctx); |
||
307 | } |
||
308 | if (arrowDecorator.getMarker() != null) { |
||
309 | IMarkerSymbol marker = arrowDecorator.getMarker(); |
||
310 | if (marker instanceof CartographicSupport) { |
||
311 | ((CartographicSupport) marker).setCartographicContext(ctx); |
||
312 | } |
||
313 | } |
||
314 | } |
||
315 | } |
||
316 | } |
||
317 | 44984 | fdiaz | } |