Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / 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 @ 36665

History | View | Annotate | Download (9.07 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.Geometry.SUBTYPES;
34
import org.gvsig.fmap.geom.Geometry.TYPES;
35
import org.gvsig.fmap.geom.GeometryLocator;
36
import org.gvsig.fmap.geom.GeometryManager;
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.DynStruct;
47
import org.gvsig.tools.persistence.PersistenceManager;
48
import org.gvsig.tools.persistence.PersistentState;
49
import org.gvsig.tools.persistence.exception.PersistenceException;
50
import org.gvsig.tools.util.Callable;
51

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

    
63
    private static final String FIELD_ROTATION = "rotation";
64

    
65
    private static final String FIELD_OFFSET = "offset";
66

    
67
    private static final String FIELD_SIZE = "size";
68
    
69
    private static final String FIELD_MASK = "mask";
70
    
71
    private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
72
        private Color color;
73
        private double rotation;
74
        private Point2D offset = new Point2D.Double();
75
        private double size = 4d;
76
        private IMask mask;
77

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

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

    
90
        public double getRotation() {
91
                return rotation;
92
        }
93

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

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

    
105
        public void setOffset(Point2D offset) {
106
                this.offset = offset;
107
        }
108

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

    
113
        public int getOnePointRgb() {
114
                return color.getRGB();
115
        }
116

    
117
        public double getSize() {
118
                return size;
119
        }
120

    
121
        public void setSize(double size) {
122
                this.size = size;
123
        }
124

    
125
        public Color getColor() {
126
                return color;
127
        }
128

    
129
        public void setColor(Color color) {
130
                this.color = color;
131
        }
132

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

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

    
145

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

    
163
        public final IMask getMask() {
164
                return mask;
165
        }
166

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

    
187

    
188

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

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

    
198

    
199

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

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

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

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

    
260
        public static class RegisterPersistence implements Callable {
261

    
262
                public Object call() throws Exception {
263
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
264
                        if( manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
265
                                DynStruct definition = manager.addDefinition(
266
                                                AbstractMarkerSymbol.class,
267
                                                MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
268
                                                MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
269
                                                null, 
270
                                                null
271
                                );
272

    
273
                                // Extend the Symbol base definition
274
                                definition.extend(manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME));
275

    
276
                                // Color
277
                                definition.addDynFieldObject(FIELD_COLOR).setMandatory(false).setClassOfValue(Color.class);
278
                                // Mask
279
                                definition.addDynFieldObject(FIELD_MASK).setMandatory(false).setClassOfValue(IMask.class);
280

    
281
                                // Offset
282
                                definition.addDynFieldObject(FIELD_OFFSET).setMandatory(false).setClassOfValue(Point2D.class);
283

    
284
                                // Rotation
285
                                definition.addDynFieldDouble(FIELD_ROTATION).setMandatory(true);
286

    
287
                                // Size
288
                                definition.addDynFieldDouble(FIELD_SIZE).setMandatory(true);
289
                        }
290
                        return Boolean.TRUE;
291
                }
292
                
293
        }
294

    
295
}