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

Last change on this file since 304 was 304, checked in by imi, 17 years ago
  • fixed a bug that nodes were not created when no data layer was loaded
  • deprecated Main.map.mapView.addLayerChangeListener.
File size: 1.9 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 * @author imi
26 */
27public class AddCommand extends Command {
28
29 /**
30 * The primitive to add to the dataset.
31 */
32 private final OsmPrimitive osm;
33
34 private DataSet ds;
35
36 /**
37 * Create the command and specify the element to add.
38 */
39 public AddCommand(OsmPrimitive osm) {
40 this.osm = osm;
41 this.ds = Main.main.editLayer().data;
42 }
43
44 @Override public void executeCommand() {
45 osm.visit(new AddVisitor(ds));
46 }
47
48 @Override public void undoCommand() {
49 osm.visit(new DeleteVisitor(ds));
50 }
51
52 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
53 added.add(osm);
54 }
55
56 // faster implementation
57 @Override public boolean invalidBecauselayerRemoved(Layer oldLayer) {
58 return oldLayer instanceof OsmDataLayer && ((OsmDataLayer)oldLayer).data == ds;
59 }
60
61 @Override public MutableTreeNode description() {
62 NameVisitor v = new NameVisitor();
63 osm.visit(v);
64 return new DefaultMutableTreeNode(new JLabel(tr("Add")+" "+tr(v.className)+" "+v.name, v.icon, JLabel.HORIZONTAL));
65 }
66}
Note: See TracBrowser for help on using the repository browser.