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 / SimpleFillSymbol.java @ 44545
History | View | Annotate | Download (9.43 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 java.awt.Graphics2D; |
28 |
import java.awt.Rectangle; |
29 |
import java.awt.geom.AffineTransform; |
30 |
|
31 |
import org.gvsig.compat.print.PrintAttributes; |
32 |
import org.gvsig.fmap.dal.feature.Feature; |
33 |
import org.gvsig.fmap.geom.Geometry; |
34 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
35 |
import org.gvsig.fmap.geom.GeometryLocator; |
36 |
import org.gvsig.fmap.geom.GeometryManager; |
37 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
38 |
import org.gvsig.fmap.geom.primitive.Envelope; |
39 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
40 |
import org.gvsig.fmap.geom.primitive.Point; |
41 |
import org.gvsig.fmap.mapcontext.MapContext; |
42 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
43 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
44 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
45 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
46 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol; |
47 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.ISimpleFillSymbol; |
48 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.impl.SimpleMarkerSymbol; |
49 |
import org.gvsig.tools.ToolsLocator; |
50 |
import org.gvsig.tools.dynobject.DynStruct; |
51 |
import org.gvsig.tools.persistence.PersistenceManager; |
52 |
import org.gvsig.tools.persistence.PersistentState; |
53 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
54 |
import org.gvsig.tools.task.Cancellable; |
55 |
import org.gvsig.tools.util.Callable; |
56 |
import org.slf4j.Logger; |
57 |
import org.slf4j.LoggerFactory; |
58 |
|
59 |
|
60 |
/**
|
61 |
* Basic fill symbol. It will allow to paint a shape with its filling color (and transparency) and the outline.
|
62 |
* @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
|
63 |
* @author 2009- <a href="cordinyana@gvsig.org">César Ordiñana</a> - gvSIG team
|
64 |
*/
|
65 |
public class SimpleFillSymbol extends AbstractFillSymbol implements ISimpleFillSymbol { |
66 |
|
67 |
|
68 |
private static final String SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleFillSymbol"; |
69 |
|
70 |
private static final Logger LOG = LoggerFactory.getLogger(SimpleFillSymbol.class); |
71 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
72 |
|
73 |
private static final String FIELD_SYMBOL_FOR_SELECTION = "symbolForSelection"; |
74 |
|
75 |
private SimpleFillSymbol symbolForSelection;
|
76 |
private SimpleMarkerSymbol tempSymbol= new SimpleMarkerSymbol(); |
77 |
|
78 |
public ISymbol getSymbolForSelection() {
|
79 |
if (symbolForSelection == null) { |
80 |
SimpleFillSymbol selectionSymbol = (SimpleFillSymbol) cloneForSelection(); |
81 |
if (selectionSymbol != null) { |
82 |
selectionSymbol.setHasFill(true);
|
83 |
setSymbolForSelection(selectionSymbol); |
84 |
} |
85 |
} else {
|
86 |
symbolForSelection.setColor(MapContext.getSelectionColor()); |
87 |
} |
88 |
return symbolForSelection;
|
89 |
} |
90 |
|
91 |
public void draw(Graphics2D g, AffineTransform affineTransform, |
92 |
Geometry geom, Feature feature, Cancellable cancel) { |
93 |
Color c = getFillColor();
|
94 |
|
95 |
if( true ) { |
96 |
// Esto deberia ser para optimiza el pintado de
|
97 |
// geometrias grandes.
|
98 |
try {
|
99 |
Geometry env = geom.getEnvelope().getGeometry(); |
100 |
env.transform(affineTransform); |
101 |
Envelope env2 = env.getEnvelope(); |
102 |
if( env2.getLength(0)<1.5 && env2.getLength(1)<1.5 ) { |
103 |
Point upperCorner = env2.getUpperCorner();
|
104 |
int y = (int) upperCorner.getY(); |
105 |
int x = (int) upperCorner.getX(); |
106 |
if (c != null && hasFill()) { |
107 |
g.setColor(c); |
108 |
g.drawLine(x, y, x, y); |
109 |
} |
110 |
if (getOutline() != null && hasOutline()) { |
111 |
g.setColor(getOutline().getColor()); |
112 |
if (getOutline().getColor()!=null) { |
113 |
|
114 |
} |
115 |
g.drawLine(x, y, x, y); |
116 |
} |
117 |
return;
|
118 |
} |
119 |
} catch(Exception ex) { |
120 |
LOG.warn("Error optimizing the drawing of the geometry. Continues with normal drawing.", ex);
|
121 |
// Do nothing, continue with the draw of the original geometry
|
122 |
} |
123 |
|
124 |
} |
125 |
|
126 |
if (c!=null && hasFill()) { |
127 |
g.setColor(c); |
128 |
g.fill(geom.getShape(affineTransform)); |
129 |
} |
130 |
if (getOutline() != null && hasOutline()) { |
131 |
getOutline().draw(g, affineTransform, geom, feature, cancel); |
132 |
} |
133 |
} |
134 |
|
135 |
|
136 |
public int getSymbolType() { |
137 |
return Geometry.TYPES.SURFACE;
|
138 |
} |
139 |
|
140 |
public void drawInsideRectangle(Graphics2D g, |
141 |
AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException { |
142 |
Rectangle rect = new Rectangle(r.x, r.y, r.width, r.height); |
143 |
rect.setFrame(((int) rect.getMinX())+1, ((int) rect.getMinY())+1, ((int) rect.getWidth())-2, ((int) rect.getHeight())-2); |
144 |
Geometry geom; |
145 |
try {
|
146 |
geom = geomManager.createSurface(new GeneralPathX(rect.getPathIterator(null)), SUBTYPES.GEOM2D); |
147 |
} catch (CreateGeometryException e) {
|
148 |
LOG.error("Creating a surface", e);
|
149 |
throw new SymbolDrawingException(getSymbolType()); |
150 |
} |
151 |
|
152 |
Color c = getFillColor();
|
153 |
if (c != null && hasFill()) { |
154 |
g.setColor(c); |
155 |
g.fillRect(rect.x, rect.y, rect.width, rect.height); |
156 |
} |
157 |
|
158 |
if (getOutline() != null && hasOutline()) { |
159 |
if (properties==null) |
160 |
getOutline().draw(g, scaleInstance, geom, null, null); |
161 |
else
|
162 |
print(g, new AffineTransform(), geom, properties); |
163 |
} |
164 |
} |
165 |
|
166 |
public String getClassName() { |
167 |
return getClass().getName();
|
168 |
} |
169 |
|
170 |
|
171 |
public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) { |
172 |
Color c = getFillColor();
|
173 |
if (c!=null && hasFill()) { |
174 |
g.setColor(c); |
175 |
g.fill(geom.getShape(at)); |
176 |
} |
177 |
if (getOutline() != null && hasOutline()) { |
178 |
getOutline().print(g, at, geom, properties); |
179 |
} |
180 |
} |
181 |
|
182 |
|
183 |
public Object clone() throws CloneNotSupportedException { |
184 |
SimpleFillSymbol copy = (SimpleFillSymbol) super.clone();
|
185 |
|
186 |
// Clone selection
|
187 |
if (symbolForSelection != null) { |
188 |
copy.symbolForSelection = (SimpleFillSymbol) symbolForSelection |
189 |
.clone(); |
190 |
} |
191 |
|
192 |
return copy;
|
193 |
} |
194 |
|
195 |
private void setSymbolForSelection(SimpleFillSymbol symbolForSelection) { |
196 |
this.symbolForSelection = symbolForSelection;
|
197 |
} |
198 |
|
199 |
public void loadFromState(PersistentState state) |
200 |
throws PersistenceException {
|
201 |
// Set parent fill symbol properties
|
202 |
super.loadFromState(state);
|
203 |
setSymbolForSelection((SimpleFillSymbol)state.get(FIELD_SYMBOL_FOR_SELECTION)); |
204 |
} |
205 |
|
206 |
public void saveToState(PersistentState state) throws PersistenceException { |
207 |
// Save parent fill symbol properties
|
208 |
super.saveToState(state);
|
209 |
|
210 |
// Don't use the getSymbolForSelection method, as it will create it
|
211 |
// if it does not exist, and persistence will enter an infinite loop
|
212 |
state.set(FIELD_SYMBOL_FOR_SELECTION, symbolForSelection); |
213 |
} |
214 |
|
215 |
public static class RegisterPersistence implements Callable { |
216 |
|
217 |
public Object call() throws Exception { |
218 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
219 |
if( manager.getDefinition(SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) { |
220 |
DynStruct definition = manager.addDefinition( |
221 |
SimpleFillSymbol.class, |
222 |
SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME, |
223 |
SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
|
224 |
null,
|
225 |
null
|
226 |
); |
227 |
|
228 |
// Extend the FillSymbol base definition
|
229 |
definition.extend(manager.getDefinition(FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME)); |
230 |
|
231 |
// Selection Symbol
|
232 |
definition.addDynFieldObject(FIELD_SYMBOL_FOR_SELECTION).setClassOfValue(SimpleFillSymbol.class).setMandatory(false);
|
233 |
} |
234 |
return Boolean.TRUE; |
235 |
} |
236 |
|
237 |
} |
238 |
|
239 |
public static class RegisterSymbol implements Callable { |
240 |
|
241 |
public Object call() throws Exception { |
242 |
int[] shapeTypes; |
243 |
SymbolManager manager = MapContextLocator.getSymbolManager(); |
244 |
|
245 |
shapeTypes = |
246 |
new int[] { Geometry.TYPES.SURFACE, Geometry.TYPES.CIRCLE, |
247 |
Geometry.TYPES.ELLIPSE, Geometry.TYPES.MULTISURFACE, |
248 |
Geometry.TYPES.ELLIPTICARC, Geometry.TYPES.FILLEDSPLINE, |
249 |
Geometry.TYPES.MULTIPOLYGON, Geometry.TYPES.POLYGON |
250 |
}; |
251 |
manager.registerSymbol(IFillSymbol.SYMBOL_NAME, |
252 |
shapeTypes, |
253 |
SimpleFillSymbol.class); |
254 |
|
255 |
return Boolean.TRUE; |
256 |
} |
257 |
|
258 |
} |
259 |
|
260 |
} |