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.Node;
010import org.openstreetmap.josm.data.osm.Relation;
011import org.openstreetmap.josm.data.osm.Way;
012import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
013import org.openstreetmap.josm.gui.DefaultNameFormatter;
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 */
021//TODO This class used to be in JOSM but it was removed. MultipleNameVisitor depends on it so I copied it here,
022// but MultipleNameVisitor should be refactored instead of using this class
023public class NameVisitor extends AbstractVisitor {
024
025    /**
026     * The name of the item class
027     */
028    public String className;
029    public String classNamePlural;
030    /**
031     * The name of this item.
032     */
033    public String name = "";
034    /**
035     * The icon of this item.
036     */
037    public Icon icon;
038
039    /**
040     * If the node has a name-key or id-key, this is displayed. If not, (lat,lon)
041     * is displayed.
042     */
043    @Override
044    public void visit(Node n) {
045        name = n.getDisplayName(DefaultNameFormatter.getInstance());
046        icon = ImageProvider.get("data", "node");
047        className = "node";
048        classNamePlural = trn("node", "nodes", 2);
049    }
050
051    /**
052     * If the way has a name-key or id-key, this is displayed. If not, (x nodes)
053     * is displayed with x being the number of nodes in the way.
054     */
055    @Override
056    public void visit(Way w) {
057        name = w.getDisplayName(DefaultNameFormatter.getInstance());
058        icon = ImageProvider.get("data", "way");
059        className = "way";
060        classNamePlural = trn("way", "ways", 2);
061    }
062
063    @Override
064    public void visit(Relation e) {
065        name = e.getDisplayName(DefaultNameFormatter.getInstance());
066        icon = ImageProvider.get("data", "relation");
067        className = "relation";
068        classNamePlural = trn("relation", "relations", 2);
069    }
070
071    public JLabel toLabel() {
072        return new JLabel(name, icon, JLabel.HORIZONTAL);
073    }
074}