Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / styles / SimpleMarkerFillPropertiesStyle.java @ 20768

History | View | Annotate | Download (4.7 KB)

1
package org.gvsig.symbology.fmap.styles;
2

    
3
import java.awt.Graphics2D;
4
import java.awt.Paint;
5
import java.awt.Rectangle;
6
import java.awt.RenderingHints;
7
import java.awt.TexturePaint;
8
import java.awt.image.BufferedImage;
9

    
10
import org.gvsig.symbology.fmap.symbols.MarkerFillSymbol;
11

    
12
import com.iver.cit.gvsig.fmap.Messages;
13
import com.iver.cit.gvsig.fmap.core.FShape;
14
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
15
import com.iver.cit.gvsig.fmap.core.styles.AbstractStyle;
16
import com.iver.cit.gvsig.fmap.core.styles.IMarkerFillPropertiesStyle;
17
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
18
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
19
import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol;
20
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
21
import com.iver.utiles.XMLEntity;
22

    
23
public class SimpleMarkerFillPropertiesStyle extends AbstractStyle implements IMarkerFillPropertiesStyle {
24
        private IMarkerSymbol sampleSymbol = new SimpleMarkerSymbol();
25
        private double rotation;
26
        private double xOffset = 0;
27
        private double yOffset = 0;
28
        private double xSeparation = 20;
29
        private double ySeparation = 20;
30
        private int fillStyle = MarkerFillSymbol.DefaultFillStyle;
31

    
32
        public void drawInsideRectangle(Graphics2D g, Rectangle r) {
33
                int s = (int) sampleSymbol.getSize();
34
                Rectangle rProv = new Rectangle();
35
                rProv.setFrame(0, 0, s, s);
36
                Paint resulPatternFill = null;
37
                BufferedImage bi = null;
38
                bi= new BufferedImage(s, s, BufferedImage.TYPE_INT_ARGB);
39
                Graphics2D gAux = bi.createGraphics();
40
                try {
41
                        sampleSymbol.drawInsideRectangle(gAux, gAux.getTransform(), rProv);
42
                } catch (SymbolDrawingException e) {
43
                        if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
44
                                try {
45
                                        SymbologyFactory.getWarningSymbol(
46
                                                        SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
47
                                                        "",
48
                                                        SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS).drawInsideRectangle(gAux, gAux.getTransform(), rProv);
49
                                } catch (SymbolDrawingException e1) {
50
                                        // IMPOSSIBLE TO REACH THIS
51
                                }
52
                        } else {
53
                                // should be unreachable code
54
                                throw new Error(Messages.getString("symbol_shapetype_mismatch"));
55
                        }
56
                }
57
                resulPatternFill = new TexturePaint(bi,rProv);
58
                g.setColor(null);
59
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
60
                        RenderingHints.VALUE_ANTIALIAS_ON);
61
                g.setPaint(resulPatternFill);
62
                g.fill(r);
63

    
64
        }
65

    
66
        public boolean isSuitableFor(ISymbol symbol) {
67
                return symbol.getSymbolType() == FShape.POLYGON;
68
        }
69

    
70
        public String getClassName() {
71
                return getClass().getName();
72
        } 
73

    
74
        public XMLEntity getXMLEntity() {
75
                XMLEntity xml = new XMLEntity();
76
                xml.putProperty("className", getClassName());
77
                xml.putProperty("rotation", rotation);
78
                xml.putProperty("xOffset", xOffset);
79
                xml.putProperty("yOffset", yOffset);
80
                xml.putProperty("xSeparation", xSeparation);
81
                xml.putProperty("ySeparation", ySeparation);
82
                xml.putProperty("fillStyle", fillStyle);
83
                // please, avoid persist "sampleSymbol" field.
84
                // it is always initialized by this style's owner
85
                // when needs it.
86
                return xml;
87
        }
88

    
89
        public void setXMLEntity(XMLEntity xml) {
90
                rotation = xml.getDoubleProperty("rotation");
91
                xOffset = xml.getDoubleProperty("xOffset");
92
                yOffset = xml.getDoubleProperty("yOffset");
93
                xSeparation = xml.getDoubleProperty("xSeparation");
94
                ySeparation = xml.getDoubleProperty("ySeparation");
95
                fillStyle = xml.getIntProperty("fillStyle");
96
                // please, avoid initialize "sampleSymbol" field. It
97
                // is already controlled by this style's owner.
98
        }
99

    
100
        public void drawOutline(Graphics2D g, Rectangle r) {
101
                drawInsideRectangle(g, r);
102
        }
103

    
104
        /**
105
         * <p>
106
         * Define an utility symbol to show up a thumbnail
107
         * by default, this symbol is a SimpleMarkerSymbol.
108
         * Thus, the drawInsideRectangle will always work. But
109
         * it can be changed with setSampleSymbol(IMakerSymbol).<br>
110
         * </p>
111
         * <p>
112
         * If <b>marker</b> is null, it does nothing
113
         * </p>
114
         */
115
        public void setSampleSymbol(IMarkerSymbol marker) {
116
                if (marker != null)
117
                        this.sampleSymbol = marker;
118
        }
119

    
120

    
121
        public double getRotation() {
122
                return rotation;
123
        }
124

    
125
        public void setRotation(double rotation) {
126
                this.rotation = rotation;
127
        }
128

    
129
        public double getXOffset() {
130
                return xOffset;
131
        }
132

    
133
        public void setXOffset(double offset) {
134
                xOffset = offset;
135
        }
136

    
137
        public double getXSeparation() {
138
                return xSeparation;
139
        }
140

    
141
        public void setXSeparation(double separation) {
142
                xSeparation = separation;
143
        }
144

    
145
        public double getYOffset() {
146
                return yOffset;
147
        }
148

    
149
        public void setYOffset(double offset) {
150
                yOffset = offset;
151
        }
152

    
153
        public double getYSeparation() {
154
                return ySeparation;
155
        }
156

    
157
        public void setYSeparation(double separation) {
158
                ySeparation = separation;
159
        }
160

    
161
        public void setFillStyle(int fillStyle) {
162
                this.fillStyle = fillStyle;
163
        }
164

    
165
        public int getFillStyle() {
166
                return fillStyle;
167
        }
168

    
169

    
170
}