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