1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
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.JLabel;
|
---|
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
|
---|
20 | * way.
|
---|
21 | *
|
---|
22 | * See {@see ChangeCommand} for comments on relation back references.
|
---|
23 | *
|
---|
24 | * @author imi
|
---|
25 | */
|
---|
26 | public class AddCommand extends Command {
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * The primitive to add to the dataset.
|
---|
30 | */
|
---|
31 | private final OsmPrimitive osm;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Create the command and specify the element to add.
|
---|
35 | */
|
---|
36 | public AddCommand(OsmPrimitive osm) {
|
---|
37 | super();
|
---|
38 | this.osm = osm;
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Create the command and specify the element to add.
|
---|
43 | */
|
---|
44 | public AddCommand(OsmDataLayer layer, OsmPrimitive osm) {
|
---|
45 | super(layer);
|
---|
46 | this.osm = osm;
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override public boolean executeCommand() {
|
---|
50 | getLayer().data.addPrimitive(osm);
|
---|
51 | return true;
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override public void undoCommand() {
|
---|
55 | getLayer().data.removePrimitive(osm);
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
59 | added.add(osm);
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override public JLabel getDescription() {
|
---|
63 | String msg;
|
---|
64 | switch(OsmPrimitiveType.from(osm)) {
|
---|
65 | case NODE: msg = marktr("Add node {0}"); break;
|
---|
66 | case WAY: msg = marktr("Add way {0}"); break;
|
---|
67 | case RELATION: msg = marktr("Add relation {0}"); break;
|
---|
68 | default: /* should not happen */msg = ""; break;
|
---|
69 | }
|
---|
70 |
|
---|
71 | return new JLabel(
|
---|
72 | tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance())),
|
---|
73 | ImageProvider.get(OsmPrimitiveType.from(osm)),
|
---|
74 | JLabel.HORIZONTAL);
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public Collection<OsmPrimitive> getParticipatingPrimitives() {
|
---|
79 | return Collections.singleton(osm);
|
---|
80 | }
|
---|
81 | }
|
---|