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 / marker / impl / AbstractMarkerSymbol.java @ 42811
History | View | Annotate | Download (9.13 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.marker.impl; |
25 |
|
26 |
import java.awt.Color; |
27 |
import java.awt.Graphics2D; |
28 |
import java.awt.Point; |
29 |
import java.awt.Rectangle; |
30 |
import java.awt.geom.AffineTransform; |
31 |
import java.awt.geom.Point2D; |
32 |
|
33 |
import org.gvsig.compat.print.PrintAttributes; |
34 |
import org.gvsig.fmap.geom.Geometry; |
35 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
36 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
37 |
import org.gvsig.fmap.geom.GeometryLocator; |
38 |
import org.gvsig.fmap.geom.GeometryManager; |
39 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
40 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
41 |
import org.gvsig.fmap.mapcontext.ViewPort; |
42 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
43 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.AbstractSymbol; |
44 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.CartographicSupportToolkit; |
45 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol; |
46 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.IMask; |
47 |
import org.gvsig.tools.ToolsLocator; |
48 |
import org.gvsig.tools.dynobject.DynStruct; |
49 |
import org.gvsig.tools.persistence.PersistenceManager; |
50 |
import org.gvsig.tools.persistence.PersistentState; |
51 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
52 |
import org.gvsig.tools.util.Callable; |
53 |
|
54 |
/**
|
55 |
* Abstract class that any MARKER SYMBOL should extend.
|
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 abstract class AbstractMarkerSymbol extends AbstractSymbol implements IMarkerSymbol { |
60 |
|
61 |
public static final String MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME = "MarkerSymbol"; |
62 |
|
63 |
private static final String FIELD_COLOR = "color"; |
64 |
|
65 |
private static final String FIELD_ROTATION = "rotation"; |
66 |
|
67 |
private static final String FIELD_OFFSET = "offset"; |
68 |
|
69 |
private static final String FIELD_SIZE = "size"; |
70 |
|
71 |
private static final String FIELD_MASK = "mask"; |
72 |
|
73 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
74 |
private Color color; |
75 |
private double rotation; |
76 |
private Point2D offset = new Point2D.Double(); |
77 |
private double size = 4d; |
78 |
private IMask mask;
|
79 |
|
80 |
public AbstractMarkerSymbol() {
|
81 |
super();
|
82 |
color = |
83 |
MapContextLocator.getSymbolManager() |
84 |
.getSymbolPreferences() |
85 |
.getDefaultSymbolFillColor(); |
86 |
} |
87 |
|
88 |
public final int getSymbolType() { |
89 |
return Geometry.TYPES.POINT;
|
90 |
} |
91 |
|
92 |
public double getRotation() { |
93 |
return rotation;
|
94 |
} |
95 |
|
96 |
public void setRotation(double r) { |
97 |
this.rotation = r;
|
98 |
} |
99 |
|
100 |
public Point2D getOffset() { |
101 |
if (offset == null) { |
102 |
offset = new Point(); |
103 |
} |
104 |
return offset;
|
105 |
} |
106 |
|
107 |
public void setOffset(Point2D offset) { |
108 |
this.offset = offset;
|
109 |
} |
110 |
|
111 |
public boolean isSuitableFor(Geometry geom) { |
112 |
return geom.getType() == Geometry.TYPES.POINT;
|
113 |
} |
114 |
|
115 |
public int getOnePointRgb() { |
116 |
return color.getRGB();
|
117 |
} |
118 |
|
119 |
public double getSize() { |
120 |
return size;
|
121 |
} |
122 |
|
123 |
public void setSize(double size) { |
124 |
this.size = size;
|
125 |
} |
126 |
|
127 |
@Override
|
128 |
public Color getColor() { |
129 |
return color;
|
130 |
} |
131 |
|
132 |
public void setColor(Color color) { |
133 |
this.color = color;
|
134 |
} |
135 |
|
136 |
public void setAlpha(int outlineAlpha) { |
137 |
Color color = getColor();
|
138 |
setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), |
139 |
outlineAlpha)); |
140 |
} |
141 |
|
142 |
public void getPixExtentPlus(Geometry geom, float[] distances, ViewPort viewPort, int dpi) { |
143 |
float cs = (float) getCartographicSize(viewPort, dpi, geom); |
144 |
distances[0] = cs;
|
145 |
distances[1] = cs;
|
146 |
} |
147 |
|
148 |
|
149 |
public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) { |
150 |
double originalSize = getSize();
|
151 |
double size=originalSize;
|
152 |
// scale it to size
|
153 |
int pq = properties.getPrintQuality();
|
154 |
if (pq == PrintAttributes.PRINT_QUALITY_NORMAL){
|
155 |
size *= (double) 300/72; |
156 |
}else if (pq == PrintAttributes.PRINT_QUALITY_HIGH){ |
157 |
size *= (double) 600/72; |
158 |
}else if (pq == PrintAttributes.PRINT_QUALITY_DRAFT){ |
159 |
// size *= 72/72; // (which is the same than doing nothing)
|
160 |
} |
161 |
// setSize(size);
|
162 |
draw(g,at,geom, null, null); |
163 |
// setSize(originalSize);
|
164 |
} |
165 |
|
166 |
public final IMask getMask() { |
167 |
return mask;
|
168 |
} |
169 |
|
170 |
public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException { |
171 |
try {
|
172 |
if (properties==null) |
173 |
draw(g, scaleInstance, geomManager.createPoint(r.getCenterX(), r.getCenterY(), SUBTYPES.GEOM2D), null, null); |
174 |
else{
|
175 |
double originalSize = getSize();
|
176 |
double size=originalSize;
|
177 |
int pq = properties.getPrintQuality();
|
178 |
if (pq == PrintAttributes.PRINT_QUALITY_NORMAL){
|
179 |
size *= (double) 300/72; |
180 |
}else if (pq == PrintAttributes.PRINT_QUALITY_HIGH){ |
181 |
size *= (double) 600/72; |
182 |
}else if (pq == PrintAttributes.PRINT_QUALITY_DRAFT){ |
183 |
// d *= 72/72; // (which is the same than doing nothing)
|
184 |
} |
185 |
setSize(size); |
186 |
print(g, scaleInstance, geomManager.createPoint(r.getCenterX(), r.getCenterY(), SUBTYPES.GEOM2D), properties); |
187 |
setSize(originalSize); |
188 |
} |
189 |
|
190 |
|
191 |
|
192 |
} catch (CreateGeometryException e) {
|
193 |
throw new SymbolDrawingException(TYPES.POINT); |
194 |
} |
195 |
} |
196 |
|
197 |
public final void setMask(IMask mask) { |
198 |
this.mask = mask;
|
199 |
} |
200 |
|
201 |
|
202 |
|
203 |
public void setCartographicSize(double cartographicSize, Geometry geom) { |
204 |
setSize(cartographicSize); |
205 |
} |
206 |
|
207 |
public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) { |
208 |
double oldSize = getSize();
|
209 |
setCartographicSize(getCartographicSize( |
210 |
viewPort, |
211 |
dpi, |
212 |
geom), |
213 |
geom); |
214 |
return oldSize;
|
215 |
} |
216 |
|
217 |
public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) { |
218 |
return CartographicSupportToolkit.
|
219 |
getCartographicLength(this,
|
220 |
getSize(), |
221 |
viewPort, |
222 |
dpi); |
223 |
} |
224 |
|
225 |
public Object clone() throws CloneNotSupportedException { |
226 |
AbstractMarkerSymbol copy = (AbstractMarkerSymbol) super.clone();
|
227 |
|
228 |
// Clone the offset
|
229 |
if (offset != null) { |
230 |
copy.offset = (Point2D) offset.clone();
|
231 |
} |
232 |
|
233 |
// clone the mask
|
234 |
if (mask != null) { |
235 |
copy.mask = (IMask) mask.clone(); |
236 |
} |
237 |
return copy;
|
238 |
} |
239 |
|
240 |
public void loadFromState(PersistentState state) |
241 |
throws PersistenceException {
|
242 |
// Set parent symbol properties
|
243 |
super.loadFromState(state);
|
244 |
// Set own properties
|
245 |
setColor((Color) state.get(FIELD_COLOR));
|
246 |
setMask((IMask) state.get(FIELD_MASK)); |
247 |
setOffset((Point2D) state.get(FIELD_OFFSET));
|
248 |
setRotation(state.getDouble(FIELD_ROTATION)); |
249 |
setSize(state.getDouble(FIELD_SIZE)); |
250 |
} |
251 |
|
252 |
public void saveToState(PersistentState state) throws PersistenceException { |
253 |
// Save parent symbol properties
|
254 |
super.saveToState(state);
|
255 |
// Save own properties
|
256 |
state.set(FIELD_COLOR, getColor()); |
257 |
state.set(FIELD_MASK, getMask()); |
258 |
state.set(FIELD_OFFSET, getOffset()); |
259 |
state.set(FIELD_ROTATION, getRotation()); |
260 |
state.set(FIELD_SIZE, getSize()); |
261 |
} |
262 |
|
263 |
public static class RegisterPersistence implements Callable { |
264 |
|
265 |
public Object call() throws Exception { |
266 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
267 |
if( manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) { |
268 |
DynStruct definition = manager.addDefinition( |
269 |
AbstractMarkerSymbol.class, |
270 |
MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME, |
271 |
MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
|
272 |
null,
|
273 |
null
|
274 |
); |
275 |
|
276 |
// Extend the Symbol base definition
|
277 |
definition.extend(manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME)); |
278 |
|
279 |
// Color
|
280 |
definition.addDynFieldObject(FIELD_COLOR).setMandatory(false).setClassOfValue(Color.class); |
281 |
// Mask
|
282 |
definition.addDynFieldObject(FIELD_MASK).setMandatory(false).setClassOfValue(IMask.class);
|
283 |
|
284 |
// Offset
|
285 |
definition.addDynFieldObject(FIELD_OFFSET).setMandatory(false).setClassOfValue(Point2D.class); |
286 |
|
287 |
// Rotation
|
288 |
definition.addDynFieldDouble(FIELD_ROTATION).setMandatory(true);
|
289 |
|
290 |
// Size
|
291 |
definition.addDynFieldDouble(FIELD_SIZE).setMandatory(true);
|
292 |
} |
293 |
return Boolean.TRUE; |
294 |
} |
295 |
|
296 |
} |
297 |
|
298 |
} |