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

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

see #8465 - replace Utils.equal by Objects.equals, new in Java 7

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