Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / symbols / MarkerLineSymbol.java @ 20028

History | View | Annotate | Download (6.58 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib��ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.symbology.fmap.symbols;
42

    
43
import java.awt.Color;
44
import java.awt.Graphics2D;
45
import java.awt.geom.AffineTransform;
46

    
47
import javax.print.attribute.PrintRequestAttributeSet;
48

    
49
import org.apache.batik.ext.awt.geom.PathLength;
50

    
51
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
52
import com.iver.cit.gvsig.fmap.core.FPoint2D;
53
import com.iver.cit.gvsig.fmap.core.FShape;
54
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
55
import com.iver.cit.gvsig.fmap.core.styles.ILineStyle;
56
import com.iver.cit.gvsig.fmap.core.symbols.AbstractLineSymbol;
57
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
58
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
59
import com.iver.utiles.XMLEntity;
60
import com.iver.utiles.swing.threads.Cancellable;
61

    
62
/**
63
 * MarkerLineSymbol allows to use any symbol defined as an image to draw a lineal object.
64
 * The line will be painted as a run of symbols through the path defined by the line.
65
 *
66
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
67
 */
68
public class MarkerLineSymbol extends AbstractLineSymbol {
69
        private IMarkerSymbol marker;
70
        private MarkerLineSymbol symSelect;
71
        private double separation = 5;
72
        private double cartographicSeparation;
73
        private double width;
74
        
75
        {
76
                marker = SymbologyFactory.createDefaultMarkerSymbol();
77
                marker.setSize(5);
78
        }
79

    
80
        public void setLineWidth(double width) {
81
                this.width = width;
82
                getLineStyle().setLineWidth((float) width);
83
        }
84

    
85
        public double getLineWidth() {
86
                return width;
87
        }
88

    
89

    
90
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) {
91
                PathLength pl = new PathLength(shp);
92
                float myLineLength = pl.lengthOfPath(); // length without the first and last arrow
93
                float separation = (float) cartographicSeparation;
94

    
95
                FPoint2D p = new FPoint2D(pl.pointAtLength(0));
96
                double lineWidth = getLineStyle().getLineWidth();
97
                marker.setSize(lineWidth);
98
                marker.draw(g, affineTransform, p, null);
99

    
100
                
101
                float length = (float) (separation + lineWidth);
102
                while ((cancel==null || !cancel.isCanceled()) && length < myLineLength) {
103
                        p = new FPoint2D(pl.pointAtLength(length));
104
                        marker.draw(g, affineTransform, p, null);
105
                        length += separation + lineWidth;
106
                }
107
        }
108

    
109

    
110
        /**
111
         * Gets the separation between the marker symbols that are used to draw the line.
112
         *
113
         * @return separation
114
         */
115
        public double getSeparation() {
116
                return separation;
117
        }
118

    
119
        public ISymbol getSymbolForSelection() {
120
                if (symSelect == null) {
121
                        symSelect = (MarkerLineSymbol) SymbologyFactory.
122
                        createSymbolFromXML(getXMLEntity(), "selection derived symbol");
123
                        symSelect.setMarker((IMarkerSymbol) symSelect.getMarker()
124
                                        .getSymbolForSelection());
125

    
126
                }
127

    
128
                return symSelect;
129
        }
130
        
131
        public Color getColor() {
132
                return marker.getColor();
133
        }
134

    
135
        public void setLineColor(Color color) {
136
                marker.setColor(color);
137
        }
138
        
139
        public XMLEntity getXMLEntity() {
140
                XMLEntity xml = new XMLEntity();
141
                xml.putProperty("className", getClassName());
142
                xml.putProperty("isShapeVisible", isShapeVisible());
143
                xml.putProperty("desc", getDescription());
144
                xml.putProperty("unit", getUnit());
145
                xml.putProperty("referenceSystem", getReferenceSystem());
146
                xml.putProperty("separation", getSeparation());
147
                XMLEntity xmlMarker = marker.getXMLEntity();
148
                xmlMarker.putProperty("id", "marker");
149
                xml.addChild(xmlMarker);
150
                setLineWidth(getLineWidth()); // not a joke;
151
                XMLEntity xmlLineStyle = getLineStyle().getXMLEntity();
152
                xmlLineStyle.putProperty("id", "lineStyle");
153
                xml.addChild(xmlLineStyle);
154
                
155
                return xml;
156
        }
157

    
158
        public void setXMLEntity(XMLEntity xml) {
159
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
160
                setDescription(xml.getStringProperty("desc"));
161
                setSeparation(xml.getDoubleProperty("separation"));
162
                setUnit(xml.getIntProperty("unit"));
163
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
164
                marker = (IMarkerSymbol) SymbologyFactory.
165
                createSymbolFromXML(xml.firstChild("id", "marker"), "theMarker");
166
                setLineStyle((ILineStyle) SymbologyFactory.
167
                                createStyleFromXML(xml.firstChild("id", "lineStyle"), "theLineStyle"));
168
                width = getLineStyle().getLineWidth();
169
        }
170
        /**
171
         * Sets the separation between the marker symbols that compose the line
172
         *
173
         * @param separation
174
         */
175
        public void setSeparation(double separation) {
176
                this.separation = separation;
177
                this.cartographicSeparation = separation;
178
        }
179

    
180
        public String getClassName() {
181
                return getClass().getName();
182
        }
183

    
184
        public void print(Graphics2D g, AffineTransform at, FShape shape,
185
                        PrintRequestAttributeSet properties) throws ReadDriverException {
186
                // TODO Auto-generated method stub
187
                throw new Error("Not yet implemented!");
188
        }
189
        /**
190
         * Returns the marker symbol that compose the line
191
         *
192
         * @return marker IMarkerSymbol
193
         */
194
        public IMarkerSymbol getMarker() {
195
                return marker;
196
        }
197
        /**
198
         * Sets the marker symbol that compose the line
199
         *
200
         * @param marker IMarkerSymbol
201
         */
202
        public void setMarker(IMarkerSymbol marker) {
203
                this.marker = marker;
204
        }
205

    
206
        @Override
207
        public void setCartographicSize(double cartographicSize, FShape shp) {
208
                double oldLineWidth = getLineWidth();
209
                super.setCartographicSize(cartographicSize, shp);
210
                double newLineWidth = getLineWidth();
211
                double scale = newLineWidth/oldLineWidth;
212
                cartographicSeparation = separation * scale;
213
        }
214

    
215
        @Override
216
        public void setUnit(int unitIndex) {
217
                super.setUnit(unitIndex);
218
                marker.setUnit(unitIndex);
219
        }
220
        
221
        @Override
222
        public void setReferenceSystem(int system) {
223
                super.setReferenceSystem(system);
224
                marker.setReferenceSystem(system);
225
        }
226
        
227

    
228
}