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

Last change on this file since 3824 was 3824, checked in by bastiK, 14 years ago

Separate styles from style generation. This may seem a little over the top, but its just an intermediate state of development, should make sense later. Regarding performance: execution time is the same, memory use is similar, or a little less (due to intern pool for StyleCache). Tested, but can have still some bugs.

  • Property svn:eol-style set to native
File size: 5.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import java.awt.Color;
5import java.util.Arrays;
6
7import org.openstreetmap.josm.data.osm.Node;
8import org.openstreetmap.josm.data.osm.OsmPrimitive;
9import org.openstreetmap.josm.data.osm.Way;
10import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
11import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
12import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
13import org.openstreetmap.josm.tools.Utils;
14
15public class LineElemStyle extends ElemStyle {
16
17 public static final LineElemStyle UNTAGGED_WAY;
18
19 static {
20 UNTAGGED_WAY = new LineElemStyle(0, Long.MAX_VALUE, -1, 0, PaintColors.UNTAGGED.get(), new float[0], null);
21 }
22
23 public static LineElemStyle createSimpleLineStyle(Color color) {
24 return new LineElemStyle(0, Long.MAX_VALUE, -1, 0, color, new float[0], null);
25 }
26
27 private int width;
28 public int realWidth; //the real width of this line in meter
29 public Color color;
30 private float[] dashed;
31 public Color dashedColor;
32
33 public LineElemStyle(long minScale, long maxScale, int width, int realWidth, Color color, float[] dashed, Color dashedColor) {
34 super(minScale, maxScale);
35 setWidth(width);
36 this.realWidth = realWidth;
37 this.color = color;
38 this.dashed = dashed;
39 this.dashedColor = dashedColor;
40 }
41
42 @Override
43 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) {
44 Way w = (Way)primitive;
45 /* show direction arrows, if draw.segment.relevant_directions_only is not set,
46 the way is tagged with a direction key
47 (even if the tag is negated as in oneway=false) or the way is selected */
48 boolean showDirection = selected || ((!paintSettings.isUseRealWidth()) && (paintSettings.isShowDirectionArrow()
49 && (!paintSettings.isShowRelevantDirectionsOnly() || w.hasDirectionKeys())));
50 boolean reversedDirection = w.reversedDirection();
51 /* head only takes over control if the option is true,
52 the direction should be shown at all and not only because it's selected */
53 boolean showOnlyHeadArrowOnly = showDirection && !selected && paintSettings.isShowHeadArrowOnly();
54 Node lastN;
55
56 Color myDashedColor = dashedColor;
57 int myWidth = getWidth();
58
59 if (realWidth > 0 && paintSettings.isUseRealWidth() && !showDirection) {
60
61 /* if we have a "width" tag, try use it */
62 /* (this might be slow and could be improved by caching the value in the Way, on the other hand only used if "real width" is enabled) */
63 String widthTag = w.get("width");
64 if(widthTag == null) {
65 widthTag = w.get("est_width");
66 }
67 if(widthTag != null) {
68 try {
69 realWidth = Integer.parseInt(widthTag);
70 }
71 catch(NumberFormatException nfe) {
72 }
73 }
74
75 myWidth = (int) (100 / (float) (painter.getCircum() / realWidth));
76 if (myWidth < getWidth()) {
77 myWidth = getWidth();
78 }
79 }
80
81 Color markColor = null;
82 if(w.isHighlighted()) {
83 markColor = paintSettings.getHighlightColor();
84 } else if (selected) {
85 markColor = paintSettings.getSelectedColor();
86 } else if (member) {
87 markColor = paintSettings.getRelationSelectedColor();
88 } else if(w.isDisabled()) {
89 markColor = paintSettings.getInactiveColor();
90 myDashedColor = paintSettings.getInactiveColor();
91 }
92
93 painter.drawWay(w, markColor != null ? markColor : color, myWidth, dashed, myDashedColor, showDirection,
94 selected ? false : reversedDirection, showOnlyHeadArrowOnly);
95
96 if(paintSettings.isShowOrderNumber()) {
97 int orderNumber = 0;
98 lastN = null;
99 for(Node n : w.getNodes()) {
100 if(lastN != null) {
101 orderNumber++;
102 painter.drawOrderNumber(lastN, n, orderNumber);
103 }
104 lastN = n;
105 }
106 }
107 }
108
109 public int getWidth() {
110 if (width == -1)
111 return MapPaintSettings.INSTANCE.getDefaultSegmentWidth();
112 return width;
113 }
114
115 public void setWidth(int width) {
116 this.width = width;
117 }
118
119 @Override
120 public boolean equals(Object obj) {
121 if (obj == null || getClass() != obj.getClass())
122 return false;
123 if (!super.equals(obj))
124 return false;
125 final LineElemStyle other = (LineElemStyle) obj;
126 return width == other.width &&
127 realWidth == other.realWidth &&
128 Utils.equal(color, other.color) &&
129 Arrays.equals(dashed, other.dashed) &&
130 Utils.equal(dashedColor, other.dashedColor);
131 }
132
133 @Override
134 public int hashCode() {
135 int hash = 3;
136 hash = 29 * hash + this.width;
137 hash = 29 * hash + this.realWidth;
138 hash = 29 * hash + this.color.hashCode();
139 hash = 29 * hash + Arrays.hashCode(this.dashed);
140 hash = 29 * hash + (this.dashedColor != null ? this.dashedColor.hashCode() : 0);
141 return hash;
142 }
143
144 @Override
145 public String toString() {
146 return "LineElemStyle{" + "width=" + width + " realWidth=" + realWidth + " color=" + color + " dashed=" + Arrays.toString(dashed) + " dashedColor=" + dashedColor + '}';
147 }
148}
Note: See TracBrowser for help on using the repository browser.