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

History | View | Annotate | Download (15.1 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.MapContextLocator;
36
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
    private Feature feature = null;
54
    private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(SimpleFillSymbol.class);
55
    private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
56

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

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

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

    
76
    }
77

    
78
    @Override
79
    public void setFeature(Feature feat) {
80
        feature = feat;
81
        if (featureSymbolTable == null) {
82
            getFeatureSymbolTable();
83
        }
84
        featureSymbolTable.setFeature(feat);
85
    }
86

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

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

    
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
        }
116

    
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 (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
                }
142
            } 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
        }
151
    }
152

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

    
158
    protected void setPolygonSymbolValues() {
159
        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
    protected void setPointSymbolValues() {
193
        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
    }
204

    
205
    protected void drawPoint(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel) {
206
        setPointSymbolValues();
207
        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
            if (feature != null) {
262
                fst.setFeature(feature);
263
            }
264
            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
        return eFillColor;
280
    }
281

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

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

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

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

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

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

    
306
    public Expression getSize() {
307
        return eSize;
308
    }
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
        eSize = exp;
318
    }
319

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

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

    
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
        return eRotation;
342
    }
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
        eRotation = exp;
352
    }
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
//    public Object getComputedValue(String field) {
361
//        Object value;
362
//        value = feature.get(field);
363
//
364
//        return value;
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
            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
        return this.listRequiredAttributeNames;
426

    
427
    }
428

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

    
432
    }
433
    
434
    public static void selfRegister() {
435
        MapContextLocator.getSymbolManager().registerSymbol(NAME, DefaultDynamicSymbol.class);
436
    }
437

    
438

    
439
}