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 / symbol / marker / impl / AbstractMarkerSymbol.java @ 45526

History | View | Annotate | Download (16.1 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.impl;
25

    
26
import java.awt.Color;
27
import java.awt.Graphics2D;
28
import java.awt.Point;
29
import java.awt.Rectangle;
30
import java.awt.geom.AffineTransform;
31
import java.awt.geom.Point2D;
32
import org.apache.commons.lang3.StringUtils;
33
import org.gvsig.compat.print.PrintAttributes;
34
import org.gvsig.expressionevaluator.ExpressionBuilder;
35
import org.gvsig.expressionevaluator.ExpressionUtils;
36
import org.gvsig.expressionevaluator.SymbolTable;
37
import org.gvsig.fmap.dal.DALLocator;
38
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
39
import org.gvsig.fmap.dal.feature.Feature;
40
import org.gvsig.fmap.geom.Geometry;
41
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
42
import org.gvsig.fmap.geom.Geometry.TYPES;
43
import org.gvsig.fmap.geom.GeometryLocator;
44
import org.gvsig.fmap.geom.GeometryManager;
45
import org.gvsig.fmap.geom.exception.CreateGeometryException;
46
import org.gvsig.fmap.mapcontext.MapContextLocator;
47
import org.gvsig.fmap.mapcontext.ViewPort;
48
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
49
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.AbstractSymbol;
50
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.CartographicSupportToolkit;
51
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
52
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.IMask;
53
import org.gvsig.tools.ToolsLocator;
54
import org.gvsig.tools.dynobject.DynStruct;
55
import org.gvsig.tools.persistence.PersistenceManager;
56
import org.gvsig.tools.persistence.PersistentState;
57
import org.gvsig.tools.persistence.exception.PersistenceException;
58
import org.gvsig.tools.util.Callable;
59

    
60
/**
61
 * Abstract class that any MARKER SYMBOL should extend.
62
 *
63
 * @author gvSIG team
64
 */
65
@SuppressWarnings("UseSpecificCatch")
66
public abstract class AbstractMarkerSymbol extends AbstractSymbol implements IMarkerSymbol {
67

    
68
    public static final String MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME = "MarkerSymbol";
69

    
70
    private static final String FIELD_COLOR = "color";
71
    private static final String FIELD_ROTATION = "rotation";
72
    private static final String FIELD_OFFSET = "offset";
73
    private static final String FIELD_SIZE = "size";
74
    private static final String FIELD_MASK = "mask";
75
    private static final String FIELD_OFFSETX_EXPRESSION = "offsetXExpression";
76
    private static final String FIELD_OFFSETY_EXPRESSION = "offsetYExpression";
77
    private static final String FIELD_ROTATION_EXPRESSION = "rotationExpression";
78
    private static final String FIELD_DRAWLINETOOFFSET = "drawLineToOffsetExpression";
79
    private static final String FIELD_LINETOOFFSETCOLOR_EXPRESSION = "lineToOffsetColorExpression";
80

    
81
    private static final GeometryManager GEOMETRY_MANAGER = GeometryLocator.getGeometryManager();
82
    private Color color;
83
    private double rotation;
84
    private Point2D offset = new Point2D.Double();
85
    private double size = 4d;
86
    private IMask mask;
87

    
88
    private boolean drawLineToOffset;
89

    
90
    private String offsetXExpression;
91
    private String offsetYExpression;
92
    private String rotationExpression;
93
    private String lineToOffsetColorExpression;
94

    
95
    public AbstractMarkerSymbol() {
96
        super();
97
        color = MapContextLocator.getSymbolManager()
98
                .getSymbolPreferences()
99
                .getDefaultSymbolFillColor();
100
    }
101

    
102
    @Override
103
    public final int getSymbolType() {
104
        return Geometry.TYPES.POINT;
105
    }
106

    
107
    @Override
108
    public double getRotation() {
109
        return rotation;
110
    }
111

    
112
    @Override
113
    public void setRotation(double r) {
114
        this.rotation = r;
115
    }
116

    
117
    @Override
118
    public Point2D getOffset() {
119
        if (offset == null) {
120
            offset = new Point();
121
        }
122
        return offset;
123
    }
124

    
125
    @Override
126
    public void setOffset(Point2D offset) {
127
        this.offset = offset;
128
    }
129

    
130
    private FeatureSymbolTable symbolTable = null;
131
    
132
    private SymbolTable getSymbolTable(Feature f) {
133
        if( symbolTable==null ) {
134
            this.symbolTable = DALLocator.getManager().createFeatureSymbolTable();
135
        }
136
        symbolTable.setFeature(f);
137
        return symbolTable;
138
    }
139
        
140
    public String getOffsetXExpression() {
141
        return offsetXExpression;
142
    }
143

    
144
    public void setOffsetXExpression(String offsetXExpression) {
145
        this.offsetXExpression = StringUtils.trimToNull(offsetXExpression);
146
    }
147

    
148
    public String getOffsetYExpression() {
149
        return offsetYExpression;
150
    }
151

    
152
    public void setOffsetYExpression(String offsetYExpression) {
153
        this.offsetYExpression = StringUtils.trimToNull(offsetYExpression);
154
    }
155

    
156
    public String getRotationExpression() {
157
        return rotationExpression;
158
    }
159

    
160
    public void setRotationExpression(String rotationExpression) {
161
        this.rotationExpression = StringUtils.trimToNull(rotationExpression);
162
    }
163

    
164
    public Color getLineToOffsetColor() {
165
        return this.getLineToOffsetColor(null);
166
    }
167

    
168
    public Color getLineToOffsetColor(Feature f) {
169
        return ExpressionUtils.parseColor(this.getSymbolTable(f), this.lineToOffsetColorExpression, Color.BLACK);
170
    }
171

    
172
    public String getLineToOffsetColorExpression() {
173
        return this.lineToOffsetColorExpression;
174
    }
175

    
176
    public void setLineToOffsetColorExpression(String lineToOffsetColorExpression) {
177
        this.lineToOffsetColorExpression = StringUtils.trimToNull(lineToOffsetColorExpression);
178
    }
179

    
180
    public void setLineToOffsetColor(Color lineToOffsetColor) {
181
        ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
182
        this.lineToOffsetColorExpression = StringUtils.trimToNull(builder.color(color).toString());
183
    }
184

    
185
    public boolean isDrawLineToOffset() {
186
        return this.isDrawLineToOffset(null);
187
    }
188

    
189
    public boolean isDrawLineToOffset(Feature f) {
190
        return this.drawLineToOffset;
191
    }
192

    
193
    public void setDrawLineToOffset(boolean drawLineToOffset) {
194
        this.drawLineToOffset = drawLineToOffset;
195
    }
196

    
197
    public Point2D getEfectiveOffset(Feature f) {
198
        if( f==null || StringUtils.isBlank(this.offsetXExpression) || StringUtils.isBlank(this.offsetYExpression) ) {
199
            return this.getOffset();
200
        }
201
        int offsetX = ExpressionUtils.parseInt(this.getSymbolTable(f), offsetXExpression, (int) this.getOffset().getX());
202
        int offsetY = ExpressionUtils.parseInt(this.getSymbolTable(f), offsetYExpression, (int) this.getOffset().getY());
203
        Point p = new Point(offsetX, offsetY);
204
        return p;
205
    }
206

    
207
    public double getEfectiveRotation(Feature f) {
208
        if( f==null || StringUtils.isBlank(this.rotationExpression) ) {
209
            return this.getRotation();
210
        }
211
        double r = ExpressionUtils.parseDouble(this.getSymbolTable(f), rotationExpression, this.getRotation());
212
        return r;
213
    }
214

    
215
    @Override
216
    public boolean isSuitableFor(Geometry geom) {
217
        return geom.getType() == Geometry.TYPES.POINT;
218
    }
219

    
220
    @Override
221
    public int getOnePointRgb() {
222
        return color.getRGB();
223
    }
224

    
225
    @Override
226
    public double getSize() {
227
        return size;
228
    }
229

    
230
    @Override
231
    public void setSize(double size) {
232
        this.size = size;
233
    }
234

    
235
    @Override
236
    public Color getColor() {
237
        return color;
238
    }
239

    
240
    @Override
241
    public void setColor(Color color) {
242
        this.color = color;
243
    }
244

    
245
    @Override
246
    public void setAlpha(int outlineAlpha) {
247
        Color theColor = getColor();
248
        setColor(new Color(theColor.getRed(), theColor.getGreen(), theColor.getBlue(),
249
                outlineAlpha));
250
    }
251

    
252
    @Override
253
    public void getPixExtentPlus(Geometry geom, float[] distances, ViewPort viewPort, int dpi) {
254
        float cs = (float) getCartographicSize(viewPort, dpi, geom);
255
        distances[0] = cs;
256
        distances[1] = cs;
257
    }
258

    
259
    @Override
260
    public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) {
261
        double originalSize = getSize();
262
        double theSize = originalSize;
263
        // scale it to size
264
        int pq = properties.getPrintQuality();
265
        switch (pq) {
266
            case PrintAttributes.PRINT_QUALITY_NORMAL:
267
                theSize *= (double) 300 / 72;
268
                break;
269
            case PrintAttributes.PRINT_QUALITY_HIGH:
270
                theSize *= (double) 600 / 72;
271
                break;
272
            case PrintAttributes.PRINT_QUALITY_DRAFT:
273
                // size *= 72/72; // (which is the same than doing nothing)
274
                break;
275
            default:
276
                break;
277
        }
278
//                setSize(theSize);
279
        draw(g, at, geom, null, null);
280
//                setSize(originalSize);
281
    }
282

    
283
    @Override
284
    public final IMask getMask() {
285
        return mask;
286
    }
287

    
288
    @Override
289
    public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
290
        try {
291
            if (properties == null) {
292
                draw(g, scaleInstance, GEOMETRY_MANAGER.createPoint(r.getCenterX(), r.getCenterY(), SUBTYPES.GEOM2D), null, null);
293
            } else {
294
                double originalSize = getSize();
295
                double theSize = originalSize;
296
                int pq = properties.getPrintQuality();
297
                switch (pq) {
298
                    case PrintAttributes.PRINT_QUALITY_NORMAL:
299
                        theSize *= (double) 300 / 72;
300
                        break;
301
                    case PrintAttributes.PRINT_QUALITY_HIGH:
302
                        theSize *= (double) 600 / 72;
303
                        break;
304
                    case PrintAttributes.PRINT_QUALITY_DRAFT:
305
                        // d *= 72/72; // (which is the same than doing nothing)
306
                        break;
307
                    default:
308
                        break;
309
                }
310
                setSize(theSize);
311
                print(g, scaleInstance, GEOMETRY_MANAGER.createPoint(r.getCenterX(), r.getCenterY(), SUBTYPES.GEOM2D), properties);
312
                setSize(originalSize);
313
            }
314

    
315
        } catch (CreateGeometryException e) {
316
            throw new SymbolDrawingException(TYPES.POINT);
317
        }
318
    }
319

    
320
    @Override
321
    public final void setMask(IMask mask) {
322
        this.mask = mask;
323
    }
324

    
325
    @Override
326
    public void setCartographicSize(double cartographicSize, Geometry geom) {
327
        setSize(cartographicSize);
328
    }
329

    
330
    @Override
331
    public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
332
        double oldSize = getSize();
333
        setCartographicSize(getCartographicSize(
334
                viewPort,
335
                dpi,
336
                geom),
337
                geom);
338
        return oldSize;
339
    }
340

    
341
    @Override
342
    public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
343
        return CartographicSupportToolkit.
344
                getCartographicLength(this,
345
                        getSize(),
346
                        viewPort,
347
                        dpi);
348
    }
349

    
350
    @Override
351
    public Object clone() throws CloneNotSupportedException {
352
        AbstractMarkerSymbol copy = (AbstractMarkerSymbol) super.clone();
353

    
354
        // Clone the offset
355
        if (offset != null) {
356
            copy.offset = (Point2D) offset.clone();
357
        }
358

    
359
        // clone the mask
360
        if (mask != null) {
361
            copy.mask = (IMask) mask.clone();
362
        }
363
        return copy;
364
    }
365

    
366
    @Override
367
    public void loadFromState(PersistentState state)
368
            throws PersistenceException {
369
        // Set parent symbol properties
370
        super.loadFromState(state);
371
        // Set own properties
372
        setColor((Color) state.get(FIELD_COLOR));
373
        setMask((IMask) state.get(FIELD_MASK));
374
        setOffset((Point2D) state.get(FIELD_OFFSET));
375
        setRotation(state.getDouble(FIELD_ROTATION));
376
        setSize(state.getDouble(FIELD_SIZE));
377

    
378
        setDrawLineToOffset(state.getBoolean(FIELD_DRAWLINETOOFFSET, false));
379
        setOffsetXExpression(state.getString(FIELD_OFFSETX_EXPRESSION));
380
        setOffsetYExpression(state.getString(FIELD_OFFSETY_EXPRESSION));
381
        setRotationExpression(state.getString(FIELD_ROTATION_EXPRESSION));
382
        setLineToOffsetColorExpression(state.getString(FIELD_LINETOOFFSETCOLOR_EXPRESSION));
383
    }
384

    
385
    @Override
386
    public void saveToState(PersistentState state) throws PersistenceException {
387
        // Save parent symbol properties
388
        super.saveToState(state);
389
        // Save own properties
390
        state.set(FIELD_COLOR, getColor());
391
        state.set(FIELD_MASK, getMask());
392
        state.set(FIELD_OFFSET, getOffset());
393
        state.set(FIELD_ROTATION, getRotation());
394
        state.set(FIELD_SIZE, getSize());
395

    
396
        state.set(FIELD_OFFSETX_EXPRESSION, getOffsetXExpression());
397
        state.set(FIELD_OFFSETY_EXPRESSION, getOffsetYExpression());
398
        state.set(FIELD_ROTATION_EXPRESSION, getRotationExpression());
399
        state.set(FIELD_DRAWLINETOOFFSET, isDrawLineToOffset());
400
        state.set(FIELD_LINETOOFFSETCOLOR_EXPRESSION, getLineToOffsetColorExpression());
401
    }
402

    
403
    public static class RegisterPersistence implements Callable {
404

    
405
        @Override
406
        public Object call() throws Exception {
407
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
408
            if (manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) {
409
                DynStruct definition = manager.addDefinition(
410
                        AbstractMarkerSymbol.class,
411
                        MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
412
                        MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
413
                        null,
414
                        null
415
                );
416

    
417
                // Extend the Symbol base definition
418
                definition.extend(manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME));
419

    
420
                // Color
421
                definition.addDynFieldObject(FIELD_COLOR).setMandatory(false).setClassOfValue(Color.class);
422
                // Mask
423
                definition.addDynFieldObject(FIELD_MASK).setMandatory(false).setClassOfValue(IMask.class);
424

    
425
                // Offset
426
                definition.addDynFieldObject(FIELD_OFFSET).setMandatory(false).setClassOfValue(Point2D.class);
427

    
428
                // Rotation
429
                definition.addDynFieldDouble(FIELD_ROTATION).setMandatory(false);
430

    
431
                // Size
432
                definition.addDynFieldDouble(FIELD_SIZE).setMandatory(true);
433

    
434

    
435
                definition.addDynFieldBoolean(FIELD_DRAWLINETOOFFSET).setMandatory(false);
436
                definition.addDynFieldString(FIELD_OFFSETX_EXPRESSION).setMandatory(false);
437
                definition.addDynFieldString(FIELD_OFFSETY_EXPRESSION).setMandatory(false);
438
                definition.addDynFieldString(FIELD_LINETOOFFSETCOLOR_EXPRESSION).setMandatory(false);
439
                definition.addDynFieldString(FIELD_ROTATION_EXPRESSION).setMandatory(false);
440
            }
441
            return true;
442
        }
443

    
444
    }
445

    
446
}