source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyle.java@ 6889

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

fix some Sonar issues (JLS order)

  • Property svn:eol-style set to native
File size: 7.4 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.Font;
7import java.util.HashMap;
8import java.util.Map;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.osm.OsmPrimitive;
12import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
13import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
14import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.RelativeFloat;
15
16public abstract class ElemStyle implements StyleKeys {
17
18 protected static final String[] ICON_KEYS = {"icon-image", "icon-width", "icon-height", "icon-opacity"};
19 protected static final String[] REPEAT_IMAGE_KEYS = {"repeat-image", "repeat-image-width", "repeat-image-height", "repeat-image-opacity"};
20
21 public float major_z_index;
22 public float z_index;
23 public float object_z_index;
24 public boolean isModifier; // false, if style can serve as main style for the
25 // primitive; true, if it is a highlight or modifier
26
27 public ElemStyle(float major_z_index, float z_index, float object_z_index, boolean isModifier) {
28 this.major_z_index = major_z_index;
29 this.z_index = z_index;
30 this.object_z_index = object_z_index;
31 this.isModifier = isModifier;
32 }
33
34 protected ElemStyle(Cascade c, float default_major_z_index) {
35 major_z_index = c.get("major-z-index", default_major_z_index, Float.class);
36 z_index = c.get(Z_INDEX, 0f, Float.class);
37 object_z_index = c.get(OBJECT_Z_INDEX, 0f, Float.class);
38 isModifier = c.get(MODIFIER, false, Boolean.class);
39 }
40
41 /**
42 * draws a primitive
43 * @param primitive
44 * @param paintSettings
45 * @param painter
46 * @param selected true, if primitive is selected
47 * @param member true, if primitive is not selected and member of a selected relation
48 */
49 public abstract void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, StyledMapRenderer painter, boolean selected, boolean member);
50
51 public boolean isProperLineStyle() {
52 return false;
53 }
54
55 /**
56 * Get a property value of type Width
57 * @param c the cascade
58 * @param key property key for the width value
59 * @param relativeTo reference width. Only needed, when relative width syntax
60 * is used, e.g. "+4".
61 */
62 protected static Float getWidth(Cascade c, String key, Float relativeTo) {
63 Float width = c.get(key, null, Float.class, true);
64 if (width != null) {
65 if (width > 0)
66 return width;
67 } else {
68 Keyword widthKW = c.get(key, null, Keyword.class, true);
69 if (equal(widthKW, Keyword.THINNEST))
70 return 0f;
71 if (equal(widthKW, Keyword.DEFAULT))
72 return (float) MapPaintSettings.INSTANCE.getDefaultSegmentWidth();
73 if (relativeTo != null) {
74 RelativeFloat width_rel = c.get(key, null, RelativeFloat.class, true);
75 if (width_rel != null)
76 return relativeTo + width_rel.val;
77 }
78 }
79 return null;
80 }
81
82 /* ------------------------------------------------------------------------------- */
83 /* cached values */
84 /* ------------------------------------------------------------------------------- */
85 /*
86 * Two preference values and the set of created fonts are cached in order to avoid
87 * expensive lookups and to avoid too many font objects
88 * (in analogy to flyweight pattern).
89 *
90 * FIXME: cached preference values are not updated if the user changes them during
91 * a JOSM session. Should have a listener listening to preference changes.
92 */
93 private static String DEFAULT_FONT_NAME = null;
94 private static Float DEFAULT_FONT_SIZE = null;
95 private static void initDefaultFontParameters() {
96 if (DEFAULT_FONT_NAME != null) return; // already initialized - skip initialization
97 DEFAULT_FONT_NAME = Main.pref.get("mappaint.font", "Helvetica");
98 DEFAULT_FONT_SIZE = (float) Main.pref.getInteger("mappaint.fontsize", 8);
99 }
100
101 private static class FontDescriptor {
102 public String name;
103 public int style;
104 public int size;
105
106 public FontDescriptor(String name, int style, int size){
107 this.name = name;
108 this.style = style;
109 this.size = size;
110 }
111
112 @Override
113 public int hashCode() {
114 final int prime = 31;
115 int result = 1;
116 result = prime * result + ((name == null) ? 0 : name.hashCode());
117 result = prime * result + size;
118 result = prime * result + style;
119 return result;
120 }
121 @Override
122 public boolean equals(Object obj) {
123 if (this == obj)
124 return true;
125 if (obj == null)
126 return false;
127 if (getClass() != obj.getClass())
128 return false;
129 FontDescriptor other = (FontDescriptor) obj;
130 if (name == null) {
131 if (other.name != null)
132 return false;
133 } else if (!name.equals(other.name))
134 return false;
135 if (size != other.size)
136 return false;
137 if (style != other.style)
138 return false;
139 return true;
140 }
141 }
142
143 private static final Map<FontDescriptor, Font> FONT_MAP = new HashMap<FontDescriptor, Font>();
144 private static Font getCachedFont(FontDescriptor fd) {
145 Font f = FONT_MAP.get(fd);
146 if (f != null) return f;
147 f = new Font(fd.name, fd.style, fd.size);
148 FONT_MAP.put(fd, f);
149 return f;
150 }
151
152 private static Font getCachedFont(String name, int style, int size){
153 return getCachedFont(new FontDescriptor(name, style, size));
154 }
155
156 protected static Font getFont(Cascade c) {
157 initDefaultFontParameters(); // populated cached preferences, if necessary
158 String name = c.get("font-family", DEFAULT_FONT_NAME, String.class);
159 float size = c.get("font-size", DEFAULT_FONT_SIZE, Float.class);
160 int weight = Font.PLAIN;
161 if ("bold".equalsIgnoreCase(c.get("font-weight", null, String.class))) {
162 weight = Font.BOLD;
163 }
164 int style = Font.PLAIN;
165 if ("italic".equalsIgnoreCase(c.get("font-style", null, String.class))) {
166 style = Font.ITALIC;
167 }
168 return getCachedFont(name, style | weight, Math.round(size));
169 }
170
171 @Override
172 public boolean equals(Object o) {
173 if (!(o instanceof ElemStyle))
174 return false;
175 ElemStyle s = (ElemStyle) o;
176 return major_z_index == s.major_z_index &&
177 z_index == s.z_index &&
178 object_z_index == s.object_z_index &&
179 isModifier == s.isModifier;
180 }
181
182 @Override
183 public int hashCode() {
184 int hash = 5;
185 hash = 41 * hash + Float.floatToIntBits(this.major_z_index);
186 hash = 41 * hash + Float.floatToIntBits(this.z_index);
187 hash = 41 * hash + Float.floatToIntBits(this.object_z_index);
188 hash = 41 * hash + (isModifier ? 1 : 0);
189 return hash;
190 }
191
192 @Override
193 public String toString() {
194 return String.format("z_idx=[%s/%s/%s] ", major_z_index, z_index, object_z_index) + (isModifier ? "modifier " : "");
195 }
196}
Note: See TracBrowser for help on using the repository browser.