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

History | View | Annotate | Download (9.12 KB)

1 40560 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40560 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 40435 jjdelcerro
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8 40560 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * of the License, or (at your option) any later version.
10 40560 jjdelcerro
 *
11 40435 jjdelcerro
 * 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 40560 jjdelcerro
 *
16 40435 jjdelcerro
 * 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 40560 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 40435 jjdelcerro
 * MA  02110-1301, USA.
20 40560 jjdelcerro
 *
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 40435 jjdelcerro
 */
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
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
57
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - 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
65
    private static final String FIELD_ROTATION = "rotation";
66
67
    private static final String FIELD_OFFSET = "offset";
68
69
    private static final String FIELD_SIZE = "size";
70
71
    private static final String FIELD_MASK = "mask";
72
73
    private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
74
        private Color color;
75
        private double rotation;
76
        private Point2D offset = new Point2D.Double();
77
        private double size = 4d;
78
        private IMask mask;
79
80
        public AbstractMarkerSymbol() {
81
                super();
82
                color =
83
                                MapContextLocator.getSymbolManager()
84
                                                .getSymbolPreferences()
85
                                                .getDefaultSymbolFillColor();
86
        }
87
88
        public final int getSymbolType() {
89
                return Geometry.TYPES.POINT;
90
        }
91
92
        public double getRotation() {
93
                return rotation;
94
        }
95
96
        public void setRotation(double r) {
97
                this.rotation = r;
98
        }
99
100
        public Point2D getOffset() {
101
                if (offset == null) {
102
                        offset = new Point();
103
                }
104
                return offset;
105
        }
106
107
        public void setOffset(Point2D offset) {
108
                this.offset = offset;
109
        }
110
111
        public boolean isSuitableFor(Geometry geom) {
112
                return geom.getType() == Geometry.TYPES.POINT;
113
        }
114
115
        public int getOnePointRgb() {
116
                return color.getRGB();
117
        }
118
119
        public double getSize() {
120
                return size;
121
        }
122
123
        public void setSize(double size) {
124
                this.size = size;
125
        }
126
127
        public Color getColor() {
128
                return color;
129
        }
130
131
        public void setColor(Color color) {
132
                this.color = color;
133
        }
134
135
        public void setAlpha(int outlineAlpha) {
136
                Color color = getColor();
137
                setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(),
138
                                outlineAlpha));
139
        }
140
141
        public void getPixExtentPlus(Geometry geom, float[] distances, ViewPort viewPort, int dpi) {
142
                float cs = (float) getCartographicSize(viewPort, dpi, geom);
143
                distances[0] = cs;
144
                distances[1] = cs;
145
        }
146
147
148
        public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) {
149
                double originalSize = getSize();
150
                double size=originalSize;
151
                // scale it to size
152
                int pq = properties.getPrintQuality();
153
                if (pq == PrintAttributes.PRINT_QUALITY_NORMAL){
154
                        size *= (double) 300/72;
155
                }else if (pq == PrintAttributes.PRINT_QUALITY_HIGH){
156
                        size *= (double) 600/72;
157
                }else if (pq == PrintAttributes.PRINT_QUALITY_DRAFT){
158
                        // size *= 72/72; // (which is the same than doing nothing)
159
                }
160
//                setSize(size);
161
                draw(g,at,geom, null, null);
162
//                setSize(originalSize);
163
        }
164
165
        public final IMask getMask() {
166
                return mask;
167
        }
168
169
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
170
                try {
171
                        if (properties==null)
172
                                draw(g, scaleInstance, geomManager.createPoint(r.getCenterX(), r.getCenterY(), SUBTYPES.GEOM2D), null, null);
173
                        else{
174
                                double originalSize = getSize();
175
                                double size=originalSize;
176
                                int pq = properties.getPrintQuality();
177
                                if (pq == PrintAttributes.PRINT_QUALITY_NORMAL){
178
                                        size *= (double) 300/72;
179
                                }else if (pq == PrintAttributes.PRINT_QUALITY_HIGH){
180
                                        size *= (double) 600/72;
181
                                }else if (pq == PrintAttributes.PRINT_QUALITY_DRAFT){
182
                                        // d *= 72/72; // (which is the same than doing nothing)
183
                                }
184
                                setSize(size);
185
                                print(g, scaleInstance, geomManager.createPoint(r.getCenterX(), r.getCenterY(), SUBTYPES.GEOM2D), properties);
186
                                setSize(originalSize);
187
                        }
188
189
190
191
                } catch (CreateGeometryException e) {
192
                        throw new SymbolDrawingException(TYPES.POINT);
193
                }
194
        }
195
196
        public final void setMask(IMask mask) {
197
                this.mask = mask;
198
        }
199
200
201
202
        public void setCartographicSize(double cartographicSize, Geometry geom) {
203
                setSize(cartographicSize);
204
        }
205
206
        public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
207
                double oldSize = getSize();
208
                setCartographicSize(getCartographicSize(
209
                                                                viewPort,
210
                                                                dpi,
211
                                                                geom),
212
                                                        geom);
213
                return oldSize;
214
        }
215
216
        public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
217
                return CartographicSupportToolkit.
218
                                        getCartographicLength(this,
219
                                                                                  getSize(),
220
                                                                                  viewPort,
221
                                                                                  dpi);
222
        }
223
224
        public Object clone() throws CloneNotSupportedException {
225
                AbstractMarkerSymbol copy  = (AbstractMarkerSymbol) super.clone();
226
227
                // Clone the offset
228
                if (offset != null) {
229
                        copy.offset = (Point2D) offset.clone();
230
                }
231
232
                // clone the mask
233
                if (mask != null) {
234
                        copy.mask = (IMask) mask.clone();
235
                }
236
                return copy;
237
        }
238
239
        public void loadFromState(PersistentState state)
240
                        throws PersistenceException {
241
                // Set parent symbol properties
242
                super.loadFromState(state);
243
                // Set own properties
244
                setColor((Color) state.get(FIELD_COLOR));
245
                setMask((IMask) state.get(FIELD_MASK));
246
                setOffset((Point2D) state.get(FIELD_OFFSET));
247
                setRotation(state.getDouble(FIELD_ROTATION));
248
                setSize(state.getDouble(FIELD_SIZE));
249
        }
250
251
        public void saveToState(PersistentState state) throws PersistenceException {
252
                // Save parent symbol properties
253
                super.saveToState(state);
254
                // Save own properties
255
                state.set(FIELD_COLOR, getColor());
256
                state.set(FIELD_MASK, getMask());
257
                state.set(FIELD_OFFSET, getOffset());
258
                state.set(FIELD_ROTATION, getRotation());
259
                state.set(FIELD_SIZE, getSize());
260
        }
261
262
        public static class RegisterPersistence implements Callable {
263
264
                public Object call() throws Exception {
265
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
266
                        if( manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
267
                                DynStruct definition = manager.addDefinition(
268
                                                AbstractMarkerSymbol.class,
269
                                                MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
270
                                                MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
271
                                                null,
272
                                                null
273
                                );
274
275
                                // Extend the Symbol base definition
276
                                definition.extend(manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME));
277
278
                                // Color
279
                                definition.addDynFieldObject(FIELD_COLOR).setMandatory(false).setClassOfValue(Color.class);
280
                                // Mask
281
                                definition.addDynFieldObject(FIELD_MASK).setMandatory(false).setClassOfValue(IMask.class);
282
283
                                // Offset
284
                                definition.addDynFieldObject(FIELD_OFFSET).setMandatory(false).setClassOfValue(Point2D.class);
285
286
                                // Rotation
287
                                definition.addDynFieldDouble(FIELD_ROTATION).setMandatory(true);
288
289
                                // Size
290
                                definition.addDynFieldDouble(FIELD_SIZE).setMandatory(true);
291
                        }
292
                        return Boolean.TRUE;
293
                }
294
295
        }
296
297
}