001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.command;
003
004import static org.openstreetmap.josm.tools.I18n.marktr;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.util.Collection;
008import java.util.Collections;
009
010import javax.swing.Icon;
011
012import org.openstreetmap.josm.data.osm.OsmPrimitive;
013import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
014import org.openstreetmap.josm.data.osm.Way;
015import org.openstreetmap.josm.gui.DefaultNameFormatter;
016import org.openstreetmap.josm.gui.layer.OsmDataLayer;
017import org.openstreetmap.josm.tools.ImageProvider;
018
019/**
020 * A command that adds an osm primitive to a dataset. Keys cannot be added this way.
021 *
022 * See {@link ChangeCommand} for comments on relation back references.
023 *
024 * @author imi
025 */
026public class AddCommand extends Command {
027
028    /**
029     * The primitive to add to the dataset.
030     */
031    private final OsmPrimitive osm;
032
033    /**
034     * Creates the command and specify the element to add in the context of the current edit layer, if any.
035     * @param osm The primitive to add
036     */
037    public AddCommand(OsmPrimitive osm) {
038        this.osm = osm;
039    }
040
041    /**
042     * Creates the command and specify the element to add in the context of the given data layer.
043     * @param layer The data layer. Must not be {@code null}
044     * @param osm The primitive to add
045     */
046    public AddCommand(OsmDataLayer layer, OsmPrimitive osm) {
047        super(layer);
048        this.osm = osm;
049    }
050
051    protected static final void checkNodeStyles(OsmPrimitive osm) {
052        if (osm instanceof Way) {
053            // Fix #10557 - node icon not updated after undoing/redoing addition of a way
054            ((Way) osm).clearCachedNodeStyles();
055        }
056    }
057
058    @Override
059    public boolean executeCommand() {
060        getLayer().data.addPrimitive(osm);
061        osm.setModified(true);
062        checkNodeStyles(osm);
063        return true;
064    }
065
066    @Override
067    public void undoCommand() {
068        getLayer().data.removePrimitive(osm);
069        checkNodeStyles(osm);
070    }
071
072    @Override
073    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
074        added.add(osm);
075    }
076
077    @Override
078    public String getDescriptionText() {
079        String msg;
080        switch(OsmPrimitiveType.from(osm)) {
081        case NODE: msg = marktr("Add node {0}"); break;
082        case WAY: msg = marktr("Add way {0}"); break;
083        case RELATION: msg = marktr("Add relation {0}"); break;
084        default: /* should not happen */msg = ""; break;
085        }
086        return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
087    }
088
089    @Override
090    public Icon getDescriptionIcon() {
091        return ImageProvider.get(osm.getDisplayType());
092    }
093
094    @Override
095    public Collection<OsmPrimitive> getParticipatingPrimitives() {
096        return Collections.singleton(osm);
097    }
098
099    @Override
100    public int hashCode() {
101        final int prime = 31;
102        int result = super.hashCode();
103        result = prime * result + ((osm == null) ? 0 : osm.hashCode());
104        return result;
105    }
106
107    @Override
108    public boolean equals(Object obj) {
109        if (this == obj)
110            return true;
111        if (!super.equals(obj))
112            return false;
113        if (getClass() != obj.getClass())
114            return false;
115        AddCommand other = (AddCommand) obj;
116        if (osm == null) {
117            if (other.osm != null)
118                return false;
119        } else if (!osm.equals(other.osm))
120            return false;
121        return true;
122    }
123}