source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/AreaElement.java@ 10760

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

sonar - squid:S2153 - Boxing and unboxing should not be immediately reversed

  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.styleelement;
3
4import java.awt.Color;
5import java.util.Objects;
6
7import org.openstreetmap.josm.Main;
8import org.openstreetmap.josm.data.osm.OsmPrimitive;
9import org.openstreetmap.josm.data.osm.Relation;
10import org.openstreetmap.josm.data.osm.Way;
11import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
12import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
13import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
14import org.openstreetmap.josm.gui.mappaint.Cascade;
15import org.openstreetmap.josm.gui.mappaint.Environment;
16import org.openstreetmap.josm.gui.mappaint.Keyword;
17import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
18import org.openstreetmap.josm.tools.CheckParameterUtil;
19import org.openstreetmap.josm.tools.Utils;
20
21public class AreaElement extends StyleElement {
22
23 /**
24 * If fillImage == null, color is the fill-color, otherwise
25 * an arbitrary color value sampled from the fillImage
26 */
27 public Color color;
28 public MapImage fillImage;
29 public TextLabel text;
30 public Float extent;
31 public Float extentThreshold;
32
33 protected AreaElement(Cascade c, Color color, MapImage fillImage, Float extent, Float extentThreshold, TextLabel text) {
34 super(c, 1f);
35 CheckParameterUtil.ensureParameterNotNull(color);
36 this.color = color;
37 this.fillImage = fillImage;
38 this.extent = extent;
39 this.extentThreshold = extentThreshold;
40 this.text = text;
41 }
42
43 public static AreaElement create(final Environment env) {
44 final Cascade c = env.mc.getCascade(env.layer);
45 MapImage fillImage = null;
46 Color color;
47
48 IconReference iconRef = c.get(FILL_IMAGE, null, IconReference.class);
49 if (iconRef != null) {
50 fillImage = new MapImage(iconRef.iconName, iconRef.source, false);
51
52 color = new Color(fillImage.getImage(false).getRGB(
53 fillImage.getWidth() / 2, fillImage.getHeight() / 2)
54 );
55
56 fillImage.alpha = Math.min(255, Math.max(0, Main.pref.getInteger("mappaint.fill-image-alpha", 255)));
57 Integer pAlpha = Utils.colorFloat2int(c.get(FILL_OPACITY, null, float.class));
58 if (pAlpha != null) {
59 fillImage.alpha = pAlpha;
60 }
61 } else {
62 color = c.get(FILL_COLOR, null, Color.class);
63 if (color != null) {
64 int alpha = color.getAlpha();
65 if (alpha == 255) {
66 // Assume alpha value has not been specified by the user if
67 // is set to fully opaque. Use default value in this case.
68 // It is not an ideal solution, but a little tricky to get this
69 // right, especially as named map colors can be changed in
70 // the preference GUI and written to the preferences file.
71 alpha = Math.min(255, Math.max(0, Main.pref.getInteger("mappaint.fillalpha", 50)));
72 }
73 Integer pAlpha = Utils.colorFloat2int(c.get(FILL_OPACITY, null, float.class));
74 if (pAlpha != null) {
75 alpha = pAlpha;
76 }
77 color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
78 }
79 }
80
81 TextLabel text = null;
82 Keyword textPos = c.get(TEXT_POSITION, null, Keyword.class);
83 if (textPos == null || "center".equals(textPos.val)) {
84 text = TextLabel.create(env, PaintColors.AREA_TEXT.get(), true);
85 }
86
87 Float extent = c.get(FILL_EXTENT, null, float.class);
88 Float extentThreshold = c.get(FILL_EXTENT_THRESHOLD, null, float.class);
89
90 if (color != null)
91 return new AreaElement(c, color, fillImage, extent, extentThreshold, text);
92 else
93 return null;
94 }
95
96 @Override
97 public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, StyledMapRenderer painter,
98 boolean selected, boolean outermember, boolean member) {
99 Color myColor = color;
100 if (osm instanceof Way) {
101 if (color != null) {
102 if (selected) {
103 myColor = paintSettings.getSelectedColor(color.getAlpha());
104 } else if (outermember) {
105 myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
106 }
107 }
108 painter.drawArea((Way) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled(), text);
109 } else if (osm instanceof Relation) {
110 if (color != null && (selected || outermember)) {
111 myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
112 }
113 painter.drawArea((Relation) osm, myColor, fillImage, extent, extentThreshold, painter.isInactiveMode() || osm.isDisabled(), text);
114 }
115 }
116
117 @Override
118 public boolean equals(Object obj) {
119 if (this == obj) return true;
120 if (obj == null || getClass() != obj.getClass()) return false;
121 if (!super.equals(obj)) return false;
122 AreaElement that = (AreaElement) obj;
123 return Objects.equals(color, that.color) &&
124 Objects.equals(fillImage, that.fillImage) &&
125 Objects.equals(text, that.text) &&
126 Objects.equals(extent, that.extent) &&
127 Objects.equals(extentThreshold, that.extentThreshold);
128 }
129
130 @Override
131 public int hashCode() {
132 return Objects.hash(super.hashCode(), color, fillImage, text, extent, extentThreshold);
133 }
134
135 @Override
136 public String toString() {
137 return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
138 " fillImage=[" + fillImage + "]}";
139 }
140}
Note: See TracBrowser for help on using the repository browser.