source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/LineElemStyle.java@ 7025

Last change on this file since 7025 was 7025, checked in by Don-vip, 11 years ago

Sonar - fix various issues

  • Property svn:eol-style set to native
File size: 14.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import static org.openstreetmap.josm.tools.Utils.equal;
5
6import java.awt.BasicStroke;
7import java.awt.Color;
8import java.util.Arrays;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.osm.Node;
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.data.osm.Way;
14import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
15import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
16import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
17import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.RelativeFloat;
18import org.openstreetmap.josm.tools.Utils;
19
20public class LineElemStyle extends ElemStyle {
21
22 public static LineElemStyle createSimpleLineStyle(Color color, boolean isAreaEdge) {
23 MultiCascade mc = new MultiCascade();
24 Cascade c = mc.getOrCreateCascade("default");
25 c.put(WIDTH, Keyword.DEFAULT);
26 c.put(COLOR, color != null ? color : PaintColors.UNTAGGED.get());
27 if (isAreaEdge) {
28 c.put(Z_INDEX, -3f);
29 }
30 return createLine(new Environment(null, mc, "default", null));
31 }
32 public static final LineElemStyle UNTAGGED_WAY = createSimpleLineStyle(null, false);
33
34 private BasicStroke line;
35 public Color color;
36 public Color dashesBackground;
37 public float offset;
38 public float realWidth; // the real width of this line in meter
39
40 private BasicStroke dashesLine;
41
42 protected enum LineType {
43 NORMAL("", 3f),
44 CASING("casing-", 2f),
45 LEFT_CASING("left-casing-", 2.1f),
46 RIGHT_CASING("right-casing-", 2.1f);
47
48 public final String prefix;
49 public final float default_major_z_index;
50
51 LineType(String prefix, float default_major_z_index) {
52 this.prefix = prefix;
53 this.default_major_z_index = default_major_z_index;
54 }
55 }
56
57 protected LineElemStyle(Cascade c, float default_major_z_index, BasicStroke line, Color color, BasicStroke dashesLine, Color dashesBackground, float offset, float realWidth) {
58 super(c, default_major_z_index);
59 this.line = line;
60 this.color = color;
61 this.dashesLine = dashesLine;
62 this.dashesBackground = dashesBackground;
63 this.offset = offset;
64 this.realWidth = realWidth;
65 }
66
67 public static LineElemStyle createLine(Environment env) {
68 return createImpl(env, LineType.NORMAL);
69 }
70
71 public static LineElemStyle createLeftCasing(Environment env) {
72 LineElemStyle leftCasing = createImpl(env, LineType.LEFT_CASING);
73 if (leftCasing != null) {
74 leftCasing.isModifier = true;
75 }
76 return leftCasing;
77 }
78
79 public static LineElemStyle createRightCasing(Environment env) {
80 LineElemStyle rightCasing = createImpl(env, LineType.RIGHT_CASING);
81 if (rightCasing != null) {
82 rightCasing.isModifier = true;
83 }
84 return rightCasing;
85 }
86
87 public static LineElemStyle createCasing(Environment env) {
88 LineElemStyle casing = createImpl(env, LineType.CASING);
89 if (casing != null) {
90 casing.isModifier = true;
91 }
92 return casing;
93 }
94
95 private static LineElemStyle createImpl(Environment env, LineType type) {
96 Cascade c = env.mc.getCascade(env.layer);
97 Cascade c_def = env.mc.getCascade("default");
98 Float width;
99 switch (type) {
100 case NORMAL:
101 width = getWidth(c, WIDTH, getWidth(c_def, WIDTH, null));
102 break;
103 case CASING:
104 Float casingWidth = c.get(type.prefix + WIDTH, null, Float.class, true);
105 if (casingWidth == null) {
106 RelativeFloat rel_casingWidth = c.get(type.prefix + WIDTH, null, RelativeFloat.class, true);
107 if (rel_casingWidth != null) {
108 casingWidth = rel_casingWidth.val / 2;
109 }
110 }
111 if (casingWidth == null)
112 return null;
113 width = getWidth(c, WIDTH, getWidth(c_def, WIDTH, null));
114 if (width == null) {
115 width = 0f;
116 }
117 width += 2 * casingWidth;
118 break;
119 case LEFT_CASING:
120 case RIGHT_CASING:
121 width = getWidth(c, type.prefix + WIDTH, null);
122 break;
123 default:
124 throw new AssertionError();
125 }
126 if (width == null)
127 return null;
128
129 float realWidth = c.get(type.prefix + REAL_WIDTH, 0f, Float.class);
130 if (realWidth > 0 && MapPaintSettings.INSTANCE.isUseRealWidth()) {
131
132 /* if we have a "width" tag, try use it */
133 String widthTag = env.osm.get("width");
134 if (widthTag == null) {
135 widthTag = env.osm.get("est_width");
136 }
137 if (widthTag != null) {
138 try {
139 realWidth = Float.valueOf(widthTag);
140 } catch(NumberFormatException nfe) {
141 Main.warn(nfe);
142 }
143 }
144 }
145
146 Float offset = c.get(OFFSET, 0f, Float.class);
147 switch (type) {
148 case NORMAL:
149 break;
150 case CASING:
151 offset += c.get(type.prefix + OFFSET, 0f, Float.class);
152 break;
153 case LEFT_CASING:
154 case RIGHT_CASING:
155 {
156 Float baseWidthOnDefault = getWidth(c_def, WIDTH, null);
157 Float baseWidth = getWidth(c, WIDTH, baseWidthOnDefault);
158 if (baseWidth == null || baseWidth < 2f) {
159 baseWidth = 2f;
160 }
161 float casingOffset = c.get(type.prefix + OFFSET, 0f, Float.class);
162 casingOffset += baseWidth / 2 + width / 2;
163 /* flip sign for the right-casing-offset */
164 if (type == LineType.RIGHT_CASING) {
165 casingOffset *= -1f;
166 }
167 offset += casingOffset;
168 break;
169 }
170 }
171
172 Color color = c.get(type.prefix + COLOR, null, Color.class);
173 if (type == LineType.NORMAL && color == null) {
174 color = c.get(FILL_COLOR, null, Color.class);
175 }
176 if (color == null) {
177 color = PaintColors.UNTAGGED.get();
178 }
179
180 int alpha = 255;
181 Integer pAlpha = Utils.color_float2int(c.get(type.prefix + OPACITY, null, Float.class));
182 if (pAlpha != null) {
183 alpha = pAlpha;
184 }
185 color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
186
187 float[] dashes = c.get(type.prefix + DASHES, null, float[].class);
188 if (dashes != null) {
189 boolean hasPositive = false;
190 for (float f : dashes) {
191 if (f > 0) {
192 hasPositive = true;
193 }
194 if (f < 0) {
195 dashes = null;
196 break;
197 }
198 }
199 if (!hasPositive || (dashes != null && dashes.length == 0)) {
200 dashes = null;
201 }
202 }
203 float dashesOffset = c.get(type.prefix + DASHES_OFFSET, 0f, Float.class);
204 Color dashesBackground = c.get(type.prefix + DASHES_BACKGROUND_COLOR, null, Color.class);
205 if (dashesBackground != null) {
206 pAlpha = Utils.color_float2int(c.get(type.prefix + DASHES_BACKGROUND_OPACITY, null, Float.class));
207 if (pAlpha != null) {
208 alpha = pAlpha;
209 }
210 dashesBackground = new Color(dashesBackground.getRed(), dashesBackground.getGreen(),
211 dashesBackground.getBlue(), alpha);
212 }
213
214 Integer cap = null;
215 Keyword capKW = c.get(type.prefix + "linecap", null, Keyword.class);
216 if (capKW != null) {
217 if (equal(capKW.val, "none")) {
218 cap = BasicStroke.CAP_BUTT;
219 } else if (equal(capKW.val, "round")) {
220 cap = BasicStroke.CAP_ROUND;
221 } else if (equal(capKW.val, "square")) {
222 cap = BasicStroke.CAP_SQUARE;
223 }
224 }
225 if (cap == null) {
226 cap = dashes != null ? BasicStroke.CAP_BUTT : BasicStroke.CAP_ROUND;
227 }
228
229 Integer join = null;
230 Keyword joinKW = c.get(type.prefix + "linejoin", null, Keyword.class);
231 if (joinKW != null) {
232 if (equal(joinKW.val, "round")) {
233 join = BasicStroke.JOIN_ROUND;
234 } else if (equal(joinKW.val, "miter")) {
235 join = BasicStroke.JOIN_MITER;
236 } else if (equal(joinKW.val, "bevel")) {
237 join = BasicStroke.JOIN_BEVEL;
238 }
239 }
240 if (join == null) {
241 join = BasicStroke.JOIN_ROUND;
242 }
243
244 float miterlimit = c.get(type.prefix + "miterlimit", 10f, Float.class);
245 if (miterlimit < 1f) {
246 miterlimit = 10f;
247 }
248
249 BasicStroke line = new BasicStroke(width, cap, join, miterlimit, dashes, dashesOffset);
250 BasicStroke dashesLine = null;
251
252 if (dashes != null && dashesBackground != null) {
253 float[] dashes2 = new float[dashes.length];
254 System.arraycopy(dashes, 0, dashes2, 1, dashes.length - 1);
255 dashes2[0] = dashes[dashes.length-1];
256 dashesLine = new BasicStroke(width, cap, join, miterlimit, dashes2, dashes2[0] + dashesOffset);
257 }
258
259 return new LineElemStyle(c, type.default_major_z_index, line, color, dashesLine, dashesBackground, offset, realWidth);
260 }
261
262 @Override
263 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, StyledMapRenderer painter, boolean selected, boolean member) {
264 Way w = (Way)primitive;
265 /* show direction arrows, if draw.segment.relevant_directions_only is not set,
266 the way is tagged with a direction key
267 (even if the tag is negated as in oneway=false) or the way is selected */
268 boolean showOrientation = !isModifier && (selected || paintSettings.isShowDirectionArrow()) && !paintSettings.isUseRealWidth();
269 boolean showOneway = !isModifier && !selected &&
270 !paintSettings.isUseRealWidth() &&
271 paintSettings.isShowOnewayArrow() && w.hasDirectionKeys();
272 boolean onewayReversed = w.reversedDirection();
273 /* head only takes over control if the option is true,
274 the direction should be shown at all and not only because it's selected */
275 boolean showOnlyHeadArrowOnly = showOrientation && !selected && paintSettings.isShowHeadArrowOnly();
276 Node lastN;
277
278 Color myDashedColor = dashesBackground;
279 BasicStroke myLine = line, myDashLine = dashesLine;
280 if (realWidth > 0 && paintSettings.isUseRealWidth() && !showOrientation) {
281 float myWidth = (int) (100 / (float) (painter.getCircum() / realWidth));
282 if (myWidth < line.getLineWidth()) {
283 myWidth = line.getLineWidth();
284 }
285 myLine = new BasicStroke(myWidth, line.getEndCap(), line.getLineJoin(),
286 line.getMiterLimit(), line.getDashArray(), line.getDashPhase());
287 if (dashesLine != null) {
288 myDashLine = new BasicStroke(myWidth, dashesLine.getEndCap(), dashesLine.getLineJoin(),
289 dashesLine.getMiterLimit(), dashesLine.getDashArray(), dashesLine.getDashPhase());
290 }
291 }
292
293 Color myColor = color;
294 if (selected) {
295 myColor = paintSettings.getSelectedColor(color.getAlpha());
296 } else if (member) {
297 myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
298 } else if(w.isDisabled()) {
299 myColor = paintSettings.getInactiveColor();
300 myDashedColor = paintSettings.getInactiveColor();
301 }
302
303 painter.drawWay(w, myColor, myLine, myDashLine, myDashedColor, offset, showOrientation,
304 showOnlyHeadArrowOnly, showOneway, onewayReversed);
305
306 if(paintSettings.isShowOrderNumber() && !painter.isInactiveMode()) {
307 int orderNumber = 0;
308 lastN = null;
309 for(Node n : w.getNodes()) {
310 if(lastN != null) {
311 orderNumber++;
312 painter.drawOrderNumber(lastN, n, orderNumber, myColor);
313 }
314 lastN = n;
315 }
316 }
317 }
318
319 @Override
320 public boolean isProperLineStyle() {
321 return !isModifier;
322 }
323
324 @Override
325 public boolean equals(Object obj) {
326 if (obj == null || getClass() != obj.getClass())
327 return false;
328 if (!super.equals(obj))
329 return false;
330 final LineElemStyle other = (LineElemStyle) obj;
331 return equal(line, other.line) &&
332 equal(color, other.color) &&
333 equal(dashesLine, other.dashesLine) &&
334 equal(dashesBackground, other.dashesBackground) &&
335 offset == other.offset &&
336 realWidth == other.realWidth;
337 }
338
339 @Override
340 public int hashCode() {
341 int hash = super.hashCode();
342 hash = 29 * hash + line.hashCode();
343 hash = 29 * hash + color.hashCode();
344 hash = 29 * hash + (dashesLine != null ? dashesLine.hashCode() : 0);
345 hash = 29 * hash + (dashesBackground != null ? dashesBackground.hashCode() : 0);
346 hash = 29 * hash + Float.floatToIntBits(offset);
347 hash = 29 * hash + Float.floatToIntBits(realWidth);
348 return hash;
349 }
350
351 @Override
352 public String toString() {
353 return "LineElemStyle{" + super.toString() + "width=" + line.getLineWidth() +
354 " realWidth=" + realWidth + " color=" + Utils.toString(color) +
355 " dashed=" + Arrays.toString(line.getDashArray()) +
356 (line.getDashPhase() == 0f ? "" : " dashesOffses=" + line.getDashPhase()) +
357 " dashedColor=" + Utils.toString(dashesBackground) +
358 " linejoin=" + linejoinToString(line.getLineJoin()) +
359 " linecap=" + linecapToString(line.getEndCap()) +
360 (offset == 0 ? "" : " offset=" + offset) +
361 '}';
362 }
363
364 public String linejoinToString(int linejoin) {
365 switch (linejoin) {
366 case BasicStroke.JOIN_BEVEL: return "bevel";
367 case BasicStroke.JOIN_ROUND: return "round";
368 case BasicStroke.JOIN_MITER: return "miter";
369 default: return null;
370 }
371 }
372 public String linecapToString(int linecap) {
373 switch (linecap) {
374 case BasicStroke.CAP_BUTT: return "none";
375 case BasicStroke.CAP_ROUND: return "round";
376 case BasicStroke.CAP_SQUARE: return "square";
377 default: return null;
378 }
379 }
380}
Note: See TracBrowser for help on using the repository browser.