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