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

History | View | Annotate | Download (12 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 = 0;
87
        int y = 0;
88
        Point p;
89
        try {
90
            p = geom.centroid();
91
        } catch(Exception ex) {
92
            return;
93
        }
94
                p.transform(affineTransform);
95

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

    
98
                int halfSize = size/2;
99
            int posX = (int) (p.getX() + getOffset().getX()) - halfSize;
100
            int posY = (int) (p.getY() + getOffset().getY()) - halfSize;
101

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

    
108
                g.translate(posX, posY);
109
                if(getRotation() != 0.0){
110
                    g.rotate(getRotation());
111
                }
112
                
113
                g.setColor(getColor());
114
                GeneralPathX genPath = null;
115
                g.setStroke(new BasicStroke(1));
116

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

    
180
                        g.fill(genPath);
181
                        break;
182
                case STAR_STYLE:
183
                        x = x + halfSize;
184
                        y = y + halfSize;
185
                        genPath = new GeneralPathX();
186
                        genPath.moveTo(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.lineTo(x+2*(halfSize/3), y-(halfSize/3));
201
                        genPath.lineTo(x+2*(halfSize/3), y-2*(halfSize/3));
202
                        genPath.lineTo(x+(halfSize/3), y-2*(halfSize/3));
203

    
204
                        genPath.lineTo(x, y-halfSize);
205

    
206
                        genPath.lineTo(x-(halfSize/3), y-2*(halfSize/3));
207
                        genPath.lineTo(x-2*(halfSize/3), y-2*(halfSize/3));
208
                        genPath.lineTo(x-2*(halfSize/3), y-(halfSize/3));
209

    
210
                        genPath.lineTo(x-halfSize, y);
211

    
212
                        genPath.closePath();
213

    
214

    
215
                        g.fill(genPath);
216

    
217
                        break;
218
                }
219

    
220
                if (outlined) {
221
                        g.setColor(outlineColor);
222
                        switch (markerStyle) {
223
                        case CIRCLE_STYLE:
224
                                g.drawOval(x, y, size, size);
225
                                break;
226
                        case SQUARE_STYLE:
227
                                g.drawRect(x, y, size, size);
228
                                break;
229
                        case CROSS_STYLE:
230
                        case DIAMOND_STYLE:
231
                        case STAR_STYLE:
232
                        case X_STYLE:
233
                        case TRIANGLE_STYLE:
234
                        case VERTICAL_LINE_STYLE:
235
                        case HORIZONTAL_LINE_STYLE:
236
                                g.draw(genPath);
237
                                break;
238
                        }
239
                   }
240
            if (getRotation() != 0.0) {
241
                g.rotate(-getRotation());
242
            };
243
            g.translate(-posX, -posY);
244

    
245
        }
246

    
247
        /**
248
         * Returns true or false depending if the simple marker symbol has an outline or not.
249
         * @return Returns the outline.
250
         */
251
        public boolean hasOutline() {
252
                return outlined;
253
        }
254

    
255
        /**
256
         * Establishes the outline for the simple marker symbol.
257
         * @param outline  The outline to set.
258
         */
259
        public void setOutlined(boolean outlined) {
260
                this.outlined = outlined;
261
        }
262

    
263
        /**
264
         * Returns the outline color for the symple marker symbol
265
         *
266
         * @return Color,outlineColor.
267
         */
268
        public Color getOutlineColor() {
269
                return outlineColor;
270
        }
271

    
272
        /**
273
         * Sets the outline color for the simple marker symbol
274
         * @param outlineColor, Color
275
         */
276
        public void setOutlineColor(Color outlineColor) {
277
                this.outlineColor = outlineColor;
278
        }
279

    
280
        /**
281
         * Gets the size of the outline for the simple marker symbol
282
         * @return  Returns the outlineSize.
283
         */
284
        public double getOutlineSize() {
285
                return outlineSize;
286
        }
287

    
288
        /**
289
         * Establishes the size for the outline of the simple marker symbol
290
         * @param outlineSize  The outlineSize to set.
291
         */
292
        public void setOutlineSize(double outlineSize) {
293
                this.outlineSize = outlineSize;
294
        }
295

    
296
        /**
297
         * @return  Returns the selectionSymbol.
298
         */
299
        public ISymbol getSelectionSymbol() {
300
                return selectionSymbol;
301
        }
302

    
303
        /**
304
         * @param selectionSymbol  The selectionSymbol to set.
305
         */
306
        public void setSelectionSymbol(ISymbol selectionSymbol) {
307
                this.selectionSymbol = selectionSymbol;
308
        }
309
        /**
310
         * Sets the style for the simple marker symbol
311
         * @param style
312
         */
313
        public void setStyle(int style) {
314
                this.markerStyle = style;
315
        }
316

    
317
        /**
318
         * Obtains the style for the simple marker symbol
319
         * @return markerStyle,int
320
         */
321
        public int getStyle() {
322
                return markerStyle;
323
        }
324

    
325
        public Object clone() throws CloneNotSupportedException {
326
                SimpleMarkerSymbol copy = (SimpleMarkerSymbol) super.clone();
327

    
328
                // clone selection
329
                if (selectionSymbol != null) {
330
                        copy.selectionSymbol = (ISymbol) selectionSymbol.clone();
331
                }
332

    
333
                return copy;
334
        }
335

    
336
        /**
337
         * Returns if the marker symbol should be drawn outlined.
338
         * @return if it is outlined
339
         */
340
        public boolean isOutlined() {
341
                return outlined;
342
        }
343

    
344
        public void loadFromState(PersistentState state)
345
                        throws PersistenceException {
346
                // Set parent fill symbol properties
347
                super.loadFromState(state);
348

    
349
                // Set own properties
350
                setStyle(state.getInt(FIELD_MARKERSTYLE));
351
                setOutlined(state.getBoolean(FIELD_OUTLINED));
352
                if (isOutlined()) {
353
                        setOutlineColor((Color) state.get(FIELD_OUTLINECOLOR));
354
                        setOutlineSize(state.getDouble(FIELD_OUTLINESIZE));
355
                }
356
        }
357

    
358
        public void saveToState(PersistentState state) throws PersistenceException {
359
                // Save parent fill symbol properties
360
                super.saveToState(state);
361

    
362
                // Save own properties
363
                state.set(FIELD_MARKERSTYLE, getStyle());
364
                state.set(FIELD_OUTLINED, isOutlined());
365
                if (isOutlined()) {
366
                        state.set(FIELD_OUTLINECOLOR, getOutlineColor());
367
                        state.set(FIELD_OUTLINESIZE, getOutlineSize());
368
                }
369
        }
370

    
371

    
372
        public static class RegisterPersistence implements Callable {
373

    
374
                public Object call() throws Exception {
375
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
376
                        if( manager.getDefinition(SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
377
                                DynStruct definition = manager.addDefinition(
378
                                                SimpleMarkerSymbol.class,
379
                                                SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
380
                                                SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
381
                                                null,
382
                                                null
383
                                );
384
                                // Extend the FillSymbol base definition
385
                                definition.extend(manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME));
386

    
387
                                // Marker style
388
                                definition.addDynFieldInt(FIELD_MARKERSTYLE).setMandatory(true);
389
                                // Outlined?
390
                                definition.addDynFieldBoolean(FIELD_OUTLINED).setMandatory(true);
391
                                // Outline color
392
                                definition.addDynFieldObject(FIELD_OUTLINECOLOR).setClassOfValue(Color.class);
393
                                // Outline size
394
                                definition.addDynFieldDouble(FIELD_OUTLINESIZE);
395
                        }
396
                        return Boolean.TRUE;
397
                }
398

    
399
        }
400

    
401
        public static class RegisterSymbol implements Callable {
402

    
403
                public Object call() throws Exception {
404
                        int[] shapeTypes;
405
                        SymbolManager manager = MapContextLocator.getSymbolManager();
406

    
407
                shapeTypes =
408
                    new int[] { Geometry.TYPES.POINT, Geometry.TYPES.MULTIPOINT };
409
                manager.registerSymbol(IMarkerSymbol.SYMBOL_NAME,
410
                    shapeTypes,
411
                    SimpleMarkerSymbol.class);
412

    
413
                        return Boolean.TRUE;
414
                }
415

    
416
        }
417

    
418
}