Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / symbols / LineFillSymbol.java @ 19091

History | View | Annotate | Download (9.45 KB)

1 18763 jdominguez
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 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.Rectangle;
46
import java.awt.geom.AffineTransform;
47
import java.awt.geom.Line2D;
48
import java.awt.geom.Rectangle2D;
49
50
import javax.print.attribute.PrintRequestAttributeSet;
51
52
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
53
import com.iver.cit.gvsig.fmap.ViewPort;
54
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit;
55
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
56
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
57
import com.iver.cit.gvsig.fmap.core.FShape;
58
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
59
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
60
import com.iver.cit.gvsig.fmap.core.symbols.AbstractFillSymbol;
61
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
62
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
63
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
64
import com.iver.utiles.StringUtilities;
65
import com.iver.utiles.XMLEntity;
66
import com.iver.utiles.swing.threads.Cancellable;
67
68
/**
69
 * LineFillSymbol allows to define a filling pattern composed by lines to complete the padding
70
 * of the polygon.
71
 * @autor jaume dominguez faus - jaume.dominguez@iver.es
72
 */
73
public class LineFillSymbol extends AbstractFillSymbol {
74
        private double angle;
75
        private ILineSymbol lineSymbol = SymbologyFactory.createDefaultLineSymbol();
76
        private double offset = 5, csOffset = offset;
77
        private double separation = 5, csSeparation = separation;
78
        private double symbolLineWidth;
79
        private LineFillSymbol symSel;
80
        /**
81
         * Returns the rotation angle of the lines that compose the filling pattern
82
         * @return angle
83
         */
84
        public double getAngle() {
85
                return angle;
86
        }
87
        /**
88
         * Sets the rotation angle of the lines that compose the filling pattern
89
         * @param angle
90
         */
91
        public void setAngle(double angle) {
92
                this.angle = angle;
93
        }
94
        /**
95
         * Gets the line symbol that is used to create the filling pattern.
96
         * @return lineSymbol, ILineSymbol
97
         */
98
        public ILineSymbol getLineSymbol() {
99
                return lineSymbol;
100
        }
101
        /**
102
         * Establishes the line symbol that is used to create the filling pattern.
103
         * @param lineSymbol,ILineSymbol
104
         */
105
        public void setLineSymbol(ILineSymbol lineSymbol) {
106
                if (lineSymbol != null)
107
                        symbolLineWidth = lineSymbol.getLineWidth();
108
                else
109
                        symbolLineWidth = 0;
110
                this.lineSymbol = lineSymbol;
111
112
        }
113
        /**
114
         * Gets the offset of the lines inside the filling pattern
115
         * @return offset
116
         */
117
        public double getOffset() {
118
                return offset;
119
        }
120
        /**
121
         * Sets the offset of the lines inside the filling pattern
122
         * @param offset
123
         */
124
        public void setOffset(double offset) {
125
                this.offset = offset;
126
                this.csOffset = offset;
127
        }
128
        /**
129
         * Returns the separation between lines that is used to create the filling pattern
130
         * @return
131
         */
132
        public double getSeparation() {
133
                return separation;
134
        }
135
        /**
136
         * Establishes the separation between lines that is used to create the filling pattern
137
         * @param separation
138
         */
139
        public void setSeparation(double separation) {
140
                if (separation == 0) separation = 1D;
141
                this.separation = separation;
142
                this.csSeparation = separation;
143
        }
144
145
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) {
146
                Color fillColor = getFillColor();
147
                if (fillColor!=null && hasFill()) {
148
                        g.setColor(getFillColor());
149
                        g.fill(shp);
150
                }
151
152
                if (lineSymbol != null) {
153
                        if (csSeparation < 1) csSeparation = 1;
154
                        g.setClip(shp);
155
                        g.setColor(Color.black);
156
157
                        Rectangle2D bounds = shp.getBounds();
158
                        //   g.draw(bounds);
159
                        double radius = Math.abs(Math.max(bounds.getHeight(), bounds.getWidth()));
160
                        double centerX = bounds.getCenterX();
161
                        double centerY = bounds.getCenterY();
162
                        double aux = -(radius+csOffset);
163
164
                        g.translate(centerX, centerY);
165
                        g.rotate(angle);
166
                        while ((cancel==null || !cancel.isCanceled()) && aux <= radius) {
167
                                double y = aux;
168
                                double x1 = - radius;
169
                                double x2 = radius;
170
                                Line2D line;
171
                                line = new Line2D.Double(x1, y, x2, y);
172
                                lineSymbol.draw(g, null, new FPolyline2D(new GeneralPathX(line)), null);
173
                                aux += csSeparation;
174
                        }
175
                        g.rotate(-angle);
176
                        g.translate(-centerX, -centerY);
177
                        g.setClip(null);
178
                }
179
180
                ILineSymbol outLineSymbol = getOutline();
181
                if (outLineSymbol != null && hasOutline())
182
                        outLineSymbol.draw(g, affineTransform, shp, cancel);
183
184
        }
185
186
        public void drawInsideRectangle(Graphics2D g,
187
                        AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException {
188
                draw(g, null, new FPolygon2D(new GeneralPathX(r)), null);
189
        }
190
191
192
        public ISymbol getSymbolForSelection() {
193
                if (symSel == null) symSel = new LineFillSymbol();
194
                symSel.setFillColor(ISymbol.SELECTION_COLOR);
195
                return symSel;
196
        }
197
198
        public int getSymbolType() {
199
                return FShape.POLYGON;
200
        }
201
202
        public XMLEntity getXMLEntity() {
203
                XMLEntity xml = new XMLEntity();
204
                xml.putProperty("className", getClassName());
205
                xml.putProperty("desc", getDescription());
206
                xml.putProperty("isShapeVisible", isShapeVisible());
207
                xml.putProperty("angle", angle);
208
                xml.putProperty("offset", offset);
209
                xml.putProperty("separation", separation);
210
                xml.putProperty("symbolLineWidth", symbolLineWidth);
211
                xml.putProperty("referenceSystem", getReferenceSystem());
212
                xml.putProperty("unit", getUnit());
213
214
                if (getFillColor() !=null)
215
                        xml.putProperty("fillColor", StringUtilities.color2String(getFillColor()));
216
                xml.putProperty("hasFill", hasFill());
217
                if (lineSymbol!=null) {
218
                        XMLEntity xmlLine = lineSymbol.getXMLEntity();
219
                        xmlLine.putProperty("id", "fillLine");
220
                        xml.addChild(xmlLine);
221
                }
222
223
                ILineSymbol outline = getOutline();
224
                if (outline!=null) {
225
                        XMLEntity xmlOutline = outline.getXMLEntity();
226
                        xmlOutline.putProperty("id", "outline");
227
                        xml.addChild(xmlOutline);
228
                }
229
                xml.putProperty("hasOutline", hasOutline());
230
                return xml;
231
        }
232
233
        public void setXMLEntity(XMLEntity xml) {
234
                setDescription(xml.getStringProperty("desc"));
235
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
236
                setAngle(xml.getDoubleProperty("angle"));
237
                setOffset(xml.getDoubleProperty("offset"));
238
                setSeparation(xml.getDoubleProperty("separation"));
239
240
                if (xml.contains("fillColor"))
241
                        setFillColor(StringUtilities.
242
                                        string2Color(xml.getStringProperty("fillColor")));
243
244
                if (xml.contains("hasFill"))
245
                        setHasFill(xml.getBooleanProperty("hasFill"));
246
247
                XMLEntity lineSymbolXML = xml.firstChild("id", "fillLine");
248
                if (lineSymbolXML != null) {
249
                        setLineSymbol((ILineSymbol) SymbologyFactory.
250
                                        createSymbolFromXML(lineSymbolXML, "fill symbol"));
251
                }
252
253
                XMLEntity outlineXML = xml.firstChild("id", "outline");
254
                if (outlineXML != null) {
255
                        setOutline((ILineSymbol) SymbologyFactory.
256
                                        createSymbolFromXML(outlineXML, "outline symbol"));
257
                }
258
                if (xml.contains("hasOutline"))
259
                        setHasOutline(xml.getBooleanProperty("hasOutline"));
260
261
                if (xml.contains("unit")) { // remove this line when done
262
                // measure unit (for outline)
263
                setUnit(xml.getIntProperty("unit"));
264
265
                // reference system (for outline)
266
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
267
                }
268
        }
269
270
        public String getClassName() {
271
                return getClass().getName();
272
        }
273
274
        public void print(Graphics2D g, AffineTransform at, FShape shape,
275
                        PrintRequestAttributeSet properties) throws ReadDriverException {
276
                // TODO Auto-generated method stub
277
                throw new Error("Not yet implemented!");
278
        }
279
280
        public void setCartographicSize(double cartographicSize, FShape shp) {
281
                super.setCartographicSize(cartographicSize, shp);
282
                if (getLineSymbol()!=null) {
283
                        getLineSymbol().setCartographicSize(cartographicSize, shp);
284
                }
285
        }
286
287
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
288
                double s = super.toCartographicSize(viewPort, dpi, shp);
289
                csOffset = CartographicSupportToolkit.
290
                        getCartographicLength(this, offset, viewPort, dpi);
291
292
                csSeparation = CartographicSupportToolkit.
293
                        getCartographicLength(this, separation, viewPort, dpi);
294
                double csLineWidth = CartographicSupportToolkit.
295
                        getCartographicLength(this, symbolLineWidth, viewPort, dpi);
296
                if (getLineSymbol()!=null) {
297
                        getLineSymbol().setCartographicSize(csLineWidth, shp);
298
                }
299
                return s;
300
        }
301
302
303
        @Override
304
        public void setUnit(int unitIndex) {
305
                super.setUnit(unitIndex);
306
                if (getLineSymbol()!=null) {
307
                        getLineSymbol().setUnit(unitIndex);
308
                }
309
        }
310
311
        @Override
312
        public void setReferenceSystem(int system) {
313
                super.setReferenceSystem(system);
314
                if (getLineSymbol()!=null) {
315
                        getLineSymbol().setReferenceSystem(system);
316
                }
317
        }
318
}