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