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