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 @ 45743
History | View | Annotate | Download (9.63 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* 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 |
*
|
16 |
* 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 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
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 |
*/
|
24 |
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.impl; |
25 |
|
26 |
import java.awt.Color; |
27 |
import java.awt.Graphics2D; |
28 |
import java.awt.Rectangle; |
29 |
import java.awt.geom.AffineTransform; |
30 |
|
31 |
import org.gvsig.compat.print.PrintAttributes; |
32 |
import org.gvsig.fmap.dal.feature.Feature; |
33 |
import org.gvsig.fmap.geom.Geometry; |
34 |
import org.gvsig.fmap.geom.GeometryLocator; |
35 |
import org.gvsig.fmap.geom.GeometryManager; |
36 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
37 |
import org.gvsig.fmap.geom.primitive.Envelope; |
38 |
import org.gvsig.fmap.geom.primitive.Point; |
39 |
import org.gvsig.fmap.mapcontext.MapContext; |
40 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
41 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
42 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
43 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
44 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol; |
45 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol; |
46 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ArrowDecoratorStyle; |
47 |
import org.gvsig.tools.ToolsLocator; |
48 |
import org.gvsig.tools.dynobject.DynStruct; |
49 |
import org.gvsig.tools.persistence.PersistenceManager; |
50 |
import org.gvsig.tools.persistence.PersistentState; |
51 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
52 |
import org.gvsig.tools.swing.api.TransparencySupport; |
53 |
import org.gvsig.tools.task.Cancellable; |
54 |
import org.gvsig.tools.util.Callable; |
55 |
import org.slf4j.Logger; |
56 |
import org.slf4j.LoggerFactory; |
57 |
|
58 |
/**
|
59 |
* SimpleLineSymbol is the most basic symbol for the representation of line
|
60 |
* objects. Allows to define the width of the line, the color and the drawn
|
61 |
* pattern.
|
62 |
*
|
63 |
* @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
|
64 |
* @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
65 |
*/
|
66 |
public class SimpleLineSymbol extends AbstractLineSymbol implements ISimpleLineSymbol, TransparencySupport { |
67 |
|
68 |
private static final Logger LOG = LoggerFactory.getLogger(SimpleLineSymbol.class); |
69 |
public static final String SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleLineSymbol"; |
70 |
|
71 |
private static final String SELECTION_SYMBOL = "symbolForSelection"; |
72 |
|
73 |
SimpleLineSymbol symbolForSelection; |
74 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
75 |
private double transparency; |
76 |
|
77 |
public SimpleLineSymbol() {
|
78 |
super();
|
79 |
setLineWidth(1d);
|
80 |
this.transparency = 1.0; |
81 |
} |
82 |
|
83 |
@Override
|
84 |
public ISymbol getSymbolForSelection() {
|
85 |
if (symbolForSelection == null) { |
86 |
symbolForSelection = (SimpleLineSymbol) cloneForSelection(); |
87 |
} else {
|
88 |
symbolForSelection.setColor(MapContext.getSelectionColor()); |
89 |
} |
90 |
if (symbolForSelection instanceof TransparencySupport) { |
91 |
symbolForSelection.setTransparency(this.transparency);
|
92 |
} |
93 |
return symbolForSelection;
|
94 |
} |
95 |
|
96 |
public void draw(Graphics2D g, AffineTransform affineTransform, |
97 |
Geometry geom, Feature feature, Cancellable cancel) { |
98 |
|
99 |
Color c = getColor();
|
100 |
c = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (this.transparency * c.getAlpha())); |
101 |
if (true) { |
102 |
// Esto deberia ser para optimiza el pintado de
|
103 |
// geometrias grandes.
|
104 |
try {
|
105 |
Geometry env = geom.getEnvelope().getGeometry(); |
106 |
env.transform(affineTransform); |
107 |
Envelope env2 = env.getEnvelope(); |
108 |
if (env2.getLength(0) < 1.5 && env2.getLength(1) < 1.5) { |
109 |
g.setColor(c); |
110 |
Point upperCorner = env2.getUpperCorner();
|
111 |
int x = (int) upperCorner.getX(); |
112 |
int y = (int) upperCorner.getY(); |
113 |
g.drawLine(x, y, x, y); |
114 |
return;
|
115 |
} |
116 |
} catch (Exception ex) { |
117 |
LOG.warn("Error optimizing the drawing of the geometry. Continues with normal drawing.", ex);
|
118 |
// Do nothing, continue with the draw of the original geometry
|
119 |
} |
120 |
} |
121 |
|
122 |
Geometry geomToDraw = geom; |
123 |
g.setStroke(getLineStyle().getStroke()); |
124 |
|
125 |
if (getLineStyle() != null && getLineStyle().getOffset() != 0) { |
126 |
double offset = getLineStyle().getOffset();
|
127 |
try {
|
128 |
geomToDraw = geomToDraw.offset(offset); |
129 |
} catch (Exception e) { |
130 |
LOG.warn("Error creating a polygon with an offset", e);
|
131 |
} |
132 |
} |
133 |
g.setColor(c); |
134 |
g.draw(geomToDraw.getShape(affineTransform)); |
135 |
|
136 |
ArrowDecoratorStyle arrowDecorator = (ArrowDecoratorStyle) getLineStyle().getArrowDecorator(); |
137 |
|
138 |
if (arrowDecorator != null) { |
139 |
try {
|
140 |
arrowDecorator.draw(g, affineTransform, geomToDraw, feature); |
141 |
} catch (CreateGeometryException e) {
|
142 |
LOG.warn("Error drawing geometry.", e);
|
143 |
} |
144 |
} |
145 |
} |
146 |
|
147 |
public int getOnePointRgb() { |
148 |
return getColor().getRGB();
|
149 |
} |
150 |
|
151 |
public void drawInsideRectangle(Graphics2D g, |
152 |
AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException { |
153 |
g.setColor(getColor()); |
154 |
g.setStroke(getLineStyle().getStroke()); |
155 |
super.drawInsideRectangle(g, scaleInstance, r, properties);
|
156 |
} |
157 |
|
158 |
public void setLineWidth(double width) { |
159 |
getLineStyle().setLineWidth((float) width);
|
160 |
} |
161 |
|
162 |
public double getLineWidth() { |
163 |
return getLineStyle().getLineWidth();
|
164 |
} |
165 |
|
166 |
public Object clone() throws CloneNotSupportedException { |
167 |
SimpleLineSymbol copy = (SimpleLineSymbol) super.clone();
|
168 |
|
169 |
if (symbolForSelection != null) { |
170 |
copy.symbolForSelection = (SimpleLineSymbol) symbolForSelection |
171 |
.clone(); |
172 |
} |
173 |
|
174 |
return copy;
|
175 |
} |
176 |
|
177 |
public void loadFromState(PersistentState state) throws PersistenceException { |
178 |
// Set parent style properties
|
179 |
super.loadFromState(state);
|
180 |
|
181 |
this.symbolForSelection = (SimpleLineSymbol) state.get(SELECTION_SYMBOL);
|
182 |
} |
183 |
|
184 |
public void saveToState(PersistentState state) throws PersistenceException { |
185 |
// Save parent fill symbol properties
|
186 |
super.saveToState(state);
|
187 |
|
188 |
// Save own properties
|
189 |
if (this.symbolForSelection != null) { |
190 |
state.set(SELECTION_SYMBOL, this.getSymbolForSelection());
|
191 |
} |
192 |
} |
193 |
|
194 |
@Override
|
195 |
public void setTransparency(double transparency) { |
196 |
this.transparency = transparency;
|
197 |
ISymbol selectionSymbol = this.symbolForSelection;
|
198 |
if (selectionSymbol != null && selectionSymbol instanceof TransparencySupport) { |
199 |
((TransparencySupport) selectionSymbol).setTransparency(transparency); |
200 |
} |
201 |
} |
202 |
|
203 |
@Override
|
204 |
public double getTransparency() { |
205 |
return this.transparency; |
206 |
} |
207 |
|
208 |
public static class RegisterPersistence implements Callable { |
209 |
|
210 |
public Object call() throws Exception { |
211 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
212 |
if (manager.getDefinition(SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) { |
213 |
DynStruct definition = manager.addDefinition( |
214 |
SimpleLineSymbol.class, |
215 |
SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME, |
216 |
SIMPLE_LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
|
217 |
null,
|
218 |
null
|
219 |
); |
220 |
// Extend the LineSymbol base definition
|
221 |
definition.extend(manager.getDefinition(LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME)); |
222 |
|
223 |
definition.addDynFieldObject(SELECTION_SYMBOL).setClassOfValue(SimpleLineSymbol.class); |
224 |
|
225 |
} |
226 |
return Boolean.TRUE; |
227 |
} |
228 |
|
229 |
} |
230 |
|
231 |
public static class RegisterSymbol implements Callable { |
232 |
|
233 |
public Object call() throws Exception { |
234 |
int[] shapeTypes; |
235 |
SymbolManager manager = MapContextLocator.getSymbolManager(); |
236 |
|
237 |
shapeTypes = new int[]{Geometry.TYPES.CURVE, Geometry.TYPES.ARC, |
238 |
Geometry.TYPES.MULTICURVE, Geometry.TYPES.CIRCUMFERENCE, |
239 |
Geometry.TYPES.PERIELLIPSE, Geometry.TYPES.SPLINE, |
240 |
Geometry.TYPES.LINE, Geometry.TYPES.MULTILINE}; |
241 |
manager.registerSymbol(ILineSymbol.SYMBOL_NAME, |
242 |
shapeTypes, |
243 |
SimpleLineSymbol.class); |
244 |
|
245 |
return Boolean.TRUE; |
246 |
} |
247 |
|
248 |
} |
249 |
|
250 |
} |