Index: trunk/src/org/openstreetmap/josm/gui/MapFrame.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/MapFrame.java	(revision 3453)
+++ trunk/src/org/openstreetmap/josm/gui/MapFrame.java	(revision 3454)
@@ -10,5 +10,7 @@
 import java.awt.event.MouseWheelListener;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.CopyOnWriteArrayList;
 
@@ -33,4 +35,5 @@
 import org.openstreetmap.josm.actions.mapmode.SelectAction;
 import org.openstreetmap.josm.actions.mapmode.ZoomAction;
+import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
 import org.openstreetmap.josm.gui.dialogs.ChangesetDialog;
 import org.openstreetmap.josm.gui.dialogs.CommandStackDialog;
@@ -45,4 +48,5 @@
 import org.openstreetmap.josm.gui.dialogs.UserListDialog;
 import org.openstreetmap.josm.gui.dialogs.properties.PropertiesDialog;
+import org.openstreetmap.josm.gui.layer.Layer;
 import org.openstreetmap.josm.tools.Destroyable;
 
@@ -53,5 +57,5 @@
  * @author imi
  */
-public class MapFrame extends JPanel implements Destroyable {
+public class MapFrame extends JPanel implements Destroyable, LayerChangeListener {
 
     /**
@@ -59,4 +63,6 @@
      */
     public MapMode mapMode;
+
+    private final List<MapMode> mapModes = new ArrayList<MapMode>();
     /**
      * The view control displayed.
@@ -90,5 +96,7 @@
      * Default width of the toggle dialog area.
      */
-    public final int DEF_TOGGLE_DLG_WIDTH = 330;
+    public static final int DEF_TOGGLE_DLG_WIDTH = 330;
+
+    private final Map<Layer, MapMode> lastMapMode = new HashMap<Layer, MapMode>();
 
     public MapFrame(JPanel contentPane) {
@@ -168,4 +176,5 @@
         // status line below the map
         statusLine = new MapStatus(this);
+        MapView.addLayerChangeListener(this);
     }
 
@@ -190,4 +199,5 @@
      */
     public void destroy() {
+        MapView.removeLayerChangeListener(this);
         dialogsPanel.destroy();
         for (int i = 0; i < toolBarActions.getComponentCount(); ++i)
@@ -243,4 +253,8 @@
         toolBarActions.add(b);
         toolGroup.add(b);
+        if (b.getAction() instanceof MapMode) {
+            mapModes.add((MapMode) b.getAction());
+        } else
+            throw new IllegalArgumentException("MapMode action must be subclass of MapMode");
     }
 
@@ -261,8 +275,8 @@
      * @param mapMode   The new mode to set.
      */
-    public void selectMapMode(MapMode newMapMode) {
+    public boolean selectMapMode(MapMode newMapMode) {
         MapMode oldMapMode = this.mapMode;
         if (newMapMode == oldMapMode)
-            return;
+            return false;
         if (oldMapMode != null) {
             oldMapMode.exitMode();
@@ -270,5 +284,7 @@
         this.mapMode = newMapMode;
         newMapMode.enterMode();
+        lastMapMode.put(mapView.getActiveLayer(), newMapMode);
         fireMapModeChanged(oldMapMode, newMapMode);
+        return true;
     }
 
@@ -361,3 +377,27 @@
         }
     }
+
+    @Override
+    public void activeLayerChange(Layer oldLayer, Layer newLayer) {
+        boolean modeChanged = false;
+        if (mapMode == null || !mapMode.layerIsSupported(newLayer)) {
+            MapMode newMapMode = lastMapMode.get(newLayer);
+            if (newMapMode != null) {
+                modeChanged = selectMapMode(newMapMode);
+            } // it would be nice to select first supported mode when layer is first selected, but it don't work well with for example editgpx layer
+        }
+        if (!modeChanged && mapMode != null) {
+            // Let mapmodes know about new active layer
+            mapMode.exitMode();
+            mapMode.enterMode();
+        }
+    }
+
+    @Override
+    public void layerAdded(Layer newLayer) { }
+
+    @Override
+    public void layerRemoved(Layer oldLayer) {
+        lastMapMode.remove(oldLayer);
+    }
 }
