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 / dynamiclegend / DefaultDynamicSymbol.java @ 47790
History | View | Annotate | Download (14.2 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.symbology.fmap.mapcontext.rendering.dynamiclegend; |
7 |
|
8 |
import java.awt.BasicStroke; |
9 |
import java.awt.Color; |
10 |
import java.awt.Graphics2D; |
11 |
import java.awt.Rectangle; |
12 |
import java.awt.geom.AffineTransform; |
13 |
import java.util.List; |
14 |
import java.util.logging.Level; |
15 |
import java.util.logging.Logger; |
16 |
import org.gvsig.compat.print.PrintAttributes; |
17 |
import org.gvsig.expressionevaluator.Expression; |
18 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
19 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
20 |
import org.gvsig.expressionevaluator.SymbolTable; |
21 |
import org.gvsig.fmap.dal.DALLocator; |
22 |
import org.gvsig.fmap.dal.exception.DataException; |
23 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
24 |
import org.gvsig.fmap.dal.feature.Feature; |
25 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
26 |
import org.gvsig.fmap.geom.Geometry; |
27 |
import org.gvsig.fmap.geom.GeometryLocator; |
28 |
import org.gvsig.fmap.geom.GeometryManager; |
29 |
import org.gvsig.fmap.geom.GeometryUtils; |
30 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
31 |
import org.gvsig.fmap.geom.primitive.Envelope; |
32 |
import org.gvsig.fmap.geom.primitive.Point; |
33 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
34 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
35 |
import org.gvsig.symbology.SymbologyLocator; |
36 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.ISimpleFillSymbol; |
37 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.SimpleFillSymbol; |
38 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.AbstractSymbol; |
39 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol; |
40 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol; |
41 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.ISimpleMarkerSymbol; |
42 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ISimpleLineStyle; |
43 |
import org.gvsig.tools.persistence.PersistentState; |
44 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
45 |
import org.gvsig.tools.task.Cancellable; |
46 |
import org.slf4j.LoggerFactory; |
47 |
|
48 |
public class DefaultDynamicSymbol extends AbstractSymbol implements DynamicSymbol { |
49 |
|
50 |
private Feature feature = null; |
51 |
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(SimpleFillSymbol.class); |
52 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
53 |
|
54 |
public static Expression eOutlineColor = null; |
55 |
public static Expression eFillColor = null; |
56 |
public static Expression eSize = null; |
57 |
public static Expression eRotation = null; |
58 |
|
59 |
// private final boolean isShapeVisible = true;
|
60 |
private ISimpleMarkerSymbol pointSymbol = null; |
61 |
private ISimpleLineSymbol lineSymbol = null; |
62 |
private ISimpleFillSymbol polygonSymbol = null; |
63 |
public FeatureSymbolTable featureSymbolTable = null; |
64 |
public SymbolTable symbolTable = null; |
65 |
private Expression eOffset; |
66 |
private List<String> listRequiredAttributeNames; |
67 |
|
68 |
public DefaultDynamicSymbol() {
|
69 |
symbolTable = ExpressionEvaluatorLocator.getManager().createSymbolTable(); |
70 |
featureSymbolTable = DALLocator.getDataManager().createFeatureSymbolTable(); |
71 |
symbolTable.addSymbolTable(featureSymbolTable); |
72 |
|
73 |
} |
74 |
|
75 |
@Override
|
76 |
public void setFeature(Feature feat) { |
77 |
feature = feat; |
78 |
if (featureSymbolTable == null) { |
79 |
getFeatureSymbolTable(); |
80 |
} |
81 |
featureSymbolTable.setFeature(feat); |
82 |
} |
83 |
|
84 |
@Override
|
85 |
public DynamicSymbol clone() throws CloneNotSupportedException { |
86 |
return null; |
87 |
} |
88 |
|
89 |
@Override
|
90 |
public ISymbol getSymbolForSelection(Color selectionColor) { |
91 |
int typeGeom = feature.getDefaultGeometry().getGeometryType().getType();
|
92 |
|
93 |
if (GeometryUtils.isSubtype(Geometry.TYPES.POINT, typeGeom)
|
94 |
|| GeometryUtils.isSubtype(Geometry.TYPES.MULTIPOINT, typeGeom)) { |
95 |
setPointSymbolValues(); |
96 |
pointSymbol.setColor(selectionColor); |
97 |
pointSymbol.setOutlineColor(selectionColor); |
98 |
return pointSymbol;
|
99 |
} else if (GeometryUtils.isSubtype(Geometry.TYPES.CURVE, typeGeom) |
100 |
|| GeometryUtils.isSubtype(Geometry.TYPES.MULTICURVE, typeGeom)) { |
101 |
setLineSymbolValues(); |
102 |
lineSymbol.setColor(selectionColor); |
103 |
return lineSymbol;
|
104 |
} else if (GeometryUtils.isSubtype(Geometry.TYPES.SURFACE, typeGeom) |
105 |
|| GeometryUtils.isSubtype(Geometry.TYPES.MULTISURFACE, typeGeom)) { |
106 |
setPolygonSymbolValues(); |
107 |
polygonSymbol.setFillColor(selectionColor); |
108 |
return polygonSymbol;
|
109 |
|
110 |
} else {
|
111 |
return null; |
112 |
} |
113 |
|
114 |
} |
115 |
|
116 |
@Override
|
117 |
public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel, Rectangle r) { |
118 |
Color c = getColor(); //TODO |
119 |
int typeGeom = geom.getGeometryType().getType();
|
120 |
if (GeometryUtils.isSubtype(Geometry.TYPES.POINT, typeGeom)
|
121 |
|| GeometryUtils.isSubtype(Geometry.TYPES.MULTIPOINT, typeGeom)) { |
122 |
drawPoint(g, affineTransform, geom, f, cancel); |
123 |
} else if (GeometryUtils.isSubtype(Geometry.TYPES.CURVE, typeGeom) |
124 |
|| GeometryUtils.isSubtype(Geometry.TYPES.MULTICURVE, typeGeom)) { |
125 |
drawLine(g, affineTransform, geom, f, cancel); |
126 |
} else if (GeometryUtils.isSubtype(Geometry.TYPES.SURFACE, typeGeom) |
127 |
|| GeometryUtils.isSubtype(Geometry.TYPES.MULTISURFACE, typeGeom)) { |
128 |
drawPolygon(g, affineTransform, geom, f, cancel); |
129 |
|
130 |
} else {
|
131 |
// Optimiza el pintado de geometrias grandes.
|
132 |
try {
|
133 |
Geometry env = geom.getEnvelope().getGeometry(); |
134 |
env.transform(affineTransform); |
135 |
Envelope env2 = env.getEnvelope(); |
136 |
if (env2.getLength(0) < 1.5 && env2.getLength(1) < 1.5) { |
137 |
geom = env2.getUpperCorner(); |
138 |
} |
139 |
} catch (Exception ex) { |
140 |
LOG.warn("Error optimizing the drawing of the geometry. Continues with normal drawing.", ex);
|
141 |
// Do nothing, continue with the draw of the original geometry
|
142 |
} |
143 |
g.setColor(c); |
144 |
g.fill(geom.getShape(affineTransform)); |
145 |
g.draw(geom.getShape(affineTransform)); |
146 |
|
147 |
} |
148 |
} |
149 |
|
150 |
protected void drawPolygon(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel) { |
151 |
setPolygonSymbolValues(); |
152 |
polygonSymbol.draw(g, affineTransform, geom, f, cancel); |
153 |
} |
154 |
|
155 |
protected void setPolygonSymbolValues() { |
156 |
if (polygonSymbol == null) { |
157 |
polygonSymbol = SymbologyLocator.getSymbologyManager().createSimpleFillSymbol(); |
158 |
} |
159 |
setLineSymbolValues(); |
160 |
lineSymbol.setColor(getComputedOutlineColor()); |
161 |
polygonSymbol.setOutline(lineSymbol); |
162 |
|
163 |
polygonSymbol.setFillColor(getComputedFillColor()); |
164 |
polygonSymbol.setHasOutline(true);
|
165 |
polygonSymbol.setHasFill(true);
|
166 |
} |
167 |
|
168 |
protected void setLineSymbolValues() { |
169 |
if (lineSymbol == null) { |
170 |
lineSymbol = SymbologyLocator.getSymbologyManager().createSimpleLineSymbol(); |
171 |
} |
172 |
lineSymbol.setLineColor(getComputedOutlineColor()); |
173 |
lineSymbol.setLineWidth(getComputedSize()); |
174 |
ISimpleLineStyle lineStyle = SymbologyLocator.getSymbologyManager().createSimpleLineStyle(); |
175 |
lineStyle.setOffset(0);
|
176 |
// Stroke stroke = lineStyle.getStroke();
|
177 |
BasicStroke stroke = new BasicStroke(getComputedSize(), |
178 |
BasicStroke.CAP_BUTT,
|
179 |
BasicStroke.JOIN_MITER);
|
180 |
lineStyle.setStroke(stroke); |
181 |
lineSymbol.setLineStyle(lineStyle); |
182 |
} |
183 |
|
184 |
protected void drawLine(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel) { |
185 |
setLineSymbolValues(); |
186 |
lineSymbol.draw(g, affineTransform, geom, f, cancel); |
187 |
} |
188 |
|
189 |
protected void setPointSymbolValues() { |
190 |
if (pointSymbol == null) { |
191 |
pointSymbol = SymbologyLocator.getSymbologyManager().createSimpleMarkerSymbol(); |
192 |
} |
193 |
pointSymbol.setColor(getColor()); |
194 |
pointSymbol.setSize(getComputedSize()); |
195 |
pointSymbol.setRotation(0);
|
196 |
pointSymbol.setStyle(IMarkerSymbol.CIRCLE_STYLE); |
197 |
pointSymbol.setOutlined(true);
|
198 |
pointSymbol.setOutlineSize(2);
|
199 |
pointSymbol.setOutlineColor(getComputedOutlineColor()); |
200 |
} |
201 |
|
202 |
protected void drawPoint(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel) { |
203 |
setPointSymbolValues(); |
204 |
pointSymbol.draw(g, affineTransform, geom, f, cancel); |
205 |
} |
206 |
|
207 |
// @Override
|
208 |
// public void getPixExtentPlus(Geometry geom, float[] distances, ViewPort viewPort, int dpi) {
|
209 |
// throw new UnsupportedOperationException("Not supported yet.");
|
210 |
// }
|
211 |
//
|
212 |
// @Override
|
213 |
// public boolean isOneDotOrPixel(Geometry geom, double[] positionOfDotOrPixel, ViewPort viewPort, int dpi) {
|
214 |
// throw new UnsupportedOperationException("Not supported yet.");
|
215 |
// }
|
216 |
|
217 |
// @Override
|
218 |
// public int getOnePointRgb() {
|
219 |
// return Color.ORANGE.getRGB();
|
220 |
// }
|
221 |
|
222 |
// @Override
|
223 |
// public String getDescription() {
|
224 |
// return "";
|
225 |
// }
|
226 |
//
|
227 |
// @Override
|
228 |
// public boolean isShapeVisible() {
|
229 |
// return isShapeVisible;
|
230 |
// }
|
231 |
//
|
232 |
// @Override
|
233 |
// public void setDescription(String desc) {
|
234 |
// throw new UnsupportedOperationException("Not supported yet.");
|
235 |
// }
|
236 |
|
237 |
@Override
|
238 |
public int getSymbolType() { |
239 |
int type = feature.getDefaultGeometry().getGeometryType().getType();
|
240 |
return type;
|
241 |
} |
242 |
|
243 |
@Override
|
244 |
public boolean isSuitableFor(Geometry geom) { |
245 |
return true; |
246 |
} |
247 |
|
248 |
// @Override
|
249 |
// public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException {
|
250 |
// throw new UnsupportedOperationException("Not supported yet.");
|
251 |
// }
|
252 |
|
253 |
public SymbolTable getFeatureSymbolTable() {
|
254 |
if (symbolTable == null) { |
255 |
MutableSymbolTable s = ExpressionEvaluatorLocator.getManager().createSymbolTable(); |
256 |
FeatureSymbolTable fst = DALLocator.getDataManager().createFeatureSymbolTable(); |
257 |
s.addSymbolTable(fst); |
258 |
if (feature != null) { |
259 |
fst.setFeature(feature); |
260 |
} |
261 |
symbolTable = s; |
262 |
|
263 |
} |
264 |
return symbolTable;
|
265 |
|
266 |
} |
267 |
|
268 |
@Override
|
269 |
public Color getColor() { |
270 |
Expression value = this.getComputedColor(); |
271 |
return getColorFromExpression(value);
|
272 |
|
273 |
} |
274 |
|
275 |
public Expression getComputedColor() { |
276 |
return eFillColor;
|
277 |
} |
278 |
|
279 |
@Override
|
280 |
public Expression getOutlineColor() { |
281 |
return eOutlineColor;
|
282 |
} |
283 |
|
284 |
public Color getComputedOutlineColor() { |
285 |
return getColorFromExpression(getOutlineColor());
|
286 |
} |
287 |
|
288 |
@Override
|
289 |
public void setOutlineColor(Expression exp) { |
290 |
eOutlineColor = exp; |
291 |
} |
292 |
|
293 |
@Override
|
294 |
public Expression getFillColor() { |
295 |
return eFillColor;
|
296 |
} |
297 |
|
298 |
public Color getComputedFillColor() { |
299 |
return getColorFromExpression(getFillColor());
|
300 |
} |
301 |
|
302 |
@Override
|
303 |
public void setFillColor(Expression exp) { |
304 |
eFillColor = exp; |
305 |
} |
306 |
|
307 |
@Override
|
308 |
public Expression getSize() { |
309 |
return eSize;
|
310 |
} |
311 |
|
312 |
public int getComputedSize() { |
313 |
Expression exp = getSize();
|
314 |
Integer size = (Integer) exp.execute(getFeatureSymbolTable()); |
315 |
return size;
|
316 |
} |
317 |
|
318 |
@Override
|
319 |
public void setSize(Expression exp) { |
320 |
eSize = exp; |
321 |
} |
322 |
|
323 |
@Override
|
324 |
public Expression getOffset() { |
325 |
return eOffset;
|
326 |
} |
327 |
|
328 |
@Override
|
329 |
public void setOffset(Expression offset) { |
330 |
eOffset = offset; |
331 |
} |
332 |
|
333 |
public Point getComputedOffset() { |
334 |
Point pointOffset = null; |
335 |
try {
|
336 |
pointOffset = GeometryLocator.getGeometryManager().createPoint(0, 0, Geometry.SUBTYPES.GEOM2D); |
337 |
} catch (CreateGeometryException ex) {
|
338 |
Logger.getLogger(DefaultDynamicSymbol.class.getName()).log(Level.SEVERE, null, ex); |
339 |
} |
340 |
return pointOffset;
|
341 |
} |
342 |
|
343 |
@Override
|
344 |
public Expression getRotation() { |
345 |
return eRotation;
|
346 |
} |
347 |
|
348 |
public int getComputedRotation() { |
349 |
Expression exp = getRotation();
|
350 |
Integer size = (Integer) exp.execute(getFeatureSymbolTable()); |
351 |
return size;
|
352 |
} |
353 |
|
354 |
@Override
|
355 |
public void setRotation(Expression exp) { |
356 |
eRotation = exp; |
357 |
} |
358 |
|
359 |
private Color getColorFromExpression(Expression exp) { |
360 |
Integer rgbcolor = (Integer) exp.execute(getFeatureSymbolTable()); |
361 |
Color color = new Color(rgbcolor, true); |
362 |
return color;
|
363 |
} |
364 |
|
365 |
@Override
|
366 |
public void setColor(Color color) { |
367 |
|
368 |
} |
369 |
|
370 |
@Override
|
371 |
public void setColor(Expression color) { |
372 |
eOutlineColor = color; |
373 |
|
374 |
} |
375 |
|
376 |
@Override
|
377 |
public void saveToState(PersistentState state) throws PersistenceException { |
378 |
|
379 |
} |
380 |
|
381 |
@Override
|
382 |
public void loadFromState(PersistentState state) throws PersistenceException { |
383 |
|
384 |
} |
385 |
|
386 |
@Override
|
387 |
public void print(Graphics2D g, AffineTransform at, Geometry shape, PrintAttributes properties) { |
388 |
if (shape.getGeometryType().getType() == Geometry.TYPES.POLYGON) {
|
389 |
Color c = getColor();
|
390 |
if (c != null) { |
391 |
g.setColor(c); |
392 |
g.fill(shape.getShape(at)); |
393 |
} |
394 |
if (getComputedOutlineColor() != null) { |
395 |
// getOutline().print(g, at, geom, properties);
|
396 |
} |
397 |
} else if (shape.getGeometryType().getType() == Geometry.TYPES.POINT) { |
398 |
draw(g, at, shape, null, null); |
399 |
} |
400 |
|
401 |
} |
402 |
|
403 |
@Override
|
404 |
public String[] getRequiredFeatureAttributeNames(FeatureStore featureStore) throws DataException { |
405 |
return this.listRequiredAttributeNames.toArray(new String[this.listRequiredAttributeNames.size()]); |
406 |
|
407 |
} |
408 |
|
409 |
@Override
|
410 |
public void setRequiredFeatureAttributesNames(List<String> listRequiredAttributeNames) { |
411 |
this.listRequiredAttributeNames = listRequiredAttributeNames;
|
412 |
|
413 |
} |
414 |
|
415 |
public static void selfRegister() { |
416 |
MapContextLocator.getSymbolManager().registerSymbol(NAME, DefaultDynamicSymbol.class); |
417 |
} |
418 |
|
419 |
|
420 |
} |