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