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 / SimpleLineStyle.java @ 40560
History | View | Annotate | Download (12.2 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.style; |
||
25 | |||
26 | import java.awt.BasicStroke; |
||
27 | import java.awt.Color; |
||
28 | import java.awt.Graphics2D; |
||
29 | import java.awt.Rectangle; |
||
30 | import java.awt.Stroke; |
||
31 | |||
32 | import org.gvsig.fmap.geom.Geometry; |
||
33 | import org.gvsig.fmap.mapcontext.ViewPort; |
||
34 | import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport; |
||
35 | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
||
36 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.CartographicSupportToolkit; |
||
37 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol; |
||
38 | import org.gvsig.tools.ToolsLocator; |
||
39 | import org.gvsig.tools.dynobject.DynStruct; |
||
40 | import org.gvsig.tools.persistence.PersistenceManager; |
||
41 | import org.gvsig.tools.persistence.PersistentState; |
||
42 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
43 | import org.gvsig.tools.util.Callable; |
||
44 | |||
45 | /**
|
||
46 | * @see http://www.oreilly.com/catalog/java2d/chapter/ch04.html
|
||
47 | * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
|
||
48 | * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
||
49 | */
|
||
50 | public class SimpleLineStyle extends AbstractStyle implements CartographicSupport, ISimpleLineStyle { |
||
51 | public static final String SIMPLE_LINE_STYLE_PERSISTENCE_DEFINITION_NAME = "SimpleLineStyle"; |
||
52 | |||
53 | private static final String FIELD_DASH_ARRAY = "dashArray"; |
||
54 | private static final String FIELD_TEMP_DASH_ARRAY = "tempDashArray"; |
||
55 | private static final String FIELD_LINE_WIDTH = "lineWidth"; |
||
56 | private static final String FIELD_DASH_PHASE = "dashPhase"; |
||
57 | private static final String FIELD_END_CAP = "endCap"; |
||
58 | private static final String FIELD_LINE_JOIN = "lineJoin"; |
||
59 | private static final String FIELD_MITER_LIMIT = "miterLimit"; |
||
60 | private static final String FIELD_OFFSET = "offset"; |
||
61 | private static final String FIELD_UNIT = "unit"; |
||
62 | private static final String FIELD_ARROW_DECORATOR = "arrowDecorator"; |
||
63 | |||
64 | private final static Color PREVIEW_COLOR_1= new Color(150, 255, 200); //light blue |
||
65 | private final static Color PREVIEW_COLOR_2 = new Color(255, 200, 100); //orange |
||
66 | private final static int COLOR1_STROKE = 1; |
||
67 | private final static int COLOR2_STROKE = 3; |
||
68 | private float[] dashArray, tempDashArray; |
||
69 | private float dashPhase; |
||
70 | private int endCap = BasicStroke.CAP_BUTT; |
||
71 | private int lineJoin; |
||
72 | private float miterlimit; |
||
73 | private float lineWidth = 1f; |
||
74 | private int measureUnit;// for the offset distance |
||
75 | private double offset = 0, csOffset = 0; |
||
76 | private int referenceSystem; |
||
77 | private IArrowDecoratorStyle arrowDecorator;
|
||
78 | /**
|
||
79 | * Constructor method
|
||
80 | *
|
||
81 | */
|
||
82 | public SimpleLineStyle() {
|
||
83 | super();
|
||
84 | BasicStroke dummy = new BasicStroke(); |
||
85 | dashArray = dummy.getDashArray(); |
||
86 | tempDashArray = dummy.getDashArray(); |
||
87 | dashPhase = dummy.getDashPhase(); |
||
88 | endCap = BasicStroke.CAP_BUTT;
|
||
89 | lineJoin = BasicStroke.JOIN_BEVEL;
|
||
90 | miterlimit = dummy.getMiterLimit(); |
||
91 | } |
||
92 | /**
|
||
93 | * Constructor method
|
||
94 | *
|
||
95 | * @param width
|
||
96 | * @param cap
|
||
97 | * @param join
|
||
98 | * @param miterlimit
|
||
99 | * @param dash
|
||
100 | * @param dash_phase
|
||
101 | */
|
||
102 | public SimpleLineStyle(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase) { |
||
103 | super();
|
||
104 | this.lineWidth = width;
|
||
105 | this.endCap = cap;
|
||
106 | this.lineJoin = join;
|
||
107 | this.miterlimit = miterlimit;
|
||
108 | this.dashArray = dash;
|
||
109 | this.tempDashArray = dash;
|
||
110 | this.dashPhase = dash_phase;
|
||
111 | } |
||
112 | |||
113 | public void drawInsideRectangle(Graphics2D g, Rectangle r) { |
||
114 | Stroke oldStroke = g.getStroke();
|
||
115 | int h = (int) (r.getHeight() / 2); |
||
116 | int margins = 2; |
||
117 | |||
118 | BasicStroke stroke;
|
||
119 | stroke = new BasicStroke(COLOR1_STROKE, endCap, lineJoin, miterlimit, tempDashArray, dashPhase); |
||
120 | g.setStroke(stroke); |
||
121 | g.setColor(PREVIEW_COLOR_1); |
||
122 | g.drawLine(margins, h, r.width-margins-margins, h); |
||
123 | |||
124 | stroke = new BasicStroke(COLOR2_STROKE, endCap, lineJoin, miterlimit, tempDashArray, dashPhase); |
||
125 | g.setStroke(stroke); |
||
126 | g.setColor(PREVIEW_COLOR_2); |
||
127 | g.drawLine(margins, h, r.width-margins-margins, h); |
||
128 | g.setStroke(oldStroke); |
||
129 | } |
||
130 | |||
131 | public String getClassName() { |
||
132 | return getClass().getName();
|
||
133 | } |
||
134 | |||
135 | public Stroke getStroke() { |
||
136 | return new BasicStroke((float) lineWidth, endCap, lineJoin, miterlimit, tempDashArray, dashPhase); |
||
137 | } |
||
138 | |||
139 | public float getLineWidth() { |
||
140 | return lineWidth;
|
||
141 | } |
||
142 | |||
143 | |||
144 | public void setLineWidth(float width) { |
||
145 | if (lineWidth != 0) { |
||
146 | double scale = width / this.lineWidth; |
||
147 | if (dashArray != null) { |
||
148 | for (int i = 0; scale > 0 && i < dashArray.length; i++) { |
||
149 | tempDashArray[i] = (float) (dashArray[i] * scale);
|
||
150 | } |
||
151 | } |
||
152 | this.csOffset = offset * scale;
|
||
153 | } |
||
154 | this.lineWidth = width;
|
||
155 | |||
156 | } |
||
157 | |||
158 | public boolean isSuitableFor(ISymbol symbol) { |
||
159 | return symbol instanceof ILineSymbol; |
||
160 | } |
||
161 | |||
162 | public void setStroke(Stroke stroke) { |
||
163 | if (stroke != null) { |
||
164 | BasicStroke dummy = (BasicStroke) stroke; |
||
165 | dashArray = dummy.getDashArray(); |
||
166 | if (dashArray != null) { |
||
167 | tempDashArray = new float[dashArray.length]; |
||
168 | for (int i = 0; i < dashArray.length; i++) { |
||
169 | tempDashArray[i] = dashArray[i]; |
||
170 | } |
||
171 | } else
|
||
172 | tempDashArray = null;
|
||
173 | |||
174 | |||
175 | dashPhase = dummy.getDashPhase(); |
||
176 | endCap = dummy.getEndCap(); |
||
177 | lineJoin = dummy.getLineJoin(); |
||
178 | miterlimit = dummy.getMiterLimit(); |
||
179 | lineWidth = dummy.getLineWidth(); |
||
180 | offset = getOffset(); |
||
181 | csOffset = offset; |
||
182 | } |
||
183 | } |
||
184 | |||
185 | public void drawOutline(Graphics2D g, Rectangle r) { |
||
186 | drawInsideRectangle(g, r); |
||
187 | } |
||
188 | |||
189 | public double getOffset() { |
||
190 | return csOffset;
|
||
191 | } |
||
192 | |||
193 | public void setOffset(double offset) { |
||
194 | this.offset = offset;
|
||
195 | this.csOffset = offset;
|
||
196 | } |
||
197 | |||
198 | public IArrowDecoratorStyle getArrowDecorator() {
|
||
199 | return arrowDecorator;
|
||
200 | |||
201 | } |
||
202 | |||
203 | public void setArrowDecorator(IArrowDecoratorStyle arrowDecoratorStyle) { |
||
204 | this.arrowDecorator = arrowDecoratorStyle;
|
||
205 | } |
||
206 | |||
207 | public void setUnit(int unitIndex) { |
||
208 | this.measureUnit = unitIndex;
|
||
209 | } |
||
210 | |||
211 | public int getUnit() { |
||
212 | return measureUnit;
|
||
213 | } |
||
214 | |||
215 | public int getReferenceSystem() { |
||
216 | return referenceSystem;
|
||
217 | } |
||
218 | |||
219 | public void setReferenceSystem(int referenceSystem) { |
||
220 | this.referenceSystem = referenceSystem;
|
||
221 | } |
||
222 | |||
223 | public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) { |
||
224 | double oldWidth = getLineWidth();
|
||
225 | setCartographicSize(getCartographicSize(viewPort, dpi, geom), geom); |
||
226 | return oldWidth;
|
||
227 | } |
||
228 | |||
229 | public void setCartographicSize(double cartographicSize, Geometry geom) { |
||
230 | setLineWidth((float) cartographicSize);
|
||
231 | |||
232 | } |
||
233 | |||
234 | public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) { |
||
235 | return CartographicSupportToolkit.
|
||
236 | getCartographicLength(this,
|
||
237 | getOffset(), |
||
238 | viewPort, |
||
239 | dpi); |
||
240 | |||
241 | } |
||
242 | |||
243 | |||
244 | public Object clone() throws CloneNotSupportedException { |
||
245 | SimpleLineStyle copy = (SimpleLineStyle) super.clone();
|
||
246 | if (arrowDecorator != null) { |
||
247 | copy.arrowDecorator = (ArrowDecoratorStyle) arrowDecorator.clone(); |
||
248 | } |
||
249 | |||
250 | if (dashArray != null) { |
||
251 | copy.dashArray = new float[dashArray.length]; |
||
252 | System.arraycopy(dashArray, 0, copy.dashArray, 0, dashArray.length); |
||
253 | } |
||
254 | if (tempDashArray != null) { |
||
255 | copy.tempDashArray = new float[tempDashArray.length]; |
||
256 | System.arraycopy(tempDashArray, 0, copy.tempDashArray, 0, |
||
257 | tempDashArray.length); |
||
258 | } |
||
259 | |||
260 | return copy;
|
||
261 | } |
||
262 | |||
263 | /**
|
||
264 | * @return the dashArray
|
||
265 | */
|
||
266 | private float[] getDashArray() { |
||
267 | return dashArray;
|
||
268 | } |
||
269 | |||
270 | /**
|
||
271 | * @param dashArray
|
||
272 | * the dashArray to set
|
||
273 | */
|
||
274 | private void setDashArray(float[] dashArray) { |
||
275 | this.dashArray = dashArray;
|
||
276 | } |
||
277 | |||
278 | /**
|
||
279 | * @return the dashPhase
|
||
280 | */
|
||
281 | private float getDashPhase() { |
||
282 | return dashPhase;
|
||
283 | } |
||
284 | |||
285 | /**
|
||
286 | * @param dashPhase
|
||
287 | * the dashPhase to set
|
||
288 | */
|
||
289 | private void setDashPhase(float dashPhase) { |
||
290 | this.dashPhase = dashPhase;
|
||
291 | } |
||
292 | |||
293 | /**
|
||
294 | * @return the endCap
|
||
295 | */
|
||
296 | private int getEndCap() { |
||
297 | return endCap;
|
||
298 | } |
||
299 | |||
300 | /**
|
||
301 | * @param endCap
|
||
302 | * the endCap to set
|
||
303 | */
|
||
304 | private void setEndCap(int endCap) { |
||
305 | this.endCap = endCap;
|
||
306 | } |
||
307 | |||
308 | /**
|
||
309 | * @return the lineJoin
|
||
310 | */
|
||
311 | private int getLineJoin() { |
||
312 | return lineJoin;
|
||
313 | } |
||
314 | |||
315 | /**
|
||
316 | * @param lineJoin
|
||
317 | * the lineJoin to set
|
||
318 | */
|
||
319 | private void setLineJoin(int lineJoin) { |
||
320 | this.lineJoin = lineJoin;
|
||
321 | } |
||
322 | |||
323 | /**
|
||
324 | * @return the miterlimit
|
||
325 | */
|
||
326 | private float getMiterlimit() { |
||
327 | return miterlimit;
|
||
328 | } |
||
329 | |||
330 | /**
|
||
331 | * @param miterlimit
|
||
332 | * the miterlimit to set
|
||
333 | */
|
||
334 | private void setMiterlimit(float miterlimit) { |
||
335 | this.miterlimit = miterlimit;
|
||
336 | } |
||
337 | |||
338 | public void loadFromState(PersistentState state) |
||
339 | throws PersistenceException {
|
||
340 | // Set parent style properties
|
||
341 | super.loadFromState(state);
|
||
342 | |||
343 | // Set own properties
|
||
344 | setArrowDecorator((ArrowDecoratorStyle) state |
||
345 | .get(FIELD_ARROW_DECORATOR)); |
||
346 | setDashArray(state.getFloatArray(FIELD_DASH_ARRAY)); |
||
347 | tempDashArray = state.getFloatArray(FIELD_TEMP_DASH_ARRAY); |
||
348 | setDashPhase(state.getFloat(FIELD_DASH_PHASE)); |
||
349 | setEndCap(state.getInt(FIELD_END_CAP)); |
||
350 | setLineJoin(state.getInt(FIELD_LINE_JOIN)); |
||
351 | setLineWidth(state.getFloat(FIELD_LINE_WIDTH)); |
||
352 | setMiterlimit(state.getFloat(FIELD_MITER_LIMIT)); |
||
353 | setOffset(state.getDouble(FIELD_OFFSET)); |
||
354 | setUnit(state.getInt(FIELD_UNIT)); |
||
355 | } |
||
356 | |||
357 | public void saveToState(PersistentState state) throws PersistenceException { |
||
358 | // Save parent fill symbol properties
|
||
359 | super.saveToState(state);
|
||
360 | |||
361 | // Save own properties
|
||
362 | state.set(FIELD_ARROW_DECORATOR, getArrowDecorator()); |
||
363 | state.set(FIELD_DASH_ARRAY, getDashArray()); |
||
364 | state.set(FIELD_TEMP_DASH_ARRAY, tempDashArray); |
||
365 | state.set(FIELD_DASH_PHASE, getDashPhase()); |
||
366 | state.set(FIELD_END_CAP, getEndCap()); |
||
367 | state.set(FIELD_LINE_JOIN, getLineJoin()); |
||
368 | state.set(FIELD_LINE_WIDTH, getLineWidth()); |
||
369 | state.set(FIELD_MITER_LIMIT, getMiterlimit()); |
||
370 | state.set(FIELD_OFFSET, getOffset()); |
||
371 | state.set(FIELD_UNIT, getUnit()); |
||
372 | } |
||
373 | |||
374 | public static class RegisterPersistence implements Callable { |
||
375 | |||
376 | public Object call() throws Exception { |
||
377 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
378 | if( manager.getDefinition(SIMPLE_LINE_STYLE_PERSISTENCE_DEFINITION_NAME)==null ) { |
||
379 | DynStruct definition = manager.addDefinition( |
||
380 | SimpleLineStyle.class, |
||
381 | SIMPLE_LINE_STYLE_PERSISTENCE_DEFINITION_NAME, |
||
382 | SIMPLE_LINE_STYLE_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
|
||
383 | null,
|
||
384 | null
|
||
385 | ); |
||
386 | |||
387 | // Extend the Style base definition
|
||
388 | definition.extend(manager.getDefinition(STYLE_PERSISTENCE_DEFINITION_NAME)); |
||
389 | |||
390 | // Arrow decorator
|
||
391 | definition.addDynFieldObject(FIELD_ARROW_DECORATOR) |
||
392 | .setClassOfValue(ArrowDecoratorStyle.class); |
||
393 | // Dash array
|
||
394 | definition.addDynFieldArray(FIELD_DASH_ARRAY) |
||
395 | .setClassOfItems(float.class);
|
||
396 | // Temporal dash array
|
||
397 | definition.addDynFieldArray(FIELD_TEMP_DASH_ARRAY) |
||
398 | .setClassOfItems(float.class);
|
||
399 | // Dash phase
|
||
400 | definition.addDynFieldFloat(FIELD_DASH_PHASE).setMandatory(true);
|
||
401 | // End cap
|
||
402 | definition.addDynFieldInt(FIELD_END_CAP).setMandatory(true);
|
||
403 | // Line join
|
||
404 | definition.addDynFieldInt(FIELD_LINE_JOIN).setMandatory(true);
|
||
405 | // Line width
|
||
406 | definition.addDynFieldFloat(FIELD_LINE_WIDTH).setMandatory(true);
|
||
407 | // Miter limit
|
||
408 | definition.addDynFieldFloat(FIELD_MITER_LIMIT).setMandatory(true);
|
||
409 | // Offset
|
||
410 | definition.addDynFieldDouble(FIELD_OFFSET).setMandatory(true);
|
||
411 | // Unit
|
||
412 | definition.addDynFieldInt(FIELD_UNIT).setMandatory(true);
|
||
413 | } |
||
414 | return Boolean.TRUE; |
||
415 | } |
||
416 | |||
417 | } |
||
418 | } |