1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.Collections;
|
---|
9 |
|
---|
10 | import javax.swing.Icon;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
14 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
15 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
16 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * A command that adds an osm primitive to a dataset. Keys cannot be added this way.
|
---|
20 | *
|
---|
21 | * See {@link ChangeCommand} for comments on relation back references.
|
---|
22 | *
|
---|
23 | * @author imi
|
---|
24 | */
|
---|
25 | public class AddCommand extends Command {
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * The primitive to add to the dataset.
|
---|
29 | */
|
---|
30 | private final OsmPrimitive osm;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Creates the command and specify the element to add in the context of the current edit layer, if any.
|
---|
34 | * @param osm The primitive to add
|
---|
35 | */
|
---|
36 | public AddCommand(OsmPrimitive osm) {
|
---|
37 | this.osm = osm;
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Creates the command and specify the element to add in the context of the given data layer.
|
---|
42 | * @param layer The data layer. Must not be {@code null}
|
---|
43 | * @param osm The primitive to add
|
---|
44 | */
|
---|
45 | public AddCommand(OsmDataLayer layer, OsmPrimitive osm) {
|
---|
46 | super(layer);
|
---|
47 | this.osm = osm;
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public boolean executeCommand() {
|
---|
52 | getLayer().data.addPrimitive(osm);
|
---|
53 | osm.setModified(true);
|
---|
54 | return true;
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public void undoCommand() {
|
---|
59 | getLayer().data.removePrimitive(osm);
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
64 | added.add(osm);
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public String getDescriptionText() {
|
---|
69 | String msg;
|
---|
70 | switch(OsmPrimitiveType.from(osm)) {
|
---|
71 | case NODE: msg = marktr("Add node {0}"); break;
|
---|
72 | case WAY: msg = marktr("Add way {0}"); break;
|
---|
73 | case RELATION: msg = marktr("Add relation {0}"); break;
|
---|
74 | default: /* should not happen */msg = ""; break;
|
---|
75 | }
|
---|
76 | return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public Icon getDescriptionIcon() {
|
---|
81 | return ImageProvider.get(osm.getDisplayType());
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public Collection<OsmPrimitive> getParticipatingPrimitives() {
|
---|
86 | return Collections.singleton(osm);
|
---|
87 | }
|
---|
88 | }
|
---|