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 @ 47476

History | View | Annotate | Download (8.57 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
import org.gvsig.fmap.mapcontext.MapContextLocator;
33
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
34
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol;
35
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
36
import org.gvsig.symbology.SymbologyLocator;
37
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
38
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.MarkerFillSymbol;
39
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
40
import org.gvsig.tools.ToolsLocator;
41
import org.gvsig.tools.dynobject.DynStruct;
42
import org.gvsig.tools.persistence.PersistenceManager;
43
import org.gvsig.tools.persistence.PersistentState;
44
import org.gvsig.tools.persistence.exception.PersistenceException;
45
import org.gvsig.tools.util.Callable;
46

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

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

    
66
        @Override
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;
72
                BufferedImage bi;
73
                bi= new BufferedImage(s, s, BufferedImage.TYPE_INT_ARGB);
74
                Graphics2D gAux = bi.createGraphics();
75
                try {
76
                        sampleSymbol.drawInsideRectangle(gAux, gAux.getTransform(), rProv);
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);
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
        @Override
103
        public boolean isSuitableFor(ISymbol symbol) {
104
                return (symbol instanceof IFillSymbol);
105
        }
106

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

    
111
        @Override
112
        public void drawOutline(Graphics2D g, Rectangle r) {
113
                drawInsideRectangle(g, r);
114
        }
115

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

    
133

    
134
        @Override
135
        public double getRotation() {
136
                return rotation;
137
        }
138

    
139
        @Override
140
        public void setRotation(double rotation) {
141
                this.rotation = rotation;
142
        }
143

    
144
        @Override
145
        public double getXOffset() {
146
                return xOffset;
147
        }
148

    
149
        @Override
150
        public void setXOffset(double offset) {
151
                xOffset = offset;
152
        }
153

    
154
        @Override
155
        public double getXSeparation() {
156
                return xSeparation;
157
        }
158

    
159
        @Override
160
        public void setXSeparation(double separation) {
161
                xSeparation = separation;
162
        }
163

    
164
        @Override
165
        public double getYOffset() {
166
                return yOffset;
167
        }
168

    
169
        @Override
170
        public void setYOffset(double offset) {
171
                yOffset = offset;
172
        }
173

    
174
        @Override
175
        public double getYSeparation() {
176
                return ySeparation;
177
        }
178

    
179
        @Override
180
        public void setYSeparation(double separation) {
181
                ySeparation = separation;
182
        }
183

    
184
        @Override
185
        public void setFillStyle(int fillStyle) {
186
                this.fillStyle = fillStyle;
187
        }
188

    
189
        @Override
190
        public int getFillStyle() {
191
                return fillStyle;
192
        }
193

    
194
        @Override
195
        public Object clone() throws CloneNotSupportedException {
196
                SimpleMarkerFillPropertiesStyle copy = (SimpleMarkerFillPropertiesStyle) super.clone();
197
                if (sampleSymbol != null) {
198
                        copy.sampleSymbol = (IMarkerSymbol) sampleSymbol.clone();
199
                }
200
                return copy;
201
        }
202

    
203
        @Override
204
        public void loadFromState(PersistentState state)
205
        throws PersistenceException {
206
                // Set parent style properties
207
                super.loadFromState(state);
208

    
209
                // Set own properties
210
                setRotation(state.getDouble(FIELD_ROTATION));
211
                setXOffset(state.getDouble(FIELD_XOFFSET));
212
                setYOffset(state.getDouble(FIELD_YOFFSET));
213
                setXSeparation(state.getDouble(FIELD_XSEPARATION));
214
                setYSeparation(state.getDouble(FIELD_YSEPARATION));
215
                setFillStyle(state.getInt(FIELD_FILL_STYLE));
216
                
217
//                please, avoid initialize "sampleSymbol" field. It
218
//                is already controlled by this style's owner.
219

    
220
        }
221

    
222
        @Override
223
        public void saveToState(PersistentState state) throws PersistenceException {
224
                // Save parent fill symbol properties
225
                super.saveToState(state);
226

    
227
                // Save own properties
228
                state.set(FIELD_ROTATION, getRotation());
229
                state.set(FIELD_XOFFSET, getXOffset());
230
                state.set(FIELD_YOFFSET, getYOffset());
231
                state.set(FIELD_XSEPARATION, getXSeparation());
232
                state.set(FIELD_YSEPARATION, getYSeparation());
233
                state.set(FIELD_FILL_STYLE, getFillStyle());
234

    
235
                /* please, avoid persist "sampleSymbol" field.
236
                 it is always initialized by this style's owner
237
                 when needs it. */
238
//                state.set(FIELD_SAMPLE_SYMBOL, this.sampleSymbol);
239
                
240
        }
241

    
242
        public static class RegisterPersistence implements Callable {
243

    
244
                @Override
245
                public Object call() throws Exception {
246
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
247
                        if( manager.getDefinition(SIMPLE_MARKER_FILL_STYLE_PERSISTENCE_DEFINITION_NAME)==null ) {
248
                                DynStruct definition = manager.addDefinition(
249
                                                SimpleMarkerFillPropertiesStyle.class,
250
                                                SIMPLE_MARKER_FILL_STYLE_PERSISTENCE_DEFINITION_NAME,
251
                                                SIMPLE_MARKER_FILL_STYLE_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
252
                                                null, 
253
                                                null
254
                                );
255

    
256
                                // Extend the Style base definition
257
                                definition.extend(manager.getDefinition(STYLE_PERSISTENCE_DEFINITION_NAME));
258

    
259
                                // Sample Symbol
260
                                definition.addDynFieldObject(FIELD_SAMPLE_SYMBOL)
261
                                .setClassOfValue(IMarkerSymbol.class);
262

    
263
                                // Rotation
264
                                definition.addDynFieldDouble(FIELD_ROTATION).setMandatory(true);
265

    
266
                                // offset
267
                                definition.addDynFieldDouble(FIELD_XOFFSET).setMandatory(true);
268
                                definition.addDynFieldDouble(FIELD_YOFFSET).setMandatory(true);
269

    
270
                                // Separation
271
                                definition.addDynFieldDouble(FIELD_XSEPARATION).setMandatory(true);
272
                                definition.addDynFieldDouble(FIELD_YSEPARATION).setMandatory(true);
273

    
274
                                // Fill Style
275
                                definition.addDynFieldInt(FIELD_FILL_STYLE).setMandatory(true);
276
                        }
277
                        return Boolean.TRUE;
278
                }
279

    
280
        }
281

    
282
}