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 40560 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40560 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 40435 jjdelcerro
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8 40560 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * of the License, or (at your option) any later version.
10 40560 jjdelcerro
 *
11 40435 jjdelcerro
 * 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 40560 jjdelcerro
 *
16 40435 jjdelcerro
 * 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 40560 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 40435 jjdelcerro
 * MA  02110-1301, USA.
20 40560 jjdelcerro
 *
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 40435 jjdelcerro
 */
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 47476 fdiaz
import java.awt.Rectangle;
30 40435 jjdelcerro
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 43491 jjdelcerro
import org.gvsig.fmap.geom.primitive.Point;
35 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.MapContextLocator;
36 47476 fdiaz
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
37 40435 jjdelcerro
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 41435 fdiaz
        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 40435 jjdelcerro
        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 41435 fdiaz
75 47476 fdiaz
        @Override
76 47790 fdiaz
        public ISymbol getSymbolForSelection(Color selectionColor) {
77 40435 jjdelcerro
                if (selectionSymbol == null) {
78
                        selectionSymbol = (SimpleMarkerSymbol) cloneForSelection();
79 42439 dmartinezizquierdo
                }else {
80 47790 fdiaz
                    selectionSymbol.setColor(selectionColor);
81 40435 jjdelcerro
                }
82 47476 fdiaz
                if(selectionSymbol instanceof CartographicSupport){
83
                    ((CartographicSupport)selectionSymbol).setUnit(this.getUnit());
84
                }
85 40435 jjdelcerro
                return selectionSymbol;
86
        }
87
88 47476 fdiaz
    @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 46158 fdiaz
        int y = 0;
99 43491 jjdelcerro
        Point p;
100
        try {
101
            p = geom.centroid();
102 47476 fdiaz
        } catch (Exception ex) {
103 43491 jjdelcerro
            return;
104
        }
105 47476 fdiaz
        p.transform(affineTransform);
106 41435 fdiaz
107 47476 fdiaz
        int halfSize = size_i / 2;
108
        int posX = (int) (p.getX() + getOffset().getX()) - halfSize;
109
        int posY = (int) (p.getY() + getOffset().getY()) - halfSize;
110 40435 jjdelcerro
111 47476 fdiaz
        g.translate(posX, posY);
112
        if (getRotation() != 0.0) {
113
            g.rotate(getRotation());
114
        }
115 40435 jjdelcerro
116 47476 fdiaz
        g.setColor(getColor());
117
        GeneralPathX genPath = null;
118
        g.setStroke(new BasicStroke(1));
119 40435 jjdelcerro
120 47476 fdiaz
        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 40435 jjdelcerro
183 47476 fdiaz
                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 40435 jjdelcerro
191 47476 fdiaz
                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 40435 jjdelcerro
195 47476 fdiaz
                genPath.lineTo(x, y + halfSize);
196 40435 jjdelcerro
197 47476 fdiaz
                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 40435 jjdelcerro
201 47476 fdiaz
                genPath.lineTo(x + (halfSize), y);
202 40435 jjdelcerro
203 47476 fdiaz
                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 40435 jjdelcerro
207 47476 fdiaz
                genPath.lineTo(x, y - halfSize);
208 40435 jjdelcerro
209 47476 fdiaz
                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 40435 jjdelcerro
213 47476 fdiaz
                genPath.lineTo(x - halfSize, y);
214 40435 jjdelcerro
215 47476 fdiaz
                genPath.closePath();
216 40435 jjdelcerro
217 47476 fdiaz
                g.fill(genPath);
218 40435 jjdelcerro
219 47476 fdiaz
                break;
220
        }
221 40435 jjdelcerro
222 47476 fdiaz
        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 40435 jjdelcerro
247 47476 fdiaz
    }
248 40435 jjdelcerro
249
        /**
250
         * Returns true or false depending if the simple marker symbol has an outline or not.
251
         * @return Returns the outline.
252
         */
253 47476 fdiaz
        @Override
254 40435 jjdelcerro
        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 47476 fdiaz
        @Override
263 40435 jjdelcerro
        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 47476 fdiaz
        @Override
273 40435 jjdelcerro
        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 47476 fdiaz
        @Override
282 40435 jjdelcerro
        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 47476 fdiaz
        @Override
291 40435 jjdelcerro
        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 47476 fdiaz
        @Override
300 40435 jjdelcerro
        public void setOutlineSize(double outlineSize) {
301
                this.outlineSize = outlineSize;
302
        }
303
304
        /**
305
         * @return  Returns the selectionSymbol.
306
         */
307 47476 fdiaz
        @Override
308 40435 jjdelcerro
        public ISymbol getSelectionSymbol() {
309
                return selectionSymbol;
310
        }
311
312
        /**
313
         * @param selectionSymbol  The selectionSymbol to set.
314
         */
315 47476 fdiaz
        @Override
316 40435 jjdelcerro
        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 47476 fdiaz
        @Override
324 40435 jjdelcerro
        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 47476 fdiaz
        @Override
333 40435 jjdelcerro
        public int getStyle() {
334
                return markerStyle;
335
        }
336
337 47476 fdiaz
        @Override
338 40435 jjdelcerro
        public Object clone() throws CloneNotSupportedException {
339
                SimpleMarkerSymbol copy = (SimpleMarkerSymbol) super.clone();
340 41435 fdiaz
341 40435 jjdelcerro
                // clone selection
342
                if (selectionSymbol != null) {
343
                        copy.selectionSymbol = (ISymbol) selectionSymbol.clone();
344
                }
345 41435 fdiaz
346 40435 jjdelcerro
                return copy;
347
        }
348
349
        /**
350
         * Returns if the marker symbol should be drawn outlined.
351
         * @return if it is outlined
352
         */
353 47476 fdiaz
        @Override
354 40435 jjdelcerro
        public boolean isOutlined() {
355
                return outlined;
356
        }
357
358 47476 fdiaz
        @Override
359 40435 jjdelcerro
        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 47476 fdiaz
        @Override
374 40435 jjdelcerro
        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 47476 fdiaz
                @Override
390 40435 jjdelcerro
                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 41435 fdiaz
                                                null,
398 40435 jjdelcerro
                                                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 41435 fdiaz
415 40435 jjdelcerro
        }
416
417
        public static class RegisterSymbol implements Callable {
418
419 47476 fdiaz
                @Override
420 40435 jjdelcerro
                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 41435 fdiaz
433 40435 jjdelcerro
        }
434
435
}