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