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

History | View | Annotate | Download (5.43 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 = (ArrowMarkerSymbol)this.cloneForSelection();
63
                        symSel.setColor(MapContext.getSelectionColor());
64
                }else{
65
                    symSel.setColor(MapContext.getSelectionColor());
66
                }
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
                Point p = (Point) geom;
99
                double radian_half_sharpeness = Math.toRadians(getSharpness()*.5); //
100

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

    
105
                g.setColor(getColor());
106

    
107
                g.setStroke(new BasicStroke());
108

    
109
                AffineTransform newAf = (AffineTransform)affineTransform.clone();
110

    
111
                newAf.translate(p.getX(), p.getY());
112
                newAf.rotate(theta);
113

    
114
                GeneralPathX gp = new GeneralPathX();
115
                gp.moveTo(0, 0);
116
                gp.lineTo(size, -halfHeight);
117
                gp.lineTo(size, halfHeight);
118
                gp.closePath();
119

    
120
                gp.transform(newAf);
121

    
122
                g.fill(gp);
123
        }
124

    
125
        public Object clone() throws CloneNotSupportedException {
126
                ArrowMarkerSymbol copy  = (ArrowMarkerSymbol) super.clone();
127

    
128
                // Clone the selection symbol
129
                if (symSel != null) {
130
                        copy.symSel = (ArrowMarkerSymbol) symSel.clone();
131
                }
132
                return copy;
133
        }
134

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

    
140
                // Set own properties
141
                setSharpness(state.getDouble(FIELD_SHARPNESS));
142
        }
143

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

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

    
152
        public static class RegisterPersistence implements Callable {
153

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

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

    
173
        }
174

    
175
}