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
028    /** The class name of the combined primitives */
029    private String multipleClassname;
030    /** Name to be displayed */
031    private String displayName;
032    /** Size of the collection */
033    private int size;
034
035    /**
036     * Visits a collection of primitives
037     * @param data The collection of primitives
038     */
039    public void visit(Collection<? extends OsmPrimitive> data) {
040        StringBuilder multipleName = new StringBuilder();
041        String multiplePluralClassname = null;
042        size = data.size();
043
044        multipleClassname = null;
045        for (OsmPrimitive osm : data) {
046            String name = osm.getDisplayName(DefaultNameFormatter.getInstance());
047            if (name != null && !name.isEmpty() && multipleName.length() <= MULTIPLE_NAME_MAX_LENGTH.get()) {
048                if (multipleName.length() > 0) {
049                    multipleName.append(", ");
050                }
051                multipleName.append(name);
052            }
053
054            osm.accept(this);
055            if (multipleClassname == null) {
056                multipleClassname = className;
057                multiplePluralClassname = classNamePlural;
058            } else if (!multipleClassname.equals(className)) {
059                multipleClassname = "object";
060                multiplePluralClassname = trn("object", "objects", 2);
061            }
062        }
063
064        if (size <= 1) {
065            displayName = name;
066        } else {
067            StringBuilder sb = new StringBuilder().append(size).append(' ').append(trn(multipleClassname, multiplePluralClassname, size));
068            if (multipleName.length() > 0) {
069                sb.append(": ");
070                if (multipleName.length() <= MULTIPLE_NAME_MAX_LENGTH.get()) {
071                    sb.append(multipleName);
072                } else {
073                    sb.append(multipleName.substring(0, MULTIPLE_NAME_MAX_LENGTH.get())).append("...");
074                }
075            }
076            displayName = sb.toString();
077        }
078    }
079
080    @Override
081    public JLabel toLabel() {
082        return new JLabel(getText(), getIcon(), JLabel.HORIZONTAL);
083    }
084
085    /**
086     * Gets the name of the items
087     * @return the name of the items
088     */
089    public String getText() {
090        return displayName;
091    }
092
093    /**
094     * Gets the icon of the items
095     * @return the icon of the items
096     */
097    public Icon getIcon() {
098        if (size <= 1)
099            return icon;
100        else
101            return ImageProvider.get("data", multipleClassname);
102    }
103
104    @Override
105    public String toString() {
106        return getText();
107    }
108}