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

Last change on this file since 628 was 627, checked in by framm, 16 years ago
  • Property svn:eol-style set to native
File size: 2.0 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. Use ChangeKeyValueCommand instead.
24 *
25 * See 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 void executeCommand() {
47 osm.visit(new AddVisitor(ds));
48 }
49
50 @Override public void undoCommand() {
51 osm.visit(new DeleteVisitor(ds));
52 }
53
54 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
55 added.add(osm);
56 }
57
58 // faster implementation
59 @Override public boolean invalidBecauselayerRemoved(Layer oldLayer) {
60 return oldLayer instanceof OsmDataLayer && ((OsmDataLayer)oldLayer).data == ds;
61 }
62
63 @Override public MutableTreeNode description() {
64 NameVisitor v = new NameVisitor();
65 osm.visit(v);
66 return new DefaultMutableTreeNode(new JLabel(tr("Add")+" "+tr(v.className)+" "+v.name, v.icon, JLabel.HORIZONTAL));
67 }
68}
Note: See TracBrowser for help on using the repository browser.