source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/LineElement.java@ 11553

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

refactor handling of null values - use Java 8 Optional where possible

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