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

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

Move label / icon placement code to new package. Unify the handling of text label positioning (path / center)

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 final Cascade c = env.mc.getCascade(env.layer);
36
37 TextLabel text = TextLabel.create(env, PaintColors.TEXT.get(), false);
38 if (text == null)
39 return null;
40 return new TextElement(c, text);
41 }
42
43 /**
44 * JOSM traditionally adds both line and content text elements if a fill style was set.
45 *
46 * For now, we simulate this by generating a TextElement if no text-position was provided.
47 * @param env The environment to read the text data from
48 * @return The text element or <code>null</code> if it could not be created.
49 */
50 public static TextElement createForContent(Environment env) {
51 final Cascade c = env.mc.getCascade(env.layer);
52 Keyword positionKeyword = c.get(AreaElement.TEXT_POSITION, null, Keyword.class);
53 if (positionKeyword != null) {
54 return null; // No need for this hack.
55 }
56
57 TextLabel text = TextLabel.create(env, PaintColors.TEXT.get(), true);
58 if (text == null) {
59 return null;
60 }
61 return new TextElement(c, text.withPosition(CompletelyInsideAreaStrategy.INSTANCE));
62 }
63
64 @Override
65 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, StyledMapRenderer painter,
66 boolean selected, boolean outermember, boolean member) {
67 painter.drawText(primitive, text);
68 }
69
70 @Override
71 public boolean equals(Object obj) {
72 if (this == obj) return true;
73 if (obj == null || getClass() != obj.getClass()) return false;
74 if (!super.equals(obj)) return false;
75 TextElement that = (TextElement) obj;
76 return Objects.equals(text, that.text);
77 }
78
79 @Override
80 public int hashCode() {
81 return Objects.hash(super.hashCode(), text);
82 }
83
84 @Override
85 public String toString() {
86 return "TextElement{" + super.toString() + "text=" + text + '}';
87 }
88}
Note: See TracBrowser for help on using the repository browser.