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 */ 021public class NameVisitor extends AbstractVisitor { 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 /** 044 * If the node has a name-key or id-key, this is displayed. If not, (lat,lon) is displayed. 045 */ 046 @Override 047 public void visit(Node n) { 048 name = n.getDisplayName(DefaultNameFormatter.getInstance()); 049 icon = ImageProvider.get("data", "node"); 050 className = "node"; 051 classNamePlural = trn("node", "nodes", 2); 052 } 053 054 /** 055 * If the way has a name-key or id-key, this is displayed. If not, (x nodes) 056 * is displayed with x being the number of nodes in the way. 057 */ 058 @Override 059 public void visit(Way w) { 060 name = w.getDisplayName(DefaultNameFormatter.getInstance()); 061 icon = ImageProvider.get("data", "way"); 062 className = "way"; 063 classNamePlural = trn("way", "ways", 2); 064 } 065 066 @Override 067 public void visit(Relation e) { 068 name = e.getDisplayName(DefaultNameFormatter.getInstance()); 069 icon = ImageProvider.get("data", "relation"); 070 className = "relation"; 071 classNamePlural = trn("relation", "relations", 2); 072 } 073 074 /** 075 * Returns an horizontal {@code JLabel} with icon and name. 076 * @return horizontal {@code JLabel} with icon and name 077 */ 078 public JLabel toLabel() { 079 return new JLabel(name, icon, JLabel.HORIZONTAL); 080 } 081}