1 |
|
---|
2 | package org.openstreetmap.josm.data.osm.visitor;
|
---|
3 |
|
---|
4 | import java.util.HashSet;
|
---|
5 | import java.util.Set;
|
---|
6 |
|
---|
7 | import javax.swing.Icon;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.data.osm.LineSegment;
|
---|
10 | import org.openstreetmap.josm.data.osm.Node;
|
---|
11 | import org.openstreetmap.josm.data.osm.Way;
|
---|
12 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Able to create a name and an icon for each data element.
|
---|
16 | *
|
---|
17 | * @author imi
|
---|
18 | */
|
---|
19 | public class SelectionComponentVisitor implements Visitor {
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * The name of this item.
|
---|
23 | */
|
---|
24 | public String name;
|
---|
25 | /**
|
---|
26 | * The icon of this item.
|
---|
27 | */
|
---|
28 | public Icon icon;
|
---|
29 |
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * If the line segment has a key named "name", its value is displayed.
|
---|
33 | * Otherwise, if it has "id", this is used. If none of these available,
|
---|
34 | * "(x1,y1) -> (x2,y2)" is displayed with the nodes coordinates.
|
---|
35 | */
|
---|
36 | public void visit(LineSegment ls) {
|
---|
37 | name = ls.get("name");
|
---|
38 | if (name == null) {
|
---|
39 | if (ls.incomplete)
|
---|
40 | name = ""+ls.id;
|
---|
41 | else
|
---|
42 | name = ls.id+" ("+ls.from.coor.lat()+","+ls.from.coor.lon()+") -> ("+ls.to.coor.lat()+","+ls.to.coor.lon()+")";
|
---|
43 | }
|
---|
44 | icon = ImageProvider.get("data", "linesegment");
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * If the node has a name-key or id-key, this is displayed. If not, (lat,lon)
|
---|
49 | * is displayed.
|
---|
50 | */
|
---|
51 | public void visit(Node n) {
|
---|
52 | name = n.get("name");
|
---|
53 | if (name == null)
|
---|
54 | name = n.id+" ("+n.coor.lat()+","+n.coor.lon()+")";
|
---|
55 | icon = ImageProvider.get("data", "node");
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * If the way has a name-key or id-key, this is displayed. If not, (x nodes)
|
---|
60 | * is displayed with x beeing the number of nodes in the way.
|
---|
61 | */
|
---|
62 | public void visit(Way w) {
|
---|
63 | name = w.get("name");
|
---|
64 | if (name == null) {
|
---|
65 | AllNodesVisitor.getAllNodes(w.segments);
|
---|
66 | Set<Node> nodes = new HashSet<Node>();
|
---|
67 | for (LineSegment ls : w.segments) {
|
---|
68 | if (!ls.incomplete) {
|
---|
69 | nodes.add(ls.from);
|
---|
70 | nodes.add(ls.to);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | name = "("+nodes.size()+" nodes)";
|
---|
74 | }
|
---|
75 | icon = ImageProvider.get("data", "way");
|
---|
76 | }
|
---|
77 | }
|
---|