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 / fill / impl / AbstractFillSymbol.java @ 44545

History | View | Annotate | Download (7.82 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.fill.impl;
25

    
26
import java.awt.BasicStroke;
27
import java.awt.Color;
28

    
29
import org.gvsig.fmap.geom.Geometry;
30
import org.gvsig.fmap.geom.type.GeometryType;
31
import org.gvsig.fmap.mapcontext.MapContextLocator;
32
import org.gvsig.fmap.mapcontext.ViewPort;
33
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
34
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolPreferences;
35
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
36
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.AbstractSymbol;
37
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.CartographicSupportToolkit;
38
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
39
import org.gvsig.tools.ToolsLocator;
40
import org.gvsig.tools.dynobject.DynStruct;
41
import org.gvsig.tools.persistence.PersistenceManager;
42
import org.gvsig.tools.persistence.PersistentState;
43
import org.gvsig.tools.persistence.exception.PersistenceException;
44
import org.gvsig.tools.util.Callable;
45

    
46
/**
47
 * Abstract class that any FILL SYMBOL should extend
48
 *
49
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
50
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
51
 */
52
public abstract class AbstractFillSymbol extends AbstractSymbol implements
53
                IFillSymbol {
54

    
55
        public static final String FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME = "FillSymbol";
56

    
57
        private static final String FIELD_OUTLINE = "outline";
58

    
59
        private static final String FIELD_HAS_OUTLINE = "hasOutline";
60

    
61
        private static final String FIELD_HAS_FILL = "hasFill";
62

    
63
        private static final String FIELD_COLOR = "color";
64

    
65
        private boolean hasFill = true;
66
        private boolean hasOutline = true;
67
        private Color color;
68
        private ILineSymbol outline;
69

    
70
        public AbstractFillSymbol() {
71
        super();
72
        SymbolManager symbolManager = MapContextLocator.getSymbolManager();
73
        SymbolPreferences symbolPreferences =
74
            symbolManager.getSymbolPreferences();
75
        color = symbolPreferences.getDefaultSymbolFillColor();
76

    
77
        outline =
78
            (ILineSymbol) symbolManager.createSymbol(ILineSymbol.SYMBOL_NAME);
79
        outline.setColor(symbolPreferences.getDefaultSymbolColor());
80
        BasicStroke basicStroke = new BasicStroke(1,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_BEVEL );
81
        outline.getLineStyle().setStroke(basicStroke);
82
        }
83

    
84
    @Override
85
        public boolean isSuitableFor(Geometry geom) {
86
        GeometryType gt = geom.getGeometryType();
87
        if( gt.isTypeOf(Geometry.TYPES.SURFACE) ) {
88
            return true;
89
        }
90
        return false;
91
        }
92

    
93
    @Override
94
        public int getOnePointRgb() {
95
                int rgb = 0;
96
                if (outline != null) {
97
                        rgb = outline.getOnePointRgb();
98
                } else if (color != null) {
99
                        rgb = color.getRGB();
100
                }
101
                return rgb;
102
        }
103

    
104
        public void getPixExtentPlus(Geometry geom, float[] distances,
105
                        ViewPort viewPort, int dpi) {
106
                if (getOutline() != null) {
107
                        getOutline().getPixExtentPlus(geom, distances, viewPort, dpi);
108
                } else {
109
                        distances[0] = 0;
110
                        distances[1] = 0;
111
                }
112
        }
113

    
114
        public void setFillColor(Color color) {
115
                this.color = color;
116
        }
117

    
118
        public void setOutline(ILineSymbol outline) {
119
                this.outline = outline;
120
        }
121

    
122
        public Color getFillColor() {
123
                return color;
124
        }
125

    
126
        public ILineSymbol getOutline() {
127
                return outline;
128
        }
129

    
130
        public int getFillAlpha() {
131
                // TODO debatir si es correcto o no (por ejemplo cuando hablamos de
132
                // LineFillSymbol's
133
                if (color == null)
134
                        return 255;
135
                return color.getAlpha();
136
        }
137

    
138
        public void setCartographicSize(double cartographicSize, Geometry geom) {
139
                if (getOutline() != null) {
140
                        getOutline().setLineWidth(cartographicSize);
141
                }
142
        }
143

    
144
        public double toCartographicSize(ViewPort viewPort, double dpi,
145
                        Geometry geom) {
146
                if (getOutline() != null) {
147
                        double oldSize = getOutline().getLineWidth();
148
                        setCartographicSize(getCartographicSize(viewPort, dpi, geom), geom);
149
                        return oldSize;
150
                }
151
                return 0;
152
        }
153

    
154
        public boolean hasFill() {
155
                return hasFill;
156
        }
157

    
158
        public void setHasFill(boolean hasFill) {
159
                this.hasFill = hasFill;
160
        }
161

    
162
        public boolean hasOutline() {
163
                return hasOutline;
164
        }
165

    
166
        public void setHasOutline(boolean hasOutline) {
167
                this.hasOutline = hasOutline;
168
        }
169

    
170
        public double getCartographicSize(ViewPort viewPort, double dpi,
171
                        Geometry geom) {
172
                if (getOutline() != null) {
173
                        return CartographicSupportToolkit.getCartographicLength(this,
174
                                        getOutline().getLineWidth(), viewPort, dpi);
175
                }
176
                return 0;
177
        }
178

    
179
        public void setUnit(int unitIndex) {
180
                super.setUnit(unitIndex);
181
                if (getOutline() != null) {
182
                        getOutline().setUnit(unitIndex);
183
                }
184
        }
185

    
186
        public Color getColor() {
187
                return getFillColor();
188
        }
189

    
190
        public void setColor(Color color) {
191
                setFillColor(color);
192
        }
193

    
194
        public Object clone() throws CloneNotSupportedException {
195
                IFillSymbol copy = (IFillSymbol) super.clone();
196

    
197
                // Clone outline
198
                ILineSymbol outline = copy.getOutline();
199
                if (outline != null) {
200
                        copy.setOutline((ILineSymbol) outline.clone());
201
                }
202
                return copy;
203
        }
204

    
205
        public void loadFromState(PersistentState state)
206
                        throws PersistenceException {
207
                // Set parent symbol properties
208
                super.loadFromState(state);
209
                // Set own properties
210
                setColor((Color) state.get(FIELD_COLOR));
211
                setHasFill(state.getBoolean(FIELD_HAS_FILL));
212
                setHasOutline(state.getBoolean(FIELD_HAS_OUTLINE));
213
                setOutline((ILineSymbol) state.get(FIELD_OUTLINE));
214
        }
215

    
216
        public void saveToState(PersistentState state) throws PersistenceException {
217
                // Save parent symbol properties
218
                super.saveToState(state);
219
                // Save own properties
220
                state.set(FIELD_COLOR, getColor());
221
                state.set(FIELD_HAS_FILL, hasFill());
222
                state.set(FIELD_HAS_OUTLINE, hasOutline());
223
                state.set(FIELD_OUTLINE, getOutline());
224
        }
225

    
226
        public static class RegisterPersistence implements Callable {
227

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

    
239
                                // Extend the Symbol base definition
240
                                definition.extend(manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME));
241

    
242
                                // Color
243
                                definition.addDynFieldObject(FIELD_COLOR).setMandatory(false).setClassOfValue(Color.class);
244
                                // Has fill?
245
                                definition.addDynFieldBoolean(FIELD_HAS_FILL).setMandatory(true);
246
                                // Has outline?
247
                                definition.addDynFieldBoolean(FIELD_HAS_OUTLINE).setMandatory(true);
248
                                // Outline
249
                                definition.addDynFieldObject(FIELD_OUTLINE).setMandatory(false).setClassOfValue(ILineSymbol.class);
250
                        }
251
                        return Boolean.TRUE;
252
                }
253

    
254
        }
255

    
256
}