001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.mappaint;
003
004import java.util.Objects;
005
006import org.openstreetmap.josm.data.osm.OsmPrimitive;
007import org.openstreetmap.josm.data.osm.Way;
008import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
009import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
010import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
011
012public class LineTextElemStyle extends ElemStyle {
013
014    private TextElement text;
015
016    protected LineTextElemStyle(Cascade c, TextElement text) {
017        super(c, 4.9f);
018        this.text = text;
019    }
020    public static LineTextElemStyle create(final Environment env) {
021        final Cascade c = env.mc.getCascade(env.layer);
022
023        Keyword textPos = c.get(TEXT_POSITION, null, Keyword.class);
024        if (textPos != null && !"line".equals(textPos.val))
025            return null;
026
027        TextElement text = TextElement.create(env, PaintColors.TEXT.get(), false);
028        if (text == null)
029            return null;
030        return new LineTextElemStyle(c, text);
031    }
032
033    @Override
034    public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, StyledMapRenderer painter, boolean selected, boolean member) {
035        Way w = (Way)primitive;
036        painter.drawTextOnPath(w, text);
037    }
038
039    @Override
040    public boolean equals(Object obj) {
041        if (obj == null || getClass() != obj.getClass())
042            return false;
043        if (!super.equals(obj))
044            return false;
045        final LineTextElemStyle other = (LineTextElemStyle) obj;
046        return Objects.equals(text, other.text);
047    }
048
049    @Override
050    public int hashCode() {
051        return text.hashCode();
052    }
053
054    @Override
055    public String toString() {
056        return "LineTextElemStyle{" + super.toString() + "text=" + text + "}";
057    }
058}