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