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 / ArrowMarkerSymbol.java @ 45523

History | View | Annotate | Download (5.54 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.Graphics2D;
28
import java.awt.geom.AffineTransform;
29
30
import org.gvsig.fmap.dal.feature.Feature;
31
import org.gvsig.fmap.geom.Geometry;
32
import org.gvsig.fmap.geom.primitive.GeneralPathX;
33
import org.gvsig.fmap.geom.primitive.Point;
34
import org.gvsig.fmap.mapcontext.MapContext;
35
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
36
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
37
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IArrowMarkerSymbol;
38
import org.gvsig.tools.ToolsLocator;
39
import org.gvsig.tools.dynobject.DynStruct;
40
import org.gvsig.tools.persistence.PersistenceManager;
41
import org.gvsig.tools.persistence.PersistentState;
42
import org.gvsig.tools.persistence.exception.PersistenceException;
43
import org.gvsig.tools.task.Cancellable;
44
import org.gvsig.tools.util.Callable;
45
46
/**
47
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
48
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
49
 */
50
public class ArrowMarkerSymbol extends AbstractMarkerSymbol implements CartographicSupport, IArrowMarkerSymbol {
51
52
        public static final String ARROW_MARKER_SYMBOL_DYNCLASS_NAME = "ArrowMarkerSymbol";
53
54 42439 dmartinezizquierdo
        private static final String FIELD_SHARPNESS = "sharpness";
55
56 40435 jjdelcerro
        private ArrowMarkerSymbol symSel;
57
        private double sharpeness;
58
59
        public ISymbol getSymbolForSelection() {
60
                if (symSel == null) {
61 42439 dmartinezizquierdo
                        //symSel = new ArrowMarkerSymbol();
62
                    symSel = (ArrowMarkerSymbol)this.cloneForSelection();
63 40435 jjdelcerro
                        symSel.setColor(MapContext.getSelectionColor());
64 42439 dmartinezizquierdo
                }else{
65
                    symSel.setColor(MapContext.getSelectionColor());
66 40435 jjdelcerro
                }
67
68
                return symSel;
69
        }
70
71
        public String getClassName() {
72
                return getClass().getName();
73
        }
74
75
        /**
76
         * To get the sharpeness attribute.It will determine the final form of the arrow
77
         * @return
78
         */
79
        public double getSharpness() {
80
                return sharpeness;
81
        }
82
83
        /**
84
         * To set the sharpeness.It will determine the final form of the arrow
85
         * @param sharpeness, the value of the arrow's edge angle IN RADIANS
86
         * @see #getSharpeness()
87
         */
88
        public void setSharpness(double sharpeness) {
89
                this.sharpeness = sharpeness;
90
        }
91
92
        public void draw(Graphics2D g, AffineTransform affineTransform,
93
                        Geometry geom, Feature feature, Cancellable cancel) {
94
95
                if (geom == null) {
96
                        return;
97
                }
98 43491 jjdelcerro
        Point p;
99
        try {
100
            p = geom.centroid();
101
        } catch(Exception ex) {
102
            return;
103
        }
104 40435 jjdelcerro
                double radian_half_sharpeness = Math.toRadians(getSharpness()*.5); //
105
106
                double size = getSize();
107
                double halfHeight = size * Math.tan(radian_half_sharpeness);
108
                double theta = getRotation();
109
110
                g.setColor(getColor());
111
112
                g.setStroke(new BasicStroke());
113
114
                AffineTransform newAf = (AffineTransform)affineTransform.clone();
115 42439 dmartinezizquierdo
116 40435 jjdelcerro
                newAf.translate(p.getX(), p.getY());
117
                newAf.rotate(theta);
118 42439 dmartinezizquierdo
119 40435 jjdelcerro
                GeneralPathX gp = new GeneralPathX();
120
                gp.moveTo(0, 0);
121
                gp.lineTo(size, -halfHeight);
122
                gp.lineTo(size, halfHeight);
123
                gp.closePath();
124
125
                gp.transform(newAf);
126 42439 dmartinezizquierdo
127 40435 jjdelcerro
                g.fill(gp);
128
        }
129 42439 dmartinezizquierdo
130 40435 jjdelcerro
        public Object clone() throws CloneNotSupportedException {
131
                ArrowMarkerSymbol copy  = (ArrowMarkerSymbol) super.clone();
132 42439 dmartinezizquierdo
133 40435 jjdelcerro
                // Clone the selection symbol
134
                if (symSel != null) {
135
                        copy.symSel = (ArrowMarkerSymbol) symSel.clone();
136
                }
137
                return copy;
138
        }
139
140
        public void loadFromState(PersistentState state)
141
                        throws PersistenceException {
142
                // Set parent fill symbol properties
143
                super.loadFromState(state);
144
145
                // Set own properties
146
                setSharpness(state.getDouble(FIELD_SHARPNESS));
147
        }
148
149
        public void saveToState(PersistentState state) throws PersistenceException {
150
                // Save parent fill symbol properties
151
                super.saveToState(state);
152
153
                // Save own properties
154
                state.set(FIELD_SHARPNESS, getSharpness());
155
        }
156
157
        public static class RegisterPersistence implements Callable {
158
159
                public Object call() throws Exception {
160
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
161
                        if( manager.getDefinition(ARROW_MARKER_SYMBOL_DYNCLASS_NAME)==null ) {
162
                                DynStruct definition = manager.addDefinition(
163
                                                ArrowMarkerSymbol.class,
164
                                                ARROW_MARKER_SYMBOL_DYNCLASS_NAME,
165
                                                ARROW_MARKER_SYMBOL_DYNCLASS_NAME+" Persistence definition",
166 42439 dmartinezizquierdo
                                                null,
167 40435 jjdelcerro
                                                null
168
                                );
169
                                // Extend the FillSymbol base definition
170
                                definition.extend(manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME));
171
172
                                // Sharpeness
173
                                definition.addDynFieldDouble(FIELD_SHARPNESS).setMandatory(true);
174
                        }
175
                        return Boolean.TRUE;
176
                }
177 42439 dmartinezizquierdo
178 40435 jjdelcerro
        }
179
180
}