svn-gvsig-desktop / tags / v2_0_0_Build_2021 / extensions / org.gvsig.symbology / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / fill / impl / AbstractFillSymbol.java @ 34111
History | View | Annotate | Download (7.1 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl; |
23 |
|
24 |
import java.awt.Color; |
25 |
|
26 |
import org.gvsig.fmap.geom.Geometry; |
27 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
28 |
import org.gvsig.fmap.mapcontext.ViewPort; |
29 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol; |
30 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.AbstractSymbol; |
31 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.CartographicSupportToolkit; |
32 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol; |
33 |
import org.gvsig.tools.ToolsLocator; |
34 |
import org.gvsig.tools.dynobject.DynStruct; |
35 |
import org.gvsig.tools.persistence.PersistenceManager; |
36 |
import org.gvsig.tools.persistence.PersistentState; |
37 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
38 |
import org.gvsig.tools.util.Callable; |
39 |
|
40 |
/**
|
41 |
* Abstract class that any FILL SYMBOL should extend
|
42 |
*
|
43 |
* @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
|
44 |
* @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
45 |
*/
|
46 |
public abstract class AbstractFillSymbol extends AbstractSymbol implements |
47 |
IFillSymbol { |
48 |
|
49 |
public static final String FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME = "FillSymbol"; |
50 |
|
51 |
private static final String FIELD_OUTLINE = "outline"; |
52 |
|
53 |
private static final String FIELD_HAS_OUTLINE = "hasOutline"; |
54 |
|
55 |
private static final String FIELD_HAS_FILL = "hasFill"; |
56 |
|
57 |
private static final String FIELD_COLOR = "color"; |
58 |
|
59 |
private boolean hasFill = true; |
60 |
private boolean hasOutline = true; |
61 |
private Color color; |
62 |
private ILineSymbol outline;
|
63 |
|
64 |
public AbstractFillSymbol() {
|
65 |
super();
|
66 |
color = |
67 |
MapContextLocator.getSymbolManager() |
68 |
.getSymbolPreferences() |
69 |
.getDefaultSymbolFillColor(); |
70 |
|
71 |
outline = |
72 |
(ILineSymbol) MapContextLocator.getSymbolManager() |
73 |
.createSymbol(ILineSymbol.SYMBOL_NAME); |
74 |
|
75 |
} |
76 |
|
77 |
public boolean isSuitableFor(Geometry geom) { |
78 |
return geom.getType() == Geometry.TYPES.SURFACE;
|
79 |
} |
80 |
|
81 |
public int getOnePointRgb() { |
82 |
int rgb = 0; |
83 |
if (outline != null) { |
84 |
rgb = outline.getOnePointRgb(); |
85 |
} else if (color != null) { |
86 |
rgb = color.getRGB(); |
87 |
} |
88 |
return rgb;
|
89 |
} |
90 |
|
91 |
public void getPixExtentPlus(Geometry geom, float[] distances, |
92 |
ViewPort viewPort, int dpi) {
|
93 |
if (getOutline() != null) { |
94 |
getOutline().getPixExtentPlus(geom, distances, viewPort, dpi); |
95 |
} else {
|
96 |
distances[0] = 0; |
97 |
distances[1] = 0; |
98 |
} |
99 |
} |
100 |
|
101 |
public void setFillColor(Color color) { |
102 |
this.color = color;
|
103 |
} |
104 |
|
105 |
public void setOutline(ILineSymbol outline) { |
106 |
this.outline = outline;
|
107 |
} |
108 |
|
109 |
public Color getFillColor() { |
110 |
return color;
|
111 |
} |
112 |
|
113 |
public ILineSymbol getOutline() {
|
114 |
return outline;
|
115 |
} |
116 |
|
117 |
public int getFillAlpha() { |
118 |
// TODO debatir si es correcto o no (por ejemplo cuando hablamos de
|
119 |
// LineFillSymbol's
|
120 |
if (color == null) |
121 |
return 255; |
122 |
return color.getAlpha();
|
123 |
} |
124 |
|
125 |
public void setCartographicSize(double cartographicSize, Geometry geom) { |
126 |
if (getOutline() != null) { |
127 |
getOutline().setLineWidth(cartographicSize); |
128 |
} |
129 |
} |
130 |
|
131 |
public double toCartographicSize(ViewPort viewPort, double dpi, |
132 |
Geometry geom) { |
133 |
if (getOutline() != null) { |
134 |
double oldSize = getOutline().getLineWidth();
|
135 |
setCartographicSize(getCartographicSize(viewPort, dpi, geom), geom); |
136 |
return oldSize;
|
137 |
} |
138 |
return 0; |
139 |
} |
140 |
|
141 |
public boolean hasFill() { |
142 |
return hasFill;
|
143 |
} |
144 |
|
145 |
public void setHasFill(boolean hasFill) { |
146 |
this.hasFill = hasFill;
|
147 |
} |
148 |
|
149 |
public boolean hasOutline() { |
150 |
return hasOutline;
|
151 |
} |
152 |
|
153 |
public void setHasOutline(boolean hasOutline) { |
154 |
this.hasOutline = hasOutline;
|
155 |
} |
156 |
|
157 |
public double getCartographicSize(ViewPort viewPort, double dpi, |
158 |
Geometry geom) { |
159 |
if (getOutline() != null) { |
160 |
return CartographicSupportToolkit.getCartographicLength(this, |
161 |
getOutline().getLineWidth(), viewPort, dpi); |
162 |
} |
163 |
return 0; |
164 |
} |
165 |
|
166 |
public void setUnit(int unitIndex) { |
167 |
super.setUnit(unitIndex);
|
168 |
if (getOutline() != null) { |
169 |
getOutline().setUnit(unitIndex); |
170 |
} |
171 |
} |
172 |
|
173 |
public Color getColor() { |
174 |
return getFillColor();
|
175 |
} |
176 |
|
177 |
public void setColor(Color color) { |
178 |
setFillColor(color); |
179 |
} |
180 |
|
181 |
public Object clone() throws CloneNotSupportedException { |
182 |
IFillSymbol copy = (IFillSymbol) super.clone();
|
183 |
|
184 |
// Clone outline
|
185 |
ILineSymbol outline = copy.getOutline(); |
186 |
if (outline != null) { |
187 |
copy.setOutline((ILineSymbol) outline.clone()); |
188 |
} |
189 |
return copy;
|
190 |
} |
191 |
|
192 |
public void loadFromState(PersistentState state) |
193 |
throws PersistenceException {
|
194 |
// Set parent symbol properties
|
195 |
super.loadFromState(state);
|
196 |
// Set own properties
|
197 |
setColor((Color) state.get(FIELD_COLOR));
|
198 |
setHasFill(state.getBoolean(FIELD_HAS_FILL)); |
199 |
setHasOutline(state.getBoolean(FIELD_HAS_OUTLINE)); |
200 |
setOutline((ILineSymbol) state.get(FIELD_OUTLINE)); |
201 |
} |
202 |
|
203 |
public void saveToState(PersistentState state) throws PersistenceException { |
204 |
// Save parent symbol properties
|
205 |
super.saveToState(state);
|
206 |
// Save own properties
|
207 |
state.set(FIELD_COLOR, getColor()); |
208 |
state.set(FIELD_HAS_FILL, hasFill()); |
209 |
state.set(FIELD_HAS_OUTLINE, hasOutline()); |
210 |
state.set(FIELD_OUTLINE, getOutline()); |
211 |
} |
212 |
|
213 |
public static class RegisterPersistence implements Callable { |
214 |
|
215 |
public Object call() throws Exception { |
216 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
217 |
if( manager.getDefinition(FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) { |
218 |
DynStruct definition = manager.addDefinition( |
219 |
AbstractFillSymbol.class, |
220 |
FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME, |
221 |
FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
|
222 |
null,
|
223 |
null
|
224 |
); |
225 |
|
226 |
// Extend the Symbol base definition
|
227 |
definition.extend(manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME)); |
228 |
|
229 |
// Color
|
230 |
definition.addDynFieldObject(FIELD_COLOR).setMandatory(false).setClassOfValue(Color.class); |
231 |
// Has fill?
|
232 |
definition.addDynFieldBoolean(FIELD_HAS_FILL).setMandatory(true);
|
233 |
// Has outline?
|
234 |
definition.addDynFieldBoolean(FIELD_HAS_OUTLINE).setMandatory(true);
|
235 |
// Outline
|
236 |
definition.addDynFieldObject(FIELD_OUTLINE).setMandatory(false).setClassOfValue(ILineSymbol.class);
|
237 |
} |
238 |
return Boolean.TRUE; |
239 |
} |
240 |
|
241 |
} |
242 |
|
243 |
} |