Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2021 / extensions / org.gvsig.symbology / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / marker / impl / AbstractMarkerSymbol.java @ 34111

History | View | Annotate | Download (9.84 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.DynClass;
47
import org.gvsig.tools.dynobject.DynStruct;
48
import org.gvsig.tools.persistence.PersistenceManager;
49
import org.gvsig.tools.persistence.PersistentState;
50
import org.gvsig.tools.persistence.exception.PersistenceException;
51
import org.gvsig.tools.util.Callable;
52

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
146

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

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

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

    
188

    
189

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

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

    
199

    
200

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

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

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

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

    
261
//        public static void registerPersistence() {
262
//                // Add the AbstractMarkerSymbol DynClass definition.
263
//                DynClass dynClass = ToolsLocator.getDynObjectManager().add(
264
//                                MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME);
265
//
266
//                // Extend the Symbol base definition
267
//                dynClass.extend(SYMBOL_PERSISTENCE_DEFINITION_NAME);
268
//
269
//                // Color
270
//                dynClass.addDynFieldObject(FIELD_COLOR).setMandatory(true);
271
//                // Mask
272
//                dynClass.addDynFieldObject(FIELD_MASK).setMandatory(true);
273
//
274
//                // Offset
275
//                dynClass.addDynFieldObject(FIELD_OFFSET).setMandatory(false);
276
//
277
//                // Rotation
278
//                dynClass.addDynFieldDouble(FIELD_ROTATION).setMandatory(true);
279
//
280
//                // Size
281
//                dynClass.addDynFieldDouble(FIELD_SIZE).setMandatory(true);
282
//        }
283
        
284
        public static class RegisterPersistence implements Callable {
285

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

    
297
                                // Extend the Symbol base definition
298
                                definition.extend(manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME));
299

    
300
                                // Color
301
                                definition.addDynFieldObject(FIELD_COLOR).setMandatory(false).setClassOfValue(Color.class);
302
                                // Mask
303
                                definition.addDynFieldObject(FIELD_MASK).setMandatory(false).setClassOfValue(IMask.class);
304

    
305
                                // Offset
306
                                definition.addDynFieldObject(FIELD_OFFSET).setMandatory(false).setClassOfValue(Point2D.class);
307

    
308
                                // Rotation
309
                                definition.addDynFieldDouble(FIELD_ROTATION).setMandatory(true);
310

    
311
                                // Size
312
                                definition.addDynFieldDouble(FIELD_SIZE).setMandatory(true);
313
                        }
314
                        return Boolean.TRUE;
315
                }
316
                
317
        }
318

    
319
}