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

Last change on this file since 11723 was 11723, checked in by michael2402, 7 years ago

Fix @since version.

File size: 3.0 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;
13
14/**
15 * The text that is drawn for a way/area. It may be drawn along the outline or onto the way.
16 *
17 * @since 11722
18 */
19public class TextElement extends StyleElement {
20
21 private final TextLabel text;
22
23 protected TextElement(Cascade c, TextLabel text) {
24 super(c, 4.9f);
25 this.text = text;
26 }
27
28 /**
29 * Create a new text element
30 * @param env The environment to read the text data from
31 * @return The text element or <code>null</code> if it could not be created.
32 */
33 public static TextElement create(final Environment env) {
34 final Cascade c = env.mc.getCascade(env.layer);
35
36 TextLabel text = TextLabel.create(env, PaintColors.TEXT.get(), false);
37 if (text == null)
38 return null;
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(PositionForAreaStrategy.INSIDE));
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.