source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/AreaElemStyle.java@ 6883

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

fix some Sonar issues

  • Property svn:eol-style set to native
File size: 4.7 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.Color;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.data.osm.OsmPrimitive;
10import org.openstreetmap.josm.data.osm.Relation;
11import org.openstreetmap.josm.data.osm.Way;
12import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
13import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
14import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
15import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
16import org.openstreetmap.josm.tools.CheckParameterUtil;
17import org.openstreetmap.josm.tools.Utils;
18
19public class AreaElemStyle extends ElemStyle {
20
21 /**
22 * If fillImage == null, color is the fill-color, otherwise
23 * an arbitrary color value sampled from the fillImage
24 */
25 public Color color;
26 public MapImage fillImage;
27 public TextElement text;
28
29 protected AreaElemStyle(Cascade c, Color color, MapImage fillImage, TextElement text) {
30 super(c, 1f);
31 CheckParameterUtil.ensureParameterNotNull(color);
32 this.color = color;
33 this.fillImage = fillImage;
34 this.text = text;
35 }
36
37 public static AreaElemStyle create(Cascade c) {
38 MapImage fillImage = null;
39 Color color = null;
40
41 IconReference iconRef = c.get(FILL_IMAGE, null, IconReference.class);
42 if (iconRef != null) {
43 fillImage = new MapImage(iconRef.iconName, iconRef.source);
44 fillImage.getImage();
45
46 color = new Color(fillImage.getImage().getRGB(
47 fillImage.getWidth() / 2, fillImage.getHeight() / 2)
48 );
49
50 fillImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255))));
51 Integer pAlpha = Utils.color_float2int(c.get(FILL_OPACITY, null, float.class));
52 if (pAlpha != null) {
53 fillImage.alpha = pAlpha;
54 }
55 } else {
56 color = c.get(FILL_COLOR, null, Color.class);
57 if (color != null) {
58 int alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50))));
59 Integer pAlpha = Utils.color_float2int(c.get(FILL_OPACITY, null, float.class));
60 if (pAlpha != null) {
61 alpha = pAlpha;
62 }
63 color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
64 }
65 }
66
67 TextElement text = null;
68 Keyword textPos = c.get(TEXT_POSITION, null, Keyword.class);
69 if (textPos == null || Utils.equal(textPos.val, "center")) {
70 text = TextElement.create(c, PaintColors.AREA_TEXT.get(), true);
71 }
72
73 if (color != null)
74 return new AreaElemStyle(c, color, fillImage, text);
75 else
76 return null;
77 }
78
79 @Override
80 public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, StyledMapRenderer painter, boolean selected, boolean member) {
81 if (osm instanceof Way) {
82 Color myColor = color;
83 if (color != null && osm.isSelected()) {
84 myColor = paintSettings.getSelectedColor(color.getAlpha());
85 }
86 painter.drawArea((Way) osm, myColor, fillImage, text);
87 } else if (osm instanceof Relation) {
88 Color myColor = color;
89 if (color != null && selected) {
90 myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
91 }
92 painter.drawArea((Relation) osm, myColor, fillImage, text);
93 }
94 }
95
96 @Override
97 public boolean equals(Object obj) {
98 if (obj == null || getClass() != obj.getClass())
99 return false;
100 if (!super.equals(obj))
101 return false;
102 AreaElemStyle other = (AreaElemStyle) obj;
103 // we should get the same image object due to caching
104 if (!equal(fillImage, other.fillImage))
105 return false;
106 if (!equal(color, other.color))
107 return false;
108 if (!equal(text, other.text))
109 return false;
110 return true;
111 }
112
113 @Override
114 public int hashCode() {
115 int hash = 3;
116 hash = 61 * hash + color.hashCode();
117 hash = 61 * hash + (fillImage != null ? fillImage.hashCode() : 0);
118 hash = 61 * hash + (text != null ? text.hashCode() : 0);
119 return hash;
120 }
121
122 @Override
123 public String toString() {
124 return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
125 " fillImage=[" + fillImage + "]}";
126 }
127}
Note: See TracBrowser for help on using the repository browser.