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

History | View | Annotate | Download (5.32 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.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
        private static final String FIELD_SHARPNESS = "sharpness"; 
55

    
56
        private ArrowMarkerSymbol symSel;
57
        private double sharpeness;
58

    
59
        public ISymbol getSymbolForSelection() {
60
                if (symSel == null) {
61
                        symSel = new ArrowMarkerSymbol();
62
                        symSel.setColor(MapContext.getSelectionColor());
63

    
64
                }
65

    
66
                return symSel;
67
        }
68

    
69
        public String getClassName() {
70
                return getClass().getName();
71
        }
72

    
73
        /**
74
         * To get the sharpeness attribute.It will determine the final form of the arrow
75
         * @return
76
         */
77
        public double getSharpness() {
78
                return sharpeness;
79
        }
80

    
81
        /**
82
         * To set the sharpeness.It will determine the final form of the arrow
83
         * @param sharpeness, the value of the arrow's edge angle IN RADIANS
84
         * @see #getSharpeness()
85
         */
86
        public void setSharpness(double sharpeness) {
87
                this.sharpeness = sharpeness;
88
        }
89

    
90
        public void draw(Graphics2D g, AffineTransform affineTransform,
91
                        Geometry geom, Feature feature, Cancellable cancel) {
92

    
93
                if (geom == null) {
94
                        return;
95
                }
96
                Point p = (Point) geom;
97
                double radian_half_sharpeness = Math.toRadians(getSharpness()*.5); //
98

    
99
                double size = getSize();
100
                double halfHeight = size * Math.tan(radian_half_sharpeness);
101
                double theta = getRotation();
102

    
103
                g.setColor(getColor());
104

    
105
                g.setStroke(new BasicStroke());
106

    
107
                AffineTransform newAf = (AffineTransform)affineTransform.clone();
108
                
109
                newAf.translate(p.getX(), p.getY());
110
                newAf.rotate(theta);
111
                
112
                GeneralPathX gp = new GeneralPathX();
113
                gp.moveTo(0, 0);
114
                gp.lineTo(size, -halfHeight);
115
                gp.lineTo(size, halfHeight);
116
                gp.closePath();
117

    
118
                gp.transform(newAf);
119
                
120
                g.fill(gp);
121
        }
122
        
123
        public Object clone() throws CloneNotSupportedException {
124
                ArrowMarkerSymbol copy  = (ArrowMarkerSymbol) super.clone();
125
                
126
                // Clone the selection symbol
127
                if (symSel != null) {
128
                        copy.symSel = (ArrowMarkerSymbol) symSel.clone();
129
                }
130
                return copy;
131
        }
132

    
133
        public void loadFromState(PersistentState state)
134
                        throws PersistenceException {
135
                // Set parent fill symbol properties
136
                super.loadFromState(state);
137

    
138
                // Set own properties
139
                setSharpness(state.getDouble(FIELD_SHARPNESS));
140
        }
141

    
142
        public void saveToState(PersistentState state) throws PersistenceException {
143
                // Save parent fill symbol properties
144
                super.saveToState(state);
145

    
146
                // Save own properties
147
                state.set(FIELD_SHARPNESS, getSharpness());
148
        }
149

    
150
        public static class RegisterPersistence implements Callable {
151

    
152
                public Object call() throws Exception {
153
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
154
                        if( manager.getDefinition(ARROW_MARKER_SYMBOL_DYNCLASS_NAME)==null ) {
155
                                DynStruct definition = manager.addDefinition(
156
                                                ArrowMarkerSymbol.class,
157
                                                ARROW_MARKER_SYMBOL_DYNCLASS_NAME,
158
                                                ARROW_MARKER_SYMBOL_DYNCLASS_NAME+" Persistence definition",
159
                                                null, 
160
                                                null
161
                                );
162
                                // Extend the FillSymbol base definition
163
                                definition.extend(manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME));
164

    
165
                                // Sharpeness
166
                                definition.addDynFieldDouble(FIELD_SHARPNESS).setMandatory(true);
167
                        }
168
                        return Boolean.TRUE;
169
                }
170
                
171
        }
172

    
173
}