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 @ 44247
History | View | Annotate | Download (16 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.Stroke; |
13 |
import java.awt.geom.AffineTransform; |
14 |
import java.util.ArrayList; |
15 |
import java.util.List; |
16 |
import java.util.logging.Level; |
17 |
import java.util.logging.Logger; |
18 |
import org.gvsig.compat.print.PrintAttributes; |
19 |
import org.gvsig.expressionevaluator.Expression; |
20 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
21 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
22 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
23 |
import org.gvsig.expressionevaluator.SymbolTable; |
24 |
import org.gvsig.fmap.dal.DALLocator; |
25 |
import org.gvsig.fmap.dal.exception.DataException; |
26 |
import org.gvsig.fmap.dal.exception.InitializeException; |
27 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
28 |
import org.gvsig.fmap.dal.feature.Feature; |
29 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
30 |
import org.gvsig.fmap.geom.Geometry; |
31 |
import org.gvsig.fmap.geom.GeometryLocator; |
32 |
import org.gvsig.fmap.geom.GeometryManager; |
33 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
34 |
import org.gvsig.fmap.geom.primitive.Envelope; |
35 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
36 |
import org.gvsig.fmap.geom.primitive.Point; |
37 |
import org.gvsig.fmap.geom.type.GeometryType; |
38 |
import org.gvsig.fmap.mapcontext.MapContext; |
39 |
import org.gvsig.fmap.mapcontext.ViewPort; |
40 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
41 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
42 |
import org.gvsig.symbology.SymbologyLocator; |
43 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.ISimpleFillSymbol; |
44 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.SimpleFillSymbol; |
45 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol; |
46 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol; |
47 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol; |
48 |
import static org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol.CIRCLE_STYLE; |
49 |
import static org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol.CROSS_STYLE; |
50 |
import static org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol.DIAMOND_STYLE; |
51 |
import static org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol.SQUARE_STYLE; |
52 |
import static org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol.STAR_STYLE; |
53 |
import static org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol.TRIANGLE_STYLE; |
54 |
import static org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol.VERTICAL_LINE_STYLE; |
55 |
import static org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol.X_STYLE; |
56 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.ISimpleMarkerSymbol; |
57 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ArrowDecoratorStyle; |
58 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ILineStyle; |
59 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ISimpleLineStyle; |
60 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.Line2DOffset; |
61 |
import org.gvsig.tools.evaluator.Evaluator; |
62 |
import org.gvsig.tools.persistence.PersistentState; |
63 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
64 |
import org.gvsig.tools.task.Cancellable; |
65 |
import org.slf4j.LoggerFactory; |
66 |
|
67 |
public class DefaultDynamicSymbol implements DynamicSymbol { |
68 |
|
69 |
private Feature feature;
|
70 |
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(SimpleFillSymbol.class); |
71 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
72 |
|
73 |
public static final String OUTLINECOLOR = "COUTLINE"; |
74 |
public static final String FILLCOLOR = "CFILL"; |
75 |
public static final String SIZE = "CSIZE"; |
76 |
private MutableSymbolTable symbolTable;
|
77 |
|
78 |
private final boolean isShapeVisible = true; |
79 |
private ISimpleMarkerSymbol pointSymbol = null; |
80 |
private ISimpleLineSymbol lineSymbol = null; |
81 |
private ISimpleFillSymbol polygonSymbol = null; |
82 |
|
83 |
public DefaultDynamicSymbol() {
|
84 |
feature = null;
|
85 |
} |
86 |
|
87 |
@Override
|
88 |
public void setFeature(Feature feat) { |
89 |
feature = feat; |
90 |
} |
91 |
|
92 |
@Override
|
93 |
public DynamicSymbol clone() throws CloneNotSupportedException { |
94 |
return null; |
95 |
} |
96 |
|
97 |
private DynamicSymbol symbolForSelection;
|
98 |
|
99 |
@Override
|
100 |
public ISymbol getSymbolForSelection() {
|
101 |
if (symbolForSelection == null) { |
102 |
DynamicSymbol selectionSymbol; |
103 |
try {
|
104 |
selectionSymbol = (DynamicSymbol) this.clone();
|
105 |
if (selectionSymbol != null) { |
106 |
// selectionSymbol.setHasFill(true);
|
107 |
// setSymbolForSelection(selectionSymbol);
|
108 |
} |
109 |
} catch (CloneNotSupportedException ex) { |
110 |
// Logger.getLogger(DefaultDynamicSymbol.class.getName()).log(Level.SEVERE, null, ex);
|
111 |
} |
112 |
|
113 |
} else {
|
114 |
symbolForSelection.setColor(MapContext.getSelectionColor()); |
115 |
} |
116 |
return symbolForSelection;
|
117 |
} |
118 |
|
119 |
@Override
|
120 |
public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel) { |
121 |
Color c = getColor(); //TODO |
122 |
int typeGeom = geom.getGeometryType().getType();
|
123 |
if (typeGeom == Geometry.TYPES.POINT || typeGeom == Geometry.TYPES.MULTIPOINT) {
|
124 |
drawPoint(g, affineTransform, geom, f, cancel); |
125 |
} else if (typeGeom == Geometry.TYPES.LINE || typeGeom == Geometry.TYPES.MULTILINE) { |
126 |
drawLine(g, affineTransform, geom, f, cancel); |
127 |
} else if (typeGeom == Geometry.TYPES.POLYGON || typeGeom == Geometry.TYPES.MULTIPOLYGON) { |
128 |
drawPolygon(g, affineTransform, geom, f, cancel); |
129 |
} else if (true) { |
130 |
// Optimiza el pintado de geometrias grandes.
|
131 |
try {
|
132 |
Geometry env = geom.getEnvelope().getGeometry(); |
133 |
env.transform(affineTransform); |
134 |
Envelope env2 = env.getEnvelope(); |
135 |
if (env2.getLength(0) < 1.5 && env2.getLength(1) < 1.5) { |
136 |
geom = env2.getUpperCorner(); |
137 |
} |
138 |
} catch (Exception ex) { |
139 |
LOG.warn("Error optimizing the drawing of the geometry. Continues with normal drawing.", ex);
|
140 |
// Do nothing, continue with the draw of the original geometry
|
141 |
} |
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 |
if (polygonSymbol == null) { |
152 |
polygonSymbol = SymbologyLocator.getSymbologyManager().createSimpleFillSymbol(); |
153 |
} |
154 |
setLineSymbolValues(); |
155 |
lineSymbol.setColor(getComputedOutlineColor()); |
156 |
polygonSymbol.setOutline(lineSymbol); |
157 |
|
158 |
polygonSymbol.setFillColor(getComputedFillColor()); |
159 |
polygonSymbol.setHasOutline(true);
|
160 |
polygonSymbol.setHasFill(true);
|
161 |
|
162 |
polygonSymbol.draw(g, affineTransform, geom, f, cancel); |
163 |
} |
164 |
|
165 |
protected void setLineSymbolValues() { |
166 |
if (lineSymbol == null) { |
167 |
lineSymbol = SymbologyLocator.getSymbologyManager().createSimpleLineSymbol(); |
168 |
} |
169 |
lineSymbol.setLineColor(getComputedFillColor()); |
170 |
lineSymbol.setLineWidth(getComputedSize()); |
171 |
ISimpleLineStyle lineStyle = SymbologyLocator.getSymbologyManager().createSimpleLineStyle(); |
172 |
lineStyle.setOffset(0);
|
173 |
// Stroke stroke = lineStyle.getStroke();
|
174 |
BasicStroke stroke = new BasicStroke(getComputedSize(), |
175 |
BasicStroke.CAP_BUTT,
|
176 |
BasicStroke.JOIN_MITER);
|
177 |
lineStyle.setStroke(stroke); |
178 |
lineSymbol.setLineStyle(lineStyle); |
179 |
} |
180 |
|
181 |
protected void drawLine(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel) { |
182 |
setLineSymbolValues(); |
183 |
lineSymbol.draw(g, affineTransform, geom, f, cancel); |
184 |
} |
185 |
|
186 |
protected void drawPoint(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel) { |
187 |
if (pointSymbol == null) { |
188 |
pointSymbol = SymbologyLocator.getSymbologyManager().createSimpleMarkerSymbol(); |
189 |
} |
190 |
pointSymbol.setColor(getColor()); |
191 |
pointSymbol.setSize(getComputedSize()); |
192 |
pointSymbol.setRotation(0);
|
193 |
pointSymbol.setStyle(IMarkerSymbol.CIRCLE_STYLE); |
194 |
pointSymbol.setOutlined(true);
|
195 |
pointSymbol.setOutlineSize(2);
|
196 |
pointSymbol.setOutlineColor(getComputedOutlineColor()); |
197 |
pointSymbol.draw(g, affineTransform, geom, f, cancel); |
198 |
} |
199 |
|
200 |
@Override
|
201 |
public void getPixExtentPlus(Geometry geom, float[] distances, ViewPort viewPort, int dpi) { |
202 |
throw new UnsupportedOperationException("Not supported yet."); |
203 |
} |
204 |
|
205 |
@Override
|
206 |
public boolean isOneDotOrPixel(Geometry geom, double[] positionOfDotOrPixel, ViewPort viewPort, int dpi) { |
207 |
throw new UnsupportedOperationException("Not supported yet."); |
208 |
} |
209 |
|
210 |
@Override
|
211 |
public int getOnePointRgb() { |
212 |
return Color.ORANGE.getRGB(); |
213 |
} |
214 |
|
215 |
@Override
|
216 |
public String getDescription() { |
217 |
return ""; |
218 |
} |
219 |
|
220 |
@Override
|
221 |
public boolean isShapeVisible() { |
222 |
return isShapeVisible;
|
223 |
} |
224 |
|
225 |
@Override
|
226 |
public void setDescription(String desc) { |
227 |
throw new UnsupportedOperationException("Not supported yet."); |
228 |
} |
229 |
|
230 |
@Override
|
231 |
public int getSymbolType() { |
232 |
int type = feature.getDefaultGeometry().getGeometryType().getType();
|
233 |
return type;
|
234 |
} |
235 |
|
236 |
@Override
|
237 |
public boolean isSuitableFor(Geometry geom) { |
238 |
return true; |
239 |
} |
240 |
|
241 |
@Override
|
242 |
public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException { |
243 |
throw new UnsupportedOperationException("Not supported yet."); |
244 |
} |
245 |
|
246 |
public SymbolTable getFeatureSymbolTable() {
|
247 |
if (symbolTable == null) { |
248 |
MutableSymbolTable s = ExpressionEvaluatorLocator.getManager().createSymbolTable(); |
249 |
FeatureSymbolTable fst = DALLocator.getDataManager().createFeatureSymbolTable(); |
250 |
s.addSymbolTable(fst); |
251 |
fst.setFeature(feature); |
252 |
symbolTable = s; |
253 |
|
254 |
} |
255 |
return symbolTable;
|
256 |
|
257 |
} |
258 |
|
259 |
@Override
|
260 |
public Color getColor() { |
261 |
Expression value = this.getComputedColor(); |
262 |
return getColorFromExpression(value);
|
263 |
|
264 |
} |
265 |
|
266 |
public Expression getComputedColor() { |
267 |
String value = feature.get(FILLCOLOR).toString();
|
268 |
Expression expression = getExpressionFromString(value);
|
269 |
return expression;
|
270 |
} |
271 |
|
272 |
public Expression getOutlineColor() { |
273 |
String value = feature.get(OUTLINECOLOR).toString();
|
274 |
Expression expression = getExpressionFromString(value);
|
275 |
return expression;
|
276 |
} |
277 |
|
278 |
public Color getComputedOutlineColor() { |
279 |
return getColorFromExpression(getOutlineColor());
|
280 |
} |
281 |
|
282 |
public void setOutlineColor(Expression exp) { |
283 |
|
284 |
} |
285 |
|
286 |
public Expression getFillColor() { |
287 |
String value = feature.get(FILLCOLOR).toString();
|
288 |
Expression expression = getExpressionFromString(value);
|
289 |
return expression;
|
290 |
} |
291 |
|
292 |
public Color getComputedFillColor() { |
293 |
return getColorFromExpression(getFillColor());
|
294 |
} |
295 |
|
296 |
public void setFillColor(Expression exp) { |
297 |
|
298 |
} |
299 |
|
300 |
public Expression getSize() { |
301 |
String value = feature.get(SIZE).toString();
|
302 |
Expression expression = getExpressionFromString(value);
|
303 |
return expression;
|
304 |
} |
305 |
|
306 |
public int getComputedSize() { |
307 |
Expression exp = getSize();
|
308 |
Integer size = (Integer) exp.execute(getFeatureSymbolTable()); |
309 |
return size;
|
310 |
} |
311 |
|
312 |
public void setSize(Expression exp) { |
313 |
|
314 |
} |
315 |
|
316 |
public Expression getOffset() { |
317 |
// TODO
|
318 |
Point pointOffset = null; |
319 |
String value = ""; |
320 |
try {
|
321 |
pointOffset = GeometryLocator.getGeometryManager().createPoint(0, 0, Geometry.SUBTYPES.GEOM2D); |
322 |
value = pointOffset.convertToWKT(); |
323 |
} catch (Exception ex) { |
324 |
Logger.getLogger(DefaultDynamicSymbol.class.getName()).log(Level.SEVERE, null, ex); |
325 |
} |
326 |
|
327 |
Expression expression = getExpressionFromString(value);
|
328 |
return expression;
|
329 |
} |
330 |
|
331 |
public Point getComputedOffset() { |
332 |
// Expression exp = getSize();
|
333 |
// Integer size = (Integer) exp.execute(getFeatureSymbolTable());
|
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 |
public void setOffset(Expression exp) { |
344 |
|
345 |
} |
346 |
|
347 |
public Expression getRotation() { |
348 |
// String value = feature.get(SIZE).toString();
|
349 |
String value = "0"; |
350 |
Expression expression = getExpressionFromString(value);
|
351 |
return expression;
|
352 |
} |
353 |
|
354 |
public int getComputedRotation() { |
355 |
Expression exp = getRotation();
|
356 |
Integer size = (Integer) exp.execute(getFeatureSymbolTable()); |
357 |
return size;
|
358 |
} |
359 |
|
360 |
public void setRotation(Expression exp) { |
361 |
|
362 |
} |
363 |
|
364 |
private Expression getExpressionFromString(String value) { |
365 |
Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
366 |
expression.setPhrase(value); |
367 |
return expression;
|
368 |
} |
369 |
|
370 |
private Color getColorFromExpression(Expression exp) { |
371 |
Integer rgbcolor = (Integer) exp.execute(getFeatureSymbolTable()); |
372 |
Color color = new Color(rgbcolor, true); |
373 |
return color;
|
374 |
} |
375 |
|
376 |
public Object getComputedValue(String field) { |
377 |
Object value;
|
378 |
value = feature.get(field); |
379 |
|
380 |
return value;
|
381 |
} |
382 |
|
383 |
@Override
|
384 |
public void setColor(Color color) { |
385 |
|
386 |
} |
387 |
|
388 |
@Override
|
389 |
public void setColor(Expression color) { |
390 |
|
391 |
} |
392 |
|
393 |
@Override
|
394 |
public void saveToState(PersistentState state) throws PersistenceException { |
395 |
|
396 |
} |
397 |
|
398 |
@Override
|
399 |
public void loadFromState(PersistentState state) throws PersistenceException { |
400 |
|
401 |
} |
402 |
|
403 |
@Override
|
404 |
public void print(Graphics2D g, AffineTransform at, Geometry shape, PrintAttributes properties) { |
405 |
if (shape.getGeometryType().getType() == Geometry.TYPES.POLYGON) {
|
406 |
Color c = getColor();
|
407 |
if (c != null) { |
408 |
g.setColor(c); |
409 |
g.fill(shape.getShape(at)); |
410 |
} |
411 |
if (getComputedOutlineColor() != null) { |
412 |
// getOutline().print(g, at, geom, properties);
|
413 |
} |
414 |
} else if (shape.getGeometryType().getType() == Geometry.TYPES.POINT) { |
415 |
double originalSize = getComputedSize();
|
416 |
// double size = originalSize;
|
417 |
// // scale it to size
|
418 |
// int pq = properties.getPrintQuality();
|
419 |
// switch (pq) {
|
420 |
// case PrintAttributes.PRINT_QUALITY_NORMAL:
|
421 |
// size *= (double) 300 / 72;
|
422 |
// break;
|
423 |
// case PrintAttributes.PRINT_QUALITY_HIGH:
|
424 |
// size *= (double) 600 / 72;
|
425 |
// break;
|
426 |
// // size *= 72/72; // (which is the same than doing nothing)
|
427 |
// case PrintAttributes.PRINT_QUALITY_DRAFT:
|
428 |
// break;
|
429 |
// default:
|
430 |
// break;
|
431 |
// }
|
432 |
// setSize(size);
|
433 |
draw(g, at, shape, null, null); |
434 |
// setSize(originalSize);
|
435 |
} |
436 |
|
437 |
} |
438 |
|
439 |
@Override
|
440 |
public List<String> getRequiredFeatureAttributeNames(FeatureStore featureStore) throws DataException { |
441 |
List<String> myList = new ArrayList<>(); |
442 |
myList.add(OUTLINECOLOR); |
443 |
myList.add(FILLCOLOR); |
444 |
myList.add(SIZE); |
445 |
return myList;
|
446 |
} |
447 |
|
448 |
} |