Statistics
| Revision:

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

History | View | Annotate | Download (10.1 KB)

1
/* 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
import java.awt.image.BufferedImage;
50

    
51
import javax.print.attribute.PrintRequestAttributeSet;
52
import javax.print.attribute.standard.PrintQuality;
53

    
54
import com.iver.cit.gvsig.fmap.ViewPort;
55
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit;
56
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
57
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
58
import com.iver.cit.gvsig.fmap.core.FShape;
59
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
60
import com.iver.cit.gvsig.fmap.core.IGeometry;
61
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
62
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
63
import com.iver.cit.gvsig.fmap.core.symbols.AbstractFillSymbol;
64
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
65
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
66
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
67
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
68
import com.iver.utiles.StringUtilities;
69
import com.iver.utiles.XMLEntity;
70
import com.iver.utiles.swing.threads.Cancellable;
71
import com.vividsolutions.jts.geom.Geometry;
72

    
73
/**
74
 * LineFillSymbol allows to define a filling pattern composed by lines to complete the padding
75
 * of the polygon.
76
 * @autor jaume dominguez faus - jaume.dominguez@iver.es
77
 */
78
public class LineFillSymbol extends AbstractFillSymbol {
79
        private double angle;
80
        private ILineSymbol lineSymbol = SymbologyFactory.createDefaultLineSymbol();
81
        private double offset = 5, csOffset = offset;
82
        private double separation = 5, csSeparation = separation;
83
        private double symbolLineWidth, csLineWidth=symbolLineWidth;
84
        private LineFillSymbol symSel;
85
        private PrintRequestAttributeSet properties;
86
        /**
87
         * Returns the rotation angle of the lines that compose the filling pattern
88
         * @return angle
89
         */
90
        public double getAngle() {
91
                return angle;
92
        }
93
        /**
94
         * Sets the rotation angle of the lines that compose the filling pattern
95
         * @param angle
96
         */
97
        public void setAngle(double angle) {
98
                this.angle = angle;
99
        }
100
        /**
101
         * Gets the line symbol that is used to create the filling pattern.
102
         * @return lineSymbol, ILineSymbol
103
         */
104
        public ILineSymbol getLineSymbol() {
105
                return lineSymbol;
106
        }
107
        /**
108
         * Establishes the line symbol that is used to create the filling pattern.
109
         * @param lineSymbol,ILineSymbol
110
         */
111
        public void setLineSymbol(ILineSymbol lineSymbol) {
112
                if (lineSymbol != null)
113
                        symbolLineWidth = lineSymbol.getLineWidth();
114
                else
115
                        symbolLineWidth = 0;
116
                this.lineSymbol = lineSymbol;
117

    
118
        }
119
        /**
120
         * Gets the offset of the lines inside the filling pattern
121
         * @return offset
122
         */
123
        public double getOffset() {
124
                return offset;
125
        }
126
        /**
127
         * Sets the offset of the lines inside the filling pattern
128
         * @param offset
129
         */
130
        public void setOffset(double offset) {
131
                this.offset = offset;
132
                this.csOffset = offset;
133
        }
134
        /**
135
         * Returns the separation between lines that is used to create the filling pattern
136
         * @return
137
         */
138
        public double getSeparation() {
139
                return separation;
140
        }
141
        /**
142
         * Establishes the separation between lines that is used to create the filling pattern
143
         * @param separation
144
         */
145
        public void setSeparation(double separation) {
146
                if (separation == 0) separation = 1D;
147
                this.separation = separation;
148
                this.csSeparation = separation;
149
        }
150

    
151
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) {
152
                Color fillColor = getFillColor();
153
                if (fillColor!=null && hasFill()) {
154
                        g.setColor(getFillColor());
155
                        g.fill(shp);
156
                }
157

    
158
                if (lineSymbol != null) {
159
                        if (csSeparation < 1) csSeparation = 1;
160
                        Rectangle rClip = null;
161
                        if (g.getClipBounds()!=null){
162
                                rClip=(Rectangle)g.getClipBounds().clone();
163
                                g.setClip(rClip.x, rClip.y, rClip.width, rClip.height);
164
                        }
165
                        g.clip(shp);
166
                        g.setColor(Color.black);
167

    
168
                        Rectangle2D bounds = shp.getBounds();
169
                        double radius = Math.abs(Math.max(bounds.getHeight(), bounds.getWidth()));
170
                        double centerX = bounds.getCenterX();
171
                        double centerY = bounds.getCenterY();
172
                        double aux = -(radius+csOffset);
173

    
174
                        g.translate(centerX, centerY);
175
                        g.rotate(angle);
176
                        while ((cancel==null || !cancel.isCanceled()) && aux <= radius) {
177
                                double y = aux;
178
                                double x1 = - radius;
179
                                double x2 = radius;
180
                                Line2D line;
181
                                line = new Line2D.Double(x1, y, x2, y);
182
                                lineSymbol.draw(g, null, new FPolyline2D(new GeneralPathX(line)), null);
183
                                aux += csSeparation;
184
                        }
185
                        g.rotate(-angle);
186
                        g.translate(-centerX, -centerY);
187
                        g.setClip(rClip);
188
                }
189

    
190
                ILineSymbol outLineSymbol = getOutline();
191
                if (outLineSymbol != null && hasOutline())
192
                        outLineSymbol.draw(g, affineTransform, shp, cancel);
193
        }
194

    
195
        public void drawInsideRectangle(Graphics2D g,
196
                        AffineTransform scaleInstance, Rectangle r, PrintRequestAttributeSet properties) throws SymbolDrawingException {
197
                if (properties==null)
198
                        draw(g, null, new FPolygon2D(new GeneralPathX(r)), null);
199
                else
200
                        print(g,new AffineTransform(),new FPolygon2D(new GeneralPathX(r)), properties);
201
        }
202

    
203

    
204
        public ISymbol getSymbolForSelection() {
205
                if (symSel == null) symSel = new LineFillSymbol();
206
                symSel.setFillColor(ISymbol.SELECTION_COLOR);
207
                return symSel;
208
        }
209

    
210
        public int getSymbolType() {
211
                return FShape.POLYGON;
212
        }
213

    
214
        public XMLEntity getXMLEntity() {
215
                XMLEntity xml = new XMLEntity();
216
                xml.putProperty("className", getClassName());
217
                xml.putProperty("desc", getDescription());
218
                xml.putProperty("isShapeVisible", isShapeVisible());
219
                xml.putProperty("angle", angle);
220
                xml.putProperty("offset", offset);
221
                xml.putProperty("separation", separation);
222
                xml.putProperty("symbolLineWidth", symbolLineWidth);
223
                xml.putProperty("referenceSystem", getReferenceSystem());
224
                xml.putProperty("unit", getUnit());
225

    
226
                if (getFillColor() !=null)
227
                        xml.putProperty("fillColor", StringUtilities.color2String(getFillColor()));
228
                xml.putProperty("hasFill", hasFill());
229
                if (lineSymbol!=null) {
230
                        XMLEntity xmlLine = lineSymbol.getXMLEntity();
231
                        xmlLine.putProperty("id", "fillLine");
232
                        xml.addChild(xmlLine);
233
                }
234

    
235
                ILineSymbol outline = getOutline();
236
                if (outline!=null) {
237
                        XMLEntity xmlOutline = outline.getXMLEntity();
238
                        xmlOutline.putProperty("id", "outline");
239
                        xml.addChild(xmlOutline);
240
                }
241
                xml.putProperty("hasOutline", hasOutline());
242
                return xml;
243
        }
244

    
245
        public void setXMLEntity(XMLEntity xml) {
246
                setDescription(xml.getStringProperty("desc"));
247
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
248
                setAngle(xml.getDoubleProperty("angle"));
249
                setOffset(xml.getDoubleProperty("offset"));
250
                setSeparation(xml.getDoubleProperty("separation"));
251

    
252
                if (xml.contains("fillColor"))
253
                        setFillColor(StringUtilities.
254
                                        string2Color(xml.getStringProperty("fillColor")));
255

    
256
                if (xml.contains("hasFill"))
257
                        setHasFill(xml.getBooleanProperty("hasFill"));
258

    
259
                XMLEntity lineSymbolXML = xml.firstChild("id", "fillLine");
260
                if (lineSymbolXML != null) {
261
                        setLineSymbol((ILineSymbol) SymbologyFactory.
262
                                        createSymbolFromXML(lineSymbolXML, "fill symbol"));
263
                }
264

    
265
                XMLEntity outlineXML = xml.firstChild("id", "outline");
266
                if (outlineXML != null) {
267
                        setOutline((ILineSymbol) SymbologyFactory.
268
                                        createSymbolFromXML(outlineXML, "outline symbol"));
269
                }
270
                if (xml.contains("hasOutline"))
271
                        setHasOutline(xml.getBooleanProperty("hasOutline"));
272

    
273
                if (xml.contains("unit")) { // remove this line when done
274
                // measure unit (for outline)
275
                setUnit(xml.getIntProperty("unit"));
276

    
277
                // reference system (for outline)
278
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
279
                }
280
        }
281

    
282
        public String getClassName() {
283
                return getClass().getName();
284
        }
285

    
286
        public void print(Graphics2D g, AffineTransform at, FShape shape,
287
                        PrintRequestAttributeSet properties) {
288
                this.properties=properties;
289
        draw(g, at, shape, null);
290
        this.properties=null;
291
        }
292

    
293
        public void setCartographicSize(double cartographicSize, FShape shp) {
294
                super.setCartographicSize(cartographicSize, shp);
295
                if (getLineSymbol()!=null) {
296
                        getLineSymbol().setCartographicSize(csLineWidth, shp);
297
                }
298
                csOffset=offset;
299
                csSeparation=separation;
300
                csLineWidth=symbolLineWidth;
301

    
302
        }
303

    
304
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
305
                double s = super.toCartographicSize(viewPort, dpi, shp);
306
                csOffset = CartographicSupportToolkit.
307
                        getCartographicLength(this, offset, viewPort, dpi);
308

    
309
                csSeparation = CartographicSupportToolkit.
310
                        getCartographicLength(this, separation, viewPort, dpi);
311
                double csLineWidth = CartographicSupportToolkit.
312
                        getCartographicLength(this, symbolLineWidth, viewPort, dpi);
313
                if (getLineSymbol()!=null) {
314
                        getLineSymbol().setCartographicSize(csLineWidth, shp);
315
                }
316
                return s;
317
        }
318

    
319

    
320
        @Override
321
        public void setUnit(int unitIndex) {
322
                super.setUnit(unitIndex);
323
                if (getLineSymbol()!=null) {
324
                        getLineSymbol().setUnit(unitIndex);
325
                }
326
        }
327

    
328
        @Override
329
        public void setReferenceSystem(int system) {
330
                super.setReferenceSystem(system);
331
                if (getLineSymbol()!=null) {
332
                        getLineSymbol().setReferenceSystem(system);
333
                }
334
        }
335
}