source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/NodeElement.java@ 10242

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

sonar - pmd:InsufficientStringBufferDeclaration - Insufficient String Buffer Declaration

  • Property svn:eol-style set to native
File size: 16.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.styleelement;
3
4import java.awt.BasicStroke;
5import java.awt.Color;
6import java.awt.Rectangle;
7import java.awt.Stroke;
8import java.util.Objects;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.osm.Node;
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.data.osm.Relation;
14import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
15import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
16import org.openstreetmap.josm.gui.mappaint.Cascade;
17import org.openstreetmap.josm.gui.mappaint.Environment;
18import org.openstreetmap.josm.gui.mappaint.Keyword;
19import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
20import org.openstreetmap.josm.gui.mappaint.MultiCascade;
21import org.openstreetmap.josm.gui.mappaint.StyleElementList;
22import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement.BoxProvider;
23import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement.SimpleBoxProvider;
24import org.openstreetmap.josm.gui.util.RotationAngle;
25import org.openstreetmap.josm.tools.CheckParameterUtil;
26import org.openstreetmap.josm.tools.Utils;
27
28/**
29 * applies for Nodes and turn restriction relations
30 */
31public class NodeElement extends StyleElement {
32 public final MapImage mapImage;
33 public final RotationAngle mapImageAngle;
34 public final Symbol symbol;
35
36 public enum SymbolShape { SQUARE, CIRCLE, TRIANGLE, PENTAGON, HEXAGON, HEPTAGON, OCTAGON, NONAGON, DECAGON }
37
38 public static class Symbol {
39 public SymbolShape symbol;
40 public int size;
41 public Stroke stroke;
42 public Color strokeColor;
43 public Color fillColor;
44
45 public Symbol(SymbolShape symbol, int size, Stroke stroke, Color strokeColor, Color fillColor) {
46 if (stroke != null && strokeColor == null)
47 throw new IllegalArgumentException("Stroke given without color");
48 if (stroke == null && fillColor == null)
49 throw new IllegalArgumentException("Either a stroke or a fill color must be given");
50 this.symbol = symbol;
51 this.size = size;
52 this.stroke = stroke;
53 this.strokeColor = strokeColor;
54 this.fillColor = fillColor;
55 }
56
57 @Override
58 public boolean equals(Object obj) {
59 if (obj == null || getClass() != obj.getClass())
60 return false;
61 final Symbol other = (Symbol) obj;
62 return symbol == other.symbol &&
63 size == other.size &&
64 Objects.equals(stroke, other.stroke) &&
65 Objects.equals(strokeColor, other.strokeColor) &&
66 Objects.equals(fillColor, other.fillColor);
67 }
68
69 @Override
70 public int hashCode() {
71 return Objects.hash(symbol, size, stroke, strokeColor, fillColor);
72 }
73
74 @Override
75 public String toString() {
76 return "symbol=" + symbol + " size=" + size +
77 (stroke != null ? " stroke=" + stroke + " strokeColor=" + strokeColor : "") +
78 (fillColor != null ? " fillColor=" + fillColor : "");
79 }
80 }
81
82 private static final String[] ICON_KEYS = {ICON_IMAGE, ICON_WIDTH, ICON_HEIGHT, ICON_OPACITY, ICON_OFFSET_X, ICON_OFFSET_Y};
83
84 public static final NodeElement SIMPLE_NODE_ELEMSTYLE;
85 public static final BoxProvider SIMPLE_NODE_ELEMSTYLE_BOXPROVIDER;
86 static {
87 MultiCascade mc = new MultiCascade();
88 mc.getOrCreateCascade("default");
89 SIMPLE_NODE_ELEMSTYLE = create(new Environment(null, mc, "default", null), 4.1f, true);
90 if (SIMPLE_NODE_ELEMSTYLE == null) throw new AssertionError();
91 SIMPLE_NODE_ELEMSTYLE_BOXPROVIDER = SIMPLE_NODE_ELEMSTYLE.getBoxProvider();
92 }
93
94 public static final StyleElementList DEFAULT_NODE_STYLELIST = new StyleElementList(NodeElement.SIMPLE_NODE_ELEMSTYLE);
95 public static final StyleElementList DEFAULT_NODE_STYLELIST_TEXT = new StyleElementList(NodeElement.SIMPLE_NODE_ELEMSTYLE,
96 BoxTextElement.SIMPLE_NODE_TEXT_ELEMSTYLE);
97
98 protected NodeElement(Cascade c, MapImage mapImage, Symbol symbol, float defaultMajorZindex, RotationAngle rotationAngle) {
99 super(c, defaultMajorZindex);
100 this.mapImage = mapImage;
101 this.symbol = symbol;
102 this.mapImageAngle = rotationAngle;
103 }
104
105 public static NodeElement create(Environment env) {
106 return create(env, 4f, false);
107 }
108
109 private static NodeElement create(Environment env, float defaultMajorZindex, boolean allowDefault) {
110 Cascade c = env.mc.getCascade(env.layer);
111
112 MapImage mapImage = createIcon(env, ICON_KEYS);
113 Symbol symbol = null;
114 if (mapImage == null) {
115 symbol = createSymbol(env);
116 }
117 RotationAngle rotationAngle = null;
118 final Float angle = c.get(ICON_ROTATION, null, Float.class, true);
119 if (angle != null) {
120 rotationAngle = RotationAngle.buildStaticRotation(angle);
121 } else {
122 final Keyword rotationKW = c.get(ICON_ROTATION, null, Keyword.class);
123 if (rotationKW != null) {
124 if ("way".equals(rotationKW.val)) {
125 rotationAngle = RotationAngle.buildWayDirectionRotation();
126 } else {
127 try {
128 rotationAngle = RotationAngle.buildStaticRotation(rotationKW.val);
129 } catch (IllegalArgumentException ignore) {
130 if (Main.isTraceEnabled()) {
131 Main.trace(ignore.getMessage());
132 }
133 }
134 }
135 }
136 }
137
138 // optimization: if we neither have a symbol, nor a mapImage
139 // we don't have to check for the remaining style properties and we don't
140 // have to allocate a node element style.
141 if (!allowDefault && symbol == null && mapImage == null) return null;
142
143 return new NodeElement(c, mapImage, symbol, defaultMajorZindex, rotationAngle);
144 }
145
146 public static MapImage createIcon(final Environment env, final String[] keys) {
147 CheckParameterUtil.ensureParameterNotNull(env, "env");
148 CheckParameterUtil.ensureParameterNotNull(keys, "keys");
149
150 Cascade c = env.mc.getCascade(env.layer);
151
152 final IconReference iconRef = c.get(keys[ICON_IMAGE_IDX], null, IconReference.class, true);
153 if (iconRef == null)
154 return null;
155
156 Cascade cDef = env.mc.getCascade("default");
157
158 Float widthOnDefault = cDef.get(keys[ICON_WIDTH_IDX], null, Float.class);
159 if (widthOnDefault != null && widthOnDefault <= 0) {
160 widthOnDefault = null;
161 }
162 Float widthF = getWidth(c, keys[ICON_WIDTH_IDX], widthOnDefault);
163
164 Float heightOnDefault = cDef.get(keys[ICON_HEIGHT_IDX], null, Float.class);
165 if (heightOnDefault != null && heightOnDefault <= 0) {
166 heightOnDefault = null;
167 }
168 Float heightF = getWidth(c, keys[ICON_HEIGHT_IDX], heightOnDefault);
169
170 int width = widthF == null ? -1 : Math.round(widthF);
171 int height = heightF == null ? -1 : Math.round(heightF);
172
173 float offsetXF = 0f;
174 float offsetYF = 0f;
175 if (keys[ICON_OFFSET_X_IDX] != null) {
176 offsetXF = c.get(keys[ICON_OFFSET_X_IDX], 0f, Float.class);
177 offsetYF = c.get(keys[ICON_OFFSET_Y_IDX], 0f, Float.class);
178 }
179
180 final MapImage mapImage = new MapImage(iconRef.iconName, iconRef.source);
181
182 mapImage.width = width;
183 mapImage.height = height;
184 mapImage.offsetX = Math.round(offsetXF);
185 mapImage.offsetY = Math.round(offsetYF);
186
187 mapImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255))));
188 Integer pAlpha = Utils.color_float2int(c.get(keys[ICON_OPACITY_IDX], null, float.class));
189 if (pAlpha != null) {
190 mapImage.alpha = pAlpha;
191 }
192 return mapImage;
193 }
194
195 private static Symbol createSymbol(Environment env) {
196 Cascade c = env.mc.getCascade(env.layer);
197 Cascade cDef = env.mc.getCascade("default");
198
199 SymbolShape shape;
200 Keyword shapeKW = c.get("symbol-shape", null, Keyword.class);
201 if (shapeKW == null)
202 return null;
203 if ("square".equals(shapeKW.val)) {
204 shape = SymbolShape.SQUARE;
205 } else if ("circle".equals(shapeKW.val)) {
206 shape = SymbolShape.CIRCLE;
207 } else if ("triangle".equals(shapeKW.val)) {
208 shape = SymbolShape.TRIANGLE;
209 } else if ("pentagon".equals(shapeKW.val)) {
210 shape = SymbolShape.PENTAGON;
211 } else if ("hexagon".equals(shapeKW.val)) {
212 shape = SymbolShape.HEXAGON;
213 } else if ("heptagon".equals(shapeKW.val)) {
214 shape = SymbolShape.HEPTAGON;
215 } else if ("octagon".equals(shapeKW.val)) {
216 shape = SymbolShape.OCTAGON;
217 } else if ("nonagon".equals(shapeKW.val)) {
218 shape = SymbolShape.NONAGON;
219 } else if ("decagon".equals(shapeKW.val)) {
220 shape = SymbolShape.DECAGON;
221 } else
222 return null;
223
224 Float sizeOnDefault = cDef.get("symbol-size", null, Float.class);
225 if (sizeOnDefault != null && sizeOnDefault <= 0) {
226 sizeOnDefault = null;
227 }
228 Float size = getWidth(c, "symbol-size", sizeOnDefault);
229
230 if (size == null) {
231 size = 10f;
232 }
233
234 if (size <= 0)
235 return null;
236
237 Float strokeWidthOnDefault = getWidth(cDef, "symbol-stroke-width", null);
238 Float strokeWidth = getWidth(c, "symbol-stroke-width", strokeWidthOnDefault);
239
240 Color strokeColor = c.get("symbol-stroke-color", null, Color.class);
241
242 if (strokeWidth == null && strokeColor != null) {
243 strokeWidth = 1f;
244 } else if (strokeWidth != null && strokeColor == null) {
245 strokeColor = Color.ORANGE;
246 }
247
248 Stroke stroke = null;
249 if (strokeColor != null && strokeWidth != null) {
250 Integer strokeAlpha = Utils.color_float2int(c.get("symbol-stroke-opacity", null, Float.class));
251 if (strokeAlpha != null) {
252 strokeColor = new Color(strokeColor.getRed(), strokeColor.getGreen(),
253 strokeColor.getBlue(), strokeAlpha);
254 }
255 stroke = new BasicStroke(strokeWidth);
256 }
257
258 Color fillColor = c.get("symbol-fill-color", null, Color.class);
259 if (stroke == null && fillColor == null) {
260 fillColor = Color.BLUE;
261 }
262
263 if (fillColor != null) {
264 Integer fillAlpha = Utils.color_float2int(c.get("symbol-fill-opacity", null, Float.class));
265 if (fillAlpha != null) {
266 fillColor = new Color(fillColor.getRed(), fillColor.getGreen(),
267 fillColor.getBlue(), fillAlpha);
268 }
269 }
270
271 return new Symbol(shape, Math.round(size), stroke, strokeColor, fillColor);
272 }
273
274 @Override
275 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings settings, StyledMapRenderer painter,
276 boolean selected, boolean outermember, boolean member) {
277 if (primitive instanceof Node) {
278 Node n = (Node) primitive;
279 if (mapImage != null && painter.isShowIcons()) {
280 painter.drawNodeIcon(n, mapImage, painter.isInactiveMode() || n.isDisabled(), selected, member,
281 mapImageAngle == null ? 0.0 : mapImageAngle.getRotationAngle(primitive));
282 } else if (symbol != null) {
283 Color fillColor = symbol.fillColor;
284 if (fillColor != null) {
285 if (painter.isInactiveMode() || n.isDisabled()) {
286 fillColor = settings.getInactiveColor();
287 } else if (defaultSelectedHandling && selected) {
288 fillColor = settings.getSelectedColor(fillColor.getAlpha());
289 } else if (member) {
290 fillColor = settings.getRelationSelectedColor(fillColor.getAlpha());
291 }
292 }
293 Color strokeColor = symbol.strokeColor;
294 if (strokeColor != null) {
295 if (painter.isInactiveMode() || n.isDisabled()) {
296 strokeColor = settings.getInactiveColor();
297 } else if (defaultSelectedHandling && selected) {
298 strokeColor = settings.getSelectedColor(strokeColor.getAlpha());
299 } else if (member) {
300 strokeColor = settings.getRelationSelectedColor(strokeColor.getAlpha());
301 }
302 }
303 painter.drawNodeSymbol(n, symbol, fillColor, strokeColor);
304 } else {
305 Color color;
306 boolean isConnection = n.isConnectionNode();
307
308 if (painter.isInactiveMode() || n.isDisabled()) {
309 color = settings.getInactiveColor();
310 } else if (selected) {
311 color = settings.getSelectedColor();
312 } else if (member) {
313 color = settings.getRelationSelectedColor();
314 } else if (isConnection) {
315 if (n.isTagged()) {
316 color = settings.getTaggedConnectionColor();
317 } else {
318 color = settings.getConnectionColor();
319 }
320 } else {
321 if (n.isTagged()) {
322 color = settings.getTaggedColor();
323 } else {
324 color = settings.getNodeColor();
325 }
326 }
327
328 final int size = Utils.max(selected ? settings.getSelectedNodeSize() : 0,
329 n.isTagged() ? settings.getTaggedNodeSize() : 0,
330 isConnection ? settings.getConnectionNodeSize() : 0,
331 settings.getUnselectedNodeSize());
332
333 final boolean fill = (selected && settings.isFillSelectedNode()) ||
334 (n.isTagged() && settings.isFillTaggedNode()) ||
335 (isConnection && settings.isFillConnectionNode()) ||
336 settings.isFillUnselectedNode();
337
338 painter.drawNode(n, color, size, fill);
339
340 }
341 } else if (primitive instanceof Relation && mapImage != null) {
342 painter.drawRestriction((Relation) primitive, mapImage, painter.isInactiveMode() || primitive.isDisabled());
343 }
344 }
345
346 public BoxProvider getBoxProvider() {
347 if (mapImage != null)
348 return mapImage.getBoxProvider();
349 else if (symbol != null)
350 return new SimpleBoxProvider(new Rectangle(-symbol.size/2, -symbol.size/2, symbol.size, symbol.size));
351 else {
352 // This is only executed once, so no performance concerns.
353 // However, it would be better, if the settings could be changed at runtime.
354 int size = Utils.max(
355 Main.pref.getInteger("mappaint.node.selected-size", 5),
356 Main.pref.getInteger("mappaint.node.unselected-size", 3),
357 Main.pref.getInteger("mappaint.node.connection-size", 5),
358 Main.pref.getInteger("mappaint.node.tagged-size", 3)
359 );
360 return new SimpleBoxProvider(new Rectangle(-size/2, -size/2, size, size));
361 }
362 }
363
364 @Override
365 public int hashCode() {
366 return Objects.hash(super.hashCode(), mapImage, mapImageAngle, symbol);
367 }
368
369 @Override
370 public boolean equals(Object obj) {
371 if (this == obj) return true;
372 if (obj == null || getClass() != obj.getClass()) return false;
373 if (!super.equals(obj)) return false;
374 NodeElement that = (NodeElement) obj;
375 return Objects.equals(mapImage, that.mapImage) &&
376 Objects.equals(mapImageAngle, that.mapImageAngle) &&
377 Objects.equals(symbol, that.symbol);
378 }
379
380 @Override
381 public String toString() {
382 StringBuilder s = new StringBuilder(64).append("NodeElemStyle{");
383 s.append(super.toString());
384 if (mapImage != null) {
385 s.append(" icon=[" + mapImage + ']');
386 }
387 if (symbol != null) {
388 s.append(" symbol=[" + symbol + ']');
389 }
390 if (mapImageAngle != null) {
391 s.append(" mapImageAngle=[" + mapImageAngle + ']');
392 }
393 s.append('}');
394 return s.toString();
395 }
396}
Note: See TracBrowser for help on using the repository browser.