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 |
|
---|
9 | import javax.swing.JLabel;
|
---|
10 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
11 | import javax.swing.tree.MutableTreeNode;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
15 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
16 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
17 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * A command that adds an osm primitive to a dataset. Keys cannot be added this
|
---|
21 | * way.
|
---|
22 | *
|
---|
23 | * See {@see ChangeCommand} for comments on relation back references.
|
---|
24 | *
|
---|
25 | * @author imi
|
---|
26 | */
|
---|
27 | public class AddCommand extends Command {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * The primitive to add to the dataset.
|
---|
31 | */
|
---|
32 | private final OsmPrimitive osm;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Create the command and specify the element to add.
|
---|
36 | */
|
---|
37 | public AddCommand(OsmPrimitive osm) {
|
---|
38 | super();
|
---|
39 | this.osm = osm;
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Create the command and specify the element to add.
|
---|
44 | */
|
---|
45 | public AddCommand(OsmDataLayer layer, OsmPrimitive osm) {
|
---|
46 | super(layer);
|
---|
47 | this.osm = osm;
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override public boolean executeCommand() {
|
---|
51 | getLayer().data.addPrimitive(osm);
|
---|
52 | return true;
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override public void undoCommand() {
|
---|
56 | getLayer().data.removePrimitive(osm);
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
60 | added.add(osm);
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override public MutableTreeNode description() {
|
---|
64 | String msg;
|
---|
65 | switch(OsmPrimitiveType.from(osm)) {
|
---|
66 | case NODE: msg = marktr("Add node {0}"); break;
|
---|
67 | case WAY: msg = marktr("Add way {0}"); break;
|
---|
68 | case RELATION: msg = marktr("Add relation {0}"); break;
|
---|
69 | default: /* should not happen */msg = ""; break;
|
---|
70 | }
|
---|
71 |
|
---|
72 | return new DefaultMutableTreeNode(
|
---|
73 | new JLabel(
|
---|
74 | tr(msg,
|
---|
75 | osm.getDisplayName(DefaultNameFormatter.getInstance())
|
---|
76 | ),
|
---|
77 | ImageProvider.get(OsmPrimitiveType.from(osm)),
|
---|
78 | JLabel.HORIZONTAL
|
---|
79 | )
|
---|
80 | );
|
---|
81 | }
|
---|
82 | }
|
---|