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

Revision 5112, 2.3 KB checked in by simon04, 2 months ago (diff)

fix #4077 - patch by sfriedle - Unsaved changes pops up when closing JOSM after undoing all changes

  • Property svn:eol-style set to native
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.util.Collection;
8import java.util.Collections;
9
10import javax.swing.Icon;
11
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
14import org.openstreetmap.josm.gui.DefaultNameFormatter;
15import org.openstreetmap.josm.gui.layer.OsmDataLayer;
16import 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 */
26public 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}
Note: See TracBrowser for help on using the repository browser.