source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java@ 3967

Last change on this file since 3967 was 3967, checked in by bastiK, 13 years ago

MapCSS: an identifier literal at the right side of a declaration is now parsed as Keyword and not as String. This means 'color: "blue";' does not work any longer, but you have to write 'color: blue;'. This is useful for future extensions.

  • Property svn:eol-style set to native
File size: 15.3 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.BasicStroke;
7import java.awt.Color;
8import java.awt.Font;
9import java.awt.Stroke;
10
11import javax.swing.GrayFilter;
12import javax.swing.ImageIcon;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.osm.Node;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.Relation;
18import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
19import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
20import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
21import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
22import org.openstreetmap.josm.tools.CheckParameterUtil;
23import org.openstreetmap.josm.tools.Utils;
24
25/**
26 * applies for Nodes and turn restriction relations
27 */
28public class NodeElemStyle extends ElemStyle {
29
30 public ImageIcon icon;
31 public int iconAlpha;
32 public Symbol symbol;
33 public NodeTextElement text;
34
35 private ImageIcon disabledIcon;
36
37 public enum SymbolShape { SQUARE, CIRCLE }
38 public enum HorizontalTextAlignment { LEFT, CENTER, RIGHT }
39 public enum VerticalTextAlignment { ABOVE, TOP, CENTER, BOTTOM, BELOW }
40
41 public static class Symbol {
42 public SymbolShape symbol;
43 public int size;
44 public Stroke stroke;
45 public Color strokeColor;
46 public Color fillColor;
47
48 public Symbol(SymbolShape symbol, int size, Stroke stroke, Color strokeColor, Color fillColor) {
49 if (stroke != null && strokeColor == null)
50 throw new IllegalArgumentException();
51 if (stroke == null && fillColor == null)
52 throw new IllegalArgumentException();
53 this.symbol = symbol;
54 this.size = size;
55 this.stroke = stroke;
56 this.strokeColor = strokeColor;
57 this.fillColor = fillColor;
58 }
59
60 @Override
61 public boolean equals(Object obj) {
62 if (obj == null || getClass() != obj.getClass())
63 return false;
64 final Symbol other = (Symbol) obj;
65 return symbol == other.symbol &&
66 size == other.size &&
67 equal(stroke, other.stroke) &&
68 equal(strokeColor, other.strokeColor) &&
69 equal(fillColor, other.fillColor);
70 }
71
72 @Override
73 public int hashCode() {
74 int hash = 7;
75 hash = 67 * hash + symbol.hashCode();
76 hash = 67 * hash + size;
77 hash = 67 * hash + (stroke != null ? stroke.hashCode() : 0);
78 hash = 67 * hash + (strokeColor != null ? strokeColor.hashCode() : 0);
79 hash = 67 * hash + (fillColor != null ? fillColor.hashCode() : 0);
80 return hash;
81 }
82
83 @Override
84 public String toString() {
85 return "symbol=" + symbol + " size=" + size +
86 (stroke != null ? (" stroke=" + stroke + " strokeColor=" + strokeColor) : "") +
87 (fillColor != null ? (" fillColor=" + fillColor) : "");
88 }
89 }
90
91 public static class NodeTextElement extends TextElement {
92 public HorizontalTextAlignment hAlign;
93 public VerticalTextAlignment vAlign;
94
95 public NodeTextElement(String textKey, HorizontalTextAlignment hAlign, VerticalTextAlignment vAlign, Font font, int xOffset, int yOffset, Color color) {
96 super(textKey, font, xOffset, yOffset, color);
97 CheckParameterUtil.ensureParameterNotNull(hAlign);
98 CheckParameterUtil.ensureParameterNotNull(vAlign);
99 this.hAlign = hAlign;
100 this.vAlign = vAlign;
101 }
102
103 @Override
104 public boolean equals(Object obj) {
105 if (!super.equals(obj))
106 return false;
107 if (obj == null || getClass() != obj.getClass())
108 return false;
109 final NodeTextElement other = (NodeTextElement) obj;
110 return hAlign == other.hAlign &&
111 vAlign == other.vAlign;
112 }
113
114 @Override
115 public int hashCode() {
116 int hash = super.hashCode();
117 hash = 97 * hash + hAlign.hashCode();
118 hash = 97 * hash + vAlign.hashCode();
119 return hash;
120 }
121
122 }
123
124 public static final NodeElemStyle SIMPLE_NODE_ELEMSTYLE;
125 static {
126 MultiCascade mc = new MultiCascade();
127 Cascade c = mc.getOrCreateCascade("default");
128 c.put("text", "auto");
129 SIMPLE_NODE_ELEMSTYLE = create(new Environment(null, mc, "default", null), true);
130 }
131
132 protected NodeElemStyle(Cascade c, ImageIcon icon, int iconAlpha, Symbol symbol, NodeTextElement text) {
133 super(c);
134 this.icon = icon;
135 this.iconAlpha = iconAlpha;
136 this.symbol = symbol;
137 this.text = text;
138 }
139
140 public static NodeElemStyle create(Environment env) {
141 return create(env, false);
142 }
143
144 private static NodeElemStyle create(Environment env, boolean allowOnlyText) {
145 Cascade c = env.mc.getCascade(env.layer);
146
147 IconReference iconRef = c.get("icon-image", null, IconReference.class);
148 ImageIcon icon = null;
149 int iconAlpha = 0;
150 Symbol symbol = null;
151
152 if (iconRef != null) {
153 icon = MapPaintStyles.getIcon(iconRef, false);
154 if (icon == null) {
155 icon = MapPaintStyles.getNoIcon_Icon(iconRef.source, false);
156 }
157 iconAlpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255))));
158 Integer pAlpha = Utils.color_float2int(c.get("icon-opacity", null, float.class));
159 if (pAlpha != null) {
160 iconAlpha = pAlpha;
161 }
162 } else {
163 symbol = createSymbol(env);
164 }
165
166 if (icon == null && symbol == null && !allowOnlyText)
167 return null;
168
169 NodeTextElement text = null;
170 TextElement te = TextElement.create(c, PaintColors.TEXT.get());
171 if (te != null) {
172 HorizontalTextAlignment hAlign = HorizontalTextAlignment.RIGHT;
173 Keyword hAlignKW = c.get("text-anchor-horizontal", Keyword.RIGHT, Keyword.class);
174 if (equal(hAlignKW.val, "left")) {
175 hAlign = HorizontalTextAlignment.LEFT;
176 } else if (equal(hAlignKW.val, "center")) {
177 hAlign = HorizontalTextAlignment.CENTER;
178 } else if (equal(hAlignKW.val, "right")) {
179 hAlign = HorizontalTextAlignment.RIGHT;
180 }
181 VerticalTextAlignment vAlign = VerticalTextAlignment.BOTTOM;
182 String vAlignStr = c.get("text-anchor-vertical", Keyword.BOTTOM, Keyword.class).val;
183 if (equal(vAlignStr, "above")) {
184 vAlign = VerticalTextAlignment.ABOVE;
185 } else if (equal(vAlignStr, "top")) {
186 vAlign = VerticalTextAlignment.TOP;
187 } else if (equal(vAlignStr, "center")) {
188 vAlign = VerticalTextAlignment.CENTER;
189 } else if (equal(vAlignStr, "bottom")) {
190 vAlign = VerticalTextAlignment.BOTTOM;
191 } else if (equal(vAlignStr, "below")) {
192 vAlign = VerticalTextAlignment.BELOW;
193 }
194 text = new NodeTextElement(te.textKey, hAlign, vAlign, te.font, te.xOffset, te.yOffset, te.color);
195 }
196
197 return new NodeElemStyle(c, icon, iconAlpha, symbol, text);
198 }
199
200 private static Symbol createSymbol(Environment env) {
201 Cascade c = env.mc.getCascade(env.layer);
202 Cascade c_def = env.mc.getCascade("default");
203
204 SymbolShape shape;
205 Keyword shapeKW = c.get("symbol-shape", null, Keyword.class);
206 if (shapeKW == null)
207 return null;
208 if (equal(shapeKW, "square")) {
209 shape = SymbolShape.SQUARE;
210 } else if (equal(shapeKW, "circle")) {
211 shape = SymbolShape.CIRCLE;
212 } else
213 return null;
214
215 Float sizeOnDefault = c_def.get("symbol-size", null, Float.class);
216 if (sizeOnDefault != null && sizeOnDefault <= 0) {
217 sizeOnDefault = null;
218 }
219 Float size = getWidth(c, "symbol-size", sizeOnDefault);
220
221 if (size == null) {
222 size = 10f;
223 }
224
225 if (size <= 0)
226 return null;
227
228 Float strokeWidthOnDefault = getWidth(c_def, "symbol-stroke-width", null);
229 Float strokeWidth = getWidth(c, "symbol-stroke-width", strokeWidthOnDefault);
230
231 Color strokeColor = c.get("symbol-stroke-color", null, Color.class);
232
233 if (strokeWidth == null && strokeColor != null) {
234 strokeWidth = 1f;
235 } else if (strokeWidth != null && strokeColor == null) {
236 strokeColor = Color.ORANGE;
237 }
238
239 Stroke stroke = null;
240 if (strokeColor != null) {
241 float strokeAlpha = c.get("symbol-stroke-opacity", 1f, Float.class);
242 strokeColor = new Color(strokeColor.getRed(), strokeColor.getGreen(),
243 strokeColor.getBlue(), Utils.color_float2int(strokeAlpha));
244 stroke = new BasicStroke(strokeWidth);
245 }
246
247 Color fillColor = c.get("symbol-fill-color", null, Color.class);
248 if (stroke == null && fillColor == null)
249 fillColor = Color.BLUE;
250
251 if (fillColor != null) {
252 float fillAlpha = c.get("symbol-fill-opacity", 1f, Float.class);
253 fillColor = new Color(fillColor.getRed(), fillColor.getGreen(),
254 fillColor.getBlue(), Utils.color_float2int(fillAlpha));
255 }
256
257 return new Symbol(shape, Math.round(size), stroke, strokeColor, fillColor);
258 }
259
260 @Override
261 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings settings, MapPainter painter, boolean selected, boolean member) {
262 if (primitive instanceof Node) {
263 Node n = (Node) primitive;
264 if (icon != null && painter.isShowIcons()) {
265 painter.drawNodeIcon(n, (painter.isInactive() || n.isDisabled()) ? getDisabledIcon() : icon,
266 Utils.color_int2float(iconAlpha), selected, member, text);
267 } else if (symbol != null) {
268 Color fillColor = symbol.fillColor;
269 if (fillColor != null) {
270 if (n.isHighlighted()) {
271 fillColor = settings.getHighlightColor();
272 } else {
273 if (painter.isInactive() || n.isDisabled()) {
274 fillColor = settings.getInactiveColor();
275 } else if (selected) {
276 fillColor = settings.getSelectedColor(fillColor.getAlpha());
277 } else if (member) {
278 fillColor = settings.getRelationSelectedColor(fillColor.getAlpha());
279 }
280 }
281 }
282 Color strokeColor = symbol.strokeColor;
283 if (strokeColor != null) {
284 if (n.isHighlighted()) {
285 strokeColor = settings.getHighlightColor();
286 } else {
287 if (painter.isInactive() || n.isDisabled()) {
288 strokeColor = settings.getInactiveColor();
289 } else if (selected) {
290 strokeColor = settings.getSelectedColor(strokeColor.getAlpha());
291 } else if (member) {
292 strokeColor = settings.getRelationSelectedColor(strokeColor.getAlpha());
293 }
294 }
295 }
296 painter.drawNodeSymbol(n, symbol, fillColor, strokeColor, text);
297 } else {
298 if (n.isHighlighted()) {
299 painter.drawNode(n, settings.getHighlightColor(), settings.getSelectedNodeSize(), settings.isFillSelectedNode(), text);
300 } else {
301 Color color;
302 boolean isConnection = n.isConnectionNode();
303
304 if (painter.isInactive() || n.isDisabled()) {
305 color = settings.getInactiveColor();
306 } else if (selected) {
307 color = settings.getSelectedColor();
308 } else if (member) {
309 color = settings.getRelationSelectedColor();
310 } else if (isConnection) {
311 if (n.isTagged()) {
312 color = settings.getTaggedConnectionColor();
313 } else {
314 color = settings.getConnectionColor();
315 }
316 } else {
317 if (n.isTagged()) {
318 color = settings.getTaggedColor();
319 } else {
320 color = settings.getNodeColor();
321 }
322 }
323
324 final int size = Utils.max((selected ? settings.getSelectedNodeSize() : 0),
325 (n.isTagged() ? settings.getTaggedNodeSize() : 0),
326 (isConnection ? settings.getConnectionNodeSize() : 0),
327 settings.getUnselectedNodeSize());
328
329 final boolean fill = (selected && settings.isFillSelectedNode()) ||
330 (n.isTagged() && settings.isFillTaggedNode()) ||
331 (isConnection && settings.isFillConnectionNode()) ||
332 settings.isFillUnselectedNode();
333
334 painter.drawNode(n, color, size, fill, text);
335 }
336 }
337 } else if (primitive instanceof Relation && icon != null) {
338 painter.drawRestriction((Relation) primitive, this);
339 }
340 }
341
342 public ImageIcon getDisabledIcon() {
343 if (disabledIcon != null)
344 return disabledIcon;
345 if (icon == null)
346 return null;
347 return disabledIcon = new ImageIcon(GrayFilter.createDisabledImage(icon.getImage()));
348 }
349
350 @Override
351 public int hashCode() {
352 int hash = super.hashCode();
353 hash = 17 * hash + (icon != null ? icon.getImage().hashCode() : 0);
354 hash = 17 * hash + iconAlpha;
355 hash = 17 * hash + (symbol != null ? symbol.hashCode() : 0);
356 hash = 17 * hash + (text != null ? text.hashCode() : 0);
357 return hash;
358 }
359
360 @Override
361 public boolean equals(Object obj) {
362 if (obj == null || getClass() != obj.getClass())
363 return false;
364 if (!super.equals(obj))
365 return false;
366
367 final NodeElemStyle other = (NodeElemStyle) obj;
368 // we should get the same image object due to caching
369 if (icon != other.icon && (icon == null || other.icon == null || icon.getImage() != other.icon.getImage()))
370 return false;
371 if (this.iconAlpha != other.iconAlpha)
372 return false;
373 if (!equal(symbol, other.symbol))
374 return false;
375 if (!equal(text, other.text))
376 return false;
377 return true;
378 }
379
380
381 @Override
382 public String toString() {
383 return "NodeElemStyle{" + super.toString() +
384 (icon != null ? ("icon=" + icon + " iconAlpha=" + iconAlpha) : "") +
385 (symbol != null ? (" symbol=[" + symbol + "]") : "") + '}';
386 }
387
388}
Note: See TracBrowser for help on using the repository browser.