Changeset 10452 in josm


Ignore:
Timestamp:
2016-06-21T23:41:42+02:00 (8 years ago)
Author:
Don-vip
Message:

fix #13019 - Make commands trigger an implicit layer redraw (patch by michael2402) - gsoc-core

Location:
trunk
Files:
5 edited

Legend:

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

    r10413 r10452  
    148148     * Executes the command on the dataset. This implementation will remember all
    149149     * primitives returned by fillModifiedData for restoring them on undo.
     150     * <p>
     151     * The layer should be invalidated after execution so that it can be re-painted.
    150152     * @return true
     153     * @see #invalidateAffectedLayers()
    151154     */
    152155    public boolean executeCommand() {
     
    300303                Objects.equals(layer, command.layer);
    301304    }
     305
     306    /**
     307     * Invalidate all layers that were affected by this command.
     308     * @see Layer#invalidate()
     309     */
     310    public void invalidateAffectedLayers() {
     311        OsmDataLayer layer = getLayer();
     312        if (layer != null) {
     313            layer.invalidate();
     314        }
     315    }
    302316}
  • trunk/src/org/openstreetmap/josm/command/SequenceCommand.java

    r9377 r10452  
    126126
    127127    @Override
     128    public void invalidateAffectedLayers() {
     129        super.invalidateAffectedLayers();
     130        for (Command c : sequence) {
     131            c.invalidateAffectedLayers();
     132        }
     133    }
     134
     135    @Override
    128136    public int hashCode() {
    129137        return Objects.hash(super.hashCode(), Arrays.hashCode(sequence), sequenceComplete, name, continueOnError);
  • trunk/src/org/openstreetmap/josm/data/UndoRedoHandler.java

    r10446 r10452  
    1515import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
    1616import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
     17import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    1718import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
    1819import org.openstreetmap.josm.tools.CheckParameterUtil;
    1920
     21/**
     22 * This is the global undo/redo handler for all {@link OsmDataLayer}s.
     23 * <p>
     24 * If you want to change a data layer, you can use {@link #add(Command)} to execute a command on it and make that command undoable.
     25 */
    2026public class UndoRedoHandler implements LayerChangeListener {
    2127
     
    4551        CheckParameterUtil.ensureParameterNotNull(c, "c");
    4652        c.executeCommand();
     53        c.invalidateAffectedLayers();
    4754        commands.add(c);
    4855        // Limit the number of commands in the undo list.
     
    5562    }
    5663
     64    /**
     65     * Fires a commands change event after adding a command.
     66     */
    5767    public void afterAdd() {
    5868        fireCommandsChanged();
    59 
    60         // the command may have changed the selection so tell the listeners about the current situation
    61         DataSet ds = Main.getLayerManager().getEditDataSet();
    62         if (ds != null) {
    63             ds.fireSelectionChanged();
    64         }
    6569    }
    6670
     
    7074     */
    7175    public synchronized void add(final Command c) {
     76        DataSet ds = Main.getLayerManager().getEditDataSet();
     77        Collection<? extends OsmPrimitive> oldSelection = ds.getSelected();
    7278        addNoRedraw(c);
    7379        afterAdd();
     80
     81        // the command may have changed the selection so tell the listeners about the current situation
     82        fireIfSelectionChanged(ds, oldSelection);
    7483    }
    7584
     
    8897        if (commands.isEmpty())
    8998            return;
    90         Collection<? extends OsmPrimitive> oldSelection = Main.getLayerManager().getEditDataSet().getSelected();
    91         Main.getLayerManager().getEditDataSet().beginUpdate();
     99        DataSet ds = Main.getLayerManager().getEditDataSet();
     100        Collection<? extends OsmPrimitive> oldSelection = ds.getSelected();
     101        ds.beginUpdate();
    92102        try {
    93103            for (int i = 1; i <= num; ++i) {
    94104                final Command c = commands.removeLast();
    95105                c.undoCommand();
     106                c.invalidateAffectedLayers();
    96107                redoCommands.addFirst(c);
    97108                if (commands.isEmpty()) {
     
    100111            }
    101112        } finally {
    102             Main.getLayerManager().getEditDataSet().endUpdate();
    103         }
    104         fireCommandsChanged();
    105         Collection<? extends OsmPrimitive> newSelection = Main.getLayerManager().getEditDataSet().getSelected();
    106         if (!oldSelection.equals(newSelection)) {
    107             Main.getLayerManager().getEditDataSet().fireSelectionChanged();
    108         }
     113            ds.endUpdate();
     114        }
     115        fireCommandsChanged();
     116        fireIfSelectionChanged(ds, oldSelection);
    109117    }
    110118
     
    123131        if (redoCommands.isEmpty())
    124132            return;
    125         Collection<? extends OsmPrimitive> oldSelection = Main.getLayerManager().getEditDataSet().getSelected();
     133        DataSet ds = Main.getLayerManager().getEditDataSet();
     134        Collection<? extends OsmPrimitive> oldSelection = ds.getSelected();
    126135        for (int i = 0; i < num; ++i) {
    127136            final Command c = redoCommands.removeFirst();
    128137            c.executeCommand();
     138            c.invalidateAffectedLayers();
    129139            commands.add(c);
    130140            if (redoCommands.isEmpty()) {
     
    133143        }
    134144        fireCommandsChanged();
    135         Collection<? extends OsmPrimitive> newSelection = Main.getLayerManager().getEditDataSet().getSelected();
     145        fireIfSelectionChanged(ds, oldSelection);
     146    }
     147
     148    private static void fireIfSelectionChanged(DataSet ds, Collection<? extends OsmPrimitive> oldSelection) {
     149        Collection<? extends OsmPrimitive> newSelection = ds.getSelected();
    136150        if (!oldSelection.equals(newSelection)) {
    137             Main.getLayerManager().getEditDataSet().fireSelectionChanged();
    138         }
    139     }
    140 
    141     public void fireCommandsChanged() {
     151            ds.fireSelectionChanged();
     152        }
     153    }
     154
     155    /**
     156     * Fires a command change to all listeners.
     157     */
     158    private void fireCommandsChanged() {
    142159        for (final CommandQueueListener l : listenerCommands) {
    143160            l.commandChanged(commands.size(), redoCommands.size());
     
    145162    }
    146163
     164    /**
     165     * Resets the undo/redo list.
     166     */
    147167    public void clean() {
    148168        redoCommands.clear();
     
    151171    }
    152172
     173    /**
     174     * Resets all commands that affect the given layer.
     175     * @param layer The layer that was affected.
     176     */
    153177    public void clean(Layer layer) {
    154178        if (layer == null)
  • trunk/src/org/openstreetmap/josm/gui/dialogs/ValidatorDialog.java

    r10446 r10452  
    608608
    609609        @Override
    610         protected void realRun() throws SAXException, IOException,
    611         OsmTransferException {
     610        protected void realRun() throws SAXException, IOException, OsmTransferException {
    612611            ProgressMonitor monitor = getProgressMonitor();
    613612            try {
    614613                monitor.setTicksCount(testErrors.size());
     614                final DataSet ds = Main.getLayerManager().getEditDataSet();
    615615                int i = 0;
    616616                SwingUtilities.invokeAndWait(new Runnable() {
    617617                    @Override
    618618                    public void run() {
    619                         Main.getLayerManager().getEditDataSet().beginUpdate();
     619                        ds.beginUpdate();
    620620                    }
    621621                });
     
    633633                        @Override
    634634                        public void run() {
    635                             Main.getLayerManager().getEditDataSet().endUpdate();
     635                            ds.endUpdate();
    636636                        }
    637637                    });
     
    644644                        Main.map.repaint();
    645645                        tree.resetErrors();
    646                         Main.getLayerManager().getEditDataSet().fireSelectionChanged();
     646                        ds.fireSelectionChanged();
    647647                    }
    648648                });
  • trunk/test/unit/org/openstreetmap/josm/actions/mapmode/DrawActionTest.java

    r10436 r10452  
    5353        DataSet dataSet = new DataSet();
    5454        OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
     55        Main.getLayerManager().addLayer(layer);
    5556
    5657        Field mapView = MapFrame.class.getDeclaredField("mapView");
     
    6869        dataSet.addPrimitive(w);
    6970
    70         Main.getLayerManager().addLayer(layer);
    7171        try {
    7272            assertTrue(Main.map.selectDrawTool(false));
Note: See TracChangeset for help on using the changeset viewer.