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 / style / ArrowDecoratorStyle.java @ 40560
History | View | Annotate | Download (12.7 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.style; |
25 |
|
26 |
import java.awt.Graphics2D; |
27 |
import java.awt.Rectangle; |
28 |
import java.awt.geom.AffineTransform; |
29 |
import java.awt.geom.Point2D; |
30 |
|
31 |
import org.apache.batik.ext.awt.geom.PathLength; |
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 |
import org.gvsig.fmap.geom.primitive.Point; |
39 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
40 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol; |
41 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IArrowMarkerSymbol; |
42 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol; |
43 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.impl.ArrowMarkerSymbol; |
44 |
import org.gvsig.tools.ToolsLocator; |
45 |
import org.gvsig.tools.dynobject.DynStruct; |
46 |
import org.gvsig.tools.persistence.PersistenceManager; |
47 |
import org.gvsig.tools.persistence.PersistentState; |
48 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
49 |
import org.gvsig.tools.util.Callable; |
50 |
|
51 |
/**
|
52 |
* Class ArrowDecoratorStyle. It is used to store the information about the
|
53 |
* different options to draw an arrow in a line (and draw it too). This
|
54 |
* information is taken from the panel.
|
55 |
*
|
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 ArrowDecoratorStyle extends AbstractStyle implements IArrowDecoratorStyle { |
60 |
public static final String ARROR_DECORATOR_STYLE_PERSISTENCE_DEFINITION_NAME = "ArrowDecoratorStyle"; |
61 |
|
62 |
private static final String FIELD_FLIP_ALL = "flipAll"; |
63 |
private static final String FIELD_FLIP_FIRST = "flipFirst"; |
64 |
private static final String FIELD_ARROW_MARKER_COUNT = "arrowMarkerCount"; |
65 |
private static final String FIELD_FOLLOW_LINE_ANGLE = "followLineAngle"; |
66 |
private static final String FIELD_MARKER = "marker"; |
67 |
|
68 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
69 |
private boolean flipAll = false; |
70 |
private boolean flipFirst = false; |
71 |
private int arrowMarkerCount = 2; |
72 |
private boolean followLineAngle = true; |
73 |
private IMarkerSymbol marker = getDefaultMarker();
|
74 |
|
75 |
public ArrowDecoratorStyle() {
|
76 |
marker.setSize(10);
|
77 |
((IArrowMarkerSymbol) marker).setSharpness(30);
|
78 |
} |
79 |
|
80 |
private IMarkerSymbol getDefaultMarker() {
|
81 |
return new ArrowMarkerSymbol(); |
82 |
} |
83 |
|
84 |
/**
|
85 |
* Obtains the number of arrows that the user wants to draw in the same line.
|
86 |
* @return
|
87 |
*/
|
88 |
public int getArrowMarkerCount() { |
89 |
return arrowMarkerCount;
|
90 |
} |
91 |
|
92 |
/**
|
93 |
* Defines the number of arrows that the user wants to draw in the same line.
|
94 |
* @return
|
95 |
*/
|
96 |
public void setArrowMarkerCount(int arrowMarkerCount) { |
97 |
this.arrowMarkerCount = arrowMarkerCount;
|
98 |
} |
99 |
|
100 |
/**
|
101 |
* Defines the flipAll attribute.If the value of this attribute is true all the
|
102 |
* arrows that we had drawn in the same line will be flipped.
|
103 |
* @return
|
104 |
*/
|
105 |
public boolean isFlipAll() { |
106 |
return flipAll;
|
107 |
} |
108 |
|
109 |
/**
|
110 |
* Obtains the flipAll attribute.If the value of this attribute is true all the
|
111 |
* arrows that we had drawn in the same line will be flipped.
|
112 |
* @return
|
113 |
*/
|
114 |
public void setFlipAll(boolean flipAll) { |
115 |
this.flipAll = flipAll;
|
116 |
} |
117 |
|
118 |
/**
|
119 |
* Obtains the flipFirst attribute.If it is true only the first arrow of the line
|
120 |
* will be flipped.The rest will keep the same orientation.
|
121 |
* @return
|
122 |
*/
|
123 |
public boolean isFlipFirst() { |
124 |
return flipFirst;
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* Sets the flipFirst attribute.If it is true only the first arrow of the line
|
129 |
* will be flipped.The rest will keep the same orientation.
|
130 |
* @return
|
131 |
*/
|
132 |
public void setFlipFirst(boolean flipFirst) { |
133 |
this.flipFirst = flipFirst;
|
134 |
} |
135 |
|
136 |
/**
|
137 |
* Gets the followLineAngle attribute.This attribute allows the arrow that we are
|
138 |
* going to draw to be more or less aligned with the line where it will be included (depending on the angle) .
|
139 |
* @return
|
140 |
*/
|
141 |
public boolean isFollowLineAngle() { |
142 |
return followLineAngle;
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* Sets the followLineAngle attribute.This attribute allows the arrow that we are
|
147 |
* going to draw to be more or less aligned with the line where it will be included.
|
148 |
* (depending on the angle).
|
149 |
* @param followingLineAngle
|
150 |
* @return
|
151 |
*/
|
152 |
public void setFollowLineAngle(boolean followLineAngle) { |
153 |
this.followLineAngle = followLineAngle;
|
154 |
} |
155 |
/**
|
156 |
* Draws an arrow(or other symbol that substitutes an arrow selected by the user)
|
157 |
* in a line.When the line is drawn, the symbol is added and takes care of the different
|
158 |
* options of the user(for example if he wants to flip the first symbol or all and
|
159 |
* the number of symbols per line to be drawn)
|
160 |
* @param g
|
161 |
* @param affineTransform
|
162 |
* @param feature
|
163 |
* @param shp
|
164 |
* @throws CreateGeometryException
|
165 |
*/
|
166 |
public void draw(Graphics2D g, AffineTransform affineTransform, |
167 |
Geometry geom, Feature feature) throws CreateGeometryException {
|
168 |
if (arrowMarkerCount <= 0) return; |
169 |
|
170 |
Geometry geomToDraw = geom.cloneGeometry(); |
171 |
geomToDraw.transform(affineTransform); |
172 |
PathLength pl = new PathLength(geomToDraw.getShape());
|
173 |
float size = (float) marker.getSize(); |
174 |
marker.setRotation(0.0);
|
175 |
float myLineLength = pl.lengthOfPath();
|
176 |
if (size > myLineLength){
|
177 |
return;
|
178 |
} |
179 |
float step = arrowMarkerCount>2 ? myLineLength/(arrowMarkerCount-1) : pl.lengthOfPath(); |
180 |
float rotation1 = 0; // rotation at the arrow's vertex |
181 |
float rotation2 = 0; // rotation at the arrow's back; |
182 |
|
183 |
Point startP = null; |
184 |
|
185 |
// the first arrow at the end of the line
|
186 |
float theLength = pl.lengthOfPath();
|
187 |
{ |
188 |
|
189 |
if ((flipFirst || flipAll) && (flipFirst != flipAll) && followLineAngle) { // logical XOR |
190 |
Point2D p = pl.pointAtLength(theLength-size);
|
191 |
if (p!=null){ |
192 |
startP = geomManager.createPoint(p.getX(), p.getY(), SUBTYPES.GEOM2D); |
193 |
if(followLineAngle){
|
194 |
rotation1 = pl.angleAtLength(theLength-size); |
195 |
rotation2 = pl.angleAtLength(theLength); |
196 |
marker.setRotation(rotation1); |
197 |
} |
198 |
marker.draw(g, new AffineTransform(), startP, feature, null); |
199 |
} |
200 |
} else {
|
201 |
Point2D p = pl.pointAtLength(theLength);
|
202 |
if (p!=null){ |
203 |
startP = geomManager.createPoint(p.getX(), p.getY(), SUBTYPES.GEOM2D); |
204 |
if(followLineAngle){
|
205 |
rotation1 = pl.angleAtLength(theLength-size)+(float) Math.PI; |
206 |
rotation2 = pl.angleAtLength(theLength)+(float) Math.PI; |
207 |
marker.setRotation(rotation2); |
208 |
} |
209 |
marker.draw(g, new AffineTransform(), startP, feature, null); |
210 |
} |
211 |
} |
212 |
} |
213 |
// the other arrows but the first and the last
|
214 |
float aLength;
|
215 |
for (int i = 1; i < arrowMarkerCount-1; i++) { |
216 |
aLength = (float) (step*i);
|
217 |
|
218 |
if (flipAll && followLineAngle) {
|
219 |
Point2D p = pl.pointAtLength(aLength);
|
220 |
if (p==null){ |
221 |
p = pl.pointAtLength(theLength); |
222 |
} |
223 |
startP = geomManager.createPoint(p.getX(), p.getY(), SUBTYPES.GEOM2D); |
224 |
if(followLineAngle){
|
225 |
rotation1 = (float) pl.angleAtLength(aLength);
|
226 |
rotation2 = (float) pl.angleAtLength((float)(aLength+size)); |
227 |
marker.setRotation(rotation1); |
228 |
} |
229 |
marker.draw(g, new AffineTransform(), startP, feature, null); |
230 |
} else {
|
231 |
Point2D p = pl.pointAtLength(aLength+size);
|
232 |
if (p==null){ |
233 |
p = pl.pointAtLength(theLength); |
234 |
} |
235 |
|
236 |
startP = geomManager.createPoint(p.getX(), p.getY(), SUBTYPES.GEOM2D); |
237 |
if(followLineAngle){
|
238 |
rotation1 = (float) (pl.angleAtLength(aLength) + Math.PI); |
239 |
rotation2 = (float) (pl.angleAtLength((float)(aLength+size)) + Math.PI); |
240 |
marker.setRotation(rotation2); |
241 |
} |
242 |
marker.draw(g, new AffineTransform(), startP, feature, null); |
243 |
} |
244 |
} |
245 |
|
246 |
startP = null;
|
247 |
// and the last arrow at the begining of the line
|
248 |
if (arrowMarkerCount>1) { |
249 |
if (flipAll) {
|
250 |
Point2D p = null; |
251 |
p = pl.pointAtLength(0);
|
252 |
if (p!=null){ |
253 |
startP = geomManager.createPoint(p.getX(), p.getY(), SUBTYPES.GEOM2D); |
254 |
if(followLineAngle){
|
255 |
rotation1 = (float) pl.angleAtLength(size);
|
256 |
rotation2 = (float) pl.angleAtLength(0); |
257 |
marker.setRotation(rotation2); |
258 |
} |
259 |
marker.draw(g, new AffineTransform(), startP, feature, null); |
260 |
} |
261 |
} else {
|
262 |
Point2D p = null; |
263 |
if(followLineAngle){
|
264 |
p = pl.pointAtLength(size); |
265 |
} else {
|
266 |
p = pl.pointAtLength(0);
|
267 |
} |
268 |
if (p!=null){ |
269 |
startP = geomManager.createPoint(p.getX(), p.getY(), SUBTYPES.GEOM2D); |
270 |
if(followLineAngle){
|
271 |
rotation1 = (float) (pl.angleAtLength(size) + Math.PI); |
272 |
rotation2 = (float) (pl.angleAtLength(0) + Math.PI); |
273 |
marker.setRotation(rotation1); |
274 |
} |
275 |
marker.draw(g, new AffineTransform(), startP, feature, null); |
276 |
} |
277 |
} |
278 |
} |
279 |
} |
280 |
|
281 |
public void drawInsideRectangle(Graphics2D g, Rectangle r) { |
282 |
// TODO Auto-generated method stub
|
283 |
throw new Error("Not yet implemented!"); |
284 |
} |
285 |
|
286 |
public void drawOutline(Graphics2D g, Rectangle r) { |
287 |
// TODO Auto-generated method stub
|
288 |
throw new Error("Not yet implemented!"); |
289 |
} |
290 |
|
291 |
public boolean isSuitableFor(ISymbol symbol) { |
292 |
return symbol instanceof ILineSymbol; |
293 |
} |
294 |
|
295 |
public String getClassName() { |
296 |
return getClass().getName();
|
297 |
} |
298 |
|
299 |
public IMarkerSymbol getMarker() {
|
300 |
if(marker == null){ |
301 |
marker = getDefaultMarker(); |
302 |
} |
303 |
return marker;
|
304 |
} |
305 |
|
306 |
public void setMarker(IMarkerSymbol marker) { |
307 |
this.marker = marker;
|
308 |
} |
309 |
|
310 |
public Object clone() throws CloneNotSupportedException { |
311 |
ArrowDecoratorStyle copy = (ArrowDecoratorStyle) super.clone();
|
312 |
if (marker != null) { |
313 |
copy.marker = (IMarkerSymbol) marker.clone(); |
314 |
} |
315 |
return copy;
|
316 |
} |
317 |
|
318 |
public void loadFromState(PersistentState state) |
319 |
throws PersistenceException {
|
320 |
// Set parent style properties
|
321 |
super.loadFromState(state);
|
322 |
|
323 |
// Set own properties
|
324 |
setArrowMarkerCount(state.getInt(FIELD_ARROW_MARKER_COUNT)); |
325 |
setFlipAll(state.getBoolean(FIELD_FLIP_ALL)); |
326 |
setFlipFirst(state.getBoolean(FIELD_FLIP_FIRST)); |
327 |
setFollowLineAngle(state.getBoolean(FIELD_FOLLOW_LINE_ANGLE)); |
328 |
setMarker((IMarkerSymbol) state.get(FIELD_MARKER)); |
329 |
} |
330 |
|
331 |
public void saveToState(PersistentState state) throws PersistenceException { |
332 |
// Save parent fill symbol properties
|
333 |
super.saveToState(state);
|
334 |
|
335 |
// Save own properties
|
336 |
state.set(FIELD_ARROW_MARKER_COUNT, getArrowMarkerCount()); |
337 |
state.set(FIELD_FLIP_ALL, isFlipAll()); |
338 |
state.set(FIELD_FLIP_FIRST, isFlipFirst()); |
339 |
state.set(FIELD_FOLLOW_LINE_ANGLE, isFollowLineAngle()); |
340 |
state.set(FIELD_MARKER, getMarker()); |
341 |
} |
342 |
|
343 |
public static class RegisterPersistence implements Callable { |
344 |
|
345 |
public Object call() throws Exception { |
346 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
347 |
if( manager.getDefinition(ARROR_DECORATOR_STYLE_PERSISTENCE_DEFINITION_NAME)==null ) { |
348 |
DynStruct definition = manager.addDefinition( |
349 |
ArrowDecoratorStyle.class, |
350 |
ARROR_DECORATOR_STYLE_PERSISTENCE_DEFINITION_NAME, |
351 |
ARROR_DECORATOR_STYLE_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
|
352 |
null,
|
353 |
null
|
354 |
); |
355 |
|
356 |
// Extend the Style base definition
|
357 |
definition.extend(manager.getDefinition(STYLE_PERSISTENCE_DEFINITION_NAME)); |
358 |
|
359 |
// Arrow marker count
|
360 |
definition.addDynFieldInt(FIELD_ARROW_MARKER_COUNT).setMandatory(true);
|
361 |
// Flip all
|
362 |
definition.addDynFieldBoolean(FIELD_FLIP_ALL).setMandatory(true);
|
363 |
// flip first
|
364 |
definition.addDynFieldBoolean(FIELD_FLIP_FIRST).setMandatory(true);
|
365 |
// Follow line angles
|
366 |
definition.addDynFieldBoolean(FIELD_FOLLOW_LINE_ANGLE).setMandatory(true);
|
367 |
// Marker
|
368 |
definition.addDynFieldObject(FIELD_MARKER) |
369 |
.setClassOfValue(IMarkerSymbol.class) |
370 |
.setMandatory(true);
|
371 |
|
372 |
} |
373 |
return Boolean.TRUE; |
374 |
} |
375 |
|
376 |
} |
377 |
|
378 |
} |