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