001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.validation.util;
003
004import static org.openstreetmap.josm.tools.I18n.trn;
005
006import javax.swing.Icon;
007import javax.swing.JLabel;
008
009import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
010import org.openstreetmap.josm.data.osm.Node;
011import org.openstreetmap.josm.data.osm.OsmPrimitive;
012import org.openstreetmap.josm.data.osm.Relation;
013import org.openstreetmap.josm.data.osm.Way;
014import org.openstreetmap.josm.data.osm.visitor.OsmPrimitiveVisitor;
015import org.openstreetmap.josm.tools.ImageProvider;
016
017/**
018 * Able to create a name and an icon for each data element.
019 *
020 * @author imi
021 */
022public class NameVisitor implements OsmPrimitiveVisitor {
023
024    /**
025     * The name of the item class
026     */
027    public String className;
028
029    /**
030     * The plural name of the item class
031     */
032    public String classNamePlural;
033
034    /**
035     * The name of this item.
036     */
037    public String name = "";
038
039    /**
040     * The icon of this item.
041     */
042    public Icon icon;
043
044    protected void setIcon(OsmPrimitive p) {
045        icon = ImageProvider.get(p.getDisplayType());
046    }
047
048    /**
049     * If the node has a name-key or id-key, this is displayed. If not, (lat,lon) is displayed.
050     */
051    @Override
052    public void visit(Node n) {
053        name = n.getDisplayName(DefaultNameFormatter.getInstance());
054        setIcon(n);
055        className = "node";
056        classNamePlural = trn("node", "nodes", 2);
057    }
058
059    /**
060     * If the way has a name-key or id-key, this is displayed. If not, (x nodes)
061     * is displayed with x being the number of nodes in the way.
062     */
063    @Override
064    public void visit(Way w) {
065        name = w.getDisplayName(DefaultNameFormatter.getInstance());
066        setIcon(w);
067        className = "way";
068        classNamePlural = trn("way", "ways", 2);
069    }
070
071    @Override
072    public void visit(Relation e) {
073        name = e.getDisplayName(DefaultNameFormatter.getInstance());
074        setIcon(e);
075        className = "relation";
076        classNamePlural = trn("relation", "relations", 2);
077    }
078
079    /**
080     * Returns an horizontal {@code JLabel} with icon and name.
081     * @return horizontal {@code JLabel} with icon and name
082     */
083    public JLabel toLabel() {
084        return new JLabel(name, icon, JLabel.HORIZONTAL);
085    }
086}