001// License: GPL. See LICENSE file for details.
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, but MultipleNameVisitor should be refactored instead of using this class
022public class NameVisitor extends AbstractVisitor {
023
024    /**
025     * The name of the item class
026     */
027    public String className;
028    public String classNamePlural;
029    /**
030     * The name of this item.
031     */
032    public String name = "";
033    /**
034     * The icon of this item.
035     */
036    public Icon icon;
037
038    /**
039     * If the node has a name-key or id-key, this is displayed. If not, (lat,lon)
040     * is displayed.
041     */
042    @Override
043    public void visit(Node n) {
044        name = n.getDisplayName(DefaultNameFormatter.getInstance());
045        icon = ImageProvider.get("data", "node");
046        className = "node";
047        classNamePlural = trn("node", "nodes", 2);
048    }
049
050    /**
051     * If the way has a name-key or id-key, this is displayed. If not, (x nodes)
052     * is displayed with x being the number of nodes in the way.
053     */
054    @Override
055    public void visit(Way w) {
056        name = w.getDisplayName(DefaultNameFormatter.getInstance());
057        icon = ImageProvider.get("data", "way");
058        className = "way";
059        classNamePlural = trn("way", "ways", 2);
060    }
061
062    @Override
063    public void visit(Relation e) {
064        name = e.getDisplayName(DefaultNameFormatter.getInstance());
065        icon = ImageProvider.get("data", "relation");
066        className = "relation";
067        classNamePlural = trn("relation", "relations", 2);
068    }
069
070    public JLabel toLabel() {
071        return new JLabel(name, icon, JLabel.HORIZONTAL);
072    }
073}