source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/TextElement.java@ 12378

Last change on this file since 12378 was 11932, checked in by Don-vip, 7 years ago

sonar - squid:S1941 - Variables should not be declared before they are relevant

File size: 3.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.styleelement;
3
4import java.util.Objects;
5
6import org.openstreetmap.josm.data.osm.OsmPrimitive;
7import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
8import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
9import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
10import org.openstreetmap.josm.gui.mappaint.Cascade;
11import org.openstreetmap.josm.gui.mappaint.Environment;
12import org.openstreetmap.josm.gui.mappaint.Keyword;
13import org.openstreetmap.josm.gui.mappaint.styleelement.placement.CompletelyInsideAreaStrategy;
14
15/**
16 * The text that is drawn for a way/area. It may be drawn along the outline or onto the way.
17 *
18 * @since 11722
19 */
20public class TextElement extends StyleElement {
21
22 private final TextLabel text;
23
24 protected TextElement(Cascade c, TextLabel text) {
25 super(c, 4.9f);
26 this.text = text;
27 }
28
29 /**
30 * Create a new text element
31 * @param env The environment to read the text data from
32 * @return The text element or <code>null</code> if it could not be created.
33 */
34 public static TextElement create(final Environment env) {
35 TextLabel text = TextLabel.create(env, PaintColors.TEXT.get(), false);
36 if (text == null)
37 return null;
38 final Cascade c = env.mc.getCascade(env.layer);
39 return new TextElement(c, text);
40 }
41
42 /**
43 * JOSM traditionally adds both line and content text elements if a fill style was set.
44 *
45 * For now, we simulate this by generating a TextElement if no text-position was provided.
46 * @param env The environment to read the text data from
47 * @return The text element or <code>null</code> if it could not be created.
48 */
49 public static TextElement createForContent(Environment env) {
50 final Cascade c = env.mc.getCascade(env.layer);
51 Keyword positionKeyword = c.get(AreaElement.TEXT_POSITION, null, Keyword.class);
52 if (positionKeyword != null) {
53 return null; // No need for this hack.
54 }
55
56 TextLabel text = TextLabel.create(env, PaintColors.TEXT.get(), true);
57 if (text == null) {
58 return null;
59 }
60 return new TextElement(c, text.withPosition(CompletelyInsideAreaStrategy.INSTANCE));
61 }
62
63 @Override
64 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, StyledMapRenderer painter,
65 boolean selected, boolean outermember, boolean member) {
66 painter.drawText(primitive, text);
67 }
68
69 @Override
70 public boolean equals(Object obj) {
71 if (this == obj) return true;
72 if (obj == null || getClass() != obj.getClass()) return false;
73 if (!super.equals(obj)) return false;
74 TextElement that = (TextElement) obj;
75 return Objects.equals(text, that.text);
76 }
77
78 @Override
79 public int hashCode() {
80 return Objects.hash(super.hashCode(), text);
81 }
82
83 @Override
84 public String toString() {
85 return "TextElement{" + super.toString() + "text=" + text + '}';
86 }
87}
Note: See TracBrowser for help on using the repository browser.