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

Last change on this file since 4960 was 4960, checked in by stoecker, 12 years ago

mv jump to action from G to key J

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