Statistics
| Revision:

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 @ 44256

History | View | Annotate | Download (14.7 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.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
import org.gvsig.fmap.geom.GeometryUtils;
31
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
import org.gvsig.fmap.mapcontext.ViewPort;
36
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
37
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
38
import org.gvsig.symbology.SymbologyLocator;
39
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.ISimpleFillSymbol;
40
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.SimpleFillSymbol;
41
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol;
42
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
43
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.ISimpleMarkerSymbol;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ISimpleLineStyle;
45
import org.gvsig.tools.persistence.PersistentState;
46
import org.gvsig.tools.persistence.exception.PersistenceException;
47
import org.gvsig.tools.task.Cancellable;
48
import org.slf4j.LoggerFactory;
49

    
50
public class DefaultDynamicSymbol implements DynamicSymbol {
51

    
52
    private Feature feature = null;
53
    private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(SimpleFillSymbol.class);
54
    private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
55

    
56
    public static Expression eOutlineColor = null;
57
    public static Expression eFillColor = null;
58
    public static Expression eSize = null;
59
    public static Expression eRotation = null;
60

    
61
    private final boolean isShapeVisible = true;
62
    private ISimpleMarkerSymbol pointSymbol = null;
63
    private ISimpleLineSymbol lineSymbol = null;
64
    private ISimpleFillSymbol polygonSymbol = null;
65
    public FeatureSymbolTable featureSymbolTable = null;
66
    public SymbolTable symbolTable = null;
67
    private Expression eOffset;
68
    private List<String> listRequiredAttributeNames;
69

    
70
    public DefaultDynamicSymbol() {
71
        symbolTable = ExpressionEvaluatorLocator.getManager().createSymbolTable();
72
        featureSymbolTable = DALLocator.getDataManager().createFeatureSymbolTable();
73
        symbolTable.addSymbolTable(featureSymbolTable);
74

    
75
    }
76

    
77
    @Override
78
    public void setFeature(Feature feat) {
79
        feature = feat;
80
        featureSymbolTable.setFeature(feat);
81
    }
82

    
83
    @Override
84
    public DynamicSymbol clone() throws CloneNotSupportedException {
85
        return null;
86
    }
87

    
88
    @Override
89
    public ISymbol getSymbolForSelection() {
90
        int typeGeom = feature.getDefaultGeometry().getGeometryType().getType();
91

    
92
        if (GeometryUtils.isSubtype(Geometry.TYPES.POINT, typeGeom)
93
                || GeometryUtils.isSubtype(Geometry.TYPES.MULTIPOINT, typeGeom)) {
94
            setPointSymbolValues();
95
            pointSymbol.setColor(MapContext.getSelectionColor());
96
            pointSymbol.setOutlineColor(MapContext.getSelectionColor());
97
            return pointSymbol;
98
        } else if (GeometryUtils.isSubtype(Geometry.TYPES.CURVE, typeGeom)
99
                || GeometryUtils.isSubtype(Geometry.TYPES.MULTICURVE, typeGeom)) {
100
            setLineSymbolValues();
101
            lineSymbol.setColor(MapContext.getSelectionColor());
102
            return lineSymbol;
103
        } else if (GeometryUtils.isSubtype(Geometry.TYPES.SURFACE, typeGeom)
104
                || GeometryUtils.isSubtype(Geometry.TYPES.MULTISURFACE, typeGeom)) {
105
            setPolygonSymbolValues();
106
            polygonSymbol.setFillColor(MapContext.getSelectionColor());
107
            return polygonSymbol;
108

    
109
        } else {
110
            return null;
111
        }
112

    
113
    }
114

    
115
    @Override
116
    public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel) {
117
        Color c = getColor(); //TODO
118
        int typeGeom = geom.getGeometryType().getType();
119
        if (GeometryUtils.isSubtype(Geometry.TYPES.POINT, typeGeom)
120
                || GeometryUtils.isSubtype(Geometry.TYPES.MULTIPOINT, typeGeom)) {
121
            drawPoint(g, affineTransform, geom, f, cancel);
122
        } else if (GeometryUtils.isSubtype(Geometry.TYPES.CURVE, typeGeom)
123
                || GeometryUtils.isSubtype(Geometry.TYPES.MULTICURVE, typeGeom)) {
124
            drawLine(g, affineTransform, geom, f, cancel);
125
        } else if (GeometryUtils.isSubtype(Geometry.TYPES.SURFACE, typeGeom)
126
                || GeometryUtils.isSubtype(Geometry.TYPES.MULTISURFACE, typeGeom)) {
127
            drawPolygon(g, affineTransform, geom, f, cancel);
128

    
129
        } else {
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
            g.setColor(c);
143
            g.fill(geom.getShape(affineTransform));
144
            g.draw(geom.getShape(affineTransform));
145

    
146
        }
147
    }
148

    
149
    protected void drawPolygon(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel) {
150
        setPolygonSymbolValues();
151
        polygonSymbol.draw(g, affineTransform, geom, f, cancel);
152
    }
153

    
154
    protected void setPolygonSymbolValues() {
155
        if (polygonSymbol == null) {
156
            polygonSymbol = SymbologyLocator.getSymbologyManager().createSimpleFillSymbol();
157
        }
158
        setLineSymbolValues();
159
        lineSymbol.setColor(getComputedOutlineColor());
160
        polygonSymbol.setOutline(lineSymbol);
161

    
162
        polygonSymbol.setFillColor(getComputedFillColor());
163
        polygonSymbol.setHasOutline(true);
164
        polygonSymbol.setHasFill(true);
165
    }
166

    
167
    protected void setLineSymbolValues() {
168
        if (lineSymbol == null) {
169
            lineSymbol = SymbologyLocator.getSymbologyManager().createSimpleLineSymbol();
170
        }
171
        lineSymbol.setLineColor(getComputedFillColor());
172
        lineSymbol.setLineWidth(getComputedSize());
173
        ISimpleLineStyle lineStyle = SymbologyLocator.getSymbologyManager().createSimpleLineStyle();
174
        lineStyle.setOffset(0);
175
//        Stroke stroke = lineStyle.getStroke();
176
        BasicStroke stroke = new BasicStroke(getComputedSize(),
177
                BasicStroke.CAP_BUTT,
178
                BasicStroke.JOIN_MITER);
179
        lineStyle.setStroke(stroke);
180
        lineSymbol.setLineStyle(lineStyle);
181
    }
182

    
183
    protected void drawLine(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel) {
184
        setLineSymbolValues();
185
        lineSymbol.draw(g, affineTransform, geom, f, cancel);
186
    }
187

    
188
    protected void setPointSymbolValues() {
189
        if (pointSymbol == null) {
190
            pointSymbol = SymbologyLocator.getSymbologyManager().createSimpleMarkerSymbol();
191
        }
192
        pointSymbol.setColor(getColor());
193
        pointSymbol.setSize(getComputedSize());
194
        pointSymbol.setRotation(0);
195
        pointSymbol.setStyle(IMarkerSymbol.CIRCLE_STYLE);
196
        pointSymbol.setOutlined(true);
197
        pointSymbol.setOutlineSize(2);
198
        pointSymbol.setOutlineColor(getComputedOutlineColor());
199
    }
200

    
201
    protected void drawPoint(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel) {
202
        setPointSymbolValues();
203
        pointSymbol.draw(g, affineTransform, geom, f, cancel);
204
    }
205

    
206
    @Override
207
    public void getPixExtentPlus(Geometry geom, float[] distances, ViewPort viewPort, int dpi) {
208
        throw new UnsupportedOperationException("Not supported yet.");
209
    }
210

    
211
    @Override
212
    public boolean isOneDotOrPixel(Geometry geom, double[] positionOfDotOrPixel, ViewPort viewPort, int dpi) {
213
        throw new UnsupportedOperationException("Not supported yet.");
214
    }
215

    
216
    @Override
217
    public int getOnePointRgb() {
218
        return Color.ORANGE.getRGB();
219
    }
220

    
221
    @Override
222
    public String getDescription() {
223
        return "";
224
    }
225

    
226
    @Override
227
    public boolean isShapeVisible() {
228
        return isShapeVisible;
229
    }
230

    
231
    @Override
232
    public void setDescription(String desc) {
233
        throw new UnsupportedOperationException("Not supported yet.");
234
    }
235

    
236
    @Override
237
    public int getSymbolType() {
238
        int type = feature.getDefaultGeometry().getGeometryType().getType();
239
        return type;
240
    }
241

    
242
    @Override
243
    public boolean isSuitableFor(Geometry geom) {
244
        return true;
245
    }
246

    
247
    @Override
248
    public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
249
        throw new UnsupportedOperationException("Not supported yet.");
250
    }
251

    
252
    public SymbolTable getFeatureSymbolTable() {
253
        if (symbolTable == null) {
254
            MutableSymbolTable s = ExpressionEvaluatorLocator.getManager().createSymbolTable();
255
            FeatureSymbolTable fst = DALLocator.getDataManager().createFeatureSymbolTable();
256
            s.addSymbolTable(fst);
257
            fst.setFeature(feature);
258
            symbolTable = s;
259

    
260
        }
261
        return symbolTable;
262

    
263
    }
264

    
265
    @Override
266
    public Color getColor() {
267
        Expression value = this.getComputedColor();
268
        return getColorFromExpression(value);
269

    
270
    }
271

    
272
    public Expression getComputedColor() {
273
        return eFillColor;
274
    }
275

    
276
    public Expression getOutlineColor() {
277
        return eOutlineColor;
278
    }
279

    
280
    public Color getComputedOutlineColor() {
281
        return getColorFromExpression(getOutlineColor());
282
    }
283

    
284
    public void setOutlineColor(Expression exp) {
285
        eOutlineColor = exp;
286
    }
287

    
288
    public Expression getFillColor() {
289
        return eFillColor;
290
    }
291

    
292
    public Color getComputedFillColor() {
293
        return getColorFromExpression(getFillColor());
294
    }
295

    
296
    public void setFillColor(Expression exp) {
297
        eFillColor = exp;
298
    }
299

    
300
    public Expression getSize() {
301
        return eSize;
302
    }
303

    
304
    public int getComputedSize() {
305
        Expression exp = getSize();
306
        Integer size = (Integer) exp.execute(getFeatureSymbolTable());
307
        return size;
308
    }
309

    
310
    public void setSize(Expression exp) {
311
        eSize = exp;
312
    }
313

    
314
    public Expression getOffset() {
315
        return eOffset;
316
    }
317

    
318
    public void setOffset(Expression offset) {
319
        eOffset = offset;
320
    }
321

    
322
    public Point getComputedOffset() {
323
//        Expression exp = getSize();
324
//        Integer size = (Integer) exp.execute(getFeatureSymbolTable());
325
        Point pointOffset = null;
326
        try {
327
            pointOffset = GeometryLocator.getGeometryManager().createPoint(0, 0, Geometry.SUBTYPES.GEOM2D);
328
        } catch (CreateGeometryException ex) {
329
            Logger.getLogger(DefaultDynamicSymbol.class.getName()).log(Level.SEVERE, null, ex);
330
        }
331
        return pointOffset;
332
    }
333

    
334
    public Expression getRotation() {
335
        return eRotation;
336
    }
337

    
338
    public int getComputedRotation() {
339
        Expression exp = getRotation();
340
        Integer size = (Integer) exp.execute(getFeatureSymbolTable());
341
        return size;
342
    }
343

    
344
    public void setRotation(Expression exp) {
345
        eRotation = exp;
346
    }
347

    
348
    private Color getColorFromExpression(Expression exp) {
349
        Integer rgbcolor = (Integer) exp.execute(getFeatureSymbolTable());
350
        Color color = new Color(rgbcolor, true);
351
        return color;
352
    }
353

    
354
//    public Object getComputedValue(String field) {
355
//        Object value;
356
//        value = feature.get(field);
357
//
358
//        return value;
359
//    }
360
    @Override
361
    public void setColor(Color color) {
362

    
363
    }
364

    
365
    @Override
366
    public void setColor(Expression color) {
367
        eOutlineColor = color;
368

    
369
    }
370

    
371
    @Override
372
    public void saveToState(PersistentState state) throws PersistenceException {
373

    
374
    }
375

    
376
    @Override
377
    public void loadFromState(PersistentState state) throws PersistenceException {
378

    
379
    }
380

    
381
    @Override
382
    public void print(Graphics2D g, AffineTransform at, Geometry shape, PrintAttributes properties) {
383
        if (shape.getGeometryType().getType() == Geometry.TYPES.POLYGON) {
384
            Color c = getColor();
385
            if (c != null) {
386
                g.setColor(c);
387
                g.fill(shape.getShape(at));
388
            }
389
            if (getComputedOutlineColor() != null) {
390
//                getOutline().print(g, at, geom, properties);
391
            }
392
        } else if (shape.getGeometryType().getType() == Geometry.TYPES.POINT) {
393
            double originalSize = getComputedSize();
394
//            double size = originalSize;
395
//            // scale it to size
396
//            int pq = properties.getPrintQuality();
397
//            switch (pq) {
398
//                case PrintAttributes.PRINT_QUALITY_NORMAL:
399
//                    size *= (double) 300 / 72;
400
//                    break;
401
//                case PrintAttributes.PRINT_QUALITY_HIGH:
402
//                    size *= (double) 600 / 72;
403
//                    break;
404
//            // size *= 72/72; // (which is the same than doing nothing)
405
//                case PrintAttributes.PRINT_QUALITY_DRAFT:
406
//                    break;
407
//                default:
408
//                    break;
409
//            }
410
//                setSize(size);
411
            draw(g, at, shape, null, null);
412
//                setSize(originalSize);
413
        }
414

    
415
    }
416

    
417
    @Override
418
    public List<String> getRequiredFeatureAttributeNames(FeatureStore featureStore) throws DataException {
419
        return this.listRequiredAttributeNames;
420

    
421
    }
422

    
423
    public void setRequiredFeatureAttributesNames(List<String> listRequiredAttributeNames) {
424
        this.listRequiredAttributeNames = listRequiredAttributeNames;
425

    
426
    }
427

    
428
}