source: josm/trunk/src/org/openstreetmap/josm/data/validation/util/MultipleNameVisitor.java@ 6490

Last change on this file since 6490 was 6490, checked in by Don-vip, 10 years ago

fix #9442 - NPE when sorting validator errors after fixing one of them

  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.data.validation.util;
3
4import static org.openstreetmap.josm.tools.I18n.trn;
5
6import java.util.Collection;
7
8import javax.swing.Icon;
9import javax.swing.JLabel;
10
11import org.openstreetmap.josm.data.osm.OsmPrimitive;
12import 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 */
19public 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}
Note: See TracBrowser for help on using the repository browser.