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

History | View | Annotate | Download (14.9 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
        if (featureSymbolTable == null) {
81
            getFeatureSymbolTable();
82
        }
83
        featureSymbolTable.setFeature(feat);
84
    }
85

    
86
    @Override
87
    public DynamicSymbol clone() throws CloneNotSupportedException {
88
        return null;
89
    }
90

    
91
    @Override
92
    public ISymbol getSymbolForSelection() {
93
        int typeGeom = feature.getDefaultGeometry().getGeometryType().getType();
94

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

    
112
        } else {
113
            return null;
114
        }
115

    
116
    }
117

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

    
132
        } else {
133
            // Optimiza el pintado de geometrias grandes.
134
            try {
135
                Geometry env = geom.getEnvelope().getGeometry();
136
                env.transform(affineTransform);
137
                Envelope env2 = env.getEnvelope();
138
                if (env2.getLength(0) < 1.5 && env2.getLength(1) < 1.5) {
139
                    geom = env2.getUpperCorner();
140
                }
141
            } catch (Exception ex) {
142
                LOG.warn("Error optimizing the drawing of the geometry. Continues with normal drawing.", ex);
143
                // Do nothing, continue with the draw of the original geometry
144
            }
145
            g.setColor(c);
146
            g.fill(geom.getShape(affineTransform));
147
            g.draw(geom.getShape(affineTransform));
148

    
149
        }
150
    }
151

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

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

    
165
        polygonSymbol.setFillColor(getComputedFillColor());
166
        polygonSymbol.setHasOutline(true);
167
        polygonSymbol.setHasFill(true);
168
    }
169

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

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

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

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

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

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

    
219
    @Override
220
    public int getOnePointRgb() {
221
        return Color.ORANGE.getRGB();
222
    }
223

    
224
    @Override
225
    public String getDescription() {
226
        return "";
227
    }
228

    
229
    @Override
230
    public boolean isShapeVisible() {
231
        return isShapeVisible;
232
    }
233

    
234
    @Override
235
    public void setDescription(String desc) {
236
        throw new UnsupportedOperationException("Not supported yet.");
237
    }
238

    
239
    @Override
240
    public int getSymbolType() {
241
        int type = feature.getDefaultGeometry().getGeometryType().getType();
242
        return type;
243
    }
244

    
245
    @Override
246
    public boolean isSuitableFor(Geometry geom) {
247
        return true;
248
    }
249

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

    
255
    public SymbolTable getFeatureSymbolTable() {
256
        if (symbolTable == null) {
257
            MutableSymbolTable s = ExpressionEvaluatorLocator.getManager().createSymbolTable();
258
            FeatureSymbolTable fst = DALLocator.getDataManager().createFeatureSymbolTable();
259
            s.addSymbolTable(fst);
260
            if (feature != null) {
261
                fst.setFeature(feature);
262
            }
263
            symbolTable = s;
264

    
265
        }
266
        return symbolTable;
267

    
268
    }
269

    
270
    @Override
271
    public Color getColor() {
272
        Expression value = this.getComputedColor();
273
        return getColorFromExpression(value);
274

    
275
    }
276

    
277
    public Expression getComputedColor() {
278
        return eFillColor;
279
    }
280

    
281
    public Expression getOutlineColor() {
282
        return eOutlineColor;
283
    }
284

    
285
    public Color getComputedOutlineColor() {
286
        return getColorFromExpression(getOutlineColor());
287
    }
288

    
289
    public void setOutlineColor(Expression exp) {
290
        eOutlineColor = exp;
291
    }
292

    
293
    public Expression getFillColor() {
294
        return eFillColor;
295
    }
296

    
297
    public Color getComputedFillColor() {
298
        return getColorFromExpression(getFillColor());
299
    }
300

    
301
    public void setFillColor(Expression exp) {
302
        eFillColor = exp;
303
    }
304

    
305
    public Expression getSize() {
306
        return eSize;
307
    }
308

    
309
    public int getComputedSize() {
310
        Expression exp = getSize();
311
        Integer size = (Integer) exp.execute(getFeatureSymbolTable());
312
        return size;
313
    }
314

    
315
    public void setSize(Expression exp) {
316
        eSize = exp;
317
    }
318

    
319
    public Expression getOffset() {
320
        return eOffset;
321
    }
322

    
323
    public void setOffset(Expression offset) {
324
        eOffset = offset;
325
    }
326

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

    
339
    public Expression getRotation() {
340
        return eRotation;
341
    }
342

    
343
    public int getComputedRotation() {
344
        Expression exp = getRotation();
345
        Integer size = (Integer) exp.execute(getFeatureSymbolTable());
346
        return size;
347
    }
348

    
349
    public void setRotation(Expression exp) {
350
        eRotation = exp;
351
    }
352

    
353
    private Color getColorFromExpression(Expression exp) {
354
        Integer rgbcolor = (Integer) exp.execute(getFeatureSymbolTable());
355
        Color color = new Color(rgbcolor, true);
356
        return color;
357
    }
358

    
359
//    public Object getComputedValue(String field) {
360
//        Object value;
361
//        value = feature.get(field);
362
//
363
//        return value;
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
            double originalSize = getComputedSize();
399
//            double size = originalSize;
400
//            // scale it to size
401
//            int pq = properties.getPrintQuality();
402
//            switch (pq) {
403
//                case PrintAttributes.PRINT_QUALITY_NORMAL:
404
//                    size *= (double) 300 / 72;
405
//                    break;
406
//                case PrintAttributes.PRINT_QUALITY_HIGH:
407
//                    size *= (double) 600 / 72;
408
//                    break;
409
//            // size *= 72/72; // (which is the same than doing nothing)
410
//                case PrintAttributes.PRINT_QUALITY_DRAFT:
411
//                    break;
412
//                default:
413
//                    break;
414
//            }
415
//                setSize(size);
416
            draw(g, at, shape, null, null);
417
//                setSize(originalSize);
418
        }
419

    
420
    }
421

    
422
    @Override
423
    public List<String> getRequiredFeatureAttributeNames(FeatureStore featureStore) throws DataException {
424
        return this.listRequiredAttributeNames;
425

    
426
    }
427

    
428
    public void setRequiredFeatureAttributesNames(List<String> listRequiredAttributeNames) {
429
        this.listRequiredAttributeNames = listRequiredAttributeNames;
430

    
431
    }
432

    
433

    
434
}