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 / SimpleMarkerSymbol.java @ 47791
History | View | Annotate | Download (14.1 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.marker.impl; |
||
25 | |||
26 | import java.awt.BasicStroke; |
||
27 | import java.awt.Color; |
||
28 | import java.awt.Graphics2D; |
||
29 | 47476 | fdiaz | import java.awt.Rectangle; |
30 | 40435 | jjdelcerro | import java.awt.geom.AffineTransform; |
31 | import org.gvsig.fmap.dal.feature.Feature; |
||
32 | import org.gvsig.fmap.geom.Geometry; |
||
33 | import org.gvsig.fmap.geom.primitive.GeneralPathX; |
||
34 | 43491 | jjdelcerro | import org.gvsig.fmap.geom.primitive.Point; |
35 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.MapContextLocator; |
36 | 47476 | fdiaz | import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport; |
37 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
38 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
||
39 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol; |
||
40 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.ISimpleMarkerSymbol; |
||
41 | import org.gvsig.tools.ToolsLocator; |
||
42 | import org.gvsig.tools.dynobject.DynStruct; |
||
43 | import org.gvsig.tools.persistence.PersistenceManager; |
||
44 | import org.gvsig.tools.persistence.PersistentState; |
||
45 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
46 | import org.gvsig.tools.task.Cancellable; |
||
47 | import org.gvsig.tools.util.Callable; |
||
48 | |||
49 | /**
|
||
50 | * SimpleMarkerSymbol is the most basic symbol for the representation of point objects
|
||
51 | * which can define its size and color apart from the rotation and the offset from a point
|
||
52 | * in the map.
|
||
53 | * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
|
||
54 | * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
||
55 | */
|
||
56 | public class SimpleMarkerSymbol extends AbstractMarkerSymbol implements ISimpleMarkerSymbol { |
||
57 | |||
58 | public static final String SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME = "SimpleMarkerSymbol"; |
||
59 | |||
60 | 41435 | fdiaz | private static final String FIELD_OUTLINED = "outlined"; |
61 | private static final String FIELD_OUTLINECOLOR = "outlineColor"; |
||
62 | private static final String FIELD_OUTLINESIZE = "outlineSize"; |
||
63 | private static final String FIELD_MARKERSTYLE = "markerStyle"; |
||
64 | |||
65 | 40435 | jjdelcerro | private boolean outlined; |
66 | private Color outlineColor; |
||
67 | private double outlineSize; |
||
68 | private ISymbol selectionSymbol;
|
||
69 | private int markerStyle; |
||
70 | |||
71 | public SimpleMarkerSymbol() {
|
||
72 | super();
|
||
73 | } |
||
74 | 41435 | fdiaz | |
75 | 47476 | fdiaz | @Override
|
76 | 47790 | fdiaz | public ISymbol getSymbolForSelection(Color selectionColor) { |
77 | 40435 | jjdelcerro | if (selectionSymbol == null) { |
78 | selectionSymbol = (SimpleMarkerSymbol) cloneForSelection(); |
||
79 | 47791 | fdiaz | } |
80 | |||
81 | selectionSymbol.setColor(selectionColor); |
||
82 | |||
83 | if(selectionSymbol instanceof CartographicSupport){ |
||
84 | ((CartographicSupport)selectionSymbol).setUnit(this.getUnit());
|
||
85 | } |
||
86 | 40435 | jjdelcerro | return selectionSymbol;
|
87 | } |
||
88 | |||
89 | 47476 | fdiaz | @Override
|
90 | public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel, Rectangle r) { |
||
91 | double size_d = getCartographicSize();
|
||
92 | size_d = this.getAdjustedSize(r, size_d);
|
||
93 | int size_i = (int) Math.round(size_d); |
||
94 | |||
95 | if(r!=null){ |
||
96 | geom = this.getSampleGeometry(r);
|
||
97 | } |
||
98 | int x = 0; |
||
99 | 46158 | fdiaz | int y = 0; |
100 | 43491 | jjdelcerro | Point p;
|
101 | try {
|
||
102 | p = geom.centroid(); |
||
103 | 47476 | fdiaz | } catch (Exception ex) { |
104 | 43491 | jjdelcerro | return;
|
105 | } |
||
106 | 47476 | fdiaz | p.transform(affineTransform); |
107 | 41435 | fdiaz | |
108 | 47476 | fdiaz | int halfSize = size_i / 2; |
109 | int posX = (int) (p.getX() + getOffset().getX()) - halfSize; |
||
110 | int posY = (int) (p.getY() + getOffset().getY()) - halfSize; |
||
111 | 40435 | jjdelcerro | |
112 | 47476 | fdiaz | g.translate(posX, posY); |
113 | if (getRotation() != 0.0) { |
||
114 | g.rotate(getRotation()); |
||
115 | } |
||
116 | 40435 | jjdelcerro | |
117 | 47476 | fdiaz | g.setColor(getColor()); |
118 | GeneralPathX genPath = null;
|
||
119 | g.setStroke(new BasicStroke(1)); |
||
120 | 40435 | jjdelcerro | |
121 | 47476 | fdiaz | switch (markerStyle) {
|
122 | case CIRCLE_STYLE:
|
||
123 | g.fillOval(x, y, size_i, size_i); |
||
124 | break;
|
||
125 | case SQUARE_STYLE:
|
||
126 | g.fillRect(x, y, size_i, size_i); |
||
127 | break;
|
||
128 | case CROSS_STYLE:
|
||
129 | x = x + halfSize; |
||
130 | y = y + halfSize; |
||
131 | genPath = new GeneralPathX();
|
||
132 | genPath.moveTo(x, y - halfSize); |
||
133 | genPath.lineTo(x, y + halfSize); |
||
134 | genPath.moveTo(x - halfSize, y); |
||
135 | genPath.lineTo(x + halfSize, y); |
||
136 | g.draw(genPath); |
||
137 | break;
|
||
138 | case VERTICAL_LINE_STYLE:
|
||
139 | genPath = new GeneralPathX();
|
||
140 | genPath.moveTo(x + halfSize, y); |
||
141 | genPath.lineTo(x + halfSize, y + size_i); |
||
142 | g.draw(genPath); |
||
143 | break;
|
||
144 | case HORIZONTAL_LINE_STYLE:
|
||
145 | genPath = new GeneralPathX();
|
||
146 | genPath.moveTo(x, y + halfSize); |
||
147 | genPath.lineTo(x + size_i, y + halfSize); |
||
148 | g.draw(genPath); |
||
149 | break;
|
||
150 | case DIAMOND_STYLE:
|
||
151 | x = x + halfSize; |
||
152 | y = y + halfSize; |
||
153 | genPath = new GeneralPathX();
|
||
154 | genPath.moveTo(x - halfSize, y); |
||
155 | genPath.lineTo(x, y + halfSize); |
||
156 | genPath.lineTo(x + halfSize, y); |
||
157 | genPath.lineTo(x, y - halfSize); |
||
158 | genPath.lineTo(x - halfSize, y); |
||
159 | genPath.closePath(); |
||
160 | g.fill(genPath); |
||
161 | break;
|
||
162 | case X_STYLE:
|
||
163 | x = x + halfSize; |
||
164 | y = y + halfSize; |
||
165 | genPath = new GeneralPathX();
|
||
166 | genPath.moveTo(x - halfSize, y - halfSize); |
||
167 | genPath.lineTo(x + halfSize, y + halfSize); |
||
168 | genPath.moveTo(x - halfSize, y + halfSize); |
||
169 | genPath.lineTo(x + halfSize, y - halfSize); |
||
170 | g.draw(genPath); |
||
171 | break;
|
||
172 | case TRIANGLE_STYLE:
|
||
173 | x = x + halfSize; |
||
174 | y = y + halfSize; |
||
175 | int otherSize = (int) (size_i * 0.55); |
||
176 | genPath = new GeneralPathX();
|
||
177 | genPath.moveTo(x - halfSize, |
||
178 | y + halfSize); |
||
179 | genPath.lineTo(x + halfSize, |
||
180 | y + halfSize); |
||
181 | genPath.lineTo(x, y - otherSize); |
||
182 | genPath.closePath(); |
||
183 | 40435 | jjdelcerro | |
184 | 47476 | fdiaz | g.fill(genPath); |
185 | break;
|
||
186 | case STAR_STYLE:
|
||
187 | x = x + halfSize; |
||
188 | y = y + halfSize; |
||
189 | genPath = new GeneralPathX();
|
||
190 | genPath.moveTo(x - halfSize, y); |
||
191 | 40435 | jjdelcerro | |
192 | 47476 | fdiaz | genPath.lineTo(x - 2 * (halfSize / 3), y + (halfSize / 3)); |
193 | genPath.lineTo(x - 2 * (halfSize / 3), y + 2 * (halfSize / 3)); |
||
194 | genPath.lineTo(x - (halfSize / 3), y + 2 * (halfSize / 3)); |
||
195 | 40435 | jjdelcerro | |
196 | 47476 | fdiaz | genPath.lineTo(x, y + halfSize); |
197 | 40435 | jjdelcerro | |
198 | 47476 | fdiaz | genPath.lineTo(x + (halfSize / 3), y + 2 * (halfSize / 3)); |
199 | genPath.lineTo(x + 2 * (halfSize / 3), y + 2 * (halfSize / 3)); |
||
200 | genPath.lineTo(x + 2 * (halfSize / 3), y + (halfSize / 3)); |
||
201 | 40435 | jjdelcerro | |
202 | 47476 | fdiaz | genPath.lineTo(x + (halfSize), y); |
203 | 40435 | jjdelcerro | |
204 | 47476 | fdiaz | genPath.lineTo(x + 2 * (halfSize / 3), y - (halfSize / 3)); |
205 | genPath.lineTo(x + 2 * (halfSize / 3), y - 2 * (halfSize / 3)); |
||
206 | genPath.lineTo(x + (halfSize / 3), y - 2 * (halfSize / 3)); |
||
207 | 40435 | jjdelcerro | |
208 | 47476 | fdiaz | genPath.lineTo(x, y - halfSize); |
209 | 40435 | jjdelcerro | |
210 | 47476 | fdiaz | genPath.lineTo(x - (halfSize / 3), y - 2 * (halfSize / 3)); |
211 | genPath.lineTo(x - 2 * (halfSize / 3), y - 2 * (halfSize / 3)); |
||
212 | genPath.lineTo(x - 2 * (halfSize / 3), y - (halfSize / 3)); |
||
213 | 40435 | jjdelcerro | |
214 | 47476 | fdiaz | genPath.lineTo(x - halfSize, y); |
215 | 40435 | jjdelcerro | |
216 | 47476 | fdiaz | genPath.closePath(); |
217 | 40435 | jjdelcerro | |
218 | 47476 | fdiaz | g.fill(genPath); |
219 | 40435 | jjdelcerro | |
220 | 47476 | fdiaz | break;
|
221 | } |
||
222 | 40435 | jjdelcerro | |
223 | 47476 | fdiaz | if (outlined) {
|
224 | g.setColor(outlineColor); |
||
225 | switch (markerStyle) {
|
||
226 | case CIRCLE_STYLE:
|
||
227 | g.drawOval(x, y, size_i, size_i); |
||
228 | break;
|
||
229 | case SQUARE_STYLE:
|
||
230 | g.drawRect(x, y, size_i, size_i); |
||
231 | break;
|
||
232 | case CROSS_STYLE:
|
||
233 | case DIAMOND_STYLE:
|
||
234 | case STAR_STYLE:
|
||
235 | case X_STYLE:
|
||
236 | case TRIANGLE_STYLE:
|
||
237 | case VERTICAL_LINE_STYLE:
|
||
238 | case HORIZONTAL_LINE_STYLE:
|
||
239 | g.draw(genPath); |
||
240 | break;
|
||
241 | } |
||
242 | } |
||
243 | if (getRotation() != 0.0) { |
||
244 | g.rotate(-getRotation()); |
||
245 | } |
||
246 | g.translate(-posX, -posY); |
||
247 | 40435 | jjdelcerro | |
248 | 47476 | fdiaz | } |
249 | 40435 | jjdelcerro | |
250 | /**
|
||
251 | * Returns true or false depending if the simple marker symbol has an outline or not.
|
||
252 | * @return Returns the outline.
|
||
253 | */
|
||
254 | 47476 | fdiaz | @Override
|
255 | 40435 | jjdelcerro | public boolean hasOutline() { |
256 | return outlined;
|
||
257 | } |
||
258 | |||
259 | /**
|
||
260 | * Establishes the outline for the simple marker symbol.
|
||
261 | * @param outline The outline to set.
|
||
262 | */
|
||
263 | 47476 | fdiaz | @Override
|
264 | 40435 | jjdelcerro | public void setOutlined(boolean outlined) { |
265 | this.outlined = outlined;
|
||
266 | } |
||
267 | |||
268 | /**
|
||
269 | * Returns the outline color for the symple marker symbol
|
||
270 | *
|
||
271 | * @return Color,outlineColor.
|
||
272 | */
|
||
273 | 47476 | fdiaz | @Override
|
274 | 40435 | jjdelcerro | public Color getOutlineColor() { |
275 | return outlineColor;
|
||
276 | } |
||
277 | |||
278 | /**
|
||
279 | * Sets the outline color for the simple marker symbol
|
||
280 | * @param outlineColor, Color
|
||
281 | */
|
||
282 | 47476 | fdiaz | @Override
|
283 | 40435 | jjdelcerro | public void setOutlineColor(Color outlineColor) { |
284 | this.outlineColor = outlineColor;
|
||
285 | } |
||
286 | |||
287 | /**
|
||
288 | * Gets the size of the outline for the simple marker symbol
|
||
289 | * @return Returns the outlineSize.
|
||
290 | */
|
||
291 | 47476 | fdiaz | @Override
|
292 | 40435 | jjdelcerro | public double getOutlineSize() { |
293 | return outlineSize;
|
||
294 | } |
||
295 | |||
296 | /**
|
||
297 | * Establishes the size for the outline of the simple marker symbol
|
||
298 | * @param outlineSize The outlineSize to set.
|
||
299 | */
|
||
300 | 47476 | fdiaz | @Override
|
301 | 40435 | jjdelcerro | public void setOutlineSize(double outlineSize) { |
302 | this.outlineSize = outlineSize;
|
||
303 | } |
||
304 | |||
305 | /**
|
||
306 | * @return Returns the selectionSymbol.
|
||
307 | */
|
||
308 | 47476 | fdiaz | @Override
|
309 | 40435 | jjdelcerro | public ISymbol getSelectionSymbol() {
|
310 | return selectionSymbol;
|
||
311 | } |
||
312 | |||
313 | /**
|
||
314 | * @param selectionSymbol The selectionSymbol to set.
|
||
315 | */
|
||
316 | 47476 | fdiaz | @Override
|
317 | 40435 | jjdelcerro | public void setSelectionSymbol(ISymbol selectionSymbol) { |
318 | this.selectionSymbol = selectionSymbol;
|
||
319 | } |
||
320 | /**
|
||
321 | * Sets the style for the simple marker symbol
|
||
322 | * @param style
|
||
323 | */
|
||
324 | 47476 | fdiaz | @Override
|
325 | 40435 | jjdelcerro | public void setStyle(int style) { |
326 | this.markerStyle = style;
|
||
327 | } |
||
328 | |||
329 | /**
|
||
330 | * Obtains the style for the simple marker symbol
|
||
331 | * @return markerStyle,int
|
||
332 | */
|
||
333 | 47476 | fdiaz | @Override
|
334 | 40435 | jjdelcerro | public int getStyle() { |
335 | return markerStyle;
|
||
336 | } |
||
337 | |||
338 | 47476 | fdiaz | @Override
|
339 | 40435 | jjdelcerro | public Object clone() throws CloneNotSupportedException { |
340 | SimpleMarkerSymbol copy = (SimpleMarkerSymbol) super.clone();
|
||
341 | 41435 | fdiaz | |
342 | 40435 | jjdelcerro | // clone selection
|
343 | if (selectionSymbol != null) { |
||
344 | copy.selectionSymbol = (ISymbol) selectionSymbol.clone(); |
||
345 | } |
||
346 | 41435 | fdiaz | |
347 | 40435 | jjdelcerro | return copy;
|
348 | } |
||
349 | |||
350 | /**
|
||
351 | * Returns if the marker symbol should be drawn outlined.
|
||
352 | * @return if it is outlined
|
||
353 | */
|
||
354 | 47476 | fdiaz | @Override
|
355 | 40435 | jjdelcerro | public boolean isOutlined() { |
356 | return outlined;
|
||
357 | } |
||
358 | |||
359 | 47476 | fdiaz | @Override
|
360 | 40435 | jjdelcerro | public void loadFromState(PersistentState state) |
361 | throws PersistenceException {
|
||
362 | // Set parent fill symbol properties
|
||
363 | super.loadFromState(state);
|
||
364 | |||
365 | // Set own properties
|
||
366 | setStyle(state.getInt(FIELD_MARKERSTYLE)); |
||
367 | setOutlined(state.getBoolean(FIELD_OUTLINED)); |
||
368 | if (isOutlined()) {
|
||
369 | setOutlineColor((Color) state.get(FIELD_OUTLINECOLOR));
|
||
370 | setOutlineSize(state.getDouble(FIELD_OUTLINESIZE)); |
||
371 | } |
||
372 | } |
||
373 | |||
374 | 47476 | fdiaz | @Override
|
375 | 40435 | jjdelcerro | public void saveToState(PersistentState state) throws PersistenceException { |
376 | // Save parent fill symbol properties
|
||
377 | super.saveToState(state);
|
||
378 | |||
379 | // Save own properties
|
||
380 | state.set(FIELD_MARKERSTYLE, getStyle()); |
||
381 | state.set(FIELD_OUTLINED, isOutlined()); |
||
382 | if (isOutlined()) {
|
||
383 | state.set(FIELD_OUTLINECOLOR, getOutlineColor()); |
||
384 | state.set(FIELD_OUTLINESIZE, getOutlineSize()); |
||
385 | } |
||
386 | } |
||
387 | |||
388 | public static class RegisterPersistence implements Callable { |
||
389 | |||
390 | 47476 | fdiaz | @Override
|
391 | 40435 | jjdelcerro | public Object call() throws Exception { |
392 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
393 | if( manager.getDefinition(SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) { |
||
394 | DynStruct definition = manager.addDefinition( |
||
395 | SimpleMarkerSymbol.class, |
||
396 | SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME, |
||
397 | SIMPLE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
|
||
398 | 41435 | fdiaz | null,
|
399 | 40435 | jjdelcerro | null
|
400 | ); |
||
401 | // Extend the FillSymbol base definition
|
||
402 | definition.extend(manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME)); |
||
403 | |||
404 | // Marker style
|
||
405 | definition.addDynFieldInt(FIELD_MARKERSTYLE).setMandatory(true);
|
||
406 | // Outlined?
|
||
407 | definition.addDynFieldBoolean(FIELD_OUTLINED).setMandatory(true);
|
||
408 | // Outline color
|
||
409 | definition.addDynFieldObject(FIELD_OUTLINECOLOR).setClassOfValue(Color.class);
|
||
410 | // Outline size
|
||
411 | definition.addDynFieldDouble(FIELD_OUTLINESIZE); |
||
412 | } |
||
413 | return Boolean.TRUE; |
||
414 | } |
||
415 | 41435 | fdiaz | |
416 | 40435 | jjdelcerro | } |
417 | |||
418 | public static class RegisterSymbol implements Callable { |
||
419 | |||
420 | 47476 | fdiaz | @Override
|
421 | 40435 | jjdelcerro | public Object call() throws Exception { |
422 | int[] shapeTypes; |
||
423 | SymbolManager manager = MapContextLocator.getSymbolManager(); |
||
424 | |||
425 | shapeTypes = |
||
426 | new int[] { Geometry.TYPES.POINT, Geometry.TYPES.MULTIPOINT }; |
||
427 | manager.registerSymbol(IMarkerSymbol.SYMBOL_NAME, |
||
428 | shapeTypes, |
||
429 | SimpleMarkerSymbol.class); |
||
430 | |||
431 | return Boolean.TRUE; |
||
432 | } |
||
433 | 41435 | fdiaz | |
434 | 40435 | jjdelcerro | } |
435 | |||
436 | } |