Changeset 10432 in josm


Ignore:
Timestamp:
2016-06-20T00:06:54+02:00 (3 years ago)
Author:
Don-vip
Message:

fix #13001 - Add MainPanel + some new methods (patch by michael2402, modified) - gsoc-core

Location:
trunk
Files:
1 added
14 edited

Legend:

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

    r10421 r10432  
    6262import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
    6363import org.openstreetmap.josm.actions.mapmode.DrawAction;
    64 import org.openstreetmap.josm.actions.mapmode.MapMode;
    6564import org.openstreetmap.josm.actions.search.SearchAction;
    6665import org.openstreetmap.josm.data.Bounds;
     
    8079import org.openstreetmap.josm.gui.GettingStarted;
    8180import org.openstreetmap.josm.gui.MainApplication.Option;
     81import org.openstreetmap.josm.gui.MainFrame;
    8282import org.openstreetmap.josm.gui.MainMenu;
     83import org.openstreetmap.josm.gui.MainPanel;
    8384import org.openstreetmap.josm.gui.MapFrame;
    8485import org.openstreetmap.josm.gui.MapFrameListener;
    85 import org.openstreetmap.josm.gui.MapView;
    86 import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
    8786import org.openstreetmap.josm.gui.help.HelpUtil;
    8887import org.openstreetmap.josm.gui.io.SaveLayersDialog;
    8988import org.openstreetmap.josm.gui.layer.AbstractModifiableLayer;
    9089import org.openstreetmap.josm.gui.layer.Layer;
    91 import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
    92 import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
    93 import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
    94 import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
    9590import org.openstreetmap.josm.gui.layer.MainLayerManager;
    9691import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     
    195190     * <p>
    196191     * There should be no need to access this to access any map data. Use {@link #layerManager} instead.
     192     *
     193     * @see MainPanel
    197194     */
    198195    public static MapFrame map;
     
    236233    /**
    237234     * The MOTD Layer.
    238      */
    239     public final GettingStarted gettingStarted = new GettingStarted();
    240 
    241     private static final Collection<MapFrameListener> mapFrameListeners = new ArrayList<>();
     235     * @deprecated Do not access this. It will be removed soon. You should not need to access the GettingStarted panel.
     236     */
     237    @Deprecated
     238    public final GettingStarted gettingStarted = mainPanel.getGettingStarted();
    242239
    243240    protected static final Map<String, Throwable> NETWORK_ERRORS = new HashMap<>();
     
    253250     */
    254251    public static int logLevel = 3;
     252
     253    /**
     254     * The real main panel. This field may be removed any time and made private to {@link MainFrame}
     255     * @see #panel
     256     */
     257    protected static final MainPanel mainPanel = new MainPanel(getLayerManager());
    255258
    256259    private static void rememberWarnErrorMsg(String msg) {
     
    548551    /**
    549552     * Set or clear (if passed <code>null</code>) the map.
     553     * <p>
     554     * To be removed any time
    550555     * @param map The map to set {@link Main#map} to. Can be null.
    551      */
     556     * @deprecated This is done automatically by {@link MainPanel}
     557     */
     558    @Deprecated
    552559    public final void setMapFrame(final MapFrame map) {
    553         MapFrame old = Main.map;
    554         panel.setVisible(false);
    555         panel.removeAll();
    556         if (map != null) {
    557             map.fillPanel(panel);
    558         } else {
    559             old.destroy();
    560             panel.add(gettingStarted, BorderLayout.CENTER);
    561         }
    562         panel.setVisible(true);
    563         redoUndoListener.commandChanged(0, 0);
    564 
    565         Main.map = map;
    566 
    567         // Notify map frame listeners, mostly plugins.
    568         if ((map == null) == (old == null)) {
    569             Main.warn("Replacing the map frame. This is not expected by some plugins and should not happen.");
    570         }
    571         for (MapFrameListener listener : mapFrameListeners) {
    572             MapView.fireDeprecatedListenerOnAdd = true;
    573             listener.mapFrameInitialized(old, map);
    574             MapView.fireDeprecatedListenerOnAdd = false;
    575         }
    576         if (map == null && currentProgressMonitor != null) {
    577             currentProgressMonitor.showForegroundDialog();
    578         }
     560        Main.warn("setMapFrame call was ignored.");
    579561    }
    580562
     
    582564     * Remove the specified layer from the map. If it is the last layer,
    583565     * remove the map as well.
     566     * <p>
     567     * To be removed end of 2016
    584568     * @param layer The layer to remove
    585      */
     569     * @deprecated You can remove the layer using {@link #getLayerManager()}
     570     */
     571    @Deprecated
    586572    public final synchronized void removeLayer(final Layer layer) {
    587573        if (map != null) {
    588574            getLayerManager().removeLayer(layer);
    589             if (isDisplayingMapView() && getLayerManager().getLayers().isEmpty()) {
    590                 setMapFrame(null);
    591             }
    592575        }
    593576    }
     
    613596    public Main() {
    614597        main = this;
    615         getLayerManager().addLayerChangeListener(new LayerChangeListener() {
     598        mainPanel.addAndFireMapFrameListener(new MapFrameListener() {
    616599            @Override
    617             public void layerAdded(LayerAddEvent e) {
    618                 Layer layer = e.getAddedLayer();
    619                 if (map == null) {
    620                     Main.main.createMapFrame(layer, null);
    621                     Main.map.setVisible(true);
    622                 }
    623                 ProjectionBounds viewProjectionBounds = layer.getViewProjectionBounds();
    624                 if (viewProjectionBounds != null) {
    625                     Main.map.mapView.scheduleZoomTo(new ViewportData(viewProjectionBounds));
    626                 }
    627             }
    628 
    629             @Override
    630             public void layerRemoving(LayerRemoveEvent e) {
    631                 // empty
    632             }
    633 
    634             @Override
    635             public void layerOrderChanged(LayerOrderChangeEvent e) {
    636                 //empty
    637             }
    638 
     600            public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
     601                redoUndoListener.commandChanged(0, 0);
     602            }
    639603        });
    640604    }
     
    836800     *
    837801     * If no map exists, create one.
     802     * <p>
     803     * To be removed end of 2016
    838804     *
    839805     * @param layer the layer
     
    841807     * @see #addLayer(Layer, ProjectionBounds)
    842808     * @see #addLayer(Layer, ViewportData)
    843      */
     809     * @deprecated You can add the layer to the layer manager: {@link #getLayerManager()}
     810     */
     811    @Deprecated
    844812    public final void addLayer(final Layer layer) {
    845         addLayer(layer, layer.getViewProjectionBounds());
     813        addLayer(layer, (ViewportData) null);
    846814    }
    847815
     
    869837    public final void addLayer(Layer layer, ViewportData viewport) {
    870838        getLayerManager().addLayer(layer);
    871         if (viewport != null) {
     839        if (viewport != null && Main.map.mapView != null) {
     840            // MapView may be null in headless mode here.
    872841            Main.map.mapView.scheduleZoomTo(viewport);
    873842        }
     
    876845    /**
    877846     * Creates the map frame. Call only in EDT Thread.
     847     * <p>
     848     * To be removed any time
    878849     * @param firstLayer The first layer that was added.
    879850     * @param viewportData The initial viewport. Can be <code>null</code> to be automatically computed.
    880      */
     851     * @deprecated Not supported. MainPanel does this automatically.
     852     */
     853    @Deprecated
    881854    public synchronized void createMapFrame(Layer firstLayer, ViewportData viewportData) {
    882855        GuiHelper.assertCallFromEdt();
    883         MapFrame mapFrame = new MapFrame(contentPanePrivate, viewportData);
    884         setMapFrame(mapFrame);
    885         if (firstLayer != null) {
    886             mapFrame.selectMapMode((MapMode) mapFrame.getDefaultButtonAction(), firstLayer);
    887         }
    888         mapFrame.initializeDialogsPane();
    889         // bootstrapping problem: make sure the layer list dialog is going to
    890         // listen to change events of the very first layer
    891         //
    892         if (firstLayer != null) {
    893             firstLayer.addPropertyChangeListener(LayerListDialog.getInstance().getModel());
    894         }
     856        Main.error("createMapFrame() not supported any more.");
    895857    }
    896858
    897859    /**
    898860     * Replies <code>true</code> if there is an edit layer
     861     * <p>
     862     * To be removed end of 2016
    899863     *
    900864     * @return <code>true</code> if there is an edit layer
    901      */
     865     * @deprecated You can get the edit layer using the layer manager and then check if it is not null: {@link #getLayerManager()}
     866     */
     867    @Deprecated
    902868    public boolean hasEditLayer() {
    903869        if (getEditLayer() == null) return false;
     
    907873    /**
    908874     * Replies the current edit layer
     875     * <p>
     876     * To be removed end of 2016
    909877     *
    910878     * @return the current edit layer. <code>null</code>, if no current edit layer exists
    911      */
     879     * @deprecated You can get the edit layer using the layer manager: {@link #getLayerManager()}
     880     */
     881    @Deprecated
    912882    public OsmDataLayer getEditLayer() {
    913         if (!isDisplayingMapView()) return null;
    914883        return getLayerManager().getEditLayer();
    915884    }
     
    917886    /**
    918887     * Replies the current data set.
     888     * <p>
     889     * To be removed end of 2016
    919890     *
    920891     * @return the current data set. <code>null</code>, if no current data set exists
    921      */
     892     * @deprecated You can get the data set using the layer manager: {@link #getLayerManager()}
     893     */
     894    @Deprecated
    922895    public DataSet getCurrentDataSet() {
    923         if (!hasEditLayer()) return null;
    924         return getEditLayer().data;
     896        return getLayerManager().getEditDataSet();
    925897    }
    926898
     
    946918    /**
    947919     * Returns the currently active  layer
     920     * <p>
     921     * To be removed end of 2016
    948922     *
    949923     * @return the currently active layer. <code>null</code>, if currently no active layer exists
    950      */
     924     * @deprecated You can get the layer using the layer manager: {@link #getLayerManager()}
     925     */
     926    @Deprecated
    951927    public Layer getActiveLayer() {
    952         if (!isDisplayingMapView()) return null;
    953928        return getLayerManager().getActiveLayer();
    954929    }
     
    1015990     * Global panel.
    1016991     */
    1017     public static final JPanel panel = new JPanel(new BorderLayout());
     992    public static final JPanel panel = mainPanel;
    1018993
    1019994    private final CommandQueueListener redoUndoListener = new CommandQueueListener() {
     
    12011176        }
    12021177        // Remove all layers because somebody may rely on layerRemoved events (like AutosaveTask)
    1203         if (Main.isDisplayingMapView()) {
    1204             Collection<Layer> layers = new ArrayList<>(getLayerManager().getLayers());
    1205             for (Layer l: layers) {
    1206                 Main.main.removeLayer(l);
    1207             }
    1208         }
     1178        getLayerManager().resetState();
    12091179        try {
    12101180            pref.saveDefaults();
     
    16921662     */
    16931663    public static boolean addMapFrameListener(MapFrameListener listener, boolean fireWhenMapViewPresent) {
    1694         boolean changed = listener != null && mapFrameListeners.add(listener);
    1695         if (fireWhenMapViewPresent && changed && map != null) {
    1696             listener.mapFrameInitialized(null, map);
    1697         }
    1698         return changed;
     1664        if (fireWhenMapViewPresent) {
     1665            return mainPanel.addAndFireMapFrameListener(listener);
     1666        } else {
     1667            return mainPanel.addMapFrameListener(listener);
     1668        }
    16991669    }
    17001670
     
    17061676     */
    17071677    public static boolean addMapFrameListener(MapFrameListener listener) {
    1708         return addMapFrameListener(listener, false);
     1678        return mainPanel.addMapFrameListener(listener);
    17091679    }
    17101680
     
    17161686     */
    17171687    public static boolean removeMapFrameListener(MapFrameListener listener) {
    1718         return listener != null && mapFrameListeners.remove(listener);
     1688        return mainPanel.removeMapFrameListener(listener);
    17191689    }
    17201690
  • trunk/src/org/openstreetmap/josm/actions/SessionLoadAction.java

    r10318 r10432  
    125125        private void addLayers() {
    126126            if (layers != null && !layers.isEmpty()) {
    127                 Layer firstLayer = layers.get(0);
    128127                boolean noMap = Main.map == null;
    129                 if (noMap) {
    130                     Main.main.createMapFrame(firstLayer, viewport);
    131                 }
    132128                for (Layer l : layers) {
    133129                    if (canceled)
    134130                        return;
    135                     Main.main.addLayer(l, (ViewportData) null);
     131                    Main.getLayerManager().addLayer(l);
    136132                }
    137133                if (active != null) {
     
    139135                }
    140136                if (noMap) {
    141                     Main.map.setVisible(true);
     137                    Main.map.mapView.scheduleZoomTo(viewport);
    142138                }
    143139            }
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTask.java

    r10318 r10432  
    1313import org.openstreetmap.josm.data.Bounds;
    1414import org.openstreetmap.josm.data.Bounds.ParseMethod;
     15import org.openstreetmap.josm.data.ViewportData;
    1516import org.openstreetmap.josm.data.gpx.GpxData;
    1617import org.openstreetmap.josm.gui.PleaseWaitRunnable;
     
    141142            if (layer == null) return null;
    142143            if (newLayer || mergeLayer == null) {
    143                 if (Main.main != null) {
    144                     Main.main.addLayer(layer);
    145                 }
     144                Main.getLayerManager().addLayer(layer);
    146145                return layer;
    147146            } else {
    148147                mergeLayer.mergeFrom(layer);
     148                mergeLayer.invalidate();
    149149                if (Main.map != null) {
    150                     Main.map.repaint();
     150                    Main.map.mapView.scheduleZoomTo(new ViewportData(layer.getViewProjectionBounds()));
    151151                }
    152152                return mergeLayer;
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r10378 r10432  
    317317        initApplicationPreferences();
    318318
     319        // Can only be called after preferences are initialized.
     320        // We can move this to MainPanel constructor as soon as noone depends on Main#panel any more.
     321        GuiHelper.runInEDTAndWait(new Runnable() {
     322            @Override
     323            public void run() {
     324                mainPanel.updateContent();
     325            }
     326        });
     327
    319328        Policy.setPolicy(new Policy() {
    320329            // Permissions for plug-ins loaded when josm is started via webstart
     
    399408                args.containsKey(Option.GEOMETRY) ? args.get(Option.GEOMETRY).iterator().next() : null,
    400409                !args.containsKey(Option.NO_MAXIMIZE) && Main.pref.getBoolean("gui.maximized", false));
    401         final MainFrame mainFrame = new MainFrame(contentPanePrivate, geometry);
     410        final MainFrame mainFrame = new MainFrame(contentPanePrivate, mainPanel, geometry);
    402411        Main.parent = mainFrame;
    403412
  • trunk/src/org/openstreetmap/josm/gui/MainFrame.java

    r10345 r10432  
    4040 */
    4141public class MainFrame extends JFrame {
    42     protected transient WindowGeometry geometry;
    43     protected int windowState = JFrame.NORMAL;
    44     private MainMenu menu;
    45 
    4642    private final transient LayerStateChangeListener updateTitleOnLayerStateChange = new LayerStateChangeListener() {
    4743        @Override
     
    5248
    5349    private final transient PropertyChangeListener updateTitleOnSaveChange = new PropertyChangeListener() {
    54 
    5550        @Override
    5651        public void propertyChange(PropertyChangeEvent evt) {
     
    6358    };
    6459
     60    protected transient WindowGeometry geometry;
     61    protected int windowState = JFrame.NORMAL;
     62    private MainMenu menu;
     63
    6564    /**
    6665     * Create a new main window.
    6766     */
    6867    public MainFrame() {
    69         this(new JPanel(), new WindowGeometry(new Rectangle(10, 10, 500, 500)));
    70     }
    71 
    72     /**
    73      * Create a new main window.
     68        this(new JPanel(), new MainPanel(Main.getLayerManager()), new WindowGeometry(new Rectangle(10, 10, 500, 500)));
     69    }
     70
     71    /**
     72     * Create a new main window. The parameters will be removed in the future.
    7473     * @param contentPanePrivate The content
     74     * @param mainPanel The main panel.
    7575     * @param geometry The inital geometry to use.
    7676     */
    77     public MainFrame(Container contentPanePrivate, WindowGeometry geometry) {
     77    public MainFrame(Container contentPanePrivate, MainPanel mainPanel, WindowGeometry geometry) {
    7878        super();
    7979        this.geometry = geometry;
     
    120120
    121121        getContentPane().add(Main.panel, BorderLayout.CENTER);
    122         Main.panel.add(Main.main.gettingStarted, BorderLayout.CENTER);
    123122        menu.initialize();
    124123    }
  • trunk/src/org/openstreetmap/josm/gui/MapFrame.java

    r10395 r10432  
    188188    /**
    189189     * Constructs a new {@code MapFrame}.
    190      * @param contentPane The content pane used to register shortcuts in its
    191      * {@link javax.swing.InputMap} and {@link javax.swing.ActionMap}
     190     * @param contentPane Ignored. Main content pane is used.
    192191     * @param viewportData the initial viewport of the map. Can be null, then
    193192     * the viewport is derived from the layer data.
  • trunk/src/org/openstreetmap/josm/gui/MapMover.java

    r10217 r10432  
    1111import java.awt.event.MouseEvent;
    1212import java.awt.event.MouseWheelEvent;
     13import java.util.ArrayList;
    1314
    1415import javax.swing.AbstractAction;
    15 import javax.swing.ActionMap;
    16 import javax.swing.InputMap;
    17 import javax.swing.JComponent;
    1816import javax.swing.JPanel;
    19 import javax.swing.KeyStroke;
    2017
    2118import org.openstreetmap.gui.jmapviewer.JMapViewer;
     
    2724import org.openstreetmap.josm.data.preferences.BooleanProperty;
    2825import org.openstreetmap.josm.tools.Destroyable;
     26import org.openstreetmap.josm.tools.Pair;
    2927import org.openstreetmap.josm.tools.Shortcut;
    3028
     
    6462
    6563        ZoomerAction(String action) {
     64            this(action, "MapMover.Zoomer." + action);
     65        }
     66
     67        /**
     68         * Constructs a new {@code ZoomerAction}.
     69         * @param action action
     70         * @param name name
     71         * @since 10432
     72         */
     73        public ZoomerAction(String action, String name) {
    6674            this.action = action;
     75            putValue(NAME, name);
    6776        }
    6877
     
    107116     */
    108117    private final NavigatableComponent nc;
    109     private final JPanel contentPane;
    110118
    111119    private boolean movementInPlace;
     120
     121    private final ArrayList<Pair<ZoomerAction, Shortcut>> registeredShortcuts = new ArrayList<>();
    112122
    113123    /**
    114124     * Constructs a new {@code MapMover}.
    115125     * @param navComp the navigatable component
    116      * @param contentPane the content pane
     126     * @param contentPane Ignored. The main action map is used.
    117127     */
    118128    public MapMover(NavigatableComponent navComp, JPanel contentPane) {
    119129        this.nc = navComp;
    120         this.contentPane = contentPane;
    121130        nc.addMouseListener(this);
    122131        nc.addMouseMotionListener(this);
    123132        nc.addMouseWheelListener(this);
    124133
    125         if (contentPane != null) {
    126             // CHECKSTYLE.OFF: LineLength
    127             contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    128                 Shortcut.registerShortcut("system:movefocusright", tr("Map: {0}", tr("Move right")), KeyEvent.VK_RIGHT, Shortcut.CTRL).getKeyStroke(),
    129                 "MapMover.Zoomer.right");
    130             contentPane.getActionMap().put("MapMover.Zoomer.right", new ZoomerAction("right"));
    131 
    132             contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    133                 Shortcut.registerShortcut("system:movefocusleft", tr("Map: {0}", tr("Move left")), KeyEvent.VK_LEFT, Shortcut.CTRL).getKeyStroke(),
    134                 "MapMover.Zoomer.left");
    135             contentPane.getActionMap().put("MapMover.Zoomer.left", new ZoomerAction("left"));
    136 
    137             contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    138                 Shortcut.registerShortcut("system:movefocusup", tr("Map: {0}", tr("Move up")), KeyEvent.VK_UP, Shortcut.CTRL).getKeyStroke(),
    139                 "MapMover.Zoomer.up");
    140             contentPane.getActionMap().put("MapMover.Zoomer.up", new ZoomerAction("up"));
    141 
    142             contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    143                 Shortcut.registerShortcut("system:movefocusdown", tr("Map: {0}", tr("Move down")), KeyEvent.VK_DOWN, Shortcut.CTRL).getKeyStroke(),
    144                 "MapMover.Zoomer.down");
    145             contentPane.getActionMap().put("MapMover.Zoomer.down", new ZoomerAction("down"));
    146             // CHECKSTYLE.ON: LineLength
    147 
    148             // see #10592 - Disable these alternate shortcuts on OS X because of conflict with system shortcut
    149             if (!Main.isPlatformOsx()) {
    150                 contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    151                     Shortcut.registerShortcut("view:zoominalternate",
    152                             tr("Map: {0}", tr("Zoom in")), KeyEvent.VK_COMMA, Shortcut.CTRL).getKeyStroke(),
    153                     "MapMover.Zoomer.in");
    154                 contentPane.getActionMap().put("MapMover.Zoomer.in", new ZoomerAction(","));
    155 
    156                 contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    157                     Shortcut.registerShortcut("view:zoomoutalternate",
    158                             tr("Map: {0}", tr("Zoom out")), KeyEvent.VK_PERIOD, Shortcut.CTRL).getKeyStroke(),
    159                     "MapMover.Zoomer.out");
    160                 contentPane.getActionMap().put("MapMover.Zoomer.out", new ZoomerAction("."));
    161             }
    162         }
     134        registerActionShortcut(new ZoomerAction("right"),
     135                Shortcut.registerShortcut("system:movefocusright", tr("Map: {0}", tr("Move right")), KeyEvent.VK_RIGHT, Shortcut.CTRL));
     136
     137        registerActionShortcut(new ZoomerAction("left"),
     138                Shortcut.registerShortcut("system:movefocusleft", tr("Map: {0}", tr("Move left")), KeyEvent.VK_LEFT, Shortcut.CTRL));
     139
     140        registerActionShortcut(new ZoomerAction("up"),
     141                Shortcut.registerShortcut("system:movefocusup", tr("Map: {0}", tr("Move up")), KeyEvent.VK_UP, Shortcut.CTRL));
     142        registerActionShortcut(new ZoomerAction("down"),
     143                Shortcut.registerShortcut("system:movefocusdown", tr("Map: {0}", tr("Move down")), KeyEvent.VK_DOWN, Shortcut.CTRL));
     144
     145        // see #10592 - Disable these alternate shortcuts on OS X because of conflict with system shortcut
     146        if (!Main.isPlatformOsx()) {
     147            registerActionShortcut(new ZoomerAction(",", "MapMover.Zoomer.in"),
     148                    Shortcut.registerShortcut("view:zoominalternate", tr("Map: {0}", tr("Zoom in")), KeyEvent.VK_COMMA, Shortcut.CTRL));
     149
     150            registerActionShortcut(new ZoomerAction(".", "MapMover.Zoomer.out"),
     151                    Shortcut.registerShortcut("view:zoomoutalternate", tr("Map: {0}", tr("Zoom out")), KeyEvent.VK_PERIOD, Shortcut.CTRL));
     152        }
     153    }
     154
     155    private void registerActionShortcut(ZoomerAction action, Shortcut shortcut) {
     156        Main.registerActionShortcut(action, shortcut);
     157        registeredShortcuts.add(new Pair<>(action, shortcut));
    163158    }
    164159
     
    270265    @Override
    271266    public void destroy() {
    272         if (this.contentPane != null) {
    273             InputMap inputMap = contentPane.getInputMap();
    274             KeyStroke[] inputKeys = inputMap.keys();
    275             if (inputKeys != null) {
    276                 for (KeyStroke key : inputKeys) {
    277                     Object binding = inputMap.get(key);
    278                     if (binding instanceof String && ((String) binding).startsWith("MapMover.")) {
    279                         inputMap.remove(key);
    280                     }
    281                 }
    282             }
    283             ActionMap actionMap = contentPane.getActionMap();
    284             Object[] actionsKeys = actionMap.keys();
    285             if (actionsKeys != null) {
    286                 for (Object key : actionsKeys) {
    287                     if (key instanceof String && ((String) key).startsWith("MapMover.")) {
    288                         actionMap.remove(key);
    289                     }
    290                 }
    291             }
     267        for (Pair<ZoomerAction, Shortcut> shortcut : registeredShortcuts) {
     268            Main.unregisterActionShortcut(shortcut.a, shortcut.b);
    292269        }
    293270    }
  • trunk/src/org/openstreetmap/josm/gui/MapView.java

    r10412 r10432  
    3232
    3333import javax.swing.AbstractButton;
    34 import javax.swing.ActionMap;
    35 import javax.swing.InputMap;
    3634import javax.swing.JComponent;
    3735import javax.swing.JPanel;
     
    4240import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
    4341import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
     42import org.openstreetmap.josm.data.ProjectionBounds;
    4443import org.openstreetmap.josm.data.SelectionChangedListener;
    4544import org.openstreetmap.josm.data.ViewportData;
     
    495494     * Constructs a new {@code MapView}.
    496495     * @param layerManager The layers to display.
    497      * @param contentPane The content pane used to register shortcuts in its
    498      * {@link InputMap} and {@link ActionMap}
     496     * @param contentPane Ignored. Main content pane is used.
    499497     * @param viewportData the initial viewport of the map. Can be null, then
    500498     * the viewport is derived from the layer data.
     
    598596        if (layer instanceof MarkerLayer && playHeadMarker == null) {
    599597            playHeadMarker = PlayHeadMarker.create();
     598        }
     599
     600        ProjectionBounds viewProjectionBounds = layer.getViewProjectionBounds();
     601        if (viewProjectionBounds != null) {
     602            scheduleZoomTo(new ViewportData(viewProjectionBounds));
    600603        }
    601604
  • trunk/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java

    r10345 r10432  
    331331    public void showNotify() {
    332332        layerManager.addActiveLayerChangeListener(activateLayerAction);
    333         layerManager.addLayerChangeListener(model);
     333        layerManager.addLayerChangeListener(model, true);
    334334        layerManager.addAndFireActiveLayerChangeListener(model);
    335335        model.populate();
     
    338338    @Override
    339339    public void hideNotify() {
    340         layerManager.removeLayerChangeListener(model);
     340        layerManager.removeLayerChangeListener(model, true);
    341341        layerManager.removeActiveLayerChangeListener(model);
    342342        layerManager.removeActiveLayerChangeListener(activateLayerAction);
  • trunk/src/org/openstreetmap/josm/gui/layer/LayerManager.java

    r10391 r10432  
    9090    public static class LayerRemoveEvent extends LayerManagerEvent {
    9191        private final Layer removedLayer;
     92        private final boolean lastLayer;
    9293
    9394        LayerRemoveEvent(LayerManager source, Layer removedLayer) {
    9495            super(source);
    9596            this.removedLayer = removedLayer;
     97            this.lastLayer = source.getLayers().size() == 1;
    9698        }
    9799
     
    102104        public Layer getRemovedLayer() {
    103105            return removedLayer;
     106        }
     107
     108        /**
     109         * Check if the layer that was removed is the last layer in the list.
     110         * @return <code>true</code> if this was the last layer.
     111         * @since 10432
     112         */
     113        public boolean isLastLayer() {
     114            return lastLayer;
    104115        }
    105116    }
     
    347358        }
    348359    }
     360
     361    /**
     362     * Reset all layer manager state. This includes removing all layers and then unregistering all listeners
     363     * @since 10432
     364     */
     365    public void resetState() {
     366        // some layer remove listeners remove other layers.
     367        while (!getLayers().isEmpty()) {
     368            removeLayer(getLayers().get(0));
     369        }
     370
     371        layerChangeListeners.clear();
     372    }
    349373}
  • trunk/src/org/openstreetmap/josm/gui/layer/MainLayerManager.java

    r10332 r10432  
    313313        return ret;
    314314    }
     315
     316    @Override
     317    public void resetState() {
     318        // active and edit layer are unset automatically
     319        super.resetState();
     320
     321        activeLayerChangeListeners.clear();
     322    }
    315323}
  • trunk/src/org/openstreetmap/josm/io/NoteImporter.java

    r10364 r10432  
    1414import org.openstreetmap.josm.gui.layer.NoteLayer;
    1515import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    16 import org.openstreetmap.josm.gui.util.GuiHelper;
    1716import org.xml.sax.SAXException;
    1817
     
    3837        try (InputStream is = Compression.getUncompressedFileInputStream(file)) {
    3938            final NoteLayer layer = loadLayer(is, file, file.getName(), progressMonitor);
    40             if (Main.map == null || !Main.getLayerManager().containsLayer(layer)) {
    41                 GuiHelper.runInEDT(new Runnable() {
    42                     @Override
    43                     public void run() {
    44                         Main.main.addLayer(layer);
    45                     }
    46                 });
     39            if (!Main.getLayerManager().containsLayer(layer)) {
     40                Main.getLayerManager().addLayer(layer);
    4741            }
    4842        } catch (SAXException e) {
  • trunk/test/unit/org/openstreetmap/josm/io/NoteImporterTest.java

    r10396 r10432  
    77import java.io.File;
    88
     9import org.junit.Rule;
    910import org.junit.Test;
    1011import org.openstreetmap.josm.Main;
    1112import org.openstreetmap.josm.TestUtils;
    12 import org.openstreetmap.josm.gui.layer.Layer;
     13import org.openstreetmap.josm.testutils.JOSMTestRules;
    1314
    1415/**
     
    1819
    1920    /**
     21     * Use the test rules to remove any layers and reset state.
     22     */
     23    @Rule
     24    public final JOSMTestRules rules = new JOSMTestRules();
     25
     26    /**
    2027     * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/12531">Bug #12531</a>.
    2128     */
    2229    @Test
    2330    public void testTicket12531() {
    24         if (Main.map != null) {
    25             for (Layer l: Main.getLayerManager().getLayers()) {
    26                 Main.getLayerManager().removeLayer(l);
    27             }
    28             Main.main.setMapFrame(null);
    29         }
     31        Main.getLayerManager().resetState();
    3032        assertNull(Main.map);
    3133        assertTrue(new NoteImporter().importDataHandleExceptions(
  • trunk/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java

    r10402 r10432  
    1313import org.openstreetmap.josm.Main;
    1414import org.openstreetmap.josm.data.projection.Projections;
    15 import org.openstreetmap.josm.gui.layer.MainLayerManager;
    1615import org.openstreetmap.josm.gui.util.GuiHelper;
    1716import org.openstreetmap.josm.io.OsmApi;
     
    223222        });
    224223        // Remove all layers
    225         MainLayerManager lm = Main.getLayerManager();
    226         while (!lm.getLayers().isEmpty()) {
    227             lm.removeLayer(lm.getLayers().get(0));
    228         }
     224        Main.getLayerManager().resetState();
    229225
    230226        // TODO: Remove global listeners and other global state.
Note: See TracChangeset for help on using the changeset viewer.