Index: trunk/src/org/openstreetmap/josm/actions/AddNodeAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/AddNodeAction.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/actions/AddNodeAction.java	(revision 10467)
@@ -66,8 +66,10 @@
         Main.main.undoRedo.add(new AddCommand(nnew));
         getLayerManager().getEditDataSet().setSelected(nnew);
-        if (Main.map.mapView.getRealBounds().contains(nnew.getCoor())) {
-            Main.map.mapView.repaint();
-        } else {
-            AutoScaleAction.zoomTo(Collections.<OsmPrimitive>singleton(nnew));
+        if (Main.map.mapView != null) {
+            if (Main.map.mapView.getRealBounds().contains(nnew.getCoor())) {
+                Main.map.mapView.repaint();
+            } else {
+                AutoScaleAction.zoomTo(Collections.<OsmPrimitive>singleton(nnew));
+            }
         }
     }
Index: trunk/src/org/openstreetmap/josm/actions/AlignInCircleAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/AlignInCircleAction.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/actions/AlignInCircleAction.java	(revision 10467)
@@ -284,5 +284,4 @@
 
         Main.main.undoRedo.add(new SequenceCommand(tr("Align Nodes in Circle"), cmds));
-        Main.map.repaint();
     }
 
Index: trunk/src/org/openstreetmap/josm/actions/AlignInLineAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/AlignInLineAction.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/actions/AlignInLineAction.java	(revision 10467)
@@ -205,5 +205,4 @@
             // Do it!
             Main.main.undoRedo.add(cmd);
-            Main.map.repaint();
 
         } catch (InvalidSelection except) {
Index: trunk/src/org/openstreetmap/josm/actions/CreateCircleAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/CreateCircleAction.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/actions/CreateCircleAction.java	(revision 10467)
@@ -241,5 +241,4 @@
 
         Main.main.undoRedo.add(new SequenceCommand(tr("Create Circle"), cmds));
-        Main.map.repaint();
     }
 
Index: trunk/src/org/openstreetmap/josm/actions/DistributeAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/DistributeAction.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/actions/DistributeAction.java	(revision 10467)
@@ -98,5 +98,4 @@
         // Do it!
         Main.main.undoRedo.add(new SequenceCommand(tr("Distribute Nodes"), cmds));
-        Main.map.repaint();
     }
 
Index: trunk/src/org/openstreetmap/josm/actions/ImageryAdjustAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/ImageryAdjustAction.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/actions/ImageryAdjustAction.java	(revision 10467)
@@ -137,5 +137,4 @@
             }
             kev.consume();
-            Main.map.repaint();
         }
     }
@@ -164,5 +163,4 @@
             offsetDialog.updateOffset();
         }
-        Main.map.repaint();
         prevEastNorth = eastNorth;
     }
Index: trunk/src/org/openstreetmap/josm/actions/JoinNodeWayAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/JoinNodeWayAction.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/actions/JoinNodeWayAction.java	(revision 10467)
@@ -160,5 +160,4 @@
         if (cmds.isEmpty()) return;
         Main.main.undoRedo.add(new SequenceCommand(getValue(NAME).toString(), cmds));
-        Main.map.repaint();
     }
 
Index: trunk/src/org/openstreetmap/josm/actions/MirrorAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/MirrorAction.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/actions/MirrorAction.java	(revision 10467)
@@ -81,5 +81,4 @@
 
         Main.main.undoRedo.add(new SequenceCommand(tr("Mirror"), cmds));
-        Main.map.repaint();
     }
 
Index: trunk/src/org/openstreetmap/josm/actions/OrthogonalizeAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/OrthogonalizeAction.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/actions/OrthogonalizeAction.java	(revision 10467)
@@ -113,5 +113,4 @@
                 if (!commands.isEmpty()) {
                     Main.main.undoRedo.add(new SequenceCommand(tr("Orthogonalize / Undo"), commands));
-                    Main.map.repaint();
                 } else {
                     throw new InvalidUserInputException("Commands are empty");
@@ -153,5 +152,4 @@
             final SequenceCommand command = orthogonalize(sel);
             Main.main.undoRedo.add(new SequenceCommand(tr("Orthogonalize"), command));
-            Main.map.repaint();
         } catch (InvalidUserInputException ex) {
             Main.debug(ex);
Index: trunk/src/org/openstreetmap/josm/actions/ReverseWayAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/ReverseWayAction.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/actions/ReverseWayAction.java	(revision 10467)
@@ -103,8 +103,8 @@
         }
         Main.main.undoRedo.add(new SequenceCommand(tr("Reverse ways"), c));
+        // FIXME: This should be handled by undoRedo.
         if (propertiesUpdated) {
             ds.fireSelectionChanged();
         }
-        Main.map.repaint();
     }
 
Index: trunk/src/org/openstreetmap/josm/actions/SimplifyWayAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/SimplifyWayAction.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/actions/SimplifyWayAction.java	(revision 10467)
@@ -124,5 +124,4 @@
             ds.endUpdate();
         }
-        Main.map.repaint();
     }
 
Index: trunk/src/org/openstreetmap/josm/actions/UnJoinNodeWayAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/UnJoinNodeWayAction.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/actions/UnJoinNodeWayAction.java	(revision 10467)
@@ -91,5 +91,4 @@
         // I'm sure there's a better way to handle this
         Main.main.undoRedo.add(new RemoveNodesCommand(selectedWay, selectedNodes));
-        Main.map.repaint();
     }
 
Index: trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadReferrersTask.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadReferrersTask.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadReferrersTask.java	(revision 10467)
@@ -122,6 +122,4 @@
                     public void run() {
                         targetLayer.onPostDownloadFromServer();
-                        if (Main.map != null)
-                            Main.map.mapView.repaint();
                     }
                 }
Index: trunk/src/org/openstreetmap/josm/actions/mapmode/DeleteAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/mapmode/DeleteAction.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/actions/mapmode/DeleteAction.java	(revision 10467)
@@ -134,21 +134,24 @@
      */
     public static void doActionPerformed(ActionEvent e) {
-        if (!Main.map.mapView.isActiveLayerDrawable())
-            return;
+        MainLayerManager lm = Main.getLayerManager();
+        OsmDataLayer editLayer = lm.getEditLayer();
+        if (editLayer == null) {
+            return;
+        }
+
         boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
         boolean alt = (e.getModifiers() & (ActionEvent.ALT_MASK | InputEvent.ALT_GRAPH_MASK)) != 0;
 
-        MainLayerManager lm = Main.getLayerManager();
         Command c;
         if (ctrl) {
-            c = DeleteCommand.deleteWithReferences(lm.getEditLayer(), lm.getEditDataSet().getSelected());
+            c = DeleteCommand.deleteWithReferences(editLayer, lm.getEditDataSet().getSelected());
         } else {
-            c = DeleteCommand.delete(lm.getEditLayer(), lm.getEditDataSet().getSelected(), !alt /* also delete nodes in way */);
+            c = DeleteCommand.delete(editLayer, lm.getEditDataSet().getSelected(), !alt /* also delete nodes in way */);
         }
         // if c is null, an error occurred or the user aborted. Don't do anything in that case.
         if (c != null) {
             Main.main.undoRedo.add(c);
+            //FIXME: This should not be required, DeleteCommand should update the selection, otherwise undo/redo won't work.
             lm.getEditDataSet().setSelected();
-            Main.map.repaint();
         }
     }
@@ -213,15 +216,15 @@
     private void repaintIfRequired(Set<OsmPrimitive> newHighlights, WaySegment newHighlightedWaySegment) {
         boolean needsRepaint = false;
-        DataSet ds = getLayerManager().getEditDataSet();
+        OsmDataLayer editLayer = getLayerManager().getEditLayer();
 
         if (newHighlightedWaySegment == null && oldHighlightedWaySegment != null) {
-            if (ds != null) {
-                ds.clearHighlightedWaySegments();
+            if (editLayer != null) {
+                editLayer.data.clearHighlightedWaySegments();
                 needsRepaint = true;
             }
             oldHighlightedWaySegment = null;
         } else if (newHighlightedWaySegment != null && !newHighlightedWaySegment.equals(oldHighlightedWaySegment)) {
-            if (ds != null) {
-                ds.setHighlightedWaySegments(Collections.singleton(newHighlightedWaySegment));
+            if (editLayer != null) {
+                editLayer.data.setHighlightedWaySegments(Collections.singleton(newHighlightedWaySegment));
                 needsRepaint = true;
             }
@@ -229,6 +232,6 @@
         }
         needsRepaint |= highlightHelper.highlightOnly(newHighlights);
-        if (needsRepaint) {
-            Main.map.mapView.repaint();
+        if (needsRepaint && editLayer != null) {
+            editLayer.invalidate();
         }
     }
Index: trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java	(revision 10467)
@@ -185,6 +185,7 @@
 
         // update selection to reflect which way being modified
-        DataSet currentDataSet = getLayerManager().getEditDataSet();
-        if (getCurrentBaseNode() != null && currentDataSet != null && !currentDataSet.selectionEmpty()) {
+        OsmDataLayer editLayer = getLayerManager().getEditLayer();
+        if (getCurrentBaseNode() != null && editLayer != null && !editLayer.data.selectionEmpty()) {
+            DataSet currentDataSet = editLayer.data;
             Way continueFrom = getWayForNode(getCurrentBaseNode());
             if (alt && continueFrom != null && (!getCurrentBaseNode().isSelected() || continueFrom.isSelected())) {
@@ -197,6 +198,6 @@
         }
 
-        if (needsRepaint) {
-            Main.map.mapView.repaint();
+        if (needsRepaint && editLayer != null) {
+            editLayer.invalidate();
         }
         return needsRepaint;
@@ -917,5 +918,6 @@
     @Override
     public void mouseExited(MouseEvent e) {
-        if (!Main.map.mapView.isActiveLayerDrawable())
+        OsmDataLayer editLayer = Main.getLayerManager().getEditLayer();
+        if (editLayer == null)
             return;
         mousePos = e.getPoint();
@@ -925,5 +927,5 @@
         // caused one already, don’t do it again.
         if (!repaintIssued) {
-            Main.map.mapView.repaint();
+            editLayer.invalidate();
         }
     }
Index: trunk/src/org/openstreetmap/josm/command/AddCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/AddCommand.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/command/AddCommand.java	(revision 10467)
@@ -59,5 +59,5 @@
     @Override
     public boolean executeCommand() {
-        getLayer().data.addPrimitive(osm);
+        getAffectedDataSet().addPrimitive(osm);
         osm.setModified(true);
         checkNodeStyles(osm);
@@ -67,5 +67,5 @@
     @Override
     public void undoCommand() {
-        getLayer().data.removePrimitive(osm);
+        getAffectedDataSet().removePrimitive(osm);
         checkNodeStyles(osm);
     }
Index: trunk/src/org/openstreetmap/josm/command/AddPrimitivesCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/AddPrimitivesCommand.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/command/AddPrimitivesCommand.java	(revision 10467)
@@ -78,5 +78,5 @@
 
             for (PrimitiveData pd : data) {
-                OsmPrimitive primitive = getLayer().data.getPrimitiveById(pd);
+                OsmPrimitive primitive = getAffectedDataSet().getPrimitiveById(pd);
                 boolean created = primitive == null;
                 if (created) {
@@ -87,5 +87,5 @@
                 }
                 if (created) {
-                    getLayer().data.addPrimitive(primitive);
+                    getAffectedDataSet().addPrimitive(primitive);
                 }
                 newPrimitives.add(primitive);
@@ -115,5 +115,5 @@
 
     @Override public void undoCommand() {
-        DataSet ds = getLayer().data;
+        DataSet ds = getAffectedDataSet();
 
         if (createdPrimitives == null) {
@@ -167,5 +167,5 @@
         Collection<OsmPrimitive> prims = new HashSet<>();
         for (PrimitiveData d : data) {
-            OsmPrimitive osm = getLayer().data.getPrimitiveById(d);
+            OsmPrimitive osm = getAffectedDataSet().getPrimitiveById(d);
             if (osm == null)
                 throw new RuntimeException();
Index: trunk/src/org/openstreetmap/josm/command/Command.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/Command.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/command/Command.java	(revision 10467)
@@ -17,4 +17,5 @@
 import org.openstreetmap.josm.data.coor.EastNorth;
 import org.openstreetmap.josm.data.coor.LatLon;
+import org.openstreetmap.josm.data.osm.DataSet;
 import org.openstreetmap.josm.data.osm.Node;
 import org.openstreetmap.josm.data.osm.OsmPrimitive;
@@ -210,4 +211,13 @@
     protected OsmDataLayer getLayer() {
         return layer;
+    }
+
+    /**
+     * Gets the data set this command affects.
+     * @return The data set. May be <code>null</code> if no layer was set and no edit layer was found.
+     * @since 10467
+     */
+    public DataSet getAffectedDataSet() {
+        return layer == null ? null : layer.data;
     }
 
Index: trunk/src/org/openstreetmap/josm/command/conflict/WayNodesConflictResolverCommand.java
===================================================================
--- trunk/src/org/openstreetmap/josm/command/conflict/WayNodesConflictResolverCommand.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/command/conflict/WayNodesConflictResolverCommand.java	(revision 10467)
@@ -59,5 +59,5 @@
         //
         for (Node n:mergedNodeList) {
-            if (!getLayer().data.getNodes().contains(n)) {
+            if (!getAffectedDataSet().getNodes().contains(n)) {
                 Main.warn(tr("Main dataset does not include node {0}", n.toString()));
             }
Index: trunk/src/org/openstreetmap/josm/data/UndoRedoHandler.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/UndoRedoHandler.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/data/UndoRedoHandler.java	(revision 10467)
@@ -74,11 +74,20 @@
      */
     public synchronized void add(final Command c) {
-        DataSet ds = Main.getLayerManager().getEditDataSet();
-        Collection<? extends OsmPrimitive> oldSelection = ds.getSelected();
+        DataSet ds = c.getAffectedDataSet();
+        if (ds == null) {
+            // old, legacy behaviour
+            ds = Main.getLayerManager().getEditDataSet();
+        }
+        Collection<? extends OsmPrimitive> oldSelection = null;
+        if (ds != null) {
+            oldSelection = ds.getSelected();
+        }
         addNoRedraw(c);
         afterAdd();
 
         // the command may have changed the selection so tell the listeners about the current situation
-        fireIfSelectionChanged(ds, oldSelection);
+        if (ds != null) {
+            fireIfSelectionChanged(ds, oldSelection);
+        }
     }
 
Index: trunk/src/org/openstreetmap/josm/data/gpx/GpxData.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/gpx/GpxData.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/data/gpx/GpxData.java	(revision 10467)
@@ -203,5 +203,5 @@
                     if (pnt.compareTo(earliest) < 0) {
                         earliest = pnt;
-                    } else {
+                    } else if (pnt.compareTo(latest) > 0) {
                         latest = pnt;
                     }
Index: trunk/src/org/openstreetmap/josm/data/gpx/WayPoint.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/gpx/WayPoint.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/data/gpx/WayPoint.java	(revision 10467)
@@ -19,5 +19,5 @@
 
     /**
-     * The seconds (not milliseconds!) since 1970-01-01.
+     * The seconds (not milliseconds!) since 1970-01-01 00:00 UTC
      */
     public double time;
Index: trunk/src/org/openstreetmap/josm/gui/MainApplication.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/MainApplication.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/gui/MainApplication.java	(revision 10467)
@@ -101,4 +101,5 @@
     @Override
     protected void initializeMainWindow() {
+        mainPanel.reAddListeners();
         if (mainFrame != null) {
             mainFrame.initialize();
@@ -410,5 +411,4 @@
         final MainFrame mainFrame = new MainFrame(contentPanePrivate, mainPanel, geometry);
         Main.parent = mainFrame;
-        mainPanel.reAddListeners();
 
         if (args.containsKey(Option.LOAD_PREFERENCES)) {
Index: trunk/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java	(revision 10467)
@@ -175,6 +175,7 @@
      * Creates a layer list and attach it to the given mapView.
      * @param layerManager The layer manager this list is for
-     */
-    private LayerListDialog(MainLayerManager layerManager) {
+     * @since 10467
+     */
+    public LayerListDialog(MainLayerManager layerManager) {
         super(tr("Layers"), "layerlist", tr("Open a list of all loaded layers."),
                 Shortcut.registerShortcut("subwindow:layers", tr("Toggle: {0}", tr("Layers")), KeyEvent.VK_L,
Index: trunk/src/org/openstreetmap/josm/gui/dialogs/ToggleDialog.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/dialogs/ToggleDialog.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/gui/dialogs/ToggleDialog.java	(revision 10467)
@@ -257,4 +257,12 @@
         Main.pref.addPreferenceChangeListener(this);
 
+        registerInWindowMenu();
+    }
+
+    /**
+     * Registers this dialog in the window menu. Called in the constructor.
+     * @since 10467
+     */
+    protected void registerInWindowMenu() {
         windowMenuItem = MainMenu.addWithCheckbox(Main.main.menu.windowMenu,
                 (JosmAction) getToggleAction(),
Index: trunk/src/org/openstreetmap/josm/gui/layer/ImageryLayer.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/layer/ImageryLayer.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/gui/layer/ImageryLayer.java	(revision 10467)
@@ -117,7 +117,13 @@
     }
 
+    /**
+     * Sets the displacement offset of this layer. The layer is automatically invalidated.
+     * @param dx The x offset
+     * @param dy The y offset
+     */
     public void setOffset(double dx, double dy) {
         this.dx = dx;
         this.dy = dy;
+        invalidate();
     }
 
Index: trunk/src/org/openstreetmap/josm/gui/layer/LayerManager.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/layer/LayerManager.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/gui/layer/LayerManager.java	(revision 10467)
@@ -7,4 +7,5 @@
 import java.util.concurrent.CopyOnWriteArrayList;
 
+import org.openstreetmap.josm.Main;
 import org.openstreetmap.josm.gui.util.GuiHelper;
 import org.openstreetmap.josm.tools.Utils;
@@ -175,5 +176,7 @@
         insertLayerAt(layer, position);
         fireLayerAdded(layer);
-        layer.hookUpMapView(); // needs to be after fireLayerAdded
+        if (Main.map != null) {
+            layer.hookUpMapView(); // needs to be after fireLayerAdded
+        }
     }
 
Index: trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java	(revision 10466)
+++ trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java	(revision 10467)
@@ -878,4 +878,5 @@
         setRequiresSaveToFile(false);
         setRequiresUploadToServer(isModified());
+        invalidate();
     }
 
@@ -886,4 +887,5 @@
         setRequiresSaveToFile(true);
         setRequiresUploadToServer(isModified());
+        invalidate();
     }
 
