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

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

mapcss: some rework of Error Handling, (Multi)Cascade and icon loading

  • Property svn:eol-style set to native
File size: 14.7 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 Cascade c = new Cascade();
127 c.put("text", "auto");
128 SIMPLE_NODE_ELEMSTYLE = create(c, true);
129 }
130
131 protected NodeElemStyle(Cascade c, ImageIcon icon, int iconAlpha, Symbol symbol, NodeTextElement text) {
132 super(c);
133 this.icon = icon;
134 this.iconAlpha = iconAlpha;
135 this.symbol = symbol;
136 this.text = text;
137 }
138
139 public static NodeElemStyle create(Cascade c) {
140 return create(c, false);
141 }
142
143 private static NodeElemStyle create(Cascade c, boolean allowOnlyText) {
144 IconReference iconRef = c.get("icon-image", null, IconReference.class);
145 ImageIcon icon = null;
146 int iconAlpha = 0;
147 Symbol symbol = null;
148
149 if (iconRef != null) {
150 icon = MapPaintStyles.getIcon(iconRef, false);
151 if (icon == null) {
152 icon = MapPaintStyles.getNoIcon_Icon(iconRef.source, false);
153 }
154 iconAlpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255))));
155 Integer pAlpha = Utils.color_float2int(c.get("icon-opacity", null, float.class));
156 if (pAlpha != null) {
157 iconAlpha = pAlpha;
158 }
159 } else {
160 symbol = createSymbol(c);
161 }
162
163 if (icon == null && symbol == null && !allowOnlyText)
164 return null;
165
166 NodeTextElement text = null;
167 TextElement te = TextElement.create(c, PaintColors.TEXT.get());
168 if (te != null) {
169 HorizontalTextAlignment hAlign = HorizontalTextAlignment.RIGHT;
170 String hAlignStr = c.get("text-anchor-horizontal", null, String.class);
171 if (equal(hAlignStr, "left")) {
172 hAlign = HorizontalTextAlignment.LEFT;
173 } else if (equal(hAlignStr, "center")) {
174 hAlign = HorizontalTextAlignment.CENTER;
175 } else if (equal(hAlignStr, "right")) {
176 hAlign = HorizontalTextAlignment.RIGHT;
177 }
178 VerticalTextAlignment vAlign = VerticalTextAlignment.BOTTOM;
179 String vAlignStr = c.get("text-anchor-vertical", null, String.class);
180 if (equal(vAlignStr, "above")) {
181 vAlign = VerticalTextAlignment.ABOVE;
182 } else if (equal(vAlignStr, "top")) {
183 vAlign = VerticalTextAlignment.TOP;
184 } else if (equal(vAlignStr, "center")) {
185 vAlign = VerticalTextAlignment.CENTER;
186 } else if (equal(vAlignStr, "bottom")) {
187 vAlign = VerticalTextAlignment.BOTTOM;
188 } else if (equal(vAlignStr, "below")) {
189 vAlign = VerticalTextAlignment.BELOW;
190 }
191 text = new NodeTextElement(te.textKey, hAlign, vAlign, te.font, te.xOffset, te.yOffset, te.color);
192 }
193
194 return new NodeElemStyle(c, icon, iconAlpha, symbol, text);
195 }
196
197 private static Symbol createSymbol(Cascade c) {
198 SymbolShape shape;
199 String shapeStr = c.get("symbol-shape", null, String.class);
200 if (equal(shapeStr, "square")) {
201 shape = SymbolShape.SQUARE;
202 } else if (equal(shapeStr, "circle")) {
203 shape = SymbolShape.CIRCLE;
204 } else
205 return null;
206
207 float size = c.get("symbol-size", 10f, Float.class);
208 if (size <= 0)
209 return null;
210
211 Float strokeWidth = c.get("symbol-stroke-width", null, Float.class);
212 if (strokeWidth != null && strokeWidth <= 0) {
213 strokeWidth = null;
214 }
215 Color strokeColor = c.get("symbol-stroke-color", null, Color.class);
216
217 if (strokeWidth == null && strokeColor != null) {
218 strokeWidth = 1f;
219 } else if (strokeWidth != null && strokeColor == null) {
220 strokeColor = Color.ORANGE;
221 }
222
223 Stroke stroke = null;
224 if (strokeColor != null) {
225 float strokeAlpha = c.get("symbol-stroke-opacity", 1f, Float.class);
226 strokeColor = new Color(strokeColor.getRed(), strokeColor.getGreen(),
227 strokeColor.getBlue(), Utils.color_float2int(strokeAlpha));
228 stroke = new BasicStroke(strokeWidth);
229 }
230
231 Color fillColor = c.get("symbol-fill-color", null, Color.class);
232 if (stroke == null && fillColor == null)
233 fillColor = Color.BLUE;
234
235 if (fillColor != null) {
236 float fillAlpha = c.get("symbol-fill-opacity", 1f, Float.class);
237 fillColor = new Color(fillColor.getRed(), fillColor.getGreen(),
238 fillColor.getBlue(), Utils.color_float2int(fillAlpha));
239 }
240
241 return new Symbol(shape, Math.round(size), stroke, strokeColor, fillColor);
242 }
243
244 @Override
245 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings settings, MapPainter painter, boolean selected, boolean member) {
246 if (primitive instanceof Node) {
247 Node n = (Node) primitive;
248 if (icon != null && painter.isShowIcons()) {
249 painter.drawNodeIcon(n, (painter.isInactive() || n.isDisabled()) ? getDisabledIcon() : icon,
250 Utils.color_int2float(iconAlpha), selected, member, text);
251 } else if (symbol != null) {
252 Color fillColor = symbol.fillColor;
253 if (fillColor != null) {
254 if (n.isHighlighted()) {
255 fillColor = settings.getHighlightColor();
256 } else {
257 if (painter.isInactive() || n.isDisabled()) {
258 fillColor = settings.getInactiveColor();
259 } else if (selected) {
260 fillColor = settings.getSelectedColor(fillColor.getAlpha());
261 } else if (member) {
262 fillColor = settings.getRelationSelectedColor(fillColor.getAlpha());
263 }
264 }
265 }
266 Color strokeColor = symbol.strokeColor;
267 if (strokeColor != null) {
268 if (n.isHighlighted()) {
269 strokeColor = settings.getHighlightColor();
270 } else {
271 if (painter.isInactive() || n.isDisabled()) {
272 strokeColor = settings.getInactiveColor();
273 } else if (selected) {
274 strokeColor = settings.getSelectedColor(strokeColor.getAlpha());
275 } else if (member) {
276 strokeColor = settings.getRelationSelectedColor(strokeColor.getAlpha());
277 }
278 }
279 }
280 painter.drawNodeSymbol(n, symbol, fillColor, strokeColor, text);
281 } else {
282 if (n.isHighlighted()) {
283 painter.drawNode(n, settings.getHighlightColor(), settings.getSelectedNodeSize(), settings.isFillSelectedNode(), text);
284 } else {
285 Color color;
286 boolean isConnection = n.isConnectionNode();
287
288 if (painter.isInactive() || n.isDisabled()) {
289 color = settings.getInactiveColor();
290 } else if (selected) {
291 color = settings.getSelectedColor();
292 } else if (member) {
293 color = settings.getRelationSelectedColor();
294 } else if (isConnection) {
295 if (n.isTagged()) {
296 color = settings.getTaggedConnectionColor();
297 } else {
298 color = settings.getConnectionColor();
299 }
300 } else {
301 if (n.isTagged()) {
302 color = settings.getTaggedColor();
303 } else {
304 color = settings.getNodeColor();
305 }
306 }
307
308 final int size = Utils.max((selected ? settings.getSelectedNodeSize() : 0),
309 (n.isTagged() ? settings.getTaggedNodeSize() : 0),
310 (isConnection ? settings.getConnectionNodeSize() : 0),
311 settings.getUnselectedNodeSize());
312
313 final boolean fill = (selected && settings.isFillSelectedNode()) ||
314 (n.isTagged() && settings.isFillTaggedNode()) ||
315 (isConnection && settings.isFillConnectionNode()) ||
316 settings.isFillUnselectedNode();
317
318 painter.drawNode(n, color, size, fill, text);
319 }
320 }
321 } else if (primitive instanceof Relation && icon != null) {
322 painter.drawRestriction((Relation) primitive, this);
323 }
324 }
325
326 public ImageIcon getDisabledIcon() {
327 if (disabledIcon != null)
328 return disabledIcon;
329 if (icon == null)
330 return null;
331 return disabledIcon = new ImageIcon(GrayFilter.createDisabledImage(icon.getImage()));
332 }
333
334 @Override
335 public int hashCode() {
336 int hash = super.hashCode();
337 hash = 17 * hash + (icon != null ? icon.getImage().hashCode() : 0);
338 hash = 17 * hash + iconAlpha;
339 hash = 17 * hash + (symbol != null ? symbol.hashCode() : 0);
340 hash = 17 * hash + (text != null ? text.hashCode() : 0);
341 return hash;
342 }
343
344 @Override
345 public boolean equals(Object obj) {
346 if (obj == null || getClass() != obj.getClass())
347 return false;
348 if (!super.equals(obj))
349 return false;
350
351 final NodeElemStyle other = (NodeElemStyle) obj;
352 // we should get the same image object due to caching
353 if (icon != other.icon && (icon == null || other.icon == null || icon.getImage() != other.icon.getImage()))
354 return false;
355 if (this.iconAlpha != other.iconAlpha)
356 return false;
357 if (!equal(symbol, other.symbol))
358 return false;
359 if (!equal(text, other.text))
360 return false;
361 return true;
362 }
363
364
365 @Override
366 public String toString() {
367 return "NodeElemStyle{" + super.toString() +
368 (icon != null ? ("icon=" + icon + " iconAlpha=" + iconAlpha) : "") +
369 (symbol != null ? (" symbol=[" + symbol + "]") : "") + '}';
370 }
371
372}
Note: See TracBrowser for help on using the repository browser.