source: josm/src/org/openstreetmap/josm/data/osm/visitor/SelectionComponentVisitor.java@ 8

Last change on this file since 8 was 8, checked in by imi, 19 years ago
  • added Selection Dialog
  • added support for graphic engines with a better default engine
  • reorganized data classes with back references
File size: 2.5 KB
Line 
1
2package org.openstreetmap.josm.data.osm.visitor;
3
4import java.util.HashSet;
5import java.util.Map;
6import java.util.Set;
7
8import javax.swing.Icon;
9import javax.swing.ImageIcon;
10
11import org.openstreetmap.josm.data.osm.Key;
12import org.openstreetmap.josm.data.osm.LineSegment;
13import org.openstreetmap.josm.data.osm.Node;
14import 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 */
21public 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}
Note: See TracBrowser for help on using the repository browser.