source: josm/trunk/src/org/openstreetmap/josm/command/AddCommand.java@ 1722

Last change on this file since 1722 was 1169, checked in by stoecker, 15 years ago

removed usage of tab stops

  • Property svn:eol-style set to native
File size: 2.1 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Collection;
7
8import javax.swing.JLabel;
9import javax.swing.tree.DefaultMutableTreeNode;
10import javax.swing.tree.MutableTreeNode;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.osm.DataSet;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.osm.visitor.AddVisitor;
16import org.openstreetmap.josm.data.osm.visitor.DeleteVisitor;
17import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
18import org.openstreetmap.josm.gui.layer.Layer;
19import org.openstreetmap.josm.gui.layer.OsmDataLayer;
20
21/**
22 * A command that adds an osm primitive to a dataset. Keys cannot be added this
23 * way.
24 *
25 * See {@link ChangeCommand ChangeCommand} for comments on relation back references.
26 *
27 * @author imi
28 */
29public class AddCommand extends Command {
30
31 /**
32 * The primitive to add to the dataset.
33 */
34 private final OsmPrimitive osm;
35
36 private DataSet ds;
37
38 /**
39 * Create the command and specify the element to add.
40 */
41 public AddCommand(OsmPrimitive osm) {
42 this.osm = osm;
43 this.ds = Main.main.editLayer().data;
44 }
45
46 @Override public boolean executeCommand() {
47 osm.visit(new AddVisitor(ds));
48 return true;
49 }
50
51 @Override public void undoCommand() {
52 osm.visit(new DeleteVisitor(ds));
53 }
54
55 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
56 added.add(osm);
57 }
58
59 // faster implementation
60 @Override public boolean invalidBecauselayerRemoved(Layer oldLayer) {
61 return oldLayer instanceof OsmDataLayer && ((OsmDataLayer)oldLayer).data == ds;
62 }
63
64 @Override public MutableTreeNode description() {
65 NameVisitor v = new NameVisitor();
66 osm.visit(v);
67 return new DefaultMutableTreeNode(new JLabel(tr("Add")+" "+tr(v.className)+" "+v.name, v.icon, JLabel.HORIZONTAL));
68 }
69}
Note: See TracBrowser for help on using the repository browser.