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 / SimpleMarkerSymbol.java @ 34111

History | View | Annotate | Download (11 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.BasicStroke;
25
import java.awt.Color;
26
import java.awt.Graphics2D;
27
import java.awt.geom.AffineTransform;
28

    
29
import org.gvsig.fmap.dal.feature.Feature;
30
import org.gvsig.fmap.geom.Geometry;
31
import org.gvsig.fmap.geom.primitive.GeneralPathX;
32
import org.gvsig.fmap.mapcontext.MapContextLocator;
33
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
34
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
35
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
36
import org.gvsig.tools.ToolsLocator;
37
import org.gvsig.tools.dynobject.DynStruct;
38
import org.gvsig.tools.persistence.PersistenceManager;
39
import org.gvsig.tools.persistence.PersistentState;
40
import org.gvsig.tools.persistence.exception.PersistenceException;
41
import org.gvsig.tools.task.Cancellable;
42
import org.gvsig.tools.util.Callable;
43

    
44
/**
45
 * SimpleMarkerSymbol is the most basic symbol for the representation of point objects
46
 * which can define its size and color apart from the rotation and the offset from a point
47
 * in the map.
48
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
49
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
50
 */
51
public class SimpleMarkerSymbol extends AbstractMarkerSymbol {
52

    
53
        public static final String SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleMarkerSymbol";
54
        
55
        private static final String FIELD_OUTLINED = "outlined"; 
56
        private static final String FIELD_OUTLINECOLOR = "outlineColor"; 
57
        private static final String FIELD_OUTLINESIZE = "outlineSize"; 
58
        private static final String FIELD_MARKERSTYLE = "markerStyle"; 
59

    
60
        private boolean outlined;
61
        private Color outlineColor;
62
        private double outlineSize;
63
        private ISymbol selectionSymbol;
64
        private int markerStyle;
65

    
66
        public SimpleMarkerSymbol() {
67
                super();
68
        }
69
        
70
        public ISymbol getSymbolForSelection() {
71
                if (selectionSymbol == null) {
72
                        selectionSymbol = (SimpleMarkerSymbol) cloneForSelection();
73
                }
74
                return selectionSymbol;
75
        }
76

    
77
        public void draw(Graphics2D g, AffineTransform affineTransform,
78
                        Geometry geom, Feature feature, Cancellable cancel) {
79
                int x, y;
80
//                Point2D p = new Point2D.Double(((FPoint2D) shp).getX(), ((FPoint2D) shp)
81
//                                .getY());
82
                org.gvsig.fmap.geom.primitive.Point p = (org.gvsig.fmap.geom.primitive.Point) geom;
83

    
84
                int size = (int) getSize();
85

    
86
                int halfSize = size/2;
87
                x = ((int) (p.getX() + getOffset().getX()) - halfSize);
88
                y = ((int) (p.getY() + getOffset().getY()) - halfSize);
89

    
90
                // IMask mask = getMask();
91
                // if (mask != null) {
92
                // IFillSymbol maskShape = mask.getFillSymbol();
93
                // // maskShape.draw(g, null, mask.getHaloShape(shp));
94
                // }
95

    
96
                g.setColor(getColor());
97
                GeneralPathX genPath = null;
98
                g.setStroke(new BasicStroke(1));
99

    
100
                switch (markerStyle) {
101
                case CIRCLE_STYLE:
102
                        g.fillOval(x, y, size, size);
103
                        break;
104
                case SQUARE_STYLE:
105
                        g.fillRect(x, y, size, size);
106
                        break;
107
                case CROSS_STYLE:
108
                        x = x + halfSize;
109
                        y = y + halfSize;
110
                        genPath = new GeneralPathX();
111
                        genPath.moveTo(x, y - halfSize);
112
                        genPath.lineTo(x, y + halfSize);
113
                        genPath.moveTo(x - halfSize, y);
114
                        genPath.lineTo(x + halfSize, y);
115
                        g.draw(genPath);
116
                        break;
117
                case DIAMOND_STYLE:
118
                        x = x + halfSize;
119
                        y = y + halfSize;
120
                        genPath = new GeneralPathX();
121
                        genPath.moveTo(x-halfSize, y);
122
                        genPath.lineTo(x, y+halfSize);
123
                        genPath.lineTo(x+halfSize, y);
124
                        genPath.lineTo(x, y-halfSize);
125
                        genPath.lineTo(x-halfSize, y);
126
                        genPath.closePath();
127
                        g.fill(genPath);
128
                        break;
129
                case X_STYLE:
130
                        x = x + halfSize;
131
                        y = y + halfSize;
132
                        genPath = new GeneralPathX();
133
                        genPath.moveTo(x-halfSize, y - halfSize);
134
                        genPath.lineTo(x+halfSize, y + halfSize);
135
                        genPath.moveTo(x - halfSize, y + halfSize);
136
                        genPath.lineTo(x + halfSize, y - halfSize);
137
                        g.draw(genPath);
138
                        break;
139
                case TRIANGLE_STYLE:
140
                        x = x + halfSize;
141
                        y = y + halfSize;
142
                        int otherSize = (int) (size * 0.55);
143
                        genPath = new GeneralPathX();
144
                        genPath.moveTo(x - halfSize,
145
                                y + halfSize);
146
                        genPath.lineTo(x + halfSize,
147
                                y + halfSize);
148
                        genPath.lineTo(x, y - otherSize);
149
                        genPath.closePath();
150

    
151
                        g.fill(genPath);
152
                        break;
153
                case STAR_STYLE:
154
                        x = x + halfSize;
155
                        y = y + halfSize;
156
                        genPath = new GeneralPathX();
157
                        genPath.moveTo(x-halfSize, y);
158

    
159
                        genPath.lineTo(x-2*(halfSize/3), y+(halfSize/3));
160
                        genPath.lineTo(x-2*(halfSize/3), y+2*(halfSize/3));
161
                        genPath.lineTo(x-(halfSize/3), y+2*(halfSize/3));
162

    
163
                        genPath.lineTo(x, y+halfSize);
164

    
165
                        genPath.lineTo(x+(halfSize/3), y+2*(halfSize/3));
166
                        genPath.lineTo(x+2*(halfSize/3), y+2*(halfSize/3));
167
                        genPath.lineTo(x+2*(halfSize/3), y+(halfSize/3));
168

    
169
                        genPath.lineTo(x+(halfSize), y);
170

    
171
                        genPath.lineTo(x+2*(halfSize/3), y-(halfSize/3));
172
                        genPath.lineTo(x+2*(halfSize/3), y-2*(halfSize/3));
173
                        genPath.lineTo(x+(halfSize/3), y-2*(halfSize/3));
174

    
175
                        genPath.lineTo(x, y-halfSize);
176

    
177
                        genPath.lineTo(x-(halfSize/3), y-2*(halfSize/3));
178
                        genPath.lineTo(x-2*(halfSize/3), y-2*(halfSize/3));
179
                        genPath.lineTo(x-2*(halfSize/3), y-(halfSize/3));
180

    
181
                        genPath.lineTo(x-halfSize, y);
182

    
183
                        genPath.closePath();
184

    
185

    
186
                        g.fill(genPath);
187

    
188
                        break;
189
                }
190

    
191

    
192
                if (outlined) {
193
                        g.setColor(outlineColor);
194
                        switch (markerStyle) {
195
                        case CIRCLE_STYLE:
196
                                g.drawOval(x, y, size, size);
197
                                break;
198
                        case SQUARE_STYLE:
199
                                g.drawRect(x, y, size, size);
200
                                break;
201
                        case CROSS_STYLE:
202
                        case DIAMOND_STYLE:
203
                        case STAR_STYLE:
204
                        case X_STYLE:
205
                        case TRIANGLE_STYLE:
206
                                g.draw(genPath);
207
                                break;
208
                        }
209
                }
210
        }
211

    
212
//        public String getClassName() {
213
//                return getClass().getName();
214
//        }
215

    
216
        /**
217
         * Returns true or false depending if the simple marker symbol has an outline or not.
218
         * @return Returns the outline.
219
         */
220
        public boolean hasOutline() {
221
                return outlined;
222
        }
223

    
224
        /**
225
         * Establishes the outline for the simple marker symbol.
226
         * @param outline  The outline to set.
227
         */
228
        public void setOutlined(boolean outlined) {
229
                this.outlined = outlined;
230
        }
231

    
232
        /**
233
         * Returns the outline color for the symple marker symbol
234
         *
235
         * @return Color,outlineColor.
236
         */
237
        public Color getOutlineColor() {
238
                return outlineColor;
239
        }
240

    
241
        /**
242
         * Sets the outline color for the simple marker symbol
243
         * @param outlineColor, Color
244
         */
245
        public void setOutlineColor(Color outlineColor) {
246
                this.outlineColor = outlineColor;
247
        }
248

    
249
        /**
250
         * Gets the size of the outline for the simple marker symbol
251
         * @return  Returns the outlineSize.
252
         */
253
        public double getOutlineSize() {
254
                return outlineSize;
255
        }
256

    
257
        /**
258
         * Establishes the size for the outline of the simple marker symbol
259
         * @param outlineSize  The outlineSize to set.
260
         */
261
        public void setOutlineSize(double outlineSize) {
262
                this.outlineSize = outlineSize;
263
        }
264

    
265
        /**
266
         * @return  Returns the selectionSymbol.
267
         */
268
        public ISymbol getSelectionSymbol() {
269
                return selectionSymbol;
270
        }
271

    
272
        /**
273
         * @param selectionSymbol  The selectionSymbol to set.
274
         */
275
        public void setSelectionSymbol(ISymbol selectionSymbol) {
276
                this.selectionSymbol = selectionSymbol;
277
        }
278
        /**
279
         * Sets the style for the simple marker symbol
280
         * @param style
281
         */
282
        public void setStyle(int style) {
283
                this.markerStyle = style;
284
        }
285

    
286
        /**
287
         * Obtains the style for the simple marker symbol
288
         * @return markerStyle,int
289
         */
290
        public int getStyle() {
291
                return markerStyle;
292
        }
293

    
294
        public Object clone() throws CloneNotSupportedException {
295
                SimpleMarkerSymbol copy = (SimpleMarkerSymbol) super.clone();
296
                
297
                // clone selection
298
                if (selectionSymbol != null) {
299
                        copy.selectionSymbol = (ISymbol) selectionSymbol.clone();
300
                }
301
                
302
                return copy;
303
        }
304

    
305
        /**
306
         * Returns if the marker symbol should be drawn outlined.
307
         * @return if it is outlined
308
         */
309
        public boolean isOutlined() {
310
                return outlined;
311
        }
312

    
313
        public void loadFromState(PersistentState state)
314
                        throws PersistenceException {
315
                // Set parent fill symbol properties
316
                super.loadFromState(state);
317

    
318
                // Set own properties
319
                setStyle(state.getInt(FIELD_MARKERSTYLE));
320
                setOutlined(state.getBoolean(FIELD_OUTLINED));
321
                if (isOutlined()) {
322
                        setOutlineColor((Color) state.get(FIELD_OUTLINECOLOR));
323
                        setOutlineSize(state.getDouble(FIELD_OUTLINESIZE));
324
                }
325
        }
326

    
327
        public void saveToState(PersistentState state) throws PersistenceException {
328
                // Save parent fill symbol properties
329
                super.saveToState(state);
330

    
331
                // Save own properties
332
                state.set(FIELD_MARKERSTYLE, getStyle());
333
                state.set(FIELD_OUTLINED, isOutlined());
334
                if (isOutlined()) {
335
                        state.set(FIELD_OUTLINECOLOR, getOutlineColor());
336
                        state.set(FIELD_OUTLINESIZE, getOutlineSize());
337
                }
338
        }
339

    
340
        
341
        public static class RegisterPersistence implements Callable {
342

    
343
                public Object call() throws Exception {
344
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
345
                        if( manager.getDefinition(SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
346
                                DynStruct definition = manager.addDefinition(
347
                                                SimpleMarkerSymbol.class,
348
                                                SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
349
                                                SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
350
                                                null, 
351
                                                null
352
                                );
353
                                // Extend the FillSymbol base definition
354
                                definition.extend(manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME));
355

    
356
                                // Marker style
357
                                definition.addDynFieldInt(FIELD_MARKERSTYLE).setMandatory(true);
358
                                // Outlined?
359
                                definition.addDynFieldBoolean(FIELD_OUTLINED).setMandatory(true);
360
                                // Outline color
361
                                definition.addDynFieldObject(FIELD_OUTLINECOLOR).setClassOfValue(Color.class);
362
                                // Outline size
363
                                definition.addDynFieldDouble(FIELD_OUTLINESIZE);
364
                        }
365
                        return Boolean.TRUE;
366
                }
367
                
368
        }
369

    
370
        public static class RegisterSymbol implements Callable {
371

    
372
                public Object call() throws Exception {
373
                        int[] shapeTypes;
374
                        SymbolManager manager = MapContextLocator.getSymbolManager();
375

    
376
                shapeTypes =
377
                    new int[] { Geometry.TYPES.POINT, Geometry.TYPES.MULTIPOINT };
378
                manager.registerSymbol(IMarkerSymbol.SYMBOL_NAME,
379
                    shapeTypes,
380
                    SimpleMarkerSymbol.class);
381

    
382
                        return Boolean.TRUE;
383
                }
384
                
385
        }
386

    
387
}