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 java.util.Collection; 007 008import javax.swing.Icon; 009import javax.swing.JLabel; 010 011import org.openstreetmap.josm.data.osm.DefaultNameFormatter; 012import org.openstreetmap.josm.data.osm.OsmPrimitive; 013import org.openstreetmap.josm.data.preferences.IntegerProperty; 014import org.openstreetmap.josm.tools.ImageProvider; 015 016/** 017 * Able to create a name and an icon for a collection of elements. 018 * 019 * @author frsantos 020 */ 021public class MultipleNameVisitor extends NameVisitor { 022 023 /** 024 * Maximum displayed length, in characters. 025 */ 026 public static final IntegerProperty MULTIPLE_NAME_MAX_LENGTH = new IntegerProperty("multiple.name.max.length", 140); 027 private static final String MULTI_CLASS_NAME = "object"; 028 private static final Icon MULTI_CLASS_ICON = ImageProvider.get("data", MULTI_CLASS_NAME); 029 030 /** Name to be displayed */ 031 private String displayName; 032 033 /** 034 * Visits a collection of primitives 035 * @param data The collection of primitives 036 */ 037 public void visit(Collection<? extends OsmPrimitive> data) { 038 StringBuilder multipleName = new StringBuilder(); 039 String multiplePluralClassname = null; 040 int size = data.size(); 041 042 // The class name of the combined primitives 043 String multipleClassname = null; 044 for (OsmPrimitive osm : data) { 045 String name = osm.getDisplayName(DefaultNameFormatter.getInstance()); 046 if (name != null && !name.isEmpty() && multipleName.length() <= MULTIPLE_NAME_MAX_LENGTH.get()) { 047 if (multipleName.length() > 0) { 048 multipleName.append(", "); 049 } 050 multipleName.append(name); 051 } 052 053 osm.accept(this); 054 if (multipleClassname == null) { 055 multipleClassname = className; 056 multiplePluralClassname = classNamePlural; 057 } else if (!multipleClassname.equals(className)) { 058 multipleClassname = MULTI_CLASS_NAME; 059 multiplePluralClassname = trn("object", "objects", 2); 060 } 061 } 062 063 if (size <= 1) { 064 displayName = name; 065 } else { 066 if (MULTI_CLASS_NAME.equals(multipleClassname)) { 067 icon = MULTI_CLASS_ICON; 068 } 069 StringBuilder sb = new StringBuilder().append(size).append(' ').append(trn(multipleClassname, multiplePluralClassname, size)); 070 if (multipleName.length() > 0) { 071 sb.append(": "); 072 if (multipleName.length() <= MULTIPLE_NAME_MAX_LENGTH.get()) { 073 sb.append(multipleName); 074 } else { 075 sb.append(multipleName.substring(0, MULTIPLE_NAME_MAX_LENGTH.get())).append("..."); 076 } 077 } 078 displayName = sb.toString(); 079 } 080 } 081 082 @Override 083 public JLabel toLabel() { 084 return new JLabel(getText(), getIcon(), JLabel.HORIZONTAL); 085 } 086 087 /** 088 * Gets the name of the items 089 * @return the name of the items 090 */ 091 public String getText() { 092 return displayName; 093 } 094 095 /** 096 * Gets the icon of the items 097 * @return the icon of the items 098 */ 099 public Icon getIcon() { 100 return icon; 101 } 102 103 @Override 104 public String toString() { 105 return getText(); 106 } 107}