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

History | View | Annotate | Download (14.1 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.Rectangle;
30
import java.awt.geom.AffineTransform;
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.MapContextLocator;
36
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
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
        @Override
76
        public ISymbol getSymbolForSelection(Color selectionColor) {
77
                if (selectionSymbol == null) {
78
                        selectionSymbol = (SimpleMarkerSymbol) cloneForSelection();
79
                }else {
80
                    selectionSymbol.setColor(selectionColor);
81
                }
82
                if(selectionSymbol instanceof CartographicSupport){
83
                    ((CartographicSupport)selectionSymbol).setUnit(this.getUnit());
84
                }
85
                return selectionSymbol;
86
        }
87

    
88
    @Override
89
    public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel, Rectangle r) {
90
        double size_d = getCartographicSize();
91
        size_d = this.getAdjustedSize(r, size_d);
92
        int size_i = (int) Math.round(size_d);
93
 
94
        if(r!=null){
95
            geom = this.getSampleGeometry(r);
96
        }
97
        int x = 0;
98
        int y = 0;
99
        Point p;
100
        try {
101
            p = geom.centroid();
102
        } catch (Exception ex) {
103
            return;
104
        }
105
        p.transform(affineTransform);
106

    
107
        int halfSize = size_i / 2;
108
        int posX = (int) (p.getX() + getOffset().getX()) - halfSize;
109
        int posY = (int) (p.getY() + getOffset().getY()) - halfSize;
110

    
111
        g.translate(posX, posY);
112
        if (getRotation() != 0.0) {
113
            g.rotate(getRotation());
114
        }
115

    
116
        g.setColor(getColor());
117
        GeneralPathX genPath = null;
118
        g.setStroke(new BasicStroke(1));
119

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

    
183
                g.fill(genPath);
184
                break;
185
            case STAR_STYLE:
186
                x = x + halfSize;
187
                y = y + halfSize;
188
                genPath = new GeneralPathX();
189
                genPath.moveTo(x - halfSize, y);
190

    
191
                genPath.lineTo(x - 2 * (halfSize / 3), y + (halfSize / 3));
192
                genPath.lineTo(x - 2 * (halfSize / 3), y + 2 * (halfSize / 3));
193
                genPath.lineTo(x - (halfSize / 3), y + 2 * (halfSize / 3));
194

    
195
                genPath.lineTo(x, y + halfSize);
196

    
197
                genPath.lineTo(x + (halfSize / 3), y + 2 * (halfSize / 3));
198
                genPath.lineTo(x + 2 * (halfSize / 3), y + 2 * (halfSize / 3));
199
                genPath.lineTo(x + 2 * (halfSize / 3), y + (halfSize / 3));
200

    
201
                genPath.lineTo(x + (halfSize), y);
202

    
203
                genPath.lineTo(x + 2 * (halfSize / 3), y - (halfSize / 3));
204
                genPath.lineTo(x + 2 * (halfSize / 3), y - 2 * (halfSize / 3));
205
                genPath.lineTo(x + (halfSize / 3), y - 2 * (halfSize / 3));
206

    
207
                genPath.lineTo(x, y - halfSize);
208

    
209
                genPath.lineTo(x - (halfSize / 3), y - 2 * (halfSize / 3));
210
                genPath.lineTo(x - 2 * (halfSize / 3), y - 2 * (halfSize / 3));
211
                genPath.lineTo(x - 2 * (halfSize / 3), y - (halfSize / 3));
212

    
213
                genPath.lineTo(x - halfSize, y);
214

    
215
                genPath.closePath();
216

    
217
                g.fill(genPath);
218

    
219
                break;
220
        }
221

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

    
247
    }
248

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

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

    
267
        /**
268
         * Returns the outline color for the symple marker symbol
269
         *
270
         * @return Color,outlineColor.
271
         */
272
        @Override
273
        public Color getOutlineColor() {
274
                return outlineColor;
275
        }
276

    
277
        /**
278
         * Sets the outline color for the simple marker symbol
279
         * @param outlineColor, Color
280
         */
281
        @Override
282
        public void setOutlineColor(Color outlineColor) {
283
                this.outlineColor = outlineColor;
284
        }
285

    
286
        /**
287
         * Gets the size of the outline for the simple marker symbol
288
         * @return  Returns the outlineSize.
289
         */
290
        @Override
291
        public double getOutlineSize() {
292
                return outlineSize;
293
        }
294

    
295
        /**
296
         * Establishes the size for the outline of the simple marker symbol
297
         * @param outlineSize  The outlineSize to set.
298
         */
299
        @Override
300
        public void setOutlineSize(double outlineSize) {
301
                this.outlineSize = outlineSize;
302
        }
303

    
304
        /**
305
         * @return  Returns the selectionSymbol.
306
         */
307
        @Override
308
        public ISymbol getSelectionSymbol() {
309
                return selectionSymbol;
310
        }
311

    
312
        /**
313
         * @param selectionSymbol  The selectionSymbol to set.
314
         */
315
        @Override
316
        public void setSelectionSymbol(ISymbol selectionSymbol) {
317
                this.selectionSymbol = selectionSymbol;
318
        }
319
        /**
320
         * Sets the style for the simple marker symbol
321
         * @param style
322
         */
323
        @Override
324
        public void setStyle(int style) {
325
                this.markerStyle = style;
326
        }
327

    
328
        /**
329
         * Obtains the style for the simple marker symbol
330
         * @return markerStyle,int
331
         */
332
        @Override
333
        public int getStyle() {
334
                return markerStyle;
335
        }
336

    
337
        @Override
338
        public Object clone() throws CloneNotSupportedException {
339
                SimpleMarkerSymbol copy = (SimpleMarkerSymbol) super.clone();
340

    
341
                // clone selection
342
                if (selectionSymbol != null) {
343
                        copy.selectionSymbol = (ISymbol) selectionSymbol.clone();
344
                }
345

    
346
                return copy;
347
        }
348

    
349
        /**
350
         * Returns if the marker symbol should be drawn outlined.
351
         * @return if it is outlined
352
         */
353
        @Override
354
        public boolean isOutlined() {
355
                return outlined;
356
        }
357

    
358
        @Override
359
        public void loadFromState(PersistentState state)
360
                        throws PersistenceException {
361
                // Set parent fill symbol properties
362
                super.loadFromState(state);
363

    
364
                // Set own properties
365
                setStyle(state.getInt(FIELD_MARKERSTYLE));
366
                setOutlined(state.getBoolean(FIELD_OUTLINED));
367
                if (isOutlined()) {
368
                        setOutlineColor((Color) state.get(FIELD_OUTLINECOLOR));
369
                        setOutlineSize(state.getDouble(FIELD_OUTLINESIZE));
370
                }
371
        }
372

    
373
        @Override
374
        public void saveToState(PersistentState state) throws PersistenceException {
375
                // Save parent fill symbol properties
376
                super.saveToState(state);
377

    
378
                // Save own properties
379
                state.set(FIELD_MARKERSTYLE, getStyle());
380
                state.set(FIELD_OUTLINED, isOutlined());
381
                if (isOutlined()) {
382
                        state.set(FIELD_OUTLINECOLOR, getOutlineColor());
383
                        state.set(FIELD_OUTLINESIZE, getOutlineSize());
384
                }
385
        }
386

    
387
        public static class RegisterPersistence implements Callable {
388

    
389
                @Override
390
                public Object call() throws Exception {
391
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
392
                        if( manager.getDefinition(SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
393
                                DynStruct definition = manager.addDefinition(
394
                                                SimpleMarkerSymbol.class,
395
                                                SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
396
                                                SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
397
                                                null,
398
                                                null
399
                                );
400
                                // Extend the FillSymbol base definition
401
                                definition.extend(manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME));
402

    
403
                                // Marker style
404
                                definition.addDynFieldInt(FIELD_MARKERSTYLE).setMandatory(true);
405
                                // Outlined?
406
                                definition.addDynFieldBoolean(FIELD_OUTLINED).setMandatory(true);
407
                                // Outline color
408
                                definition.addDynFieldObject(FIELD_OUTLINECOLOR).setClassOfValue(Color.class);
409
                                // Outline size
410
                                definition.addDynFieldDouble(FIELD_OUTLINESIZE);
411
                        }
412
                        return Boolean.TRUE;
413
                }
414

    
415
        }
416

    
417
        public static class RegisterSymbol implements Callable {
418

    
419
                @Override
420
                public Object call() throws Exception {
421
                        int[] shapeTypes;
422
                        SymbolManager manager = MapContextLocator.getSymbolManager();
423

    
424
                shapeTypes =
425
                    new int[] { Geometry.TYPES.POINT, Geometry.TYPES.MULTIPOINT };
426
                manager.registerSymbol(IMarkerSymbol.SYMBOL_NAME,
427
                    shapeTypes,
428
                    SimpleMarkerSymbol.class);
429

    
430
                        return Boolean.TRUE;
431
                }
432

    
433
        }
434

    
435
}