Changeset 3454 in josm for trunk


Ignore:
Timestamp:
2010-08-21T17:43:56+02:00 (14 years ago)
Author:
jttt
Message:

Select last used mapmode after active layer change when old mode doesn't support new layer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/MapFrame.java

    r3450 r3454  
    1010import java.awt.event.MouseWheelListener;
    1111import java.util.ArrayList;
     12import java.util.HashMap;
    1213import java.util.List;
     14import java.util.Map;
    1315import java.util.concurrent.CopyOnWriteArrayList;
    1416
     
    3335import org.openstreetmap.josm.actions.mapmode.SelectAction;
    3436import org.openstreetmap.josm.actions.mapmode.ZoomAction;
     37import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
    3538import org.openstreetmap.josm.gui.dialogs.ChangesetDialog;
    3639import org.openstreetmap.josm.gui.dialogs.CommandStackDialog;
     
    4548import org.openstreetmap.josm.gui.dialogs.UserListDialog;
    4649import org.openstreetmap.josm.gui.dialogs.properties.PropertiesDialog;
     50import org.openstreetmap.josm.gui.layer.Layer;
    4751import org.openstreetmap.josm.tools.Destroyable;
    4852
     
    5357 * @author imi
    5458 */
    55 public class MapFrame extends JPanel implements Destroyable {
     59public class MapFrame extends JPanel implements Destroyable, LayerChangeListener {
    5660
    5761    /**
     
    5963     */
    6064    public MapMode mapMode;
     65
     66    private final List<MapMode> mapModes = new ArrayList<MapMode>();
    6167    /**
    6268     * The view control displayed.
     
    9096     * Default width of the toggle dialog area.
    9197     */
    92     public final int DEF_TOGGLE_DLG_WIDTH = 330;
     98    public static final int DEF_TOGGLE_DLG_WIDTH = 330;
     99
     100    private final Map<Layer, MapMode> lastMapMode = new HashMap<Layer, MapMode>();
    93101
    94102    public MapFrame(JPanel contentPane) {
     
    168176        // status line below the map
    169177        statusLine = new MapStatus(this);
     178        MapView.addLayerChangeListener(this);
    170179    }
    171180
     
    190199     */
    191200    public void destroy() {
     201        MapView.removeLayerChangeListener(this);
    192202        dialogsPanel.destroy();
    193203        for (int i = 0; i < toolBarActions.getComponentCount(); ++i)
     
    243253        toolBarActions.add(b);
    244254        toolGroup.add(b);
     255        if (b.getAction() instanceof MapMode) {
     256            mapModes.add((MapMode) b.getAction());
     257        } else
     258            throw new IllegalArgumentException("MapMode action must be subclass of MapMode");
    245259    }
    246260
     
    261275     * @param mapMode   The new mode to set.
    262276     */
    263     public void selectMapMode(MapMode newMapMode) {
     277    public boolean selectMapMode(MapMode newMapMode) {
    264278        MapMode oldMapMode = this.mapMode;
    265279        if (newMapMode == oldMapMode)
    266             return;
     280            return false;
    267281        if (oldMapMode != null) {
    268282            oldMapMode.exitMode();
     
    270284        this.mapMode = newMapMode;
    271285        newMapMode.enterMode();
     286        lastMapMode.put(mapView.getActiveLayer(), newMapMode);
    272287        fireMapModeChanged(oldMapMode, newMapMode);
     288        return true;
    273289    }
    274290
     
    361377        }
    362378    }
     379
     380    @Override
     381    public void activeLayerChange(Layer oldLayer, Layer newLayer) {
     382        boolean modeChanged = false;
     383        if (mapMode == null || !mapMode.layerIsSupported(newLayer)) {
     384            MapMode newMapMode = lastMapMode.get(newLayer);
     385            if (newMapMode != null) {
     386                modeChanged = selectMapMode(newMapMode);
     387            } // 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
     388        }
     389        if (!modeChanged && mapMode != null) {
     390            // Let mapmodes know about new active layer
     391            mapMode.exitMode();
     392            mapMode.enterMode();
     393        }
     394    }
     395
     396    @Override
     397    public void layerAdded(Layer newLayer) { }
     398
     399    @Override
     400    public void layerRemoved(Layer oldLayer) {
     401        lastMapMode.remove(oldLayer);
     402    }
    363403}
Note: See TracChangeset for help on using the changeset viewer.