Index: trunk/src/org/openstreetmap/josm/gui/MainApplication.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/MainApplication.java	(revision 12713)
+++ trunk/src/org/openstreetmap/josm/gui/MainApplication.java	(revision 12718)
@@ -72,4 +72,5 @@
 import org.openstreetmap.josm.data.Bounds;
 import org.openstreetmap.josm.data.UndoRedoHandler;
+import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener;
 import org.openstreetmap.josm.data.Version;
 import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder;
@@ -84,6 +85,11 @@
 import org.openstreetmap.josm.gui.io.SaveLayersDialog;
 import org.openstreetmap.josm.gui.layer.AutosaveTask;
+import org.openstreetmap.josm.gui.layer.Layer;
+import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
+import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
+import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
+import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
 import org.openstreetmap.josm.gui.layer.MainLayerManager;
-import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
+import org.openstreetmap.josm.gui.layer.OsmDataLayer;
 import org.openstreetmap.josm.gui.layer.TMSLayer;
 import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
@@ -187,7 +193,7 @@
     /**
      * The commands undo/redo handler.
-     * @since 12641 (as a replacement to {@code Main.main.undoRedo})
-     */
-    public static final UndoRedoHandler undoRedo = new UndoRedoHandler(); // Must be declared after layerManager
+     * @since 12641
+     */
+    public static UndoRedoHandler undoRedo;
 
     /**
@@ -213,4 +219,24 @@
     public MainApplication(MainFrame mainFrame) {
         this.mainFrame = mainFrame;
+        undoRedo = super.undoRedo;
+        getLayerManager().addLayerChangeListener(new LayerChangeListener() {
+            @Override
+            public void layerRemoving(LayerRemoveEvent e) {
+                Layer layer = e.getRemovedLayer();
+                if (layer instanceof OsmDataLayer) {
+                    undoRedo.clean(((OsmDataLayer) layer).data);
+                }
+            }
+
+            @Override
+            public void layerOrderChanged(LayerOrderChangeEvent e) {
+                // Do nothing
+            }
+
+            @Override
+            public void layerAdded(LayerAddEvent e) {
+                // Do nothing
+            }
+        });
     }
 
@@ -405,4 +431,18 @@
     public DataSet getEditDataSet() {
         return getLayerManager().getEditDataSet();
+    }
+
+    @Override
+    public void setEditDataSet(DataSet ds) {
+        Optional<OsmDataLayer> layer = getLayerManager().getLayersOfType(OsmDataLayer.class).stream()
+                .filter(l -> l.data.equals(ds)).findFirst();
+        if (layer.isPresent()) {
+            getLayerManager().setActiveLayer(layer.get());
+        }
+    }
+
+    @Override
+    public boolean containsDataSet(DataSet ds) {
+        return getLayerManager().getLayersOfType(OsmDataLayer.class).stream().anyMatch(l -> l.data.equals(ds));
     }
 
Index: trunk/src/org/openstreetmap/josm/gui/datatransfer/importers/PrimitiveDataPaster.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/datatransfer/importers/PrimitiveDataPaster.java	(revision 12713)
+++ trunk/src/org/openstreetmap/josm/gui/datatransfer/importers/PrimitiveDataPaster.java	(revision 12718)
@@ -83,5 +83,5 @@
             }
         }
-        return new AddPrimitivesCommand(bufferCopy, toSelect, layer);
+        return new AddPrimitivesCommand(bufferCopy, toSelect, layer.data);
     }
 
Index: trunk/src/org/openstreetmap/josm/gui/dialogs/CommandStackDialog.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/dialogs/CommandStackDialog.java	(revision 12713)
+++ trunk/src/org/openstreetmap/josm/gui/dialogs/CommandStackDialog.java	(revision 12718)
@@ -39,4 +39,5 @@
 import org.openstreetmap.josm.command.Command;
 import org.openstreetmap.josm.command.PseudoCommand;
+import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener;
 import org.openstreetmap.josm.data.osm.DataSet;
 import org.openstreetmap.josm.data.osm.OsmPrimitive;
@@ -44,5 +45,4 @@
 import org.openstreetmap.josm.gui.SideButton;
 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
-import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
 import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
 import org.openstreetmap.josm.tools.GBC;
Index: trunk/src/org/openstreetmap/josm/gui/dialogs/relation/actions/RefreshAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/dialogs/relation/actions/RefreshAction.java	(revision 12713)
+++ trunk/src/org/openstreetmap/josm/gui/dialogs/relation/actions/RefreshAction.java	(revision 12718)
@@ -11,4 +11,5 @@
 
 import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener;
 import org.openstreetmap.josm.data.osm.Relation;
 import org.openstreetmap.josm.gui.HelpAwareOptionPane;
@@ -19,5 +20,4 @@
 import org.openstreetmap.josm.gui.dialogs.relation.MemberTableModel;
 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
-import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
 import org.openstreetmap.josm.gui.tagging.TagEditorModel;
 import org.openstreetmap.josm.tools.ImageProvider;
Index: trunk/src/org/openstreetmap/josm/gui/dialogs/relation/actions/SavingAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/dialogs/relation/actions/SavingAction.java	(revision 12713)
+++ trunk/src/org/openstreetmap/josm/gui/dialogs/relation/actions/SavingAction.java	(revision 12718)
@@ -73,5 +73,5 @@
         if (newRelation.getMembersCount() == 0 && !newRelation.hasKeys())
             return;
-        MainApplication.undoRedo.add(new AddCommand(layer, newRelation));
+        MainApplication.undoRedo.add(new AddCommand(layer.data, newRelation));
 
         // make sure everybody is notified about the changes
@@ -95,5 +95,5 @@
         memberTableModel.applyToRelation(editedRelation);
         Conflict<Relation> conflict = new Conflict<>(editor.getRelation(), editedRelation);
-        MainApplication.undoRedo.add(new ConflictAddCommand(layer, conflict));
+        MainApplication.undoRedo.add(new ConflictAddCommand(layer.data, conflict));
     }
 
Index: trunk/src/org/openstreetmap/josm/gui/layer/Layer.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/layer/Layer.java	(revision 12713)
+++ trunk/src/org/openstreetmap/josm/gui/layer/Layer.java	(revision 12718)
@@ -339,5 +339,5 @@
      * @param name the name. If null, the name is set to the empty string.
      */
-    public final void setName(String name) {
+    public void setName(String name) {
         if (this.name != null) {
             removeColorPropertyListener();
Index: trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java	(revision 12713)
+++ trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java	(revision 12718)
@@ -295,17 +295,4 @@
 
     /**
-     * A listener that gets notified of command queue (undo/redo) size changes.
-     */
-    @FunctionalInterface
-    public interface CommandQueueListener {
-        /**
-         * Notifies the listener about the new queue size
-         * @param queueSize Undo stack size
-         * @param redoSize Redo stack size
-         */
-        void commandChanged(int queueSize, int redoSize);
-    }
-
-    /**
      * Listener called when a state of this layer has changed.
      * @since 10600 (functional interface)
@@ -401,4 +388,5 @@
         CheckParameterUtil.ensureParameterNotNull(data, "data");
         this.data = data;
+        this.data.setName(name);
         this.setAssociatedFile(associatedFile);
         data.addDataSetListener(new DataSetListenerAdapter(this));
@@ -603,5 +591,5 @@
             return;
 
-        MainApplication.undoRedo.clean(this);
+        MainApplication.undoRedo.clean(data);
 
         // if uploaded, clean the modified flags as well
@@ -1146,3 +1134,11 @@
         invalidate();
     }
+
+    @Override
+    public void setName(String name) {
+        if (data != null) {
+            data.setName(name);
+        }
+        super.setName(name);
+    }
 }
