source: osm/applications/editors/josm/plugins/reltoolbox/src/relcontext/ChosenRelationComponent.java@ 34881

Last change on this file since 34881 was 32398, checked in by donvip, 9 years ago

checkstyle

  • Property svn:eol-style set to native
File size: 2.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package relcontext;
3
4import javax.swing.JLabel;
5
6import org.openstreetmap.josm.data.osm.Relation;
7
8/**
9 * Renderer for chosen relation.
10 * [Icon] na=wood U
11 * key is 2-letter; type = icon; to the right — symbol of relation topology (closed, lines, broken).
12 *
13 * @author Zverik
14 */
15public class ChosenRelationComponent extends JLabel implements ChosenRelationListener {
16
17 public ChosenRelationComponent(ChosenRelation rel) {
18 super("");
19 rel.addChosenRelationListener(this);
20 }
21
22 @Override
23 public void chosenRelationChanged(Relation oldRelation, Relation newRelation) {
24 setText(prepareText(newRelation));
25 repaint();
26 }
27
28 private static final String[] TYPE_KEYS = new String[] {
29 "natural", "landuse", "place", "waterway", "leisure", "amenity", "restriction", "public_transport", "route", "enforcement"
30 };
31
32 private static final String[] NAMING_TAGS = new String[] {
33 "name", "place_name", "ref", "destination", "note"
34 };
35
36 protected String prepareText(Relation rel) {
37 if (rel == null)
38 return "";
39
40 String type = rel.get("type");
41 if (type == null || type.length() == 0) {
42 type = "-";
43 }
44
45 String tag = null;
46 for (int i = 0; i < TYPE_KEYS.length && tag == null; i++) {
47 if (rel.hasKey(TYPE_KEYS[i])) {
48 tag = TYPE_KEYS[i];
49 }
50 }
51 if (tag != null) {
52 tag = tag.substring(0, 2) + "=" + rel.get(tag);
53 }
54
55 String name = null;
56 for (int i = 0; i < NAMING_TAGS.length && name == null; i++) {
57 if (rel.hasKey(NAMING_TAGS[i])) {
58 name = rel.get(NAMING_TAGS[i]);
59 }
60 }
61
62 StringBuilder sb = new StringBuilder();
63 sb.append(type.substring(0, 1));
64 if (type.equals("boundary") && rel.hasKey("admin_level")) {
65 sb.append(rel.get("admin_level"));
66 }
67 if (name != null) {
68 sb.append(" \"").append(name).append('"');
69 }
70 if (tag != null) {
71 sb.append("; ").append(tag);
72 }
73 sb.append(" (").append(rel.getMembersCount()).append(')');
74
75 return sb.toString();
76 }
77}
Note: See TracBrowser for help on using the repository browser.