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.tr;
|
---|
5 |
|
---|
6 | import java.util.Collection;
|
---|
7 |
|
---|
8 | import javax.swing.JLabel;
|
---|
9 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
10 | import javax.swing.tree.MutableTreeNode;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
14 | import org.openstreetmap.josm.gui.PrimitiveNameFormatter;
|
---|
15 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * A command that adds an osm primitive to a dataset. Keys cannot be added this
|
---|
19 | * way.
|
---|
20 | *
|
---|
21 | * See {@see 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 | * Create the command and specify the element to add.
|
---|
34 | */
|
---|
35 | public AddCommand(OsmPrimitive osm) {
|
---|
36 | super();
|
---|
37 | this.osm = osm;
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override public boolean executeCommand() {
|
---|
41 | getLayer().data.addPrimitive(osm);
|
---|
42 | return true;
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override public void undoCommand() {
|
---|
46 | getLayer().data.removePrimitive(osm);
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
50 | added.add(osm);
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override public MutableTreeNode description() {
|
---|
54 | return new DefaultMutableTreeNode(
|
---|
55 | new JLabel(
|
---|
56 | tr("Add {0} {1}",
|
---|
57 | OsmPrimitiveType.from(osm).getLocalizedDisplayNameSingular(),
|
---|
58 | new PrimitiveNameFormatter().getName(osm)
|
---|
59 | ),
|
---|
60 | ImageProvider.get(OsmPrimitiveType.from(osm)),
|
---|
61 | JLabel.HORIZONTAL
|
---|
62 | )
|
---|
63 | );
|
---|
64 | }
|
---|
65 | }
|
---|