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

History | View | Annotate | Download (11.5 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.geom.primitive.Point;
35
import org.gvsig.fmap.mapcontext.MapContext;
36
import org.gvsig.fmap.mapcontext.MapContextLocator;
37
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
38
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
39
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
40
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.ISimpleMarkerSymbol;
41
import org.gvsig.tools.ToolsLocator;
42
import org.gvsig.tools.dynobject.DynStruct;
43
import org.gvsig.tools.persistence.PersistenceManager;
44
import org.gvsig.tools.persistence.PersistentState;
45
import org.gvsig.tools.persistence.exception.PersistenceException;
46
import org.gvsig.tools.task.Cancellable;
47
import org.gvsig.tools.util.Callable;
48

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

    
58
        public static final String SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleMarkerSymbol";
59

    
60
        private static final String FIELD_OUTLINED = "outlined";
61
        private static final String FIELD_OUTLINECOLOR = "outlineColor";
62
        private static final String FIELD_OUTLINESIZE = "outlineSize";
63
        private static final String FIELD_MARKERSTYLE = "markerStyle";
64

    
65
        private boolean outlined;
66
        private Color outlineColor;
67
        private double outlineSize;
68
        private ISymbol selectionSymbol;
69
        private int markerStyle;
70

    
71
        public SimpleMarkerSymbol() {
72
                super();
73
        }
74

    
75
        public ISymbol getSymbolForSelection() {
76
                if (selectionSymbol == null) {
77
                        selectionSymbol = (SimpleMarkerSymbol) cloneForSelection();
78
                }else {
79
                    selectionSymbol.setColor(MapContext.getSelectionColor());
80
                }
81
                return selectionSymbol;
82
        }
83

    
84
        public void draw(Graphics2D g, AffineTransform affineTransform,
85
                        Geometry geom, Feature feature, Cancellable cancel) {
86
                int x, y;
87
        Point p;
88
        try {
89
            p = geom.centroid();
90
        } catch(Exception ex) {
91
            return;
92
        }
93
                p.transform(affineTransform);
94

    
95
                int size = (int) getSize();
96

    
97
                int halfSize = size/2;
98
                x = ((int) (p.getX() + getOffset().getX()) - halfSize);
99
                y = ((int) (p.getY() + getOffset().getY()) - halfSize);
100

    
101
                // IMask mask = getMask();
102
                // if (mask != null) {
103
                // IFillSymbol maskShape = mask.getFillSymbol();
104
                // // maskShape.draw(g, null, mask.getHaloShape(shp));
105
                // }
106

    
107
                g.setColor(getColor());
108
                GeneralPathX genPath = null;
109
                g.setStroke(new BasicStroke(1));
110

    
111
                switch (markerStyle) {
112
                case CIRCLE_STYLE:
113
                        g.fillOval(x, y, size, size);
114
                        break;
115
                case SQUARE_STYLE:
116
                        g.fillRect(x, y, size, size);
117
                        break;
118
                case CROSS_STYLE:
119
                        x = x + halfSize;
120
                        y = y + halfSize;
121
                        genPath = new GeneralPathX();
122
                        genPath.moveTo(x, y - halfSize);
123
                        genPath.lineTo(x, y + halfSize);
124
                        genPath.moveTo(x - halfSize, y);
125
                        genPath.lineTo(x + halfSize, y);
126
                        g.draw(genPath);
127
                        break;
128
                case VERTICAL_LINE_STYLE:
129
                        genPath = new GeneralPathX();
130
                        genPath.moveTo(x + halfSize, y);
131
                        genPath.lineTo(x + halfSize, y + size);
132
                        g.draw(genPath);
133
                        break;
134
                case DIAMOND_STYLE:
135
                        x = x + halfSize;
136
                        y = y + halfSize;
137
                        genPath = new GeneralPathX();
138
                        genPath.moveTo(x-halfSize, y);
139
                        genPath.lineTo(x, y+halfSize);
140
                        genPath.lineTo(x+halfSize, y);
141
                        genPath.lineTo(x, y-halfSize);
142
                        genPath.lineTo(x-halfSize, y);
143
                        genPath.closePath();
144
                        g.fill(genPath);
145
                        break;
146
                case X_STYLE:
147
                        x = x + halfSize;
148
                        y = y + halfSize;
149
                        genPath = new GeneralPathX();
150
                        genPath.moveTo(x-halfSize, y - halfSize);
151
                        genPath.lineTo(x+halfSize, y + halfSize);
152
                        genPath.moveTo(x - halfSize, y + halfSize);
153
                        genPath.lineTo(x + halfSize, y - halfSize);
154
                        g.draw(genPath);
155
                        break;
156
                case TRIANGLE_STYLE:
157
                        x = x + halfSize;
158
                        y = y + halfSize;
159
                        int otherSize = (int) (size * 0.55);
160
                        genPath = new GeneralPathX();
161
                        genPath.moveTo(x - halfSize,
162
                                y + halfSize);
163
                        genPath.lineTo(x + halfSize,
164
                                y + halfSize);
165
                        genPath.lineTo(x, y - otherSize);
166
                        genPath.closePath();
167

    
168
                        g.fill(genPath);
169
                        break;
170
                case STAR_STYLE:
171
                        x = x + halfSize;
172
                        y = y + halfSize;
173
                        genPath = new GeneralPathX();
174
                        genPath.moveTo(x-halfSize, y);
175

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

    
180
                        genPath.lineTo(x, y+halfSize);
181

    
182
                        genPath.lineTo(x+(halfSize/3), y+2*(halfSize/3));
183
                        genPath.lineTo(x+2*(halfSize/3), y+2*(halfSize/3));
184
                        genPath.lineTo(x+2*(halfSize/3), y+(halfSize/3));
185

    
186
                        genPath.lineTo(x+(halfSize), y);
187

    
188
                        genPath.lineTo(x+2*(halfSize/3), y-(halfSize/3));
189
                        genPath.lineTo(x+2*(halfSize/3), y-2*(halfSize/3));
190
                        genPath.lineTo(x+(halfSize/3), y-2*(halfSize/3));
191

    
192
                        genPath.lineTo(x, y-halfSize);
193

    
194
                        genPath.lineTo(x-(halfSize/3), y-2*(halfSize/3));
195
                        genPath.lineTo(x-2*(halfSize/3), y-2*(halfSize/3));
196
                        genPath.lineTo(x-2*(halfSize/3), y-(halfSize/3));
197

    
198
                        genPath.lineTo(x-halfSize, y);
199

    
200
                        genPath.closePath();
201

    
202

    
203
                        g.fill(genPath);
204

    
205
                        break;
206
                }
207

    
208

    
209
                if (outlined) {
210
                        g.setColor(outlineColor);
211
                        switch (markerStyle) {
212
                        case CIRCLE_STYLE:
213
                                g.drawOval(x, y, size, size);
214
                                break;
215
                        case SQUARE_STYLE:
216
                                g.drawRect(x, y, size, size);
217
                                break;
218
                        case CROSS_STYLE:
219
                        case DIAMOND_STYLE:
220
                        case STAR_STYLE:
221
                        case X_STYLE:
222
                        case TRIANGLE_STYLE:
223
                        case VERTICAL_LINE_STYLE:
224
                                g.draw(genPath);
225
                                break;
226
                        }
227
                }
228
        }
229

    
230
        /**
231
         * Returns true or false depending if the simple marker symbol has an outline or not.
232
         * @return Returns the outline.
233
         */
234
        public boolean hasOutline() {
235
                return outlined;
236
        }
237

    
238
        /**
239
         * Establishes the outline for the simple marker symbol.
240
         * @param outline  The outline to set.
241
         */
242
        public void setOutlined(boolean outlined) {
243
                this.outlined = outlined;
244
        }
245

    
246
        /**
247
         * Returns the outline color for the symple marker symbol
248
         *
249
         * @return Color,outlineColor.
250
         */
251
        public Color getOutlineColor() {
252
                return outlineColor;
253
        }
254

    
255
        /**
256
         * Sets the outline color for the simple marker symbol
257
         * @param outlineColor, Color
258
         */
259
        public void setOutlineColor(Color outlineColor) {
260
                this.outlineColor = outlineColor;
261
        }
262

    
263
        /**
264
         * Gets the size of the outline for the simple marker symbol
265
         * @return  Returns the outlineSize.
266
         */
267
        public double getOutlineSize() {
268
                return outlineSize;
269
        }
270

    
271
        /**
272
         * Establishes the size for the outline of the simple marker symbol
273
         * @param outlineSize  The outlineSize to set.
274
         */
275
        public void setOutlineSize(double outlineSize) {
276
                this.outlineSize = outlineSize;
277
        }
278

    
279
        /**
280
         * @return  Returns the selectionSymbol.
281
         */
282
        public ISymbol getSelectionSymbol() {
283
                return selectionSymbol;
284
        }
285

    
286
        /**
287
         * @param selectionSymbol  The selectionSymbol to set.
288
         */
289
        public void setSelectionSymbol(ISymbol selectionSymbol) {
290
                this.selectionSymbol = selectionSymbol;
291
        }
292
        /**
293
         * Sets the style for the simple marker symbol
294
         * @param style
295
         */
296
        public void setStyle(int style) {
297
                this.markerStyle = style;
298
        }
299

    
300
        /**
301
         * Obtains the style for the simple marker symbol
302
         * @return markerStyle,int
303
         */
304
        public int getStyle() {
305
                return markerStyle;
306
        }
307

    
308
        public Object clone() throws CloneNotSupportedException {
309
                SimpleMarkerSymbol copy = (SimpleMarkerSymbol) super.clone();
310

    
311
                // clone selection
312
                if (selectionSymbol != null) {
313
                        copy.selectionSymbol = (ISymbol) selectionSymbol.clone();
314
                }
315

    
316
                return copy;
317
        }
318

    
319
        /**
320
         * Returns if the marker symbol should be drawn outlined.
321
         * @return if it is outlined
322
         */
323
        public boolean isOutlined() {
324
                return outlined;
325
        }
326

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

    
332
                // Set own properties
333
                setStyle(state.getInt(FIELD_MARKERSTYLE));
334
                setOutlined(state.getBoolean(FIELD_OUTLINED));
335
                if (isOutlined()) {
336
                        setOutlineColor((Color) state.get(FIELD_OUTLINECOLOR));
337
                        setOutlineSize(state.getDouble(FIELD_OUTLINESIZE));
338
                }
339
        }
340

    
341
        public void saveToState(PersistentState state) throws PersistenceException {
342
                // Save parent fill symbol properties
343
                super.saveToState(state);
344

    
345
                // Save own properties
346
                state.set(FIELD_MARKERSTYLE, getStyle());
347
                state.set(FIELD_OUTLINED, isOutlined());
348
                if (isOutlined()) {
349
                        state.set(FIELD_OUTLINECOLOR, getOutlineColor());
350
                        state.set(FIELD_OUTLINESIZE, getOutlineSize());
351
                }
352
        }
353

    
354

    
355
        public static class RegisterPersistence implements Callable {
356

    
357
                public Object call() throws Exception {
358
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
359
                        if( manager.getDefinition(SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
360
                                DynStruct definition = manager.addDefinition(
361
                                                SimpleMarkerSymbol.class,
362
                                                SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
363
                                                SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
364
                                                null,
365
                                                null
366
                                );
367
                                // Extend the FillSymbol base definition
368
                                definition.extend(manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME));
369

    
370
                                // Marker style
371
                                definition.addDynFieldInt(FIELD_MARKERSTYLE).setMandatory(true);
372
                                // Outlined?
373
                                definition.addDynFieldBoolean(FIELD_OUTLINED).setMandatory(true);
374
                                // Outline color
375
                                definition.addDynFieldObject(FIELD_OUTLINECOLOR).setClassOfValue(Color.class);
376
                                // Outline size
377
                                definition.addDynFieldDouble(FIELD_OUTLINESIZE);
378
                        }
379
                        return Boolean.TRUE;
380
                }
381

    
382
        }
383

    
384
        public static class RegisterSymbol implements Callable {
385

    
386
                public Object call() throws Exception {
387
                        int[] shapeTypes;
388
                        SymbolManager manager = MapContextLocator.getSymbolManager();
389

    
390
                shapeTypes =
391
                    new int[] { Geometry.TYPES.POINT, Geometry.TYPES.MULTIPOINT };
392
                manager.registerSymbol(IMarkerSymbol.SYMBOL_NAME,
393
                    shapeTypes,
394
                    SimpleMarkerSymbol.class);
395

    
396
                        return Boolean.TRUE;
397
                }
398

    
399
        }
400

    
401
}