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