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 | 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 | 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 | 43326 | jjdelcerro | import org.gvsig.fmap.geom.primitive.Envelope; |
39 | 40435 | jjdelcerro | import org.gvsig.fmap.geom.primitive.GeneralPathX; |
40 | 44545 | omartinez | import org.gvsig.fmap.geom.primitive.Point; |
41 | 42439 | dmartinezizquierdo | import org.gvsig.fmap.mapcontext.MapContext; |
42 | 40435 | jjdelcerro | 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 | 44545 | omartinez | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.impl.SimpleMarkerSymbol; |
49 | 40435 | jjdelcerro | 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 | 43326 | jjdelcerro | * @author 2009- <a href="cordinyana@gvsig.org">César Ordiñana</a> - gvSIG team
|
64 | 40435 | jjdelcerro | */
|
65 | public class SimpleFillSymbol extends AbstractFillSymbol implements ISimpleFillSymbol { |
||
66 | |||
67 | 42352 | fdiaz | |
68 | 40435 | jjdelcerro | private static final String SIMPLE_FILL_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleFillSymbol"; |
69 | 42352 | fdiaz | |
70 | 40435 | jjdelcerro | 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 | 44545 | omartinez | private SimpleMarkerSymbol tempSymbol= new SimpleMarkerSymbol(); |
77 | 40435 | jjdelcerro | |
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 | 42439 | dmartinezizquierdo | } else {
|
86 | symbolForSelection.setColor(MapContext.getSelectionColor()); |
||
87 | 40435 | jjdelcerro | } |
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 | 43326 | jjdelcerro | |
95 | 43377 | jjdelcerro | if( true ) { |
96 | 43326 | jjdelcerro | // Esto deberia ser para optimiza el pintado de
|
97 | // geometrias grandes.
|
||
98 | 43407 | jjdelcerro | 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 | 44545 | omartinez | 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 | 43407 | jjdelcerro | } 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 | 43326 | jjdelcerro | } |
125 | |||
126 | 40435 | jjdelcerro | 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 | 41692 | jjdelcerro | g.fill(geom.getShape(at)); |
176 | 40435 | jjdelcerro | } |
177 | if (getOutline() != null && hasOutline()) { |
||
178 | getOutline().print(g, at, geom, properties); |
||
179 | } |
||
180 | } |
||
181 | 42352 | fdiaz | |
182 | |||
183 | 40435 | jjdelcerro | 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 | 42352 | fdiaz | |
195 | 40435 | jjdelcerro | 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 | 42352 | fdiaz | null,
|
225 | 40435 | jjdelcerro | 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 | 42352 | fdiaz | |
237 | 40435 | jjdelcerro | } |
238 | |||
239 | public static class RegisterSymbol implements Callable { |
||
240 | |||
241 | public Object call() throws Exception { |
||
242 | int[] shapeTypes; |
||
243 | SymbolManager manager = MapContextLocator.getSymbolManager(); |
||
244 | 42352 | fdiaz | |
245 | 40435 | jjdelcerro | shapeTypes = |
246 | new int[] { Geometry.TYPES.SURFACE, Geometry.TYPES.CIRCLE, |
||
247 | 42352 | fdiaz | Geometry.TYPES.ELLIPSE, Geometry.TYPES.MULTISURFACE, |
248 | Geometry.TYPES.ELLIPTICARC, Geometry.TYPES.FILLEDSPLINE, |
||
249 | 44509 | omartinez | Geometry.TYPES.MULTIPOLYGON, Geometry.TYPES.POLYGON |
250 | 42352 | fdiaz | }; |
251 | 40435 | jjdelcerro | manager.registerSymbol(IFillSymbol.SYMBOL_NAME, |
252 | shapeTypes, |
||
253 | SimpleFillSymbol.class); |
||
254 | 42352 | fdiaz | |
255 | 40435 | jjdelcerro | return Boolean.TRUE; |
256 | } |
||
257 | 42352 | fdiaz | |
258 | 40435 | jjdelcerro | } |
259 | |||
260 | } |