1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.data.validation.util;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
5 |
|
---|
6 | import java.util.Collection;
|
---|
7 |
|
---|
8 | import javax.swing.Icon;
|
---|
9 | import javax.swing.JLabel;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Able to create a name and an icon for a collection of elements.
|
---|
16 | *
|
---|
17 | * @author frsantos
|
---|
18 | */
|
---|
19 | public class MultipleNameVisitor extends NameVisitor
|
---|
20 | {
|
---|
21 | public static final int MULTIPLE_NAME_MAX_LENGTH = 80;
|
---|
22 |
|
---|
23 | /** The class name of the combined primitives */
|
---|
24 | private String multipleClassname;
|
---|
25 | /** Name to be displayed */
|
---|
26 | private String displayName;
|
---|
27 | /** Size of the collection */
|
---|
28 | private int size;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Visits a collection of primitives
|
---|
32 | * @param data The collection of primitives
|
---|
33 | */
|
---|
34 | public void visit(Collection<? extends OsmPrimitive> data) {
|
---|
35 | StringBuilder multipleName = new StringBuilder();
|
---|
36 | String multiplePluralClassname = null;
|
---|
37 | size = data.size();
|
---|
38 |
|
---|
39 | multipleClassname = null;
|
---|
40 | for (OsmPrimitive osm : data) {
|
---|
41 | String name = osm.get("name");
|
---|
42 | if (name == null) {
|
---|
43 | name = osm.get("ref");
|
---|
44 | }
|
---|
45 | if (name != null && !name.isEmpty() && multipleName.length() <= MULTIPLE_NAME_MAX_LENGTH) {
|
---|
46 | if (multipleName.length() > 0) {
|
---|
47 | multipleName.append(", ");
|
---|
48 | }
|
---|
49 | multipleName.append(name);
|
---|
50 | }
|
---|
51 |
|
---|
52 | osm.accept(this);
|
---|
53 | if (multipleClassname == null) {
|
---|
54 | multipleClassname = className;
|
---|
55 | multiplePluralClassname = classNamePlural;
|
---|
56 | } else if (!multipleClassname.equals(className)) {
|
---|
57 | multipleClassname = "object";
|
---|
58 | multiplePluralClassname = trn("object", "objects", 2);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | if (size <= 1) {
|
---|
63 | displayName = name;
|
---|
64 | } else {
|
---|
65 | displayName = size + " " + trn(multipleClassname, multiplePluralClassname, size);
|
---|
66 | if (multipleName.length() > 0) {
|
---|
67 | if (multipleName.length() <= MULTIPLE_NAME_MAX_LENGTH) {
|
---|
68 | displayName += ": " + multipleName;
|
---|
69 | } else {
|
---|
70 | displayName += ": " + multipleName.substring(0, MULTIPLE_NAME_MAX_LENGTH) + "...";
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public JLabel toLabel() {
|
---|
78 | return new JLabel(getText(), getIcon(), JLabel.HORIZONTAL);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Gets the name of the items
|
---|
83 | * @return the name of the items
|
---|
84 | */
|
---|
85 | public String getText() {
|
---|
86 | return displayName;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Gets the icon of the items
|
---|
91 | * @return the icon of the items
|
---|
92 | */
|
---|
93 | public Icon getIcon() {
|
---|
94 | if (size <= 1)
|
---|
95 | return icon;
|
---|
96 | else
|
---|
97 | return ImageProvider.get("data", multipleClassname);
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public String toString() {
|
---|
102 | return getText();
|
---|
103 | }
|
---|
104 | }
|
---|