Ignore:
Timestamp:
2020-03-21T21:27:36+01:00 (4 years ago)
Author:
Don-vip
Message:

fix #18962 - introduce DataSet.update to avoid repetitive begin/endUpdate statements

Location:
trunk/src/org/openstreetmap/josm/actions
Files:
5 edited

Legend:

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

    r16048 r16187  
    592592            // revert all executed commands
    593593            ds = executedCmds.getFirst().getAffectedDataSet();
    594             ds.beginUpdate();
    595             while (!executedCmds.isEmpty()) {
    596                 executedCmds.removeLast().undoCommand();
    597             }
    598             ds.endUpdate();
     594            ds.update(() -> {
     595                while (!executedCmds.isEmpty()) {
     596                    executedCmds.removeLast().undoCommand();
     597                }
     598            });
    599599        }
    600600    }
     
    17481748        @Override
    17491749        public void undoCommand() {
    1750             getAffectedDataSet().beginUpdate();
    1751             super.undoCommand();
    1752             getAffectedDataSet().endUpdate();
     1750            getAffectedDataSet().update(super::undoCommand);
    17531751        }
    17541752
    17551753        @Override
    17561754        public boolean executeCommand() {
    1757             getAffectedDataSet().beginUpdate();
    1758             boolean rc = super.executeCommand();
    1759             getAffectedDataSet().endUpdate();
    1760             return rc;
     1755            return getAffectedDataSet().update(super::executeCommand);
    17611756        }
    17621757    }
  • trunk/src/org/openstreetmap/josm/actions/MoveAction.java

    r15676 r16187  
    1212import javax.swing.JOptionPane;
    1313
    14 import org.openstreetmap.josm.command.Command;
    1514import org.openstreetmap.josm.command.MoveCommand;
    1615import org.openstreetmap.josm.data.UndoRedoHandler;
     
    9897    }
    9998
    100     @Override
    101     public void actionPerformed(ActionEvent event) {
    102         DataSet ds = getLayerManager().getEditDataSet();
    103 
    104         if (!MainApplication.isDisplayingMapView() || ds == null)
    105             return;
    106 
    107         // find out how many "real" units the objects have to be moved in order to
    108         // achive an 1-pixel movement
    109 
    110         MapView mapView = MainApplication.getMap().mapView;
     99    /**
     100     * Find out how many "real" units the objects have to be moved in order to achieve an 1-pixel movement
     101     * @param mapView map view
     102     * @return move offset
     103     */
     104    private EastNorth getOffset(MapView mapView) {
    111105        EastNorth en1 = mapView.getEastNorth(100, 100);
    112106        EastNorth en2 = mapView.getEastNorth(101, 101);
     
    131125        }
    132126
     127        return new EastNorth(distx, disty);
     128    }
     129
     130    @Override
     131    public void actionPerformed(ActionEvent event) {
     132        DataSet ds = getLayerManager().getEditDataSet();
     133
     134        if (!MainApplication.isDisplayingMapView() || ds == null)
     135            return;
     136
     137        MapView mapView = MainApplication.getMap().mapView;
     138        final EastNorth dist = getOffset(mapView);
     139
    133140        Collection<OsmPrimitive> selection = ds.getSelected();
    134141        Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
    135142
    136         Command c = UndoRedoHandler.getInstance().getLastCommand();
    137 
    138         ds.beginUpdate();
    139         try {
     143        MoveCommand cmd = ds.update(c -> {
     144            MoveCommand moveCmd;
    140145            if (c instanceof MoveCommand && ds.equals(c.getAffectedDataSet())
    141146                    && affectedNodes.equals(((MoveCommand) c).getParticipatingPrimitives())) {
    142                 ((MoveCommand) c).moveAgain(distx, disty);
     147                moveCmd = (MoveCommand) c;
     148                moveCmd.moveAgain(dist.east(), dist.north());
    143149            } else {
    144                 c = new MoveCommand(ds, selection, distx, disty);
    145                 UndoRedoHandler.getInstance().add(c);
     150                moveCmd = new MoveCommand(ds, selection, dist.east(), dist.north());
     151                UndoRedoHandler.getInstance().add(moveCmd);
    146152            }
    147         } finally {
    148             ds.endUpdate();
    149         }
     153            return moveCmd;
     154        }, UndoRedoHandler.getInstance().getLastCommand());
    150155
    151156        for (Node n : affectedNodes) {
    152157            if (n.isLatLonKnown() && n.isOutSideWorld()) {
    153158                // Revert move
    154                 ((MoveCommand) c).moveAgain(-distx, -disty);
     159                cmd.moveAgain(-dist.east(), -dist.north());
    155160                JOptionPane.showMessageDialog(
    156161                        MainApplication.getMainFrame(),
  • trunk/src/org/openstreetmap/josm/actions/SimplifyWayAction.java

    r15586 r16187  
    172172    public void actionPerformed(ActionEvent e) {
    173173        DataSet ds = getLayerManager().getEditDataSet();
    174         ds.beginUpdate();
    175         try {
     174        ds.update(() -> {
    176175            List<Way> ways = ds.getSelectedWays().stream()
    177176                    .filter(p -> !p.isIncomplete())
     
    195194                simplifyWays(ways, err);
    196195            }
    197         } finally {
    198             ds.endUpdate();
    199         }
     196        });
    200197    }
    201198
  • trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java

    r15735 r16187  
    247247
    248248    private static void addRemoveSelection(DataSet ds, OsmPrimitive toAdd, OsmPrimitive toRemove) {
    249         ds.beginUpdate(); // to prevent the selection listener to screw around with the state
    250         try {
     249        ds.update(() -> { // to prevent the selection listener to screw around with the state
    251250            addSelection(ds, toAdd);
    252251            clearSelection(ds, toRemove);
    253         } finally {
    254             ds.endUpdate();
    255         }
     252        });
    256253    }
    257254
  • trunk/src/org/openstreetmap/josm/actions/mapmode/SelectAction.java

    r16177 r16187  
    712712        if (mode == Mode.MOVE) {
    713713            if (startEN == null) return false; // fix #8128
    714             ds.beginUpdate();
    715             try {
     714            return ds.update(() -> {
     715                MoveCommand moveCmd = null;
    716716                if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand) c).getParticipatingPrimitives())) {
    717                     ((MoveCommand) c).saveCheckpoint();
    718                     ((MoveCommand) c).applyVectorTo(currentEN);
     717                    moveCmd = (MoveCommand) c;
     718                    moveCmd.saveCheckpoint();
     719                    moveCmd.applyVectorTo(currentEN);
    719720                } else if (!selection.isEmpty()) {
    720                     c = new MoveCommand(selection, startEN, currentEN);
    721                     UndoRedoHandler.getInstance().add(c);
     721                    moveCmd = new MoveCommand(selection, startEN, currentEN);
     722                    UndoRedoHandler.getInstance().add(moveCmd);
    722723                }
    723724                for (Node n : affectedNodes) {
    724725                    if (n.isOutSideWorld()) {
    725726                        // Revert move
    726                         if (c instanceof MoveCommand) {
    727                             ((MoveCommand) c).resetToCheckpoint();
     727                        if (moveCmd != null) {
     728                            moveCmd.resetToCheckpoint();
    728729                        }
    729730                        // TODO: We might use a simple notification in the lower left corner.
     
    737738                    }
    738739                }
    739             } finally {
    740                 ds.endUpdate();
    741             }
     740                return true;
     741            });
    742742        } else {
    743743            startEN = currentEN; // drag can continue after scaling/rotation
     
    747747            }
    748748
    749             ds.beginUpdate();
    750             try {
     749            return ds.update(() -> {
    751750                if (mode == Mode.ROTATE) {
    752751                    if (c instanceof RotateCommand && affectedNodes.equals(((RotateCommand) c).getTransformedNodes())) {
     
    767766                    MainApplication.getMap().statusLine.setDist(ways);
    768767                }
    769             } finally {
    770                 ds.endUpdate();
    771             }
    772         }
    773         return true;
     768                return true;
     769            });
     770        }
    774771    }
    775772
     
    910907            Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
    911908            Command c = getLastCommandInDataset(ds);
    912             ds.beginUpdate();
    913             try {
     909            ds.update(() -> {
    914910                if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand) c).getParticipatingPrimitives())) {
    915911                    Node selectedNode = selNodes.iterator().next();
     
    919915                                                targetEN.getY() - selectedEN.getY());
    920916                }
    921             } finally {
    922                 ds.endUpdate();
    923             }
     917            });
    924918        }
    925919
Note: See TracChangeset for help on using the changeset viewer.