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

Last change on this file since 4058 was 3671, checked in by bastiK, 13 years ago

adapt coding style (to some degree); there shouldn't be any semantic changes in this commit

  • 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.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.util.Collection;
8
9import javax.swing.Icon;
10import javax.swing.JLabel;
11
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.tools.ImageProvider;
14
15/**
16 * Able to create a name and an icon for a collection of elements.
17 *
18 * @author frsantos
19 */
20public class MultipleNameVisitor extends NameVisitor
21{
22 /** The class name of the combined primitives */
23 String multipleClassname;
24 /* name to be displayed */
25 String displayName;
26 /** Size of the collection */
27 int size;
28
29 /**
30 * Visits a collection of primitives
31 * @param data The collection of primitives
32 */
33 public void visit(Collection<? extends OsmPrimitive> data) {
34 String multipleName = null;
35 String multiplePluralClassname = null;
36 String firstName = null;
37 boolean initializedname = false;
38 size = data.size();
39
40 multipleClassname = null;
41 for (OsmPrimitive osm : data) {
42 String name = osm.get("name");
43 if (name == null) {
44 name = osm.get("ref");
45 }
46 if (!initializedname) {
47 multipleName = name; initializedname = true;
48 } else if (multipleName != null && (name == null || !name.equals(multipleName))) {
49 multipleName = null;
50 }
51
52 if (firstName == null && name != null) {
53 firstName = name;
54 }
55 osm.visit(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 if (multipleName != null) {
68 displayName = size + " " + trn(multipleClassname, multiplePluralClassname, size) + ": " + multipleName;
69 } else if (firstName != null) {
70 displayName = size + " " + trn(multipleClassname, multiplePluralClassname, size) + ": " + tr("{0}, ...", firstName);
71 } else {
72 displayName = size + " " + trn(multipleClassname, multiplePluralClassname, size);
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}
Note: See TracBrowser for help on using the repository browser.