1 | package org.openstreetmap.josm.command;
|
---|
2 |
|
---|
3 | import java.awt.Component;
|
---|
4 | import java.util.Iterator;
|
---|
5 |
|
---|
6 | import javax.swing.JLabel;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.osm.Key;
|
---|
9 | import org.openstreetmap.josm.data.osm.LineSegment;
|
---|
10 | import org.openstreetmap.josm.data.osm.Node;
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.Track;
|
---|
13 | import org.openstreetmap.josm.data.osm.visitor.SelectionComponentVisitor;
|
---|
14 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * A command that adds an osm primitive to a dataset. Keys cannot be added this
|
---|
18 | * way. Use ChangeKeyValueCommand instead.
|
---|
19 | *
|
---|
20 | * @author imi
|
---|
21 | */
|
---|
22 | public class AddCommand implements Command, Visitor {
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * The primitive to add to the dataset.
|
---|
26 | */
|
---|
27 | private final OsmPrimitive osm;
|
---|
28 | /**
|
---|
29 | * The dataset to add the primitive to.
|
---|
30 | */
|
---|
31 | private final DataSet ds;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Create the command and specify the element to add.
|
---|
35 | */
|
---|
36 | public AddCommand(OsmPrimitive osm, DataSet dataSet) {
|
---|
37 | this.osm = osm;
|
---|
38 | this.ds = dataSet;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public void executeCommand() {
|
---|
42 | osm.visit(this);
|
---|
43 | }
|
---|
44 |
|
---|
45 | public Component commandDescription() {
|
---|
46 | SelectionComponentVisitor v = new SelectionComponentVisitor();
|
---|
47 | osm.visit(v);
|
---|
48 | return new JLabel(v.name, v.icon, JLabel.LEADING);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Add the node to the nodes - list only.
|
---|
53 | * @param n The node to add.
|
---|
54 | */
|
---|
55 | public void visit(Node n) {
|
---|
56 | ds.nodes.add(n);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Add the line segment to the list of pending line segments.
|
---|
61 | * @param ls The line segment to add.
|
---|
62 | */
|
---|
63 | public void visit(LineSegment ls) {
|
---|
64 | ds.pendingLineSegments.add(ls);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Add the track to the dataset. Remove all line segments that were pending
|
---|
69 | * from the dataset.
|
---|
70 | */
|
---|
71 | public void visit(Track t) {
|
---|
72 | ds.addTrack(t);
|
---|
73 | for (Iterator<LineSegment> it = ds.pendingLineSegments.iterator(); it.hasNext();)
|
---|
74 | if (t.segments().contains(it.next()))
|
---|
75 | it.remove();
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Add the key to the parent specified by the constructor
|
---|
80 | */
|
---|
81 | public void visit(Key k) {
|
---|
82 | throw new IllegalStateException("Keys are added by using ChangeKeyValueCommand");
|
---|
83 | }
|
---|
84 | }
|
---|