1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.styleelement;
|
---|
3 |
|
---|
4 | import java.util.Objects;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
7 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
---|
8 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
9 | import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
|
---|
10 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
11 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
12 | import org.openstreetmap.josm.gui.mappaint.Keyword;
|
---|
13 | import org.openstreetmap.josm.gui.mappaint.styleelement.placement.CompletelyInsideAreaStrategy;
|
---|
14 | import org.openstreetmap.josm.gui.mappaint.styleelement.placement.PositionForAreaStrategy;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * The text that is drawn for a way/area. It may be drawn along the outline or onto the way.
|
---|
18 | *
|
---|
19 | * @since 11722
|
---|
20 | */
|
---|
21 | public class TextElement extends StyleElement {
|
---|
22 |
|
---|
23 | private final TextLabel text;
|
---|
24 | /**
|
---|
25 | * The position strategy for this text label.
|
---|
26 | */
|
---|
27 | private final PositionForAreaStrategy labelPositionStrategy;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Create a new way/area text element definition
|
---|
31 | * @param c The cascade
|
---|
32 | * @param text The text
|
---|
33 | * @param labelPositionStrategy The position in the area.
|
---|
34 | */
|
---|
35 | protected TextElement(Cascade c, TextLabel text, PositionForAreaStrategy labelPositionStrategy) {
|
---|
36 | super(c, 4.9f);
|
---|
37 | this.text = Objects.requireNonNull(text, "text");
|
---|
38 | this.labelPositionStrategy = Objects.requireNonNull(labelPositionStrategy, "labelPositionStrategy");
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Gets the strategy that defines where to place the label.
|
---|
43 | * @return The strategy. Never null.
|
---|
44 | * @since 12475
|
---|
45 | */
|
---|
46 | public PositionForAreaStrategy getLabelPositionStrategy() {
|
---|
47 | return labelPositionStrategy;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Create a new text element
|
---|
52 | * @param env The environment to read the text data from
|
---|
53 | * @return The text element or <code>null</code> if it could not be created.
|
---|
54 | */
|
---|
55 | public static TextElement create(final Environment env) {
|
---|
56 | TextLabel text = TextLabel.create(env, PaintColors.TEXT.get(), false);
|
---|
57 | if (text == null)
|
---|
58 | return null;
|
---|
59 | final Cascade c = env.mc.getCascade(env.layer);
|
---|
60 |
|
---|
61 | Keyword positionKeyword = c.get(AreaElement.TEXT_POSITION, null, Keyword.class);
|
---|
62 | PositionForAreaStrategy position = PositionForAreaStrategy.forKeyword(positionKeyword);
|
---|
63 | position = position.withAddedOffset(TextLabel.getTextOffset(c));
|
---|
64 |
|
---|
65 | return new TextElement(c, text, position);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * JOSM traditionally adds both line and content text elements if a fill style was set.
|
---|
70 | *
|
---|
71 | * For now, we simulate this by generating a TextElement if no text-position was provided.
|
---|
72 | * @param env The environment to read the text data from
|
---|
73 | * @return The text element or <code>null</code> if it could not be created.
|
---|
74 | */
|
---|
75 | public static TextElement createForContent(Environment env) {
|
---|
76 | final Cascade c = env.mc.getCascade(env.layer);
|
---|
77 | Keyword positionKeyword = c.get(AreaElement.TEXT_POSITION, null, Keyword.class);
|
---|
78 | if (positionKeyword != null) {
|
---|
79 | return null; // No need for this hack.
|
---|
80 | }
|
---|
81 |
|
---|
82 | TextLabel text = TextLabel.create(env, PaintColors.TEXT.get(), true);
|
---|
83 | if (text == null) {
|
---|
84 | return null;
|
---|
85 | }
|
---|
86 | return new TextElement(c, text, CompletelyInsideAreaStrategy.INSTANCE);
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public void paintPrimitive(IPrimitive primitive, MapPaintSettings paintSettings, StyledMapRenderer painter,
|
---|
91 | boolean selected, boolean outermember, boolean member) {
|
---|
92 | painter.drawText(primitive, text, getLabelPositionStrategy());
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public boolean equals(Object obj) {
|
---|
97 | if (this == obj) return true;
|
---|
98 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
99 | if (!super.equals(obj)) return false;
|
---|
100 | TextElement that = (TextElement) obj;
|
---|
101 | return Objects.equals(labelPositionStrategy, that.labelPositionStrategy)
|
---|
102 | && Objects.equals(text, that.text);
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public int hashCode() {
|
---|
107 | return Objects.hash(super.hashCode(), text, labelPositionStrategy);
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | public String toString() {
|
---|
112 | return "TextElement{" + super.toString() + "text=" + text + " labelPositionStrategy=" + labelPositionStrategy + '}';
|
---|
113 | }
|
---|
114 | }
|
---|