Changeset 16187 in josm for trunk


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
Files:
11 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
  • trunk/src/org/openstreetmap/josm/command/PurgeCommand.java

    r13513 r16187  
    8484    @Override
    8585    public boolean executeCommand() {
    86         getAffectedDataSet().beginUpdate();
    87         try {
     86        getAffectedDataSet().update(() -> {
    8887            purgedConflicts.get().clear();
    8988            // unselect primitives in advance to not fire a selection change for every one of them
     
    116115            }
    117116            getAffectedDataSet().clearMappaintCache();
    118         } finally {
    119             getAffectedDataSet().endUpdate();
    120         }
     117        });
    121118        return true;
    122119    }
     
    127124            return;
    128125
    129         getAffectedDataSet().beginUpdate();
    130         try {
     126        getAffectedDataSet().update(() -> {
    131127            for (OsmPrimitive osm : toPurge) {
    132128                PrimitiveData data = makeIncompleteDataByPrimId.get(osm);
     
    147143            }
    148144            getAffectedDataSet().clearMappaintCache();
    149         } finally {
    150             getAffectedDataSet().endUpdate();
    151         }
     145        });
    152146    }
    153147
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r16119 r16187  
    2424import java.util.function.Function;
    2525import java.util.function.Predicate;
     26import java.util.function.Supplier;
    2627import java.util.stream.Collectors;
    2728import java.util.stream.Stream;
     
    256257    public DataSet(OsmPrimitive... osmPrimitives) {
    257258        this();
    258         beginUpdate();
    259         try {
     259        update(() -> {
    260260            for (OsmPrimitive o : osmPrimitives) {
    261261                addPrimitive(o);
    262262            }
    263         } finally {
    264             endUpdate();
    265         }
     263        });
    266264    }
    267265
     
    499497        Objects.requireNonNull(primitive, "primitive");
    500498        checkModifiable();
    501         beginUpdate();
    502         try {
     499        update(() -> {
    503500            if (getPrimitiveById(primitive) != null)
    504501                throw new DataIntegrityProblemException(
     
    511508            store.addPrimitive(primitive);
    512509            firePrimitivesAdded(Collections.singletonList(primitive), false);
    513         } finally {
    514             endUpdate();
    515         }
     510        });
    516511    }
    517512
     
    528523    public void removePrimitive(PrimitiveId primitiveId) {
    529524        checkModifiable();
    530         beginUpdate();
    531         try {
     525        update(() -> {
    532526            OsmPrimitive primitive = getPrimitiveByIdChecked(primitiveId);
    533527            if (primitive == null)
     
    535529            removePrimitiveImpl(primitive);
    536530            firePrimitivesRemoved(Collections.singletonList(primitive), false);
    537         } finally {
    538             endUpdate();
    539         }
     531        });
    540532    }
    541533
     
    552544    void removePrimitive(OsmPrimitive primitive) {
    553545        checkModifiable();
    554         beginUpdate();
    555         try {
     546        update(() -> {
    556547            removePrimitiveImpl(primitive);
    557548            firePrimitivesRemoved(Collections.singletonList(primitive), false);
    558         } finally {
    559             endUpdate();
    560         }
     549        });
    561550    }
    562551
     
    804793    public Set<Way> unlinkNodeFromWays(Node node) {
    805794        checkModifiable();
    806         Set<Way> result = new HashSet<>();
    807         beginUpdate();
    808         try {
     795        return update(() -> {
     796            Set<Way> result = new HashSet<>();
    809797            for (Way way : node.getParentWays()) {
    810798                List<Node> wayNodes = way.getNodes();
     
    818806                }
    819807            }
    820         } finally {
    821             endUpdate();
    822         }
    823         return result;
     808            return result;
     809        });
    824810    }
    825811
     
    833819    public Set<Relation> unlinkPrimitiveFromRelations(OsmPrimitive primitive) {
    834820        checkModifiable();
    835         Set<Relation> result = new HashSet<>();
    836         beginUpdate();
    837         try {
     821        return update(() -> {
     822            Set<Relation> result = new HashSet<>();
    838823            for (Relation relation : getRelations()) {
    839824                List<RelationMember> members = relation.getMembers();
     
    854839                }
    855840            }
    856         } finally {
    857             endUpdate();
    858         }
    859         return result;
     841            return result;
     842        });
    860843    }
    861844
     
    869852    public Set<OsmPrimitive> unlinkReferencesToPrimitive(OsmPrimitive referencedPrimitive) {
    870853        checkModifiable();
    871         Set<OsmPrimitive> result = new HashSet<>();
    872         beginUpdate();
    873         try {
     854        return update(() -> {
     855            Set<OsmPrimitive> result = new HashSet<>();
    874856            if (referencedPrimitive instanceof Node) {
    875857                result.addAll(unlinkNodeFromWays((Node) referencedPrimitive));
    876858            }
    877859            result.addAll(unlinkPrimitiveFromRelations(referencedPrimitive));
    878         } finally {
    879             endUpdate();
    880         }
    881         return result;
     860            return result;
     861        });
    882862    }
    883863
     
    979959    }
    980960
     961    /**
     962     * Performs the update runnable between {@link #beginUpdate()} / {@link #endUpdate()} calls.
     963     * @param runnable update action
     964     * @since 16187
     965     */
     966    public void update(Runnable runnable) {
     967        beginUpdate();
     968        try {
     969            runnable.run();
     970        } finally {
     971            endUpdate();
     972        }
     973    }
     974
     975    /**
     976     * Performs the update function between {@link #beginUpdate()} / {@link #endUpdate()} calls.
     977     * @param function update function
     978     * @param t function argument
     979     * @param <T> argument type
     980     * @param <R> result type
     981     * @return function result
     982     * @since 16187
     983     */
     984    public <T, R> R update(Function<T, R> function, T t) {
     985        beginUpdate();
     986        try {
     987            return function.apply(t);
     988        } finally {
     989            endUpdate();
     990        }
     991    }
     992
     993    /**
     994     * Performs the update supplier between {@link #beginUpdate()} / {@link #endUpdate()} calls.
     995     * @param supplier update supplier
     996     * @param <R> result type
     997     * @return supplier result
     998     * @since 16187
     999     */
     1000    public <R> R update(Supplier<R> supplier) {
     1001        beginUpdate();
     1002        try {
     1003            return supplier.get();
     1004        } finally {
     1005            endUpdate();
     1006        }
     1007    }
     1008
    9811009    private void fireEventToListeners(AbstractDatasetChangedEvent event) {
    9821010        for (DataSetListener listener : listeners) {
     
    10441072     * Invalidates the internal cache of projected east/north coordinates.
    10451073     *
    1046      * This method can be invoked after the globally configured projection method
    1047      * changed.
     1074     * This method can be invoked after the globally configured projection method changed.
    10481075     */
    10491076    public void invalidateEastNorthCache() {
    10501077        if (ProjectionRegistry.getProjection() == null)
    10511078            return; // sanity check
    1052         beginUpdate();
    1053         try {
    1054             for (Node n : getNodes()) {
    1055                 n.invalidateEastNorthCache();
    1056             }
    1057         } finally {
    1058             endUpdate();
    1059         }
     1079        update(() -> getNodes().forEach(Node::invalidateEastNorthCache));
    10601080    }
    10611081
     
    10641084     */
    10651085    public void cleanupDeletedPrimitives() {
    1066         beginUpdate();
    1067         try {
     1086        update(() -> {
    10681087            Collection<OsmPrimitive> toCleanUp = getPrimitives(
    10691088                    primitive -> primitive.isDeleted() && (!primitive.isVisible() || primitive.isNew()));
     
    10761095                firePrimitivesRemoved(toCleanUp, false);
    10771096            }
    1078         } finally {
    1079             endUpdate();
    1080         }
     1097        });
    10811098    }
    10821099
     
    10911108        //TODO: Report listeners that are still active (should be none)
    10921109        checkModifiable();
    1093         beginUpdate();
    1094         try {
     1110        update(() -> {
    10951111            clearSelection();
    10961112            for (OsmPrimitive primitive : allPrimitives) {
     
    10991115            store.clear();
    11001116            allPrimitives.clear();
    1101         } finally {
    1102             endUpdate();
    1103         }
     1117        });
    11041118    }
    11051119
  • trunk/src/org/openstreetmap/josm/data/osm/DataSetMerger.java

    r15418 r16187  
    424424            progressMonitor.beginTask(tr("Merging data..."), sourceDataSet.allPrimitives().size());
    425425        }
    426         targetDataSet.beginUpdate();
    427         try {
     426        targetDataSet.update(() -> {
    428427            List<? extends OsmPrimitive> candidates = new ArrayList<>(targetDataSet.getNodes());
    429428            for (Node node: sourceDataSet.getNodes()) {
     
    481480                targetDataSet.lock();
    482481            }
    483         } finally {
    484             targetDataSet.endUpdate();
    485         }
     482        });
    486483        if (progressMonitor != null) {
    487484            progressMonitor.finishTask();
  • trunk/src/org/openstreetmap/josm/data/osm/FilterModel.java

    r15390 r16187  
    110110            ds.beginUpdate();
    111111            try {
    112 
    113112                final Collection<OsmPrimitive> all = ds.allNonDeletedCompletePrimitives();
    114113
     
    158157        List<OsmPrimitive> deselect = new ArrayList<>();
    159158
    160         ds.beginUpdate();
    161         try {
     159        ds.update(() -> {
    162160            for (int i = 0; i < 2; i++) {
    163161                for (OsmPrimitive primitive: primitives) {
     
    190188                }
    191189            }
    192         } finally {
    193             ds.endUpdate();
    194         }
     190        });
    195191
    196192        if (!deselect.isEmpty()) {
  • trunk/src/org/openstreetmap/josm/gui/dialogs/ValidatorDialog.java

    r15586 r16187  
    694694         */
    695695        private void tryUndo() {
    696             final DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
    697             int i = fixCommands.size() - 1;
    698             ds.beginUpdate();
    699             for (; i >= 0; i--) {
    700                 fixCommands.get(i).undoCommand();
    701             }
    702             ds.endUpdate();
    703         }
    704 
     696            MainApplication.getLayerManager().getActiveDataSet().update(() -> {
     697                for (int i = fixCommands.size() - 1; i >= 0; i--) {
     698                    fixCommands.get(i).undoCommand();
     699                }
     700            });
     701        }
    705702    }
    706703
     
    723720        @Override
    724721        public void undoCommand() {
    725             getAffectedDataSet().beginUpdate();
    726             super.undoCommand();
    727             getAffectedDataSet().endUpdate();
     722            getAffectedDataSet().update(super::undoCommand);
    728723        }
    729724
    730725        @Override
    731726        public boolean executeCommand() {
    732             getAffectedDataSet().beginUpdate();
    733             boolean rc = super.executeCommand();
    734             getAffectedDataSet().endUpdate();
    735             return rc;
     727            return getAffectedDataSet().update(super::executeCommand);
    736728        }
    737729    }
  • trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r16080 r16187  
    652652        // if uploaded, clean the modified flags as well
    653653        data.cleanupDeletedPrimitives();
    654         data.beginUpdate();
    655         try {
     654        data.update(() -> {
    656655            for (OsmPrimitive p: data.allPrimitives()) {
    657656                if (processed.contains(p)) {
     
    659658                }
    660659            }
    661         } finally {
    662             data.endUpdate();
    663         }
     660        });
    664661    }
    665662
Note: See TracChangeset for help on using the changeset viewer.