Statistics
| Revision:

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

History | View | Annotate | Download (10.3 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.Paint;
46
import java.awt.Rectangle;
47
import java.awt.TexturePaint;
48
import java.awt.geom.AffineTransform;
49
import java.awt.image.BufferedImage;
50
import java.io.File;
51
import java.io.IOException;
52

    
53
import javax.print.attribute.PrintRequestAttributeSet;
54

    
55
import org.apache.log4j.Logger;
56
import org.gvsig.symbology.fmap.styles.BackgroundFileStyle;
57
import org.gvsig.symbology.fmap.styles.ImageStyle;
58
import org.gvsig.symbology.fmap.styles.SVGStyle;
59
import org.gvsig.symbology.fmap.styles.SimpleMarkerFillPropertiesStyle;
60

    
61
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
62
import com.iver.cit.gvsig.fmap.Messages;
63
import com.iver.cit.gvsig.fmap.ViewPort;
64
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
65
import com.iver.cit.gvsig.fmap.core.FShape;
66
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
67
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
68
import com.iver.cit.gvsig.fmap.core.styles.IMarkerFillPropertiesStyle;
69
import com.iver.cit.gvsig.fmap.core.symbols.AbstractFillSymbol;
70
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
71
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
72
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
73
import com.iver.utiles.StringUtilities;
74
import com.iver.utiles.XMLEntity;
75
import com.iver.utiles.swing.threads.Cancellable;
76

    
77
/**
78
 * PictureFillSymbol allows to use an image file suported by gvSIG as a padding
79
 * for the polygons.This image can be modified using methods to scale or rotate it.
80
 * @author jaume dominguez faus - jaume.dominguez@iver.es
81
 */
82
public class PictureFillSymbol extends AbstractFillSymbol {
83
        private double angle = 0;
84
        private double xScale = 1;
85
        private double yScale = 1;
86
        
87
        private IMarkerFillPropertiesStyle markerFillProperties = new SimpleMarkerFillPropertiesStyle();
88
        private String imagePath;
89
        private BackgroundFileStyle bgImage;
90

    
91
        
92
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) {
93
                Color fillColor = getFillColor();
94
                if (fillColor != null) {
95
                        g.setColor(getFillColor());
96
                        g.fill(shp);
97
                }
98
                
99
                g.setClip(shp);
100

    
101
                if (xScale<=0 &&  yScale<=0)
102
                        return;
103

    
104
                BackgroundFileStyle bg =  bgImage ;
105

    
106
                final double imageWidth  = bg.getBounds().getWidth()  * xScale;
107
                final double imageHeight = bg.getBounds().getWidth() * yScale;
108

    
109
                if (imageWidth==0 || imageHeight==0) return;
110

    
111
                Rectangle rProv = new Rectangle();
112
                rProv.setFrame(0, 0, imageWidth, imageHeight);
113
                Paint resulPatternFill = null;
114

    
115
                Graphics2D gAux;// = img.createGraphics();
116

    
117
                double xSeparation = markerFillProperties.getXSeparation(); 
118
                double ySeparation = markerFillProperties.getYSeparation(); 
119
                double xOffset = markerFillProperties.getXOffset();
120
                double yOffset = markerFillProperties.getYOffset();
121

    
122
                rProv.setRect(0, 0,
123
                                rProv.getWidth() + xSeparation,
124
                                rProv.getHeight() + ySeparation);
125

    
126
                BufferedImage bi = new BufferedImage(rProv.width, rProv.height, BufferedImage.TYPE_INT_ARGB);
127
                gAux = bi.createGraphics();
128
                AffineTransform tx = AffineTransform.getScaleInstance(xScale, yScale);
129
                tx.translate(xSeparation*0.5, ySeparation*0.5);
130
                tx.rotate(angle, rProv.getWidth(), rProv.getHeight());
131
                gAux.setTransform(tx);
132
                bg.drawInsideRectangle(gAux, rProv);
133

    
134
                resulPatternFill = new TexturePaint(bi,rProv);
135
                g.translate(xOffset, yOffset);
136
                g.setPaint(resulPatternFill);
137
                g.fill(shp);
138
                g.translate(-xOffset, -yOffset);
139

    
140

    
141
                g.setClip(null);
142
                if (getOutline()!=null) {
143
                        getOutline().draw(g, affineTransform, shp, cancel);
144
                }
145
        }
146

    
147
        /**
148
         * Defines the file from where the picture to fill the polygon is taken.
149
         * @param imageFile
150
         * @throws IOException
151
         */
152
        public void setImage(File imageFile) throws IOException{
153
                if(imageFile!=null && imageFile.exists()) {
154
                        imagePath = imageFile.getAbsolutePath();
155
                        bgImage= (imageFile.getAbsolutePath().toLowerCase().endsWith(".svg")) ?
156
                                        new SVGStyle() :
157
                                        new ImageStyle();
158
                        bgImage.setSource(imageFile.toURL());
159
                }
160
        }
161

    
162
        public void drawInsideRectangle(Graphics2D g,
163
                        AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException {
164
                draw(g, null, new FPolygon2D(new GeneralPathX(r)), null);
165
        }
166

    
167
        public ISymbol getSymbolForSelection() {
168
                // TODO Auto-generated method stub
169
                throw new Error("Not yet implemented!");
170
        }
171

    
172
        public int getSymbolType() {
173
                return FShape.POLYGON;
174
        }
175

    
176
        public XMLEntity getXMLEntity() {
177
                XMLEntity xml = new XMLEntity();
178
                xml.putProperty("desc", getDescription());
179
                xml.putProperty("className", getClassName());
180
                xml.putProperty("isShapeVisible", isShapeVisible());
181
                xml.putProperty("referenceSystem", getReferenceSystem());
182
                xml.putProperty("unit", getUnit());
183
        
184
                xml.putProperty("angle", angle);
185
                xml.putProperty("scaleX", xScale);
186
                xml.putProperty("scaleY", yScale);
187
                xml.putProperty("imagePath", imagePath);
188
                if (getFillColor()!=null)
189
                        xml.putProperty("fillColor", StringUtilities.color2String(getFillColor()));
190
                
191
                xml.putProperty("hasFill", hasFill());
192
                XMLEntity fillPropertiesXML = markerFillProperties.getXMLEntity();
193
                fillPropertiesXML.putProperty("id", "fillProperties");
194
                xml.addChild(fillPropertiesXML);
195
                if (getOutline()!=null) {
196
                        XMLEntity outlineXML = getOutline().getXMLEntity();
197
                        outlineXML.putProperty("id", "outline symbol");
198
                        xml.addChild(outlineXML);
199
                }
200
                xml.putProperty("hasOutline", hasOutline());
201
                return xml;
202
        }
203

    
204
        public void setXMLEntity(XMLEntity xml) {
205
                try {
206
                        File f = new File(xml.getStringProperty("imagePath"));
207
                        setImage(f);
208
                        setDescription(xml.getStringProperty("desc"));
209
                        setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
210
                        setAngle(xml.getDoubleProperty("angle"));
211
                        setXScale(xml.getDoubleProperty("scaleX"));
212
                        setYScale(xml.getDoubleProperty("scaleY"));
213
                        if (xml.contains("fillColor"))
214
                                setFillColor(StringUtilities.string2Color(
215
                                                xml.getStringProperty("fillColor")));
216
                        markerFillProperties = (IMarkerFillPropertiesStyle) SymbologyFactory.
217
                        createStyleFromXML(
218
                                        xml.firstChild("id", "fillProperties"), "fill properties");
219
                        
220
                        try {
221
                                setImage(new File(imagePath));
222
                                
223
                        } catch (IOException e) {
224
                                e.printStackTrace();
225
                        }
226
                        
227
                        if (xml.contains("hasFill"))
228
                                setHasFill(xml.getBooleanProperty("hasFill"));
229
                        
230
                        XMLEntity outlineXML = xml.firstChild("id", "outline symbol");
231
                        if (outlineXML != null) {
232
                                setOutline((ILineSymbol) SymbologyFactory.createSymbolFromXML(outlineXML, "outline"));
233
                        }
234
                        
235
                        if (xml.contains("unit")) { // remove this line when done
236
                        // measure unit (for outline)
237
                        setUnit(xml.getIntProperty("unit"));
238
                        
239
                        // reference system (for outline)
240
                        setReferenceSystem(xml.getIntProperty("referenceSystem"));
241
                        }
242
                        
243
                        if (xml.contains("hasOutline"))
244
                                setHasOutline(xml.getBooleanProperty("hasOutline"));
245

    
246
                }catch (IOException e) {
247
                        Logger.getLogger(this.getClass()).error(Messages.getString("failed_to_read_image_file"), e);
248
                }
249

    
250
        }
251

    
252
        public String getClassName() {
253
                return getClass().getName();
254
        }
255

    
256
        public void print(Graphics2D g, AffineTransform at, FShape shape,
257
                        PrintRequestAttributeSet properties) throws ReadDriverException {
258
                // TODO Auto-generated method stub
259
                throw new Error("Not yet implemented!");
260
        }
261
        /**
262
         * Returns the IMarkerFillProperties that allows this class to treat the picture as
263
         * a marker in order to scale it, rotate it and so on.
264
         * @return markerFillProperties,IMarkerFillPropertiesStyle
265
         */
266
        public IMarkerFillPropertiesStyle getMarkerFillProperties() {
267
                return markerFillProperties;
268
        }
269

    
270
        /**
271
         * Sets the MarkerFillProperties in order to allow the user to modify the picture as
272
         * a marker (it is possible to scale it, rotate it and so on)
273
         * @param prop
274
         */
275
        public void setMarkerFillProperties(IMarkerFillPropertiesStyle prop) {
276
                this.markerFillProperties = prop;
277
        }
278
        /**
279
         * Defines the angle for the rotation of the image when it is added to create the
280
         * padding
281
         *
282
         * @return angle
283
         */
284
        public double getAngle() {
285
                return angle;
286
        }
287
        /**
288
         * Sets the angle for the rotation of the image when it is added to create the padding
289
         * @param angle
290
         */
291
        public void setAngle(double angle) {
292
                this.angle = angle;
293
        }
294

    
295
        /**
296
         * Defines the scale for the x axis of the image when it is added to create the
297
         * padding
298
         * @return xScale
299
         */
300
        public double getXScale() {
301
                return xScale;
302
        }
303
        /**
304
         * Returns the scale for the x axis of the image when it is added to create the
305
         * padding
306
         * @param xScale
307
         */
308
        public void setXScale(double xScale) {
309
                this.xScale = xScale;
310
        }
311
        /**
312
         * Defines the scale for the y axis of the image when it is added to create the
313
         * padding
314
         * @return yScale
315
         */
316
        public double getYScale() {
317
                return yScale;
318
        }
319
        /**
320
         * Returns the scale for the y axis of the image when it is added to create the
321
         * padding
322
         * @param yScale
323
         */
324
        public void setYScale(double yScale) {
325
                this.yScale = yScale;
326
        }
327
        /**
328
         * Returns the path of the image that is used to create the padding to fill the
329
         * polygon
330
         * @return imagePath
331
         */
332
        public String getImagePath() {
333
                return imagePath;
334
        }
335
        
336
        @Override
337
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
338
                return super.toCartographicSize(viewPort, dpi, shp);
339
                // this symbol cannot apply any cartographic transfomation to its filling
340
        }
341
        
342
        
343
}