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