Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.symbology / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / marker / impl / AbstractMarkerSymbol.java @ 31544

History | View | Annotate | Download (8.44 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.impl;
23

    
24
import java.awt.Color;
25
import java.awt.Graphics2D;
26
import java.awt.Point;
27
import java.awt.Rectangle;
28
import java.awt.geom.AffineTransform;
29
import java.awt.geom.Point2D;
30

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

    
50
/**
51
 * Abstract class that any MARKER SYMBOL should extend.
52
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
53
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
54
 */
55
public abstract class AbstractMarkerSymbol extends AbstractSymbol implements IMarkerSymbol {
56
    
57
    public static final String MARKER_SYMBOL_DYNCLASS_NAME = "MarkerSymbol";    
58
    
59
    private static final String FIELD_COLOR = "color";
60

    
61
    private static final String FIELD_ROTATION = "rotation";
62

    
63
    private static final String FIELD_OFFSET = "offset";
64

    
65
    private static final String FIELD_SIZE = "size";
66
    
67
    private static final String FIELD_MASK = "mask";
68
    
69
            private static final GeometryManager geomManager = 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 =
79
                                MapContextLocator.getSymbolManager()
80
                                                .getSymbolPreferences()
81
                                                .getDefaultSymbolColor();
82
        }
83

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

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

    
92
        public void setRotation(double r) {
93
                this.rotation = r;
94
        }
95

    
96
        public Point2D getOffset() {
97
                if (offset == null) {
98
                        offset = new Point();
99
                }
100
                return offset;
101
        }
102

    
103
        public void setOffset(Point2D offset) {
104
                this.offset = offset;
105
        }
106

    
107
        public boolean isSuitableFor(Geometry geom) {
108
                return geom.getType() == Geometry.TYPES.POINT;
109
        }
110

    
111
        public int getOnePointRgb() {
112
                return color.getRGB();
113
        }
114

    
115
        public double getSize() {
116
                return size;
117
        }
118

    
119
        public void setSize(double size) {
120
                this.size = size;
121
        }
122

    
123
        public Color getColor() {
124
                return color;
125
        }
126

    
127
        public void setColor(Color color) {
128
                this.color = color;
129
        }
130

    
131
        public void setAlpha(int outlineAlpha) {
132
                Color color = getColor();
133
                setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(),
134
                                outlineAlpha));
135
        }
136

    
137
        public void getPixExtentPlus(Geometry geom, float[] distances, ViewPort viewPort, int dpi) {
138
                float cs = (float) getCartographicSize(viewPort, dpi, geom);
139
                distances[0] = cs;
140
                distances[1] = cs;
141
        }
142

    
143

    
144
        public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) {
145
                double originalSize = getSize();
146
                double size=originalSize;
147
                // scale it to size
148
                int pq = properties.getPrintQuality();
149
                if (pq == PrintAttributes.PRINT_QUALITY_NORMAL){
150
                        size *= (double) 300/72;
151
                }else if (pq == PrintAttributes.PRINT_QUALITY_HIGH){
152
                        size *= (double) 600/72;
153
                }else if (pq == PrintAttributes.PRINT_QUALITY_DRAFT){
154
                        // size *= 72/72; // (which is the same than doing nothing)
155
                }
156
//                setSize(size);
157
                draw(g,at,geom, null, null);
158
//                setSize(originalSize);
159
        }
160

    
161
        public final IMask getMask() {
162
                return mask;
163
        }
164

    
165
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
166
                try {
167
                        if (properties==null)
168
                                draw(g, scaleInstance, geomManager.createPoint(r.getCenterX(), r.getCenterY(), SUBTYPES.GEOM2D), null, null);
169
                        else{
170
                                double originalSize = getSize();
171
                                double size=originalSize;
172
                                int pq = properties.getPrintQuality();
173
                                if (pq == PrintAttributes.PRINT_QUALITY_NORMAL){
174
                                        size *= (double) 300/72;
175
                                }else if (pq == PrintAttributes.PRINT_QUALITY_HIGH){
176
                                        size *= (double) 600/72;
177
                                }else if (pq == PrintAttributes.PRINT_QUALITY_DRAFT){
178
                                        // d *= 72/72; // (which is the same than doing nothing)
179
                                }
180
                                setSize(size);
181
                                print(g, scaleInstance, geomManager.createPoint(r.getCenterX(), r.getCenterY(), SUBTYPES.GEOM2D), properties);
182
                                setSize(originalSize);
183
                        }
184

    
185

    
186

    
187
                } catch (CreateGeometryException e) {
188
                        throw new SymbolDrawingException(TYPES.POINT);
189
                }
190
        }
191

    
192
        public final void setMask(IMask mask) {
193
                this.mask = mask;
194
        }
195

    
196

    
197

    
198
        public void setCartographicSize(double cartographicSize, Geometry geom) {
199
                setSize(cartographicSize);
200
        }
201

    
202
        public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
203
                double oldSize = getSize();
204
                setCartographicSize(getCartographicSize(
205
                                                                viewPort,
206
                                                                dpi,
207
                                                                geom),
208
                                                        geom);
209
                return oldSize;
210
        }
211

    
212
        public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
213
                return CartographicSupportToolkit.
214
                                        getCartographicLength(this,
215
                                                                                  getSize(),
216
                                                                                  viewPort,
217
                                                                                  dpi);
218
        }
219
        
220
        public Object clone() throws CloneNotSupportedException {
221
                AbstractMarkerSymbol copy  = (AbstractMarkerSymbol) super.clone();
222
                
223
                // Clone the offset
224
                if (offset != null) {
225
                        copy.offset = (Point2D) offset.clone();
226
                }
227
                
228
                // clone the mask
229
                if (mask != null) {
230
                        copy.mask = (IMask) mask.clone();
231
                }
232
                return copy;
233
        }
234
        
235
        public void loadFromState(PersistentState state)
236
                        throws PersistenceException {
237
                // Set parent symbol properties
238
                super.loadFromState(state);
239
                // Set own properties
240
                setColor((Color) state.get(FIELD_COLOR));
241
                setMask((IMask) state.get(FIELD_MASK));
242
                setOffset((Point2D) state.get(FIELD_OFFSET));
243
                setRotation(state.getDouble(FIELD_ROTATION));
244
                setSize(state.getDouble(FIELD_SIZE));
245
        }
246

    
247
        public void saveToState(PersistentState state) throws PersistenceException {
248
                // Save parent symbol properties
249
                super.saveToState(state);
250
                // Save own properties
251
                state.set(FIELD_COLOR, getColor());
252
                state.set(FIELD_MASK, getMask());
253
                state.set(FIELD_OFFSET, getOffset());
254
                state.set(FIELD_ROTATION, getRotation());
255
                state.set(FIELD_SIZE, getSize());
256
        }
257

    
258
        public static void registerPersistence() {
259
                // Add the AbstractMarkerSymbol DynClass definition.
260
                DynClass dynClass = ToolsLocator.getDynObjectManager().add(
261
                                MARKER_SYMBOL_DYNCLASS_NAME);
262

    
263
                // Extend the Symbol base definition
264
                dynClass.extend(SYMBOL_DYNCLASS_NAME);
265

    
266
                // Color
267
                dynClass.addDynFieldObject(FIELD_COLOR).setMandatory(true);
268
                // Mask
269
                dynClass.addDynFieldObject(FIELD_MASK).setMandatory(true);
270

    
271
                // Offset
272
                dynClass.addDynFieldObject(FIELD_OFFSET).setMandatory(true);
273

    
274
                // Rotation
275
                dynClass.addDynFieldDouble(FIELD_ROTATION).setMandatory(true);
276

    
277
                // Size
278
                dynClass.addDynFieldDouble(FIELD_SIZE).setMandatory(true);
279
        }
280
}