1 |
|
---|
2 | package org.openstreetmap.josm.data.osm.visitor;
|
---|
3 |
|
---|
4 | import java.util.HashSet;
|
---|
5 | import java.util.Map;
|
---|
6 | import java.util.Set;
|
---|
7 |
|
---|
8 | import javax.swing.Icon;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.osm.Key;
|
---|
11 | import org.openstreetmap.josm.data.osm.LineSegment;
|
---|
12 | import org.openstreetmap.josm.data.osm.Node;
|
---|
13 | import org.openstreetmap.josm.data.osm.Track;
|
---|
14 | import org.openstreetmap.josm.gui.ImageProvider;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Able to create a name and an icon for each data element.
|
---|
18 | *
|
---|
19 | * @author imi
|
---|
20 | */
|
---|
21 | public class SelectionComponentVisitor implements Visitor {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * The name of this item.
|
---|
25 | */
|
---|
26 | public String name;
|
---|
27 | /**
|
---|
28 | * The icon of this item.
|
---|
29 | */
|
---|
30 | public Icon icon;
|
---|
31 |
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * A key icon and the name of the key.
|
---|
35 | */
|
---|
36 | public void visit(Key k) {
|
---|
37 | name = k.name;
|
---|
38 | icon = ImageProvider.get("data", "key");
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * If the line segment has a key named "name", its value is displayed.
|
---|
43 | * Otherwise, if it has "id", this is used. If none of these available,
|
---|
44 | * "(x1,y1) -> (x2,y2)" is displayed with the nodes coordinates.
|
---|
45 | */
|
---|
46 | public void visit(LineSegment ls) {
|
---|
47 | String name = getName(ls.keys);
|
---|
48 | if (name == null)
|
---|
49 | name = "("+ls.getStart().coor.lat+","+ls.getStart().coor.lon+") -> ("+ls.getEnd().coor.lat+","+ls.getEnd().coor.lon+")";
|
---|
50 |
|
---|
51 | this.name = name;
|
---|
52 | icon = ImageProvider.get("data", "linesegment");
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * If the node has a name-key or id-key, this is displayed. If not, (lat,lon)
|
---|
57 | * is displayed.
|
---|
58 | */
|
---|
59 | public void visit(Node n) {
|
---|
60 | String name = getName(n.keys);
|
---|
61 | if (name == null)
|
---|
62 | name = "("+n.coor.lat+","+n.coor.lon+")";
|
---|
63 |
|
---|
64 | this.name = name;
|
---|
65 | icon = ImageProvider.get("data", "node");
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * If the track has a name-key or id-key, this is displayed. If not, (x nodes)
|
---|
70 | * is displayed with x beeing the number of nodes in the track.
|
---|
71 | */
|
---|
72 | public void visit(Track t) {
|
---|
73 | String name = getName(t.keys);
|
---|
74 | if (name == null) {
|
---|
75 | Set<Node> nodes = new HashSet<Node>();
|
---|
76 | for (LineSegment ls : t.segments()) {
|
---|
77 | nodes.add(ls.getStart());
|
---|
78 | nodes.add(ls.getEnd());
|
---|
79 | }
|
---|
80 | name = "("+nodes.size()+" nodes)";
|
---|
81 | }
|
---|
82 |
|
---|
83 | this.name = name;
|
---|
84 | icon = ImageProvider.get("data", "track");
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Try to read a name from the given properties.
|
---|
90 | * @param keys The properties to search for a name. Can be <code>null</code>.
|
---|
91 | * @return If a name could be found, return it here.
|
---|
92 | */
|
---|
93 | public String getName(Map<Key, String> keys) {
|
---|
94 | String name = null;
|
---|
95 | if (keys != null) {
|
---|
96 | name = keys.get(Key.get("name"));
|
---|
97 | if (name == null)
|
---|
98 | name = keys.get(Key.get("id"));
|
---|
99 | }
|
---|
100 | return name;
|
---|
101 | }
|
---|
102 |
|
---|
103 | }
|
---|