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

History | View | Annotate | Download (11.1 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.BasicStroke;
27
import java.awt.Color;
28
import java.awt.Graphics2D;
29
import java.awt.geom.AffineTransform;
30
31
import org.gvsig.fmap.dal.feature.Feature;
32
import org.gvsig.fmap.geom.Geometry;
33
import org.gvsig.fmap.geom.primitive.GeneralPathX;
34
import org.gvsig.fmap.mapcontext.MapContextLocator;
35
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
36
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
37
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
38
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.ISimpleMarkerSymbol;
39
import org.gvsig.tools.ToolsLocator;
40
import org.gvsig.tools.dynobject.DynStruct;
41
import org.gvsig.tools.persistence.PersistenceManager;
42
import org.gvsig.tools.persistence.PersistentState;
43
import org.gvsig.tools.persistence.exception.PersistenceException;
44
import org.gvsig.tools.task.Cancellable;
45
import org.gvsig.tools.util.Callable;
46
47
/**
48
 * SimpleMarkerSymbol is the most basic symbol for the representation of point objects
49
 * which can define its size and color apart from the rotation and the offset from a point
50
 * in the map.
51
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
52
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
53
 */
54
public class SimpleMarkerSymbol extends AbstractMarkerSymbol implements ISimpleMarkerSymbol {
55
56
        public static final String SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleMarkerSymbol";
57
58
        private static final String FIELD_OUTLINED = "outlined";
59
        private static final String FIELD_OUTLINECOLOR = "outlineColor";
60
        private static final String FIELD_OUTLINESIZE = "outlineSize";
61
        private static final String FIELD_MARKERSTYLE = "markerStyle";
62
63
        private boolean outlined;
64
        private Color outlineColor;
65
        private double outlineSize;
66
        private ISymbol selectionSymbol;
67
        private int markerStyle;
68
69
        public SimpleMarkerSymbol() {
70
                super();
71
        }
72
73
        public ISymbol getSymbolForSelection() {
74
                if (selectionSymbol == null) {
75
                        selectionSymbol = (SimpleMarkerSymbol) cloneForSelection();
76
                }
77
                return selectionSymbol;
78
        }
79
80
        public void draw(Graphics2D g, AffineTransform affineTransform,
81
                        Geometry geom, Feature feature, Cancellable cancel) {
82
                int x, y;
83
                org.gvsig.fmap.geom.primitive.Point p = (org.gvsig.fmap.geom.primitive.Point)geom.cloneGeometry();
84
                p.transform(affineTransform);
85
86
                int size = (int) getSize();
87
88
                int halfSize = size/2;
89
                x = ((int) (p.getX() + getOffset().getX()) - halfSize);
90
                y = ((int) (p.getY() + getOffset().getY()) - halfSize);
91
92
                // IMask mask = getMask();
93
                // if (mask != null) {
94
                // IFillSymbol maskShape = mask.getFillSymbol();
95
                // // maskShape.draw(g, null, mask.getHaloShape(shp));
96
                // }
97
98
                g.setColor(getColor());
99
                GeneralPathX genPath = null;
100
                g.setStroke(new BasicStroke(1));
101
102
                switch (markerStyle) {
103
                case CIRCLE_STYLE:
104
                        g.fillOval(x, y, size, size);
105
                        break;
106
                case SQUARE_STYLE:
107
                        g.fillRect(x, y, size, size);
108
                        break;
109
                case CROSS_STYLE:
110
                        x = x + halfSize;
111
                        y = y + halfSize;
112
                        genPath = new GeneralPathX();
113
                        genPath.moveTo(x, y - halfSize);
114
                        genPath.lineTo(x, y + halfSize);
115
                        genPath.moveTo(x - halfSize, y);
116
                        genPath.lineTo(x + halfSize, y);
117
                        g.draw(genPath);
118
                        break;
119
                case DIAMOND_STYLE:
120
                        x = x + halfSize;
121
                        y = y + halfSize;
122
                        genPath = new GeneralPathX();
123
                        genPath.moveTo(x-halfSize, y);
124
                        genPath.lineTo(x, y+halfSize);
125
                        genPath.lineTo(x+halfSize, y);
126
                        genPath.lineTo(x, y-halfSize);
127
                        genPath.lineTo(x-halfSize, y);
128
                        genPath.closePath();
129
                        g.fill(genPath);
130
                        break;
131
                case X_STYLE:
132
                        x = x + halfSize;
133
                        y = y + halfSize;
134
                        genPath = new GeneralPathX();
135
                        genPath.moveTo(x-halfSize, y - halfSize);
136
                        genPath.lineTo(x+halfSize, y + halfSize);
137
                        genPath.moveTo(x - halfSize, y + halfSize);
138
                        genPath.lineTo(x + halfSize, y - halfSize);
139
                        g.draw(genPath);
140
                        break;
141
                case TRIANGLE_STYLE:
142
                        x = x + halfSize;
143
                        y = y + halfSize;
144
                        int otherSize = (int) (size * 0.55);
145
                        genPath = new GeneralPathX();
146
                        genPath.moveTo(x - halfSize,
147
                                y + halfSize);
148
                        genPath.lineTo(x + halfSize,
149
                                y + halfSize);
150
                        genPath.lineTo(x, y - otherSize);
151
                        genPath.closePath();
152
153
                        g.fill(genPath);
154
                        break;
155
                case STAR_STYLE:
156
                        x = x + halfSize;
157
                        y = y + halfSize;
158
                        genPath = new GeneralPathX();
159
                        genPath.moveTo(x-halfSize, y);
160
161
                        genPath.lineTo(x-2*(halfSize/3), y+(halfSize/3));
162
                        genPath.lineTo(x-2*(halfSize/3), y+2*(halfSize/3));
163
                        genPath.lineTo(x-(halfSize/3), y+2*(halfSize/3));
164
165
                        genPath.lineTo(x, y+halfSize);
166
167
                        genPath.lineTo(x+(halfSize/3), y+2*(halfSize/3));
168
                        genPath.lineTo(x+2*(halfSize/3), y+2*(halfSize/3));
169
                        genPath.lineTo(x+2*(halfSize/3), y+(halfSize/3));
170
171
                        genPath.lineTo(x+(halfSize), y);
172
173
                        genPath.lineTo(x+2*(halfSize/3), y-(halfSize/3));
174
                        genPath.lineTo(x+2*(halfSize/3), y-2*(halfSize/3));
175
                        genPath.lineTo(x+(halfSize/3), y-2*(halfSize/3));
176
177
                        genPath.lineTo(x, y-halfSize);
178
179
                        genPath.lineTo(x-(halfSize/3), y-2*(halfSize/3));
180
                        genPath.lineTo(x-2*(halfSize/3), y-2*(halfSize/3));
181
                        genPath.lineTo(x-2*(halfSize/3), y-(halfSize/3));
182
183
                        genPath.lineTo(x-halfSize, y);
184
185
                        genPath.closePath();
186
187
188
                        g.fill(genPath);
189
190
                        break;
191
                }
192
193
194
                if (outlined) {
195
                        g.setColor(outlineColor);
196
                        switch (markerStyle) {
197
                        case CIRCLE_STYLE:
198
                                g.drawOval(x, y, size, size);
199
                                break;
200
                        case SQUARE_STYLE:
201
                                g.drawRect(x, y, size, size);
202
                                break;
203
                        case CROSS_STYLE:
204
                        case DIAMOND_STYLE:
205
                        case STAR_STYLE:
206
                        case X_STYLE:
207
                        case TRIANGLE_STYLE:
208
                                g.draw(genPath);
209
                                break;
210
                        }
211
                }
212
        }
213
214
        /**
215
         * Returns true or false depending if the simple marker symbol has an outline or not.
216
         * @return Returns the outline.
217
         */
218
        public boolean hasOutline() {
219
                return outlined;
220
        }
221
222
        /**
223
         * Establishes the outline for the simple marker symbol.
224
         * @param outline  The outline to set.
225
         */
226
        public void setOutlined(boolean outlined) {
227
                this.outlined = outlined;
228
        }
229
230
        /**
231
         * Returns the outline color for the symple marker symbol
232
         *
233
         * @return Color,outlineColor.
234
         */
235
        public Color getOutlineColor() {
236
                return outlineColor;
237
        }
238
239
        /**
240
         * Sets the outline color for the simple marker symbol
241
         * @param outlineColor, Color
242
         */
243
        public void setOutlineColor(Color outlineColor) {
244
                this.outlineColor = outlineColor;
245
        }
246
247
        /**
248
         * Gets the size of the outline for the simple marker symbol
249
         * @return  Returns the outlineSize.
250
         */
251
        public double getOutlineSize() {
252
                return outlineSize;
253
        }
254
255
        /**
256
         * Establishes the size for the outline of the simple marker symbol
257
         * @param outlineSize  The outlineSize to set.
258
         */
259
        public void setOutlineSize(double outlineSize) {
260
                this.outlineSize = outlineSize;
261
        }
262
263
        /**
264
         * @return  Returns the selectionSymbol.
265
         */
266
        public ISymbol getSelectionSymbol() {
267
                return selectionSymbol;
268
        }
269
270
        /**
271
         * @param selectionSymbol  The selectionSymbol to set.
272
         */
273
        public void setSelectionSymbol(ISymbol selectionSymbol) {
274
                this.selectionSymbol = selectionSymbol;
275
        }
276
        /**
277
         * Sets the style for the simple marker symbol
278
         * @param style
279
         */
280
        public void setStyle(int style) {
281
                this.markerStyle = style;
282
        }
283
284
        /**
285
         * Obtains the style for the simple marker symbol
286
         * @return markerStyle,int
287
         */
288
        public int getStyle() {
289
                return markerStyle;
290
        }
291
292
        public Object clone() throws CloneNotSupportedException {
293
                SimpleMarkerSymbol copy = (SimpleMarkerSymbol) super.clone();
294
295
                // clone selection
296
                if (selectionSymbol != null) {
297
                        copy.selectionSymbol = (ISymbol) selectionSymbol.clone();
298
                }
299
300
                return copy;
301
        }
302
303
        /**
304
         * Returns if the marker symbol should be drawn outlined.
305
         * @return if it is outlined
306
         */
307
        public boolean isOutlined() {
308
                return outlined;
309
        }
310
311
        public void loadFromState(PersistentState state)
312
                        throws PersistenceException {
313
                // Set parent fill symbol properties
314
                super.loadFromState(state);
315
316
                // Set own properties
317
                setStyle(state.getInt(FIELD_MARKERSTYLE));
318
                setOutlined(state.getBoolean(FIELD_OUTLINED));
319
                if (isOutlined()) {
320
                        setOutlineColor((Color) state.get(FIELD_OUTLINECOLOR));
321
                        setOutlineSize(state.getDouble(FIELD_OUTLINESIZE));
322
                }
323
        }
324
325
        public void saveToState(PersistentState state) throws PersistenceException {
326
                // Save parent fill symbol properties
327
                super.saveToState(state);
328
329
                // Save own properties
330
                state.set(FIELD_MARKERSTYLE, getStyle());
331
                state.set(FIELD_OUTLINED, isOutlined());
332
                if (isOutlined()) {
333
                        state.set(FIELD_OUTLINECOLOR, getOutlineColor());
334
                        state.set(FIELD_OUTLINESIZE, getOutlineSize());
335
                }
336
        }
337
338
339
        public static class RegisterPersistence implements Callable {
340
341
                public Object call() throws Exception {
342
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
343
                        if( manager.getDefinition(SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
344
                                DynStruct definition = manager.addDefinition(
345
                                                SimpleMarkerSymbol.class,
346
                                                SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
347
                                                SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
348
                                                null,
349
                                                null
350
                                );
351
                                // Extend the FillSymbol base definition
352
                                definition.extend(manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME));
353
354
                                // Marker style
355
                                definition.addDynFieldInt(FIELD_MARKERSTYLE).setMandatory(true);
356
                                // Outlined?
357
                                definition.addDynFieldBoolean(FIELD_OUTLINED).setMandatory(true);
358
                                // Outline color
359
                                definition.addDynFieldObject(FIELD_OUTLINECOLOR).setClassOfValue(Color.class);
360
                                // Outline size
361
                                definition.addDynFieldDouble(FIELD_OUTLINESIZE);
362
                        }
363
                        return Boolean.TRUE;
364
                }
365
366
        }
367
368
        public static class RegisterSymbol implements Callable {
369
370
                public Object call() throws Exception {
371
                        int[] shapeTypes;
372
                        SymbolManager manager = MapContextLocator.getSymbolManager();
373
374
                shapeTypes =
375
                    new int[] { Geometry.TYPES.POINT, Geometry.TYPES.MULTIPOINT };
376
                manager.registerSymbol(IMarkerSymbol.SYMBOL_NAME,
377
                    shapeTypes,
378
                    SimpleMarkerSymbol.class);
379
380
                        return Boolean.TRUE;
381
                }
382
383
        }
384
385
}