001 // License: GPL. Copyright 2007 by Immanuel Scholz and others 002 package org.openstreetmap.josm.command; 003 004 import static org.openstreetmap.josm.tools.I18n.marktr; 005 import static org.openstreetmap.josm.tools.I18n.tr; 006 007 import java.util.Collection; 008 import java.util.Collections; 009 010 import javax.swing.Icon; 011 012 import org.openstreetmap.josm.data.osm.OsmPrimitive; 013 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 014 import org.openstreetmap.josm.gui.DefaultNameFormatter; 015 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 016 import org.openstreetmap.josm.tools.ImageProvider; 017 018 /** 019 * A command that adds an osm primitive to a dataset. Keys cannot be added this 020 * way. 021 * 022 * See {@link ChangeCommand} for comments on relation back references. 023 * 024 * @author imi 025 */ 026 public class AddCommand extends Command { 027 028 /** 029 * The primitive to add to the dataset. 030 */ 031 private final OsmPrimitive osm; 032 033 /** 034 * Create the command and specify the element to add. 035 */ 036 public AddCommand(OsmPrimitive osm) { 037 super(); 038 this.osm = osm; 039 } 040 041 /** 042 * Create the command and specify the element to add. 043 */ 044 public AddCommand(OsmDataLayer layer, OsmPrimitive osm) { 045 super(layer); 046 this.osm = osm; 047 } 048 049 @Override public boolean executeCommand() { 050 getLayer().data.addPrimitive(osm); 051 osm.setModified(true); 052 return true; 053 } 054 055 @Override public void undoCommand() { 056 getLayer().data.removePrimitive(osm); 057 } 058 059 @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) { 060 added.add(osm); 061 } 062 063 @Override 064 public String getDescriptionText() { 065 String msg; 066 switch(OsmPrimitiveType.from(osm)) { 067 case NODE: msg = marktr("Add node {0}"); break; 068 case WAY: msg = marktr("Add way {0}"); break; 069 case RELATION: msg = marktr("Add relation {0}"); break; 070 default: /* should not happen */msg = ""; break; 071 } 072 return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance())); 073 } 074 075 @Override 076 public Icon getDescriptionIcon() { 077 return ImageProvider.get(osm.getDisplayType()); 078 } 079 080 @Override 081 public Collection<OsmPrimitive> getParticipatingPrimitives() { 082 return Collections.singleton(osm); 083 } 084 }