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

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

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

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

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

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

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

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

    
83
        public void draw(Graphics2D g, AffineTransform affineTransform,
84
                        Geometry geom, Feature feature, Cancellable cancel) {
85
                int x, y;
86
                org.gvsig.fmap.geom.primitive.Point p = (org.gvsig.fmap.geom.primitive.Point)geom.cloneGeometry();
87
                p.transform(affineTransform);
88

    
89
                int size = (int) getSize();
90

    
91
                int halfSize = size/2;
92
                x = ((int) (p.getX() + getOffset().getX()) - halfSize);
93
                y = ((int) (p.getY() + getOffset().getY()) - halfSize);
94

    
95
                // IMask mask = getMask();
96
                // if (mask != null) {
97
                // IFillSymbol maskShape = mask.getFillSymbol();
98
                // // maskShape.draw(g, null, mask.getHaloShape(shp));
99
                // }
100

    
101
                g.setColor(getColor());
102
                GeneralPathX genPath = null;
103
                g.setStroke(new BasicStroke(1));
104

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

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

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

    
174
                        genPath.lineTo(x, y+halfSize);
175

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

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

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

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

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

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

    
194
                        genPath.closePath();
195

    
196

    
197
                        g.fill(genPath);
198

    
199
                        break;
200
                }
201

    
202

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

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

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

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

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

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

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

    
273
        /**
274
         * @return  Returns the selectionSymbol.
275
         */
276
        public ISymbol getSelectionSymbol() {
277
                return selectionSymbol;
278
        }
279

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

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

    
302
        public Object clone() throws CloneNotSupportedException {
303
                SimpleMarkerSymbol copy = (SimpleMarkerSymbol) super.clone();
304

    
305
                // clone selection
306
                if (selectionSymbol != null) {
307
                        copy.selectionSymbol = (ISymbol) selectionSymbol.clone();
308
                }
309

    
310
                return copy;
311
        }
312

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

    
321
        public void loadFromState(PersistentState state)
322
                        throws PersistenceException {
323
                // Set parent fill symbol properties
324
                super.loadFromState(state);
325

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

    
335
        public void saveToState(PersistentState state) throws PersistenceException {
336
                // Save parent fill symbol properties
337
                super.saveToState(state);
338

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

    
348

    
349
        public static class RegisterPersistence implements Callable {
350

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

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

    
376
        }
377

    
378
        public static class RegisterSymbol implements Callable {
379

    
380
                public Object call() throws Exception {
381
                        int[] shapeTypes;
382
                        SymbolManager manager = MapContextLocator.getSymbolManager();
383

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

    
390
                        return Boolean.TRUE;
391
                }
392

    
393
        }
394

    
395
}