Index: trunk/src/org/openstreetmap/josm/command/AddCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/AddCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/AddCommand.java	(revision 12726)
@@ -36,5 +36,7 @@
      * Creates the command and specify the element to add in the context of the current edit layer, if any.
      * @param osm The primitive to add
+     * @deprecated to be removed end of 2017. Use {@link #AddCommand(DataSet, OsmPrimitive)} instead
      */
+    @Deprecated
     public AddCommand(OsmPrimitive osm) {
         this.osm = Objects.requireNonNull(osm, "osm");
Index: trunk/src/org/openstreetmap/josm/command/AddPrimitivesCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/AddPrimitivesCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/AddPrimitivesCommand.java	(revision 12726)
@@ -39,5 +39,7 @@
      * Constructs a new {@code AddPrimitivesCommand} to add data to the current edit layer.
      * @param data The OSM primitives data to add. Must not be {@code null}
-     */
+     * @deprecated to be removed end of 2017. Use {@link #AddPrimitivesCommand(List, DataSet)} instead
+     */
+    @Deprecated
     public AddPrimitivesCommand(List<PrimitiveData> data) {
         this(data, data);
@@ -49,5 +51,7 @@
      * @param toSelect The OSM primitives to select at the end. Can be {@code null}
      * @since 5953
-     */
+     * @deprecated to be removed end of 2017. Use {@link #AddPrimitivesCommand(List, List, DataSet)} instead
+     */
+    @Deprecated
     public AddPrimitivesCommand(List<PrimitiveData> data, List<PrimitiveData> toSelect) {
         init(data, toSelect);
@@ -77,4 +81,14 @@
         super(ds);
         init(data, toSelect);
+    }
+
+    /**
+     * Constructs a new {@code AddPrimitivesCommand} to add data to the given data set.
+     * @param data The OSM primitives data to add and select. Must not be {@code null}
+     * @param ds The target data set. Must not be {@code null}
+     * @since 12726
+     */
+    public AddPrimitivesCommand(List<PrimitiveData> data, DataSet ds) {
+        this(data, data, ds);
     }
 
Index: trunk/src/org/openstreetmap/josm/command/ChangeCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/ChangeCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/ChangeCommand.java	(revision 12726)
@@ -30,12 +30,10 @@
 
     /**
-     * Constructs a new {@code ChangeCommand} in the context of the current edit layer, if any.
-     * @param osm The existing primitive to modify
+     * Constructs a new {@code ChangeCommand} in the context of {@code osm} data set.
+     * @param osm The existing primitive to modify. It must belong to a data set
      * @param newOsm The new primitive
      */
     public ChangeCommand(OsmPrimitive osm, OsmPrimitive newOsm) {
-        this.osm = osm;
-        this.newOsm = newOsm;
-        sanityChecks();
+        this(osm.getDataSet(), osm, newOsm);
     }
 
Index: trunk/src/org/openstreetmap/josm/command/ChangeNodesCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/ChangeNodesCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/ChangeNodesCommand.java	(revision 12726)
@@ -10,4 +10,5 @@
 import javax.swing.Icon;
 
+import org.openstreetmap.josm.data.osm.DataSet;
 import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
 import org.openstreetmap.josm.data.osm.Node;
@@ -36,4 +37,16 @@
      */
     public ChangeNodesCommand(Way way, List<Node> newNodes) {
+        this(way.getDataSet(), way, newNodes);
+    }
+
+    /**
+     * Constructs a new {@code ChangeNodesCommand}.
+     * @param ds The target data set. Must not be {@code null}
+     * @param way The way to modify
+     * @param newNodes The new list of nodes for the given way
+     * @since 12726
+     */
+    public ChangeNodesCommand(DataSet ds, Way way, List<Node> newNodes) {
+        super(ds);
         this.way = way;
         this.newNodes = newNodes;
Index: trunk/src/org/openstreetmap/josm/command/ChangePropertyCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/ChangePropertyCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/ChangePropertyCommand.java	(revision 12726)
@@ -13,4 +13,5 @@
 import java.util.List;
 import java.util.Map;
+import java.util.NoSuchElementException;
 import java.util.Objects;
 import java.util.stream.Collectors;
@@ -72,8 +73,11 @@
      * Creates a command to change multiple tags of multiple objects
      *
-     * @param objects the objects to modify
+     * @param ds The target data set. Must not be {@code null}
+     * @param objects the objects to modify. Must not be empty
      * @param tags the tags to set
-     */
-    public ChangePropertyCommand(Collection<? extends OsmPrimitive> objects, Map<String, String> tags) {
+     * @since 12726
+     */
+    public ChangePropertyCommand(DataSet ds, Collection<? extends OsmPrimitive> objects, Map<String, String> tags) {
+        super(ds);
         this.tags = tags;
         init(objects);
@@ -81,11 +85,26 @@
 
     /**
+     * Creates a command to change multiple tags of multiple objects
+     *
+     * @param objects the objects to modify. Must not be empty, and objects must belong to a data set
+     * @param tags the tags to set
+     * @throws NullPointerException if objects is null or contain null item
+     * @throws NoSuchElementException if objects is empty
+     */
+    public ChangePropertyCommand(Collection<? extends OsmPrimitive> objects, Map<String, String> tags) {
+        this(objects.iterator().next().getDataSet(), objects, tags);
+    }
+
+    /**
      * Creates a command to change one tag of multiple objects
      *
-     * @param objects the objects to modify
+     * @param objects the objects to modify. Must not be empty, and objects must belong to a data set
      * @param key the key of the tag to set
      * @param value the value of the key to set
+     * @throws NullPointerException if objects is null or contain null item
+     * @throws NoSuchElementException if objects is empty
      */
     public ChangePropertyCommand(Collection<? extends OsmPrimitive> objects, String key, String value) {
+        super(objects.iterator().next().getDataSet());
         this.tags = new HashMap<>(1);
         this.tags.put(key, value);
@@ -96,7 +115,8 @@
      * Creates a command to change one tag of one object
      *
-     * @param object the object to modify
+     * @param object the object to modify. Must belong to a data set
      * @param key the key of the tag to set
      * @param value the value of the key to set
+     * @throws NullPointerException if object is null
      */
     public ChangePropertyCommand(OsmPrimitive object, String key, String value) {
@@ -134,6 +154,4 @@
     @Override
     public boolean executeCommand() {
-        if (objects.isEmpty())
-            return true;
         final DataSet dataSet = objects.get(0).getDataSet();
         if (dataSet != null) {
Index: trunk/src/org/openstreetmap/josm/command/ChangePropertyKeyCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/ChangePropertyKeyCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/ChangePropertyKeyCommand.java	(revision 12726)
@@ -10,8 +10,10 @@
 import java.util.LinkedList;
 import java.util.List;
+import java.util.NoSuchElementException;
 import java.util.Objects;
 
 import javax.swing.Icon;
 
+import org.openstreetmap.josm.data.osm.DataSet;
 import org.openstreetmap.josm.data.osm.OsmPrimitive;
 import org.openstreetmap.josm.data.validation.util.NameVisitor;
@@ -66,5 +68,5 @@
      * Constructs a new {@code ChangePropertyKeyCommand}.
      *
-     * @param object the object subject to change replacement
+     * @param object the object subject to change replacement. Must not be null, and belong to a data set
      * @param key The key to replace
      * @param newKey the new value of the key
@@ -78,9 +80,25 @@
      * Constructs a new {@code ChangePropertyKeyCommand}.
      *
-     * @param objects all objects subject to change replacement
+     * @param objects all objects subject to change replacement. Must not be null or empty, and objects must belong to a data set
      * @param key The key to replace
      * @param newKey the new value of the key
+     * @throws NullPointerException if objects is null or contain null item
+     * @throws NoSuchElementException if objects is empty
      */
     public ChangePropertyKeyCommand(Collection<? extends OsmPrimitive> objects, String key, String newKey) {
+        this(objects.iterator().next().getDataSet(), objects, key, newKey);
+    }
+
+    /**
+     * Constructs a new {@code ChangePropertyKeyCommand}.
+     *
+     * @param ds The target data set. Must not be {@code null}
+     * @param objects all objects subject to change replacement.
+     * @param key The key to replace
+     * @param newKey the new value of the key
+     * @since 12726
+     */
+    public ChangePropertyKeyCommand(DataSet ds, Collection<? extends OsmPrimitive> objects, String key, String newKey) {
+        super(ds);
         this.objects = new LinkedList<>(objects);
         this.key = key;
Index: trunk/src/org/openstreetmap/josm/command/ChangeRelationMemberRoleCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/ChangeRelationMemberRoleCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/ChangeRelationMemberRoleCommand.java	(revision 12726)
@@ -37,17 +37,15 @@
     /**
      * Constructs a new {@code ChangeRelationMemberRoleCommand}.
-     * @param relation The relation to be changed
+     * @param relation The relation to be changed. Must not be null, and belong to a data set
      * @param position Member position
      * @param newRole New role
      */
     public ChangeRelationMemberRoleCommand(Relation relation, int position, String newRole) {
-        this.relation = relation;
-        this.position = position;
-        this.newRole = newRole;
+        this(relation.getDataSet(), relation, position, newRole);
     }
 
     /**
      * Constructs a new {@code ChangeRelationMemberRoleCommand}.
-     * @param dataSet The data set the role is in
+     * @param dataSet The data set the role is in. Must not be {@code null}
      * @param relation The relation to be changed
      * @param position Member position
Index: trunk/src/org/openstreetmap/josm/command/Command.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/Command.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/Command.java	(revision 12726)
@@ -147,5 +147,7 @@
     /**
      * Creates a new command in the context of the current edit layer, if any
-     */
+     * @deprecated to be removed end of 2017. Use {@link #Command(DataSet)} instead
+     */
+    @Deprecated
     public Command() {
         this.layer = MainApplication.getLayerManager().getEditLayer();
Index: trunk/src/org/openstreetmap/josm/command/DeleteCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/DeleteCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/DeleteCommand.java	(revision 12726)
@@ -75,5 +75,5 @@
         @Override
         public String toString() {
-            return "DeleteChildCommand [osm=" + osm + "]";
+            return "DeleteChildCommand [osm=" + osm + ']';
         }
     }
@@ -88,11 +88,9 @@
      * Constructor. Deletes a collection of primitives in the current edit layer.
      *
-     * @param data the primitives to delete. Must neither be null nor empty.
+     * @param data the primitives to delete. Must neither be null nor empty, and belong to a data set
      * @throws IllegalArgumentException if data is null or empty
      */
     public DeleteCommand(Collection<? extends OsmPrimitive> data) {
-        CheckParameterUtil.ensureParameterNotNull(data, "data");
-        this.toDelete = data;
-        checkConsistency();
+        this(data.iterator().next().getDataSet(), data);
     }
 
Index: trunk/src/org/openstreetmap/josm/command/MoveCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/MoveCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/MoveCommand.java	(revision 12726)
@@ -9,4 +9,5 @@
 import java.util.LinkedList;
 import java.util.List;
+import java.util.NoSuchElementException;
 import java.util.Objects;
 
@@ -84,9 +85,12 @@
     /**
      * Constructs a new {@code MoveCommand} and assign the initial object set and movement vector.
-     * @param objects The primitives to move
+     * @param objects The primitives to move. Must neither be null nor empty. Objects must belong to a data set
      * @param x X difference movement. Coordinates are in northern/eastern
      * @param y Y difference movement. Coordinates are in northern/eastern
+     * @throws NullPointerException if objects is null or contain null item
+     * @throws NoSuchElementException if objects is empty
      */
     public MoveCommand(Collection<OsmPrimitive> objects, double x, double y) {
+        super(objects.iterator().next().getDataSet());
         startEN = null;
         saveCheckpoint(); // (0,0) displacement will be saved
Index: trunk/src/org/openstreetmap/josm/command/RemoveNodesCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/RemoveNodesCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/RemoveNodesCommand.java	(revision 12726)
@@ -33,8 +33,9 @@
     /**
      * Constructs a new {@code RemoveNodesCommand}.
-     * @param way The way to modify
+     * @param way The way to modify. Must not be null, and belong to a data set
      * @param rmNodes The list of nodes to remove
      */
     public RemoveNodesCommand(Way way, List<Node> rmNodes) {
+        super(way.getDataSet());
         this.way = way;
         this.rmNodes = new HashSet<>(rmNodes);
Index: trunk/src/org/openstreetmap/josm/command/SelectCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/SelectCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/SelectCommand.java	(revision 12726)
@@ -28,5 +28,7 @@
      * Constructs a new select command.
      * @param newSelection the primitives to select when executing the command.
+     * @deprecated to be removed end of 2017. Use {@link #SelectCommand(DataSet, Collection)} instead
      */
+    @Deprecated
     public SelectCommand(Collection<OsmPrimitive> newSelection) {
         if (newSelection == null || newSelection.isEmpty()) {
Index: trunk/src/org/openstreetmap/josm/command/SequenceCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/SequenceCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/SequenceCommand.java	(revision 12726)
@@ -11,4 +11,5 @@
 import javax.swing.Icon;
 
+import org.openstreetmap.josm.data.osm.DataSet;
 import org.openstreetmap.josm.data.osm.OsmPrimitive;
 import org.openstreetmap.josm.gui.layer.Layer;
@@ -33,4 +34,19 @@
     /**
      * Create the command by specifying the list of commands to execute.
+     * @param ds The target data set. Must not be {@code null}
+     * @param name The description text
+     * @param sequenz The sequence that should be executed
+     * @param continueOnError Determines if the sequence execution should continue after one of its commands fails
+     * @since 12726
+     */
+    public SequenceCommand(DataSet ds, String name, Collection<Command> sequenz, boolean continueOnError) {
+        super(ds);
+        this.name = name;
+        this.sequence = sequenz.toArray(new Command[sequenz.size()]);
+        this.continueOnError = continueOnError;
+    }
+
+    /**
+     * Create the command by specifying the list of commands to execute.
      * @param name The description text
      * @param sequenz The sequence that should be executed. Must not be null or empty
@@ -39,8 +55,5 @@
      */
     public SequenceCommand(String name, Collection<Command> sequenz, boolean continueOnError) {
-        super(sequenz.iterator().next().getAffectedDataSet());
-        this.name = name;
-        this.sequence = sequenz.toArray(new Command[sequenz.size()]);
-        this.continueOnError = continueOnError;
+        this(sequenz.iterator().next().getAffectedDataSet(), name, sequenz, continueOnError);
     }
 
Index: trunk/src/org/openstreetmap/josm/command/TransformNodesCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/TransformNodesCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/TransformNodesCommand.java	(revision 12726)
@@ -6,6 +6,6 @@
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.LinkedList;
 import java.util.Map;
+import java.util.NoSuchElementException;
 import java.util.Objects;
 
@@ -28,10 +28,10 @@
      * The nodes to transform.
      */
-    protected Collection<Node> nodes = new LinkedList<>();
+    protected final Collection<Node> nodes;
 
     /**
      * List of all old states of the nodes.
      */
-    protected Map<Node, OldNodeState> oldStates = new HashMap<>();
+    protected final Map<Node, OldNodeState> oldStates = new HashMap<>();
 
     /**
@@ -47,7 +47,10 @@
      * Creates a TransformNodesObject.
      * Find out the impacted nodes and store their initial state.
-     * @param objects objects to fetch nodes from
+     * @param objects objects to fetch nodes from. Must neither be null nor empty. Items must belong to a data set
+     * @throws NullPointerException if objects is null or contain null item
+     * @throws NoSuchElementException if objects is empty
      */
     public TransformNodesCommand(Collection<? extends OsmPrimitive> objects) {
+        super(objects.iterator().next().getDataSet());
         this.nodes = AllNodesVisitor.getAllNodes(objects);
         storeOldState();
Index: trunk/src/org/openstreetmap/josm/command/conflict/ConflictAddCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/conflict/ConflictAddCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/conflict/ConflictAddCommand.java	(revision 12726)
@@ -79,5 +79,5 @@
     public void undoCommand() {
         DataSet ds = getAffectedDataSet();
-        if (!Main.main.containsDataSet(ds)) {
+        if (Main.main != null && !Main.main.containsDataSet(ds)) {
             Logging.warn(tr("Layer ''{0}'' does not exist any more. Cannot remove conflict for object ''{1}''.",
                     ds.getName(),
Index: trunk/src/org/openstreetmap/josm/command/conflict/ConflictResolveCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/conflict/ConflictResolveCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/conflict/ConflictResolveCommand.java	(revision 12726)
@@ -28,7 +28,9 @@
     /**
      * Constructs a new {@code ConflictResolveCommand} in the context of the current edit layer, if any.
+     * @deprecated to be removed end of 2017. Use {@link #ConflictResolveCommand(DataSet)} instead
      */
+    @Deprecated
     public ConflictResolveCommand() {
-        // Do nothing
+        this(Main.main.getEditDataSet());
     }
 
@@ -81,13 +83,15 @@
 
         DataSet ds = getAffectedDataSet();
-        if (!Main.main.containsDataSet(ds)) {
-            Logging.warn(tr("Cannot undo command ''{0}'' because layer ''{1}'' is not present any more",
-                    this.toString(),
-                    ds.getName()
-            ));
-            return;
+        if (Main.main != null) {
+            if (!Main.main.containsDataSet(ds)) {
+                Logging.warn(tr("Cannot undo command ''{0}'' because layer ''{1}'' is not present any more",
+                        this.toString(),
+                        ds.getName()
+                ));
+                return;
+            }
+
+            Main.main.setEditDataSet(ds);
         }
-
-        Main.main.setEditDataSet(ds);
         reconstituteConflicts();
     }
Index: trunk/src/org/openstreetmap/josm/command/conflict/CoordinateConflictResolveCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/conflict/CoordinateConflictResolveCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/conflict/CoordinateConflictResolveCommand.java	(revision 12726)
@@ -34,4 +34,5 @@
      */
     public CoordinateConflictResolveCommand(Conflict<? extends OsmPrimitive> conflict, MergeDecisionType decision) {
+        super(conflict.getMy().getDataSet());
         this.conflict = conflict;
         this.decision = decision;
Index: trunk/src/org/openstreetmap/josm/command/conflict/DeletedStateConflictResolveCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/conflict/DeletedStateConflictResolveCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/conflict/DeletedStateConflictResolveCommand.java	(revision 12726)
@@ -34,4 +34,5 @@
      */
     public DeletedStateConflictResolveCommand(Conflict<? extends OsmPrimitive> conflict, MergeDecisionType decision) {
+        super(conflict.getMy().getDataSet());
         this.conflict = conflict;
         this.decision = decision;
Index: trunk/src/org/openstreetmap/josm/command/conflict/ModifiedConflictResolveCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/conflict/ModifiedConflictResolveCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/conflict/ModifiedConflictResolveCommand.java	(revision 12726)
@@ -29,4 +29,5 @@
      */
     public ModifiedConflictResolveCommand(Conflict<? extends OsmPrimitive> conflict) {
+        super(conflict.getMy().getDataSet());
         this.conflict = conflict;
     }
Index: trunk/src/org/openstreetmap/josm/command/conflict/RelationMemberConflictResolverCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/conflict/RelationMemberConflictResolverCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/conflict/RelationMemberConflictResolverCommand.java	(revision 12726)
@@ -36,4 +36,5 @@
     @SuppressWarnings("unchecked")
     public RelationMemberConflictResolverCommand(Conflict<? extends OsmPrimitive> conflict, List<RelationMember> mergedMembers) {
+        super(conflict.getMy().getDataSet());
         this.conflict = (Conflict<Relation>) conflict;
         this.mergedMembers = mergedMembers;
Index: trunk/src/org/openstreetmap/josm/command/conflict/TagConflictResolveCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/conflict/TagConflictResolveCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/conflict/TagConflictResolveCommand.java	(revision 12726)
@@ -50,4 +50,5 @@
      */
     public TagConflictResolveCommand(Conflict<? extends OsmPrimitive> conflict, List<TagMergeItem> mergeItems) {
+        super(conflict.getMy().getDataSet());
         this.conflict = conflict;
         this.mergeItems = mergeItems;
Index: trunk/src/org/openstreetmap/josm/command/conflict/VersionConflictResolveCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/conflict/VersionConflictResolveCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/conflict/VersionConflictResolveCommand.java	(revision 12726)
@@ -29,4 +29,5 @@
      */
     public VersionConflictResolveCommand(Conflict<? extends OsmPrimitive> conflict) {
+        super(conflict.getMy().getDataSet());
         this.conflict = conflict;
     }
Index: trunk/src/org/openstreetmap/josm/command/conflict/WayNodesConflictResolverCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/conflict/WayNodesConflictResolverCommand.java	(revision 12721)
+++ trunk/src/org/openstreetmap/josm/command/conflict/WayNodesConflictResolverCommand.java	(revision 12726)
@@ -36,4 +36,5 @@
     @SuppressWarnings("unchecked")
     public WayNodesConflictResolverCommand(Conflict<? extends OsmPrimitive> conflict, List<Node> mergedNodeList) {
+        super(conflict.getMy().getDataSet());
         this.conflict = (Conflict<Way>) conflict;
         this.mergedNodeList = mergedNodeList;
