1 | // License: GPL. For details, see LICENSE file.
|
---|
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 | /**
|
---|
22 | * Maximum displayed length, in characters.
|
---|
23 | */
|
---|
24 | public static final int MULTIPLE_NAME_MAX_LENGTH = 80;
|
---|
25 |
|
---|
26 | /** The class name of the combined primitives */
|
---|
27 | private String multipleClassname;
|
---|
28 | /** Name to be displayed */
|
---|
29 | private String displayName;
|
---|
30 | /** Size of the collection */
|
---|
31 | private int size;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Visits a collection of primitives
|
---|
35 | * @param data The collection of primitives
|
---|
36 | */
|
---|
37 | public void visit(Collection<? extends OsmPrimitive> data) {
|
---|
38 | StringBuilder multipleName = new StringBuilder();
|
---|
39 | String multiplePluralClassname = null;
|
---|
40 | size = data.size();
|
---|
41 |
|
---|
42 | multipleClassname = null;
|
---|
43 | for (OsmPrimitive osm : data) {
|
---|
44 | String name = osm.get("name");
|
---|
45 | if (name == null) {
|
---|
46 | name = osm.get("ref");
|
---|
47 | }
|
---|
48 | if (name != null && !name.isEmpty() && multipleName.length() <= MULTIPLE_NAME_MAX_LENGTH) {
|
---|
49 | if (multipleName.length() > 0) {
|
---|
50 | multipleName.append(", ");
|
---|
51 | }
|
---|
52 | multipleName.append(name);
|
---|
53 | }
|
---|
54 |
|
---|
55 | osm.accept(this);
|
---|
56 | if (multipleClassname == null) {
|
---|
57 | multipleClassname = className;
|
---|
58 | multiplePluralClassname = classNamePlural;
|
---|
59 | } else if (!multipleClassname.equals(className)) {
|
---|
60 | multipleClassname = "object";
|
---|
61 | multiplePluralClassname = trn("object", "objects", 2);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | if (size <= 1) {
|
---|
66 | displayName = name;
|
---|
67 | } else {
|
---|
68 | StringBuilder sb = new StringBuilder().append(size).append(' ').append(trn(multipleClassname, multiplePluralClassname, size));
|
---|
69 | if (multipleName.length() > 0) {
|
---|
70 | sb.append(": ");
|
---|
71 | if (multipleName.length() <= MULTIPLE_NAME_MAX_LENGTH) {
|
---|
72 | sb.append(multipleName);
|
---|
73 | } else {
|
---|
74 | sb.append(multipleName.substring(0, MULTIPLE_NAME_MAX_LENGTH)).append("...");
|
---|
75 | }
|
---|
76 | }
|
---|
77 | displayName = sb.toString();
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | public JLabel toLabel() {
|
---|
83 | return new JLabel(getText(), getIcon(), JLabel.HORIZONTAL);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Gets the name of the items
|
---|
88 | * @return the name of the items
|
---|
89 | */
|
---|
90 | public String getText() {
|
---|
91 | return displayName;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Gets the icon of the items
|
---|
96 | * @return the icon of the items
|
---|
97 | */
|
---|
98 | public Icon getIcon() {
|
---|
99 | 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 | }
|
---|