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.Relation;
012import org.openstreetmap.josm.data.osm.Way;
013import org.openstreetmap.josm.data.osm.visitor.OsmPrimitiveVisitor;
014import org.openstreetmap.josm.tools.ImageProvider;
015
016/**
017 * Able to create a name and an icon for each data element.
018 *
019 * @author imi
020 */
021public class NameVisitor implements OsmPrimitiveVisitor {
022
023    /**
024     * The name of the item class
025     */
026    public String className;
027
028    /**
029     * The plural name of the item class
030     */
031    public String classNamePlural;
032
033    /**
034     * The name of this item.
035     */
036    public String name = "";
037
038    /**
039     * The icon of this item.
040     */
041    public Icon icon;
042
043    private static final Icon nodeIcon = ImageProvider.get("data", "node");
044    private static final Icon wayIcon = ImageProvider.get("data", "way");
045    private static final Icon relIcon = ImageProvider.get("data", "relation");
046
047    /**
048     * If the node has a name-key or id-key, this is displayed. If not, (lat,lon) is displayed.
049     */
050    @Override
051    public void visit(Node n) {
052        name = n.getDisplayName(DefaultNameFormatter.getInstance());
053        icon = nodeIcon;
054        className = "node";
055        classNamePlural = trn("node", "nodes", 2);
056    }
057
058    /**
059     * If the way has a name-key or id-key, this is displayed. If not, (x nodes)
060     * is displayed with x being the number of nodes in the way.
061     */
062    @Override
063    public void visit(Way w) {
064        name = w.getDisplayName(DefaultNameFormatter.getInstance());
065        icon = wayIcon;
066        className = "way";
067        classNamePlural = trn("way", "ways", 2);
068    }
069
070    @Override
071    public void visit(Relation e) {
072        name = e.getDisplayName(DefaultNameFormatter.getInstance());
073        icon = relIcon;
074        className = "relation";
075        classNamePlural = trn("relation", "relations", 2);
076    }
077
078    /**
079     * Returns an horizontal {@code JLabel} with icon and name.
080     * @return horizontal {@code JLabel} with icon and name
081     */
082    public JLabel toLabel() {
083        return new JLabel(name, icon, JLabel.HORIZONTAL);
084    }
085}