| 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.Main;
|
|---|
| 13 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 16 | import org.openstreetmap.josm.data.osm.visitor.AddVisitor;
|
|---|
| 17 | import org.openstreetmap.josm.data.osm.visitor.DeleteVisitor;
|
|---|
| 18 | import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
|
|---|
| 19 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 20 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * A command that adds an osm primitive to a dataset. Keys cannot be added this
|
|---|
| 24 | * way. Use ChangeKeyValueCommand instead.
|
|---|
| 25 | *
|
|---|
| 26 | * See ChangeCommand for comments on relation back references.
|
|---|
| 27 | *
|
|---|
| 28 | * @author imi
|
|---|
| 29 | */
|
|---|
| 30 | public class AddCommand extends Command {
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * The primitive to add to the dataset.
|
|---|
| 34 | */
|
|---|
| 35 | private final OsmPrimitive osm;
|
|---|
| 36 |
|
|---|
| 37 | private DataSet ds;
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * Create the command and specify the element to add.
|
|---|
| 41 | */
|
|---|
| 42 | public AddCommand(OsmPrimitive osm) {
|
|---|
| 43 | this.osm = osm;
|
|---|
| 44 | this.ds = Main.main.editLayer().data;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | @Override public void executeCommand() {
|
|---|
| 48 | osm.visit(new AddVisitor(ds));
|
|---|
| 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 | }
|
|---|