Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.lib / org.gvsig.symbology.lib.impl / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / style / SimpleMarkerFillPropertiesStyle.java @ 40560

History | View | Annotate | Download (8.23 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style;
25

    
26
import java.awt.Graphics2D;
27
import java.awt.Paint;
28
import java.awt.Rectangle;
29
import java.awt.RenderingHints;
30
import java.awt.TexturePaint;
31
import java.awt.image.BufferedImage;
32

    
33
import org.gvsig.fmap.mapcontext.MapContextLocator;
34
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
35
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol;
36
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
37
import org.gvsig.symbology.SymbologyLocator;
38
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
39
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.MarkerFillSymbol;
40
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
41
import org.gvsig.tools.ToolsLocator;
42
import org.gvsig.tools.dynobject.DynStruct;
43
import org.gvsig.tools.persistence.PersistenceManager;
44
import org.gvsig.tools.persistence.PersistentState;
45
import org.gvsig.tools.persistence.exception.PersistenceException;
46
import org.gvsig.tools.util.Callable;
47

    
48
public class SimpleMarkerFillPropertiesStyle extends AbstractStyle implements IMarkerFillPropertiesStyle {
49
        public static final String SIMPLE_MARKER_FILL_STYLE_PERSISTENCE_DEFINITION_NAME = "SimpleMarkerFillPropertiesStyle";
50

    
51
        private static final String FIELD_SAMPLE_SYMBOL = "sampleSymbol";
52
        private static final String FIELD_ROTATION = "rotation";
53
        private static final String FIELD_XOFFSET = "xOffset";
54
        private static final String FIELD_YOFFSET = "yOffset";
55
        private static final String FIELD_XSEPARATION = "xSeparation";
56
        private static final String FIELD_YSEPARATION = "ySeparation";
57
        private static final String FIELD_FILL_STYLE = "fillStyle";
58
        
59
        private IMarkerSymbol sampleSymbol = SymbologyLocator.getSymbologyManager().createSimpleMarkerSymbol();
60
        private double rotation = 0;
61
        private double xOffset = 0;
62
        private double yOffset = 0;
63
        private double xSeparation = 20;
64
        private double ySeparation = 20;
65
        private int fillStyle = MarkerFillSymbol.DefaultFillStyle;
66

    
67
        public void drawInsideRectangle(Graphics2D g, Rectangle r) {
68
                int s = (int) sampleSymbol.getSize();
69
                Rectangle rProv = new Rectangle();
70
                rProv.setFrame(0, 0, s, s);
71
                Paint resulPatternFill = null;
72
                BufferedImage bi = null;
73
                bi= new BufferedImage(s, s, BufferedImage.TYPE_INT_ARGB);
74
                Graphics2D gAux = bi.createGraphics();
75
                try {
76
                        sampleSymbol.drawInsideRectangle(gAux, gAux.getTransform(), rProv, null);
77
                } catch (SymbolDrawingException e) {
78
                        if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
79
                                try {
80
                                        IWarningSymbol warning =
81
                                                        (IWarningSymbol) MapContextLocator.getSymbolManager()
82
                                                                        .getWarningSymbol(
83
                                                                                        SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
84
                                                                                        "",
85
                                                                                        SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS);
86
                                        warning.drawInsideRectangle(gAux, gAux.getTransform(), rProv, null);
87
                                } catch (SymbolDrawingException e1) {
88
                                        // IMPOSSIBLE TO REACH THIS
89
                                }
90
                        }
91

    
92
                }
93
                resulPatternFill = new TexturePaint(bi,rProv);
94
                g.setColor(null);
95
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
96
                        RenderingHints.VALUE_ANTIALIAS_ON);
97
                g.setPaint(resulPatternFill);
98
                g.fill(r);
99

    
100
        }
101

    
102
        public boolean isSuitableFor(ISymbol symbol) {
103
                return (symbol instanceof IFillSymbol);
104
        }
105

    
106
        public String getClassName() {
107
                return getClass().getName();
108
        }
109

    
110
        public void drawOutline(Graphics2D g, Rectangle r) {
111
                drawInsideRectangle(g, r);
112
        }
113

    
114
        /**
115
         * <p>
116
         * Define an utility symbol to show up a thumbnail
117
         * by default, this symbol is a SimpleMarkerSymbol.
118
         * Thus, the drawInsideRectangle will always work. But
119
         * it can be changed with setSampleSymbol(IMakerSymbol).<br>
120
         * </p>
121
         * <p>
122
         * If <b>marker</b> is null, it does nothing
123
         * </p>
124
         */
125
        public void setSampleSymbol(IMarkerSymbol marker) {
126
                if (marker != null)
127
                        this.sampleSymbol = marker;
128
        }
129

    
130

    
131
        public double getRotation() {
132
                return rotation;
133
        }
134

    
135
        public void setRotation(double rotation) {
136
                this.rotation = rotation;
137
        }
138

    
139
        public double getXOffset() {
140
                return xOffset;
141
        }
142

    
143
        public void setXOffset(double offset) {
144
                xOffset = offset;
145
        }
146

    
147
        public double getXSeparation() {
148
                return xSeparation;
149
        }
150

    
151
        public void setXSeparation(double separation) {
152
                xSeparation = separation;
153
        }
154

    
155
        public double getYOffset() {
156
                return yOffset;
157
        }
158

    
159
        public void setYOffset(double offset) {
160
                yOffset = offset;
161
        }
162

    
163
        public double getYSeparation() {
164
                return ySeparation;
165
        }
166

    
167
        public void setYSeparation(double separation) {
168
                ySeparation = separation;
169
        }
170

    
171
        public void setFillStyle(int fillStyle) {
172
                this.fillStyle = fillStyle;
173
        }
174

    
175
        public int getFillStyle() {
176
                return fillStyle;
177
        }
178

    
179
        public Object clone() throws CloneNotSupportedException {
180
                SimpleMarkerFillPropertiesStyle copy = (SimpleMarkerFillPropertiesStyle) super.clone();
181
                if (sampleSymbol != null) {
182
                        copy.sampleSymbol = (IMarkerSymbol) sampleSymbol.clone();
183
                }
184
                return copy;
185
        }
186

    
187
        public void loadFromState(PersistentState state)
188
        throws PersistenceException {
189
                // Set parent style properties
190
                super.loadFromState(state);
191

    
192
                // Set own properties
193
                setRotation(state.getDouble(FIELD_ROTATION));
194
                setXOffset(state.getDouble(FIELD_XOFFSET));
195
                setYOffset(state.getDouble(FIELD_YOFFSET));
196
                setXSeparation(state.getDouble(FIELD_XSEPARATION));
197
                setYSeparation(state.getDouble(FIELD_YSEPARATION));
198
                setFillStyle(state.getInt(FIELD_FILL_STYLE));
199
                
200
//                please, avoid initialize "sampleSymbol" field. It
201
//                is already controlled by this style's owner.
202

    
203
        }
204

    
205
        public void saveToState(PersistentState state) throws PersistenceException {
206
                // Save parent fill symbol properties
207
                super.saveToState(state);
208

    
209
                // Save own properties
210
                state.set(FIELD_ROTATION, getRotation());
211
                state.set(FIELD_XOFFSET, getXOffset());
212
                state.set(FIELD_YOFFSET, getYOffset());
213
                state.set(FIELD_XSEPARATION, getXSeparation());
214
                state.set(FIELD_YSEPARATION, getYSeparation());
215
                state.set(FIELD_FILL_STYLE, getFillStyle());
216

    
217
                /* please, avoid persist "sampleSymbol" field.
218
                 it is always initialized by this style's owner
219
                 when needs it. */
220
//                state.set(FIELD_SAMPLE_SYMBOL, this.sampleSymbol);
221
                
222
        }
223

    
224
        public static class RegisterPersistence implements Callable {
225

    
226
                public Object call() throws Exception {
227
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
228
                        if( manager.getDefinition(SIMPLE_MARKER_FILL_STYLE_PERSISTENCE_DEFINITION_NAME)==null ) {
229
                                DynStruct definition = manager.addDefinition(
230
                                                SimpleMarkerFillPropertiesStyle.class,
231
                                                SIMPLE_MARKER_FILL_STYLE_PERSISTENCE_DEFINITION_NAME,
232
                                                SIMPLE_MARKER_FILL_STYLE_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
233
                                                null, 
234
                                                null
235
                                );
236

    
237
                                // Extend the Style base definition
238
                                definition.extend(manager.getDefinition(STYLE_PERSISTENCE_DEFINITION_NAME));
239

    
240
                                // Sample Symbol
241
                                definition.addDynFieldObject(FIELD_SAMPLE_SYMBOL)
242
                                .setClassOfValue(IMarkerSymbol.class);
243

    
244
                                // Rotation
245
                                definition.addDynFieldDouble(FIELD_ROTATION).setMandatory(true);
246

    
247
                                // offset
248
                                definition.addDynFieldDouble(FIELD_XOFFSET).setMandatory(true);
249
                                definition.addDynFieldDouble(FIELD_YOFFSET).setMandatory(true);
250

    
251
                                // Separation
252
                                definition.addDynFieldDouble(FIELD_XSEPARATION).setMandatory(true);
253
                                definition.addDynFieldDouble(FIELD_YSEPARATION).setMandatory(true);
254

    
255
                                // Fill Style
256
                                definition.addDynFieldInt(FIELD_FILL_STYLE).setMandatory(true);
257
                        }
258
                        return Boolean.TRUE;
259
                }
260

    
261
        }
262

    
263
}