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

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

mapcss: several small fixes

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