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

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
                } 
80

    
81
        selectionSymbol.setColor(selectionColor);
82

    
83
        if(selectionSymbol instanceof CartographicSupport){
84
            ((CartographicSupport)selectionSymbol).setUnit(this.getUnit());
85
        }
86
                return selectionSymbol;
87
        }
88

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
216
                genPath.closePath();
217

    
218
                g.fill(genPath);
219

    
220
                break;
221
        }
222

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

    
248
    }
249

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

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

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

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

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

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

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

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

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

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

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

    
347
                return copy;
348
        }
349

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

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

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

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

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

    
388
        public static class RegisterPersistence implements Callable {
389

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

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

    
416
        }
417

    
418
        public static class RegisterSymbol implements Callable {
419

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

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

    
431
                        return Boolean.TRUE;
432
                }
433

    
434
        }
435

    
436
}