Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2030 / libraries / 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 @ 35919

History | View | Annotate | Download (5.27 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.impl;
23

    
24
import java.awt.BasicStroke;
25
import java.awt.Graphics2D;
26
import java.awt.geom.AffineTransform;
27

    
28
import org.gvsig.fmap.dal.feature.Feature;
29
import org.gvsig.fmap.geom.Geometry;
30
import org.gvsig.fmap.geom.primitive.GeneralPathX;
31
import org.gvsig.fmap.geom.primitive.Point;
32
import org.gvsig.fmap.mapcontext.MapContext;
33
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
34
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
35
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IArrowMarkerSymbol;
36
import org.gvsig.tools.ToolsLocator;
37
import org.gvsig.tools.dynobject.DynStruct;
38
import org.gvsig.tools.persistence.PersistenceManager;
39
import org.gvsig.tools.persistence.PersistentState;
40
import org.gvsig.tools.persistence.exception.PersistenceException;
41
import org.gvsig.tools.task.Cancellable;
42
import org.gvsig.tools.util.Callable;
43

    
44
/**
45
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
46
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
47
 */
48
public class ArrowMarkerSymbol extends AbstractMarkerSymbol implements CartographicSupport, IArrowMarkerSymbol {
49

    
50
        public static final String ARROW_MARKER_SYMBOL_DYNCLASS_NAME = "ArrowMarkerSymbol";
51
        
52
        private static final String FIELD_SHARPNESS = "sharpness"; 
53

    
54
        private ArrowMarkerSymbol symSel;
55
        private double sharpeness;
56

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

    
62
                }
63

    
64
                return symSel;
65
        }
66

    
67
        public String getClassName() {
68
                return getClass().getName();
69
        }
70

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

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

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

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

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

    
101
                g.setColor(getColor());
102

    
103
                g.setStroke(new BasicStroke());
104

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

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

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

    
136
                // Set own properties
137
                setSharpness(state.getDouble(FIELD_SHARPNESS));
138
        }
139

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

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

    
148
        public static class RegisterPersistence implements Callable {
149

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

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

    
171
}