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

History | View | Annotate | Download (10.9 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

    
33
import org.gvsig.compat.print.PrintAttributes;
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
36
import org.gvsig.fmap.geom.Geometry.TYPES;
37
import org.gvsig.fmap.geom.GeometryLocator;
38
import org.gvsig.fmap.geom.GeometryManager;
39
import org.gvsig.fmap.geom.exception.CreateGeometryException;
40
import org.gvsig.fmap.mapcontext.MapContextLocator;
41
import org.gvsig.fmap.mapcontext.ViewPort;
42
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
43
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.AbstractSymbol;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.CartographicSupportToolkit;
45
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
46
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.IMask;
47
import org.gvsig.tools.ToolsLocator;
48
import org.gvsig.tools.dynobject.DynStruct;
49
import org.gvsig.tools.persistence.PersistenceManager;
50
import org.gvsig.tools.persistence.PersistentState;
51
import org.gvsig.tools.persistence.exception.PersistenceException;
52
import org.gvsig.tools.util.Callable;
53

    
54
/**
55
 * Abstract class that any MARKER SYMBOL should extend.
56
 *
57
 * @author gvSIG team
58
 */
59
public abstract class AbstractMarkerSymbol extends AbstractSymbol implements IMarkerSymbol {
60

    
61
    public static final String MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME = "MarkerSymbol";
62

    
63
    private static final String FIELD_COLOR = "color";
64
    private static final String FIELD_ROTATION = "rotation";
65
    private static final String FIELD_OFFSET = "offset";
66
    private static final String FIELD_SIZE = "size";
67
    private static final String FIELD_MASK = "mask";
68

    
69
    private static final GeometryManager GEOMETRY_MANAGER = GeometryLocator.getGeometryManager();
70
    private Color color;
71
    private double rotation;
72
    private Point2D offset = new Point2D.Double();
73
    private double size = 4d;
74
    private IMask mask;
75

    
76
    public AbstractMarkerSymbol() {
77
        super();
78
        color = MapContextLocator.getSymbolManager()
79
                .getSymbolPreferences()
80
                .getDefaultSymbolFillColor();
81
    }
82

    
83
    @Override
84
    public final int getSymbolType() {
85
        return Geometry.TYPES.POINT;
86
    }
87

    
88
    @Override
89
    public double getRotation() {
90
        return rotation;
91
    }
92

    
93
    @Override
94
    public void setRotation(double r) {
95
        this.rotation = r;
96
    }
97

    
98
    @Override
99
    public Point2D getOffset() {
100
        if (offset == null) {
101
            offset = new Point();
102
        }
103
        return offset;
104
    }
105

    
106
    @Override
107
    public void setOffset(Point2D offset) {
108
        this.offset = offset;
109
    }
110

    
111
    @Override
112
    public boolean isSuitableFor(Geometry geom) {
113
        return geom.getType() == Geometry.TYPES.POINT;
114
    }
115

    
116
    @Override
117
    public int getOnePointRgb() {
118
        return color.getRGB();
119
    }
120

    
121
    @Override
122
    public double getSize() {
123
        return size;
124
    }
125

    
126
    @Override
127
    public void setSize(double size) {
128
        this.size = size;
129
    }
130

    
131
    @Override
132
    public Color getColor() {
133
        return color;
134
    }
135

    
136
    @Override
137
    public void setColor(Color color) {
138
        this.color = color;
139
    }
140

    
141
    @Override
142
    public void setAlpha(int outlineAlpha) {
143
        Color theColor = getColor();
144
        setColor(new Color(theColor.getRed(), theColor.getGreen(), theColor.getBlue(),
145
                outlineAlpha));
146
    }
147

    
148
    @Override
149
    public void getPixExtentPlus(Geometry geom, float[] distances, ViewPort viewPort, int dpi) {
150
        float cs = (float) getCartographicSize(viewPort, dpi, geom);
151
        distances[0] = cs;
152
        distances[1] = cs;
153
    }
154

    
155
    @Override
156
    public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) {
157
        double originalSize = getSize();
158
        double theSize = originalSize;
159
        // scale it to size
160
        int pq = properties.getPrintQuality();
161
        switch (pq) {
162
            case PrintAttributes.PRINT_QUALITY_NORMAL:
163
                theSize *= (double) 300 / 72;
164
                break;
165
            case PrintAttributes.PRINT_QUALITY_HIGH:
166
                theSize *= (double) 600 / 72;
167
                break;
168
            case PrintAttributes.PRINT_QUALITY_DRAFT:
169
                // size *= 72/72; // (which is the same than doing nothing)
170
                break;
171
            default:
172
                break;
173
        }
174
//                setSize(theSize);
175
        draw(g, at, geom, null, null);
176
//                setSize(originalSize);
177
    }
178

    
179
    @Override
180
    public final IMask getMask() {
181
        return mask;
182
    }
183

    
184
    @Override
185
    public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
186
        try {
187
            if (properties == null) {
188
                draw(g, scaleInstance, GEOMETRY_MANAGER.createPoint(r.getCenterX(), r.getCenterY(), SUBTYPES.GEOM2D), null, null);
189
            } else {
190
                double originalSize = getSize();
191
                double theSize = originalSize;
192
                int pq = properties.getPrintQuality();
193
                switch (pq) {
194
                    case PrintAttributes.PRINT_QUALITY_NORMAL:
195
                        theSize *= (double) 300 / 72;
196
                        break;
197
                    case PrintAttributes.PRINT_QUALITY_HIGH:
198
                        theSize *= (double) 600 / 72;
199
                        break;
200
                    case PrintAttributes.PRINT_QUALITY_DRAFT:
201
                        // d *= 72/72; // (which is the same than doing nothing)
202
                        break;
203
                    default:
204
                        break;
205
                }
206
                setSize(theSize);
207
                print(g, scaleInstance, GEOMETRY_MANAGER.createPoint(r.getCenterX(), r.getCenterY(), SUBTYPES.GEOM2D), properties);
208
                setSize(originalSize);
209
            }
210

    
211
        } catch (CreateGeometryException e) {
212
            throw new SymbolDrawingException(TYPES.POINT);
213
        }
214
    }
215

    
216
    @Override
217
    public final void setMask(IMask mask) {
218
        this.mask = mask;
219
    }
220

    
221
    @Override
222
    public void setCartographicSize(double cartographicSize, Geometry geom) {
223
        setSize(cartographicSize);
224
    }
225

    
226
    @Override
227
    public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
228
        double oldSize = getSize();
229
        setCartographicSize(getCartographicSize(
230
                viewPort,
231
                dpi,
232
                geom),
233
                geom);
234
        return oldSize;
235
    }
236

    
237
    @Override
238
    public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
239
        return CartographicSupportToolkit.
240
                getCartographicLength(this,
241
                        getSize(),
242
                        viewPort,
243
                        dpi);
244
    }
245

    
246
    @Override
247
    public Object clone() throws CloneNotSupportedException {
248
        AbstractMarkerSymbol copy = (AbstractMarkerSymbol) super.clone();
249

    
250
        // Clone the offset
251
        if (offset != null) {
252
            copy.offset = (Point2D) offset.clone();
253
        }
254

    
255
        // clone the mask
256
        if (mask != null) {
257
            copy.mask = (IMask) mask.clone();
258
        }
259
        return copy;
260
    }
261

    
262
    @Override
263
    public void loadFromState(PersistentState state)
264
            throws PersistenceException {
265
        // Set parent symbol properties
266
        super.loadFromState(state);
267
        // Set own properties
268
        setColor((Color) state.get(FIELD_COLOR));
269
        setMask((IMask) state.get(FIELD_MASK));
270
        setOffset((Point2D) state.get(FIELD_OFFSET));
271
        setRotation(state.getDouble(FIELD_ROTATION));
272
        setSize(state.getDouble(FIELD_SIZE));
273
    }
274

    
275
    @Override
276
    public void saveToState(PersistentState state) throws PersistenceException {
277
        // Save parent symbol properties
278
        super.saveToState(state);
279
        // Save own properties
280
        state.set(FIELD_COLOR, getColor());
281
        state.set(FIELD_MASK, getMask());
282
        state.set(FIELD_OFFSET, getOffset());
283
        state.set(FIELD_ROTATION, getRotation());
284
        state.set(FIELD_SIZE, getSize());
285
    }
286

    
287
    public static class RegisterPersistence implements Callable {
288

    
289
        @Override
290
        public Object call() throws Exception {
291
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
292
            if (manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) {
293
                DynStruct definition = manager.addDefinition(
294
                        AbstractMarkerSymbol.class,
295
                        MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
296
                        MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
297
                        null,
298
                        null
299
                );
300

    
301
                // Extend the Symbol base definition
302
                definition.extend(manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME));
303

    
304
                // Color
305
                definition.addDynFieldObject(FIELD_COLOR).setMandatory(false).setClassOfValue(Color.class);
306
                // Mask
307
                definition.addDynFieldObject(FIELD_MASK).setMandatory(false).setClassOfValue(IMask.class);
308

    
309
                // Offset
310
                definition.addDynFieldObject(FIELD_OFFSET).setMandatory(false).setClassOfValue(Point2D.class);
311

    
312
                // Rotation
313
                definition.addDynFieldDouble(FIELD_ROTATION).setMandatory(true);
314

    
315
                // Size
316
                definition.addDynFieldDouble(FIELD_SIZE).setMandatory(true);
317
            }
318
            return true;
319
        }
320

    
321
    }
322

    
323
}