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

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

mapcss: minor fixes

  • Property svn:eol-style set to native
File size: 15.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.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 String hAlignStr = c.get("text-anchor-horizontal", null, String.class);
174 if (equal(hAlignStr, "left")) {
175 hAlign = HorizontalTextAlignment.LEFT;
176 } else if (equal(hAlignStr, "center")) {
177 hAlign = HorizontalTextAlignment.CENTER;
178 } else if (equal(hAlignStr, "right")) {
179 hAlign = HorizontalTextAlignment.RIGHT;
180 }
181 VerticalTextAlignment vAlign = VerticalTextAlignment.BOTTOM;
182 String vAlignStr = c.get("text-anchor-vertical", null, String.class);
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 String shapeStr = c.get("symbol-shape", null, String.class);
206 if (equal(shapeStr, "square")) {
207 shape = SymbolShape.SQUARE;
208 } else if (equal(shapeStr, "circle")) {
209 shape = SymbolShape.CIRCLE;
210 } else
211 return null;
212
213 Float sizeOnDefault = c_def.get("symbol-size", null, Float.class);
214 if (sizeOnDefault != null && sizeOnDefault <= 0) {
215 sizeOnDefault = null;
216 }
217 Float size = getWidth(c, "symbol-size", sizeOnDefault);
218
219 if (size == null) {
220 size = 10f;
221 }
222
223 if (size <= 0)
224 return null;
225
226 Float strokeWidthOnDefault = getWidth(c_def, "symbol-stroke-width", null);
227 Float strokeWidth = getWidth(c, "symbol-stroke-width", strokeWidthOnDefault);
228
229 Color strokeColor = c.get("symbol-stroke-color", null, Color.class);
230
231 if (strokeWidth == null && strokeColor != null) {
232 strokeWidth = 1f;
233 } else if (strokeWidth != null && strokeColor == null) {
234 strokeColor = Color.ORANGE;
235 }
236
237 Stroke stroke = null;
238 if (strokeColor != null) {
239 float strokeAlpha = c.get("symbol-stroke-opacity", 1f, Float.class);
240 strokeColor = new Color(strokeColor.getRed(), strokeColor.getGreen(),
241 strokeColor.getBlue(), Utils.color_float2int(strokeAlpha));
242 stroke = new BasicStroke(strokeWidth);
243 }
244
245 Color fillColor = c.get("symbol-fill-color", null, Color.class);
246 if (stroke == null && fillColor == null)
247 fillColor = Color.BLUE;
248
249 if (fillColor != null) {
250 float fillAlpha = c.get("symbol-fill-opacity", 1f, Float.class);
251 fillColor = new Color(fillColor.getRed(), fillColor.getGreen(),
252 fillColor.getBlue(), Utils.color_float2int(fillAlpha));
253 }
254
255 return new Symbol(shape, Math.round(size), stroke, strokeColor, fillColor);
256 }
257
258 @Override
259 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings settings, MapPainter painter, boolean selected, boolean member) {
260 if (primitive instanceof Node) {
261 Node n = (Node) primitive;
262 if (icon != null && painter.isShowIcons()) {
263 painter.drawNodeIcon(n, (painter.isInactive() || n.isDisabled()) ? getDisabledIcon() : icon,
264 Utils.color_int2float(iconAlpha), selected, member, text);
265 } else if (symbol != null) {
266 Color fillColor = symbol.fillColor;
267 if (fillColor != null) {
268 if (n.isHighlighted()) {
269 fillColor = settings.getHighlightColor();
270 } else {
271 if (painter.isInactive() || n.isDisabled()) {
272 fillColor = settings.getInactiveColor();
273 } else if (selected) {
274 fillColor = settings.getSelectedColor(fillColor.getAlpha());
275 } else if (member) {
276 fillColor = settings.getRelationSelectedColor(fillColor.getAlpha());
277 }
278 }
279 }
280 Color strokeColor = symbol.strokeColor;
281 if (strokeColor != null) {
282 if (n.isHighlighted()) {
283 strokeColor = settings.getHighlightColor();
284 } else {
285 if (painter.isInactive() || n.isDisabled()) {
286 strokeColor = settings.getInactiveColor();
287 } else if (selected) {
288 strokeColor = settings.getSelectedColor(strokeColor.getAlpha());
289 } else if (member) {
290 strokeColor = settings.getRelationSelectedColor(strokeColor.getAlpha());
291 }
292 }
293 }
294 painter.drawNodeSymbol(n, symbol, fillColor, strokeColor, text);
295 } else {
296 if (n.isHighlighted()) {
297 painter.drawNode(n, settings.getHighlightColor(), settings.getSelectedNodeSize(), settings.isFillSelectedNode(), text);
298 } else {
299 Color color;
300 boolean isConnection = n.isConnectionNode();
301
302 if (painter.isInactive() || n.isDisabled()) {
303 color = settings.getInactiveColor();
304 } else if (selected) {
305 color = settings.getSelectedColor();
306 } else if (member) {
307 color = settings.getRelationSelectedColor();
308 } else if (isConnection) {
309 if (n.isTagged()) {
310 color = settings.getTaggedConnectionColor();
311 } else {
312 color = settings.getConnectionColor();
313 }
314 } else {
315 if (n.isTagged()) {
316 color = settings.getTaggedColor();
317 } else {
318 color = settings.getNodeColor();
319 }
320 }
321
322 final int size = Utils.max((selected ? settings.getSelectedNodeSize() : 0),
323 (n.isTagged() ? settings.getTaggedNodeSize() : 0),
324 (isConnection ? settings.getConnectionNodeSize() : 0),
325 settings.getUnselectedNodeSize());
326
327 final boolean fill = (selected && settings.isFillSelectedNode()) ||
328 (n.isTagged() && settings.isFillTaggedNode()) ||
329 (isConnection && settings.isFillConnectionNode()) ||
330 settings.isFillUnselectedNode();
331
332 painter.drawNode(n, color, size, fill, text);
333 }
334 }
335 } else if (primitive instanceof Relation && icon != null) {
336 painter.drawRestriction((Relation) primitive, this);
337 }
338 }
339
340 public ImageIcon getDisabledIcon() {
341 if (disabledIcon != null)
342 return disabledIcon;
343 if (icon == null)
344 return null;
345 return disabledIcon = new ImageIcon(GrayFilter.createDisabledImage(icon.getImage()));
346 }
347
348 @Override
349 public int hashCode() {
350 int hash = super.hashCode();
351 hash = 17 * hash + (icon != null ? icon.getImage().hashCode() : 0);
352 hash = 17 * hash + iconAlpha;
353 hash = 17 * hash + (symbol != null ? symbol.hashCode() : 0);
354 hash = 17 * hash + (text != null ? text.hashCode() : 0);
355 return hash;
356 }
357
358 @Override
359 public boolean equals(Object obj) {
360 if (obj == null || getClass() != obj.getClass())
361 return false;
362 if (!super.equals(obj))
363 return false;
364
365 final NodeElemStyle other = (NodeElemStyle) obj;
366 // we should get the same image object due to caching
367 if (icon != other.icon && (icon == null || other.icon == null || icon.getImage() != other.icon.getImage()))
368 return false;
369 if (this.iconAlpha != other.iconAlpha)
370 return false;
371 if (!equal(symbol, other.symbol))
372 return false;
373 if (!equal(text, other.text))
374 return false;
375 return true;
376 }
377
378
379 @Override
380 public String toString() {
381 return "NodeElemStyle{" + super.toString() +
382 (icon != null ? ("icon=" + icon + " iconAlpha=" + iconAlpha) : "") +
383 (symbol != null ? (" symbol=[" + symbol + "]") : "") + '}';
384 }
385
386}
Note: See TracBrowser for help on using the repository browser.