001    // License: GPL. See LICENSE file for details.
002    package org.openstreetmap.josm.data.validation.util;
003    
004    import static org.openstreetmap.josm.tools.I18n.trn;
005    
006    import java.util.Collection;
007    
008    import javax.swing.Icon;
009    import javax.swing.JLabel;
010    
011    import org.openstreetmap.josm.data.osm.OsmPrimitive;
012    import 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     */
019    public 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            String multipleName = "";
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.isEmpty()) {
047                        multipleName += ", ";
048                    }
049                    multipleName += name;
050                }
051    
052                osm.visit(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                displayName = size + " " + trn(multipleClassname, multiplePluralClassname, size);
066                if (!multipleName.isEmpty()) {
067                    if (multipleName.length() <= MULTIPLE_NAME_MAX_LENGTH) {
068                        displayName += ": " + multipleName;
069                    } else {
070                        displayName += ": " + multipleName.substring(0, MULTIPLE_NAME_MAX_LENGTH) + "...";
071                    }
072                }
073            }
074        }
075    
076        @Override
077        public JLabel toLabel() {
078            return new JLabel(getText(), getIcon(), JLabel.HORIZONTAL);
079        }
080    
081        /**
082         * Gets the name of the items
083         * @return the name of the items
084         */
085        public String getText() {
086            return displayName;
087        }
088    
089        /**
090         * Gets the icon of the items
091         * @return the icon of the items
092         */
093        public Icon getIcon() {
094            if (size == 1)
095                return icon;
096            else
097                return ImageProvider.get("data", multipleClassname);
098        }
099    
100        @Override
101        public String toString() {
102            return getText();
103        }
104    }