Ticket #17196: 17196.3.patch

File 17196.3.patch, 5.4 KB (added by GerdP, 5 years ago)

updated patch, shows empty command stack when active layer is not an edit layer

  • src/org/openstreetmap/josm/data/UndoRedoHandler.java

     
    33
    44import java.util.Collections;
    55import java.util.EventObject;
     6import java.util.HashMap;
    67import java.util.LinkedList;
    78import java.util.List;
     9import java.util.Map;
    810import java.util.Objects;
    911
    1012import org.openstreetmap.josm.command.Command;
    1113import org.openstreetmap.josm.data.osm.DataSet;
    1214import org.openstreetmap.josm.data.osm.OsmDataManager;
     15import org.openstreetmap.josm.gui.MainApplication;
     16import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    1317import org.openstreetmap.josm.gui.util.GuiHelper;
    1418import org.openstreetmap.josm.spi.preferences.Config;
    1519import org.openstreetmap.josm.tools.CheckParameterUtil;
     
    4044    private final LinkedList<CommandQueuePreciseListener> preciseListenerCommands = new LinkedList<>();
    4145
    4246    private static class InstanceHolder {
    43         static final UndoRedoHandler INSTANCE = new UndoRedoHandler();
     47        static final UndoRedoHandler NO_DATA_SET_INSTANCE = new UndoRedoHandler();
     48        static final Map<DataSet, UndoRedoHandler> map = new HashMap<>();
     49
    4450    }
    4551
    4652    /**
     
    4955     * @since 14134
    5056     */
    5157    public static UndoRedoHandler getInstance() {
    52         return InstanceHolder.INSTANCE;
     58        OsmDataLayer editLayer = MainApplication.getLayerManager().getEditLayer();
     59        if (editLayer != null) {
     60            if (editLayer == MainApplication.getLayerManager().getActiveLayer()) {
     61                return InstanceHolder.map.computeIfAbsent(editLayer.data, k -> new UndoRedoHandler());
     62            }
     63        }
     64        return InstanceHolder.NO_DATA_SET_INSTANCE;
    5365    }
    5466
    5567    /**
     
    426438        for (final CommandQueueListener l : listenerCommands) {
    427439            l.commandChanged(commands.size(), redoCommands.size());
    428440        }
     441        if (getInstance() != InstanceHolder.NO_DATA_SET_INSTANCE) {
     442            for (final CommandQueueListener l : InstanceHolder.NO_DATA_SET_INSTANCE.listenerCommands) {
     443                l.commandChanged(commands.size(), redoCommands.size());
     444            }
     445
     446        }
    429447    }
    430448
    431449    private void fireEvent(CommandQueueEvent e) {
     
    450468    public synchronized void clean(DataSet dataSet) {
    451469        if (dataSet == null)
    452470            return;
    453         boolean changed = false;
    454         changed |= commands.removeIf(c -> c.getAffectedDataSet() == dataSet);
    455         changed |= redoCommands.removeIf(c -> c.getAffectedDataSet() == dataSet);
    456         if (changed) {
     471        if (InstanceHolder.map.remove(dataSet) != null) {
     472            redoCommands.clear();
     473            commands.clear();
    457474            fireEvent(new CommandQueueCleanedEvent(this, dataSet));
    458475            fireCommandsChanged();
    459476        }
  • src/org/openstreetmap/josm/gui/dialogs/CommandStackDialog.java

     
    5050import org.openstreetmap.josm.data.osm.OsmPrimitive;
    5151import org.openstreetmap.josm.gui.MainApplication;
    5252import org.openstreetmap.josm.gui.SideButton;
     53import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
     54import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
    5355import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    5456import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
    5557import org.openstreetmap.josm.tools.GBC;
     
    6264 * Dialog displaying list of all executed commands (undo/redo buffer).
    6365 * @since 94
    6466 */
    65 public class CommandStackDialog extends ToggleDialog implements CommandQueuePreciseListener {
     67public class CommandStackDialog extends ToggleDialog implements CommandQueuePreciseListener, ActiveLayerChangeListener {
    6668
    6769    private final DefaultTreeModel undoTreeModel = new DefaultTreeModel(new DefaultMutableTreeNode());
    6870    private final DefaultTreeModel redoTreeModel = new DefaultTreeModel(new DefaultMutableTreeNode());
     
    144146
    145147        InputMapUtils.addEnterAction(undoTree, selectAndZoomAction);
    146148        InputMapUtils.addEnterAction(redoTree, selectAndZoomAction);
     149        MainApplication.getLayerManager().addActiveLayerChangeListener(this);
    147150    }
    148151
    149152    private static class CommandCellRenderer extends DefaultTreeCellRenderer {
     
    552555            add(selectAndZoomAction);
    553556        }
    554557    }
     558
     559    @Override
     560    public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
     561        hideNotify();
     562        showNotify();
     563    }
    555564}
  • test/unit/org/openstreetmap/josm/gui/dialogs/CommandStackDialogTest.java

     
    2727     */
    2828    @Rule
    2929    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    30     public JOSMTestRules test = new JOSMTestRules().main().projection();
     30    public JOSMTestRules test = new JOSMTestRules().main().projection().preferences();
    3131
    3232    /**
    3333     * Unit test of {@link CommandStackDialog} class - empty case.