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 @ 41435

History | View | Annotate | Download (11.3 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
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 VERTICAL_LINE_STYLE:
120
                        genPath = new GeneralPathX();
121
                        genPath.moveTo(x + halfSize, y);
122
                        genPath.lineTo(x + halfSize, y + size);
123
                        g.draw(genPath);
124
                        break;
125
                case DIAMOND_STYLE:
126
                        x = x + halfSize;
127
                        y = y + halfSize;
128
                        genPath = new GeneralPathX();
129
                        genPath.moveTo(x-halfSize, y);
130
                        genPath.lineTo(x, y+halfSize);
131
                        genPath.lineTo(x+halfSize, y);
132
                        genPath.lineTo(x, y-halfSize);
133
                        genPath.lineTo(x-halfSize, y);
134
                        genPath.closePath();
135
                        g.fill(genPath);
136
                        break;
137
                case X_STYLE:
138
                        x = x + halfSize;
139
                        y = y + halfSize;
140
                        genPath = new GeneralPathX();
141
                        genPath.moveTo(x-halfSize, y - halfSize);
142
                        genPath.lineTo(x+halfSize, y + halfSize);
143
                        genPath.moveTo(x - halfSize, y + halfSize);
144
                        genPath.lineTo(x + halfSize, y - halfSize);
145
                        g.draw(genPath);
146
                        break;
147
                case TRIANGLE_STYLE:
148
                        x = x + halfSize;
149
                        y = y + halfSize;
150
                        int otherSize = (int) (size * 0.55);
151
                        genPath = new GeneralPathX();
152
                        genPath.moveTo(x - halfSize,
153
                                y + halfSize);
154
                        genPath.lineTo(x + halfSize,
155
                                y + halfSize);
156
                        genPath.lineTo(x, y - otherSize);
157
                        genPath.closePath();
158

    
159
                        g.fill(genPath);
160
                        break;
161
                case STAR_STYLE:
162
                        x = x + halfSize;
163
                        y = y + halfSize;
164
                        genPath = new GeneralPathX();
165
                        genPath.moveTo(x-halfSize, y);
166

    
167
                        genPath.lineTo(x-2*(halfSize/3), y+(halfSize/3));
168
                        genPath.lineTo(x-2*(halfSize/3), y+2*(halfSize/3));
169
                        genPath.lineTo(x-(halfSize/3), y+2*(halfSize/3));
170

    
171
                        genPath.lineTo(x, y+halfSize);
172

    
173
                        genPath.lineTo(x+(halfSize/3), y+2*(halfSize/3));
174
                        genPath.lineTo(x+2*(halfSize/3), y+2*(halfSize/3));
175
                        genPath.lineTo(x+2*(halfSize/3), y+(halfSize/3));
176

    
177
                        genPath.lineTo(x+(halfSize), y);
178

    
179
                        genPath.lineTo(x+2*(halfSize/3), y-(halfSize/3));
180
                        genPath.lineTo(x+2*(halfSize/3), y-2*(halfSize/3));
181
                        genPath.lineTo(x+(halfSize/3), y-2*(halfSize/3));
182

    
183
                        genPath.lineTo(x, y-halfSize);
184

    
185
                        genPath.lineTo(x-(halfSize/3), y-2*(halfSize/3));
186
                        genPath.lineTo(x-2*(halfSize/3), y-2*(halfSize/3));
187
                        genPath.lineTo(x-2*(halfSize/3), y-(halfSize/3));
188

    
189
                        genPath.lineTo(x-halfSize, y);
190

    
191
                        genPath.closePath();
192

    
193

    
194
                        g.fill(genPath);
195

    
196
                        break;
197
                }
198

    
199

    
200
                if (outlined) {
201
                        g.setColor(outlineColor);
202
                        switch (markerStyle) {
203
                        case CIRCLE_STYLE:
204
                                g.drawOval(x, y, size, size);
205
                                break;
206
                        case SQUARE_STYLE:
207
                                g.drawRect(x, y, size, size);
208
                                break;
209
                        case CROSS_STYLE:
210
                        case DIAMOND_STYLE:
211
                        case STAR_STYLE:
212
                        case X_STYLE:
213
                        case TRIANGLE_STYLE:
214
                        case VERTICAL_LINE_STYLE:
215
                                g.draw(genPath);
216
                                break;
217
                        }
218
                }
219
        }
220

    
221
        /**
222
         * Returns true or false depending if the simple marker symbol has an outline or not.
223
         * @return Returns the outline.
224
         */
225
        public boolean hasOutline() {
226
                return outlined;
227
        }
228

    
229
        /**
230
         * Establishes the outline for the simple marker symbol.
231
         * @param outline  The outline to set.
232
         */
233
        public void setOutlined(boolean outlined) {
234
                this.outlined = outlined;
235
        }
236

    
237
        /**
238
         * Returns the outline color for the symple marker symbol
239
         *
240
         * @return Color,outlineColor.
241
         */
242
        public Color getOutlineColor() {
243
                return outlineColor;
244
        }
245

    
246
        /**
247
         * Sets the outline color for the simple marker symbol
248
         * @param outlineColor, Color
249
         */
250
        public void setOutlineColor(Color outlineColor) {
251
                this.outlineColor = outlineColor;
252
        }
253

    
254
        /**
255
         * Gets the size of the outline for the simple marker symbol
256
         * @return  Returns the outlineSize.
257
         */
258
        public double getOutlineSize() {
259
                return outlineSize;
260
        }
261

    
262
        /**
263
         * Establishes the size for the outline of the simple marker symbol
264
         * @param outlineSize  The outlineSize to set.
265
         */
266
        public void setOutlineSize(double outlineSize) {
267
                this.outlineSize = outlineSize;
268
        }
269

    
270
        /**
271
         * @return  Returns the selectionSymbol.
272
         */
273
        public ISymbol getSelectionSymbol() {
274
                return selectionSymbol;
275
        }
276

    
277
        /**
278
         * @param selectionSymbol  The selectionSymbol to set.
279
         */
280
        public void setSelectionSymbol(ISymbol selectionSymbol) {
281
                this.selectionSymbol = selectionSymbol;
282
        }
283
        /**
284
         * Sets the style for the simple marker symbol
285
         * @param style
286
         */
287
        public void setStyle(int style) {
288
                this.markerStyle = style;
289
        }
290

    
291
        /**
292
         * Obtains the style for the simple marker symbol
293
         * @return markerStyle,int
294
         */
295
        public int getStyle() {
296
                return markerStyle;
297
        }
298

    
299
        public Object clone() throws CloneNotSupportedException {
300
                SimpleMarkerSymbol copy = (SimpleMarkerSymbol) super.clone();
301

    
302
                // clone selection
303
                if (selectionSymbol != null) {
304
                        copy.selectionSymbol = (ISymbol) selectionSymbol.clone();
305
                }
306

    
307
                return copy;
308
        }
309

    
310
        /**
311
         * Returns if the marker symbol should be drawn outlined.
312
         * @return if it is outlined
313
         */
314
        public boolean isOutlined() {
315
                return outlined;
316
        }
317

    
318
        public void loadFromState(PersistentState state)
319
                        throws PersistenceException {
320
                // Set parent fill symbol properties
321
                super.loadFromState(state);
322

    
323
                // Set own properties
324
                setStyle(state.getInt(FIELD_MARKERSTYLE));
325
                setOutlined(state.getBoolean(FIELD_OUTLINED));
326
                if (isOutlined()) {
327
                        setOutlineColor((Color) state.get(FIELD_OUTLINECOLOR));
328
                        setOutlineSize(state.getDouble(FIELD_OUTLINESIZE));
329
                }
330
        }
331

    
332
        public void saveToState(PersistentState state) throws PersistenceException {
333
                // Save parent fill symbol properties
334
                super.saveToState(state);
335

    
336
                // Save own properties
337
                state.set(FIELD_MARKERSTYLE, getStyle());
338
                state.set(FIELD_OUTLINED, isOutlined());
339
                if (isOutlined()) {
340
                        state.set(FIELD_OUTLINECOLOR, getOutlineColor());
341
                        state.set(FIELD_OUTLINESIZE, getOutlineSize());
342
                }
343
        }
344

    
345

    
346
        public static class RegisterPersistence implements Callable {
347

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

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

    
373
        }
374

    
375
        public static class RegisterSymbol implements Callable {
376

    
377
                public Object call() throws Exception {
378
                        int[] shapeTypes;
379
                        SymbolManager manager = MapContextLocator.getSymbolManager();
380

    
381
                shapeTypes =
382
                    new int[] { Geometry.TYPES.POINT, Geometry.TYPES.MULTIPOINT };
383
                manager.registerSymbol(IMarkerSymbol.SYMBOL_NAME,
384
                    shapeTypes,
385
                    SimpleMarkerSymbol.class);
386

    
387
                        return Boolean.TRUE;
388
                }
389

    
390
        }
391

    
392
}