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