001 // License: GPL. See LICENSE file for details.
002 package org.openstreetmap.josm.data.validation.util;
003
004 import static org.openstreetmap.josm.tools.I18n.trn;
005
006 import javax.swing.Icon;
007 import javax.swing.JLabel;
008
009 import org.openstreetmap.josm.data.osm.Node;
010 import org.openstreetmap.josm.data.osm.Relation;
011 import org.openstreetmap.josm.data.osm.Way;
012 import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
013 import org.openstreetmap.josm.gui.DefaultNameFormatter;
014 import 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
022 public 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 /**
063 */
064 @Override
065 public void visit(Relation e) {
066 name = e.getDisplayName(DefaultNameFormatter.getInstance());
067 icon = ImageProvider.get("data", "relation");
068 className = "relation";
069 classNamePlural = trn("relation", "relations", 2);
070 }
071
072 public JLabel toLabel() {
073 return new JLabel(name, icon, JLabel.HORIZONTAL);
074 }
075 }