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

History | View | Annotate | Download (7.67 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.Color;
27
import static javax.management.Query.gt;
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
        }
81

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

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

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

    
112
        public void setFillColor(Color color) {
113
                this.color = color;
114
        }
115

    
116
        public void setOutline(ILineSymbol outline) {
117
                this.outline = outline;
118
        }
119

    
120
        public Color getFillColor() {
121
                return color;
122
        }
123

    
124
        public ILineSymbol getOutline() {
125
                return outline;
126
        }
127

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

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

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

    
152
        public boolean hasFill() {
153
                return hasFill;
154
        }
155

    
156
        public void setHasFill(boolean hasFill) {
157
                this.hasFill = hasFill;
158
        }
159

    
160
        public boolean hasOutline() {
161
                return hasOutline;
162
        }
163

    
164
        public void setHasOutline(boolean hasOutline) {
165
                this.hasOutline = hasOutline;
166
        }
167

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

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

    
184
        public Color getColor() {
185
                return getFillColor();
186
        }
187

    
188
        public void setColor(Color color) {
189
                setFillColor(color);
190
        }
191

    
192
        public Object clone() throws CloneNotSupportedException {
193
                IFillSymbol copy = (IFillSymbol) super.clone();
194

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

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

    
214
        public void saveToState(PersistentState state) throws PersistenceException {
215
                // Save parent symbol properties
216
                super.saveToState(state);
217
                // Save own properties
218
                state.set(FIELD_COLOR, getColor());
219
                state.set(FIELD_HAS_FILL, hasFill());
220
                state.set(FIELD_HAS_OUTLINE, hasOutline());
221
                state.set(FIELD_OUTLINE, getOutline());
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(FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
229
                                DynStruct definition = manager.addDefinition(
230
                                                AbstractFillSymbol.class,
231
                                                FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME,
232
                                                FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
233
                                                null,
234
                                                null
235
                                );
236

    
237
                                // Extend the Symbol base definition
238
                                definition.extend(manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME));
239

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

    
252
        }
253

    
254
}