Ignore:
Timestamp:
2018-01-23T21:36:34+01:00 (7 years ago)
Author:
donvip
Message:

fix #josm15821 - better handling of batch reverts

Location:
applications/editors/josm/plugins/reverter/src/reverter
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/reverter/src/reverter/RevertChangesetAction.java

    r33572 r34028  
    1414import reverter.ChangesetReverter.RevertType;
    1515
    16 @SuppressWarnings("serial")
    1716public class RevertChangesetAction extends JosmAction {
    1817
    1918    public RevertChangesetAction() {
    2019        super(tr("Revert changeset"), "revert-changeset", tr("Revert changeset"),
    21             Shortcut.registerShortcut("tool:revert",
    22                 tr("Tool: {0}", tr("Revert changeset")),
    23                 KeyEvent.VK_T, Shortcut.CTRL_SHIFT),
     20            Shortcut.registerShortcut("tool:revert", tr("Tool: {0}", tr("Revert changeset")), KeyEvent.VK_T, Shortcut.CTRL_SHIFT),
    2421                true);
    2522    }
    2623
    2724    @Override
    28     public void actionPerformed(ActionEvent arg0) {
     25    public void actionPerformed(ActionEvent e) {
    2926        final ChangesetIdQuery dlg = new ChangesetIdQuery();
    3027        if (dlg.showDialog().getValue() != 1) return;
     
    3532        boolean newLayer = dlg.isNewLayerRequired();
    3633        final boolean autoConfirmDownload = newLayer || changesetIds.size() > 1;
    37         for (Integer changesetId : changesetIds) {
    38             MainApplication.worker.submit(new RevertChangesetTask(changesetId, revertType, autoConfirmDownload, newLayer));
    39             newLayer = false; // reuse layer for subsequent reverts
    40         }
     34        MainApplication.worker.submit(new RevertChangesetTask(changesetIds, revertType, autoConfirmDownload, newLayer));
    4135    }
    4236}
  • applications/editors/josm/plugins/reverter/src/reverter/RevertChangesetTask.java

    r33572 r34028  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.text.MessageFormat;
     7import java.util.ArrayList;
     8import java.util.Collection;
     9import java.util.Collections;
    610import java.util.List;
    711import java.util.concurrent.Callable;
     
    1115import org.openstreetmap.josm.Main;
    1216import org.openstreetmap.josm.command.Command;
     17import org.openstreetmap.josm.command.SequenceCommand;
    1318import org.openstreetmap.josm.command.conflict.ConflictAddCommand;
    1419import org.openstreetmap.josm.gui.MainApplication;
     
    1924import org.openstreetmap.josm.gui.util.GuiHelper;
    2025import org.openstreetmap.josm.io.OsmTransferException;
     26import org.openstreetmap.josm.tools.Logging;
     27import org.openstreetmap.josm.tools.UserCancelException;
    2128
    2229import reverter.ChangesetReverter.RevertType;
    2330
    2431public class RevertChangesetTask extends PleaseWaitRunnable {
    25     private final int changesetId;
     32    private final Collection<Integer> changesetIds;
    2633    private final RevertType revertType;
    27     private final boolean newLayer;
     34    private boolean newLayer;
    2835
    2936    private ChangesetReverter rev;
    3037    private boolean downloadConfirmed;
     38    private int numberOfConflicts;
    3139
    3240    public RevertChangesetTask(int changesetId, RevertType revertType) {
     
    3947
    4048    public RevertChangesetTask(int changesetId, RevertType revertType, boolean autoConfirmDownload, boolean newLayer) {
     49        this(Collections.singleton(changesetId), revertType, autoConfirmDownload, newLayer);
     50    }
     51
     52    public RevertChangesetTask(Collection<Integer> changesetIds, RevertType revertType, boolean autoConfirmDownload, boolean newLayer) {
    4153        super(tr("Reverting..."));
    42         this.changesetId = changesetId;
     54        this.changesetIds = new ArrayList<>(changesetIds);
    4355        this.revertType = revertType;
    4456        this.downloadConfirmed = autoConfirmDownload;
     
    7385    @Override
    7486    protected void realRun() throws OsmTransferException {
     87        numberOfConflicts = 0;
     88        final List<Command> allcmds = new ArrayList<>();
     89        Logging.info("Reverting {0} changeset(s): {1}", changesetIds.size(), changesetIds);
     90        for (int changesetId : changesetIds) {
     91            try {
     92                Logging.info("Reverting changeset {0}", changesetId);
     93                allcmds.add(revertChangeset(changesetId));
     94                Logging.info("Reverted changeset {0}", changesetId);
     95                newLayer = false; // reuse layer for subsequent reverts
     96            } catch (OsmTransferException e) {
     97                Logging.error(e);
     98                throw e;
     99            } catch (UserCancelException e) {
     100                Logging.warn("Revert canceled");
     101                Logging.trace(e);
     102                return;
     103            }
     104        }
     105        if (!allcmds.isEmpty()) {
     106            Command cmd = allcmds.size() == 1 ? allcmds.get(0) : new SequenceCommand(tr("Revert changeset"), allcmds);
     107            GuiHelper.runInEDT(() -> {
     108                MainApplication.undoRedo.add(cmd);
     109                if (numberOfConflicts > 0) {
     110                    MainApplication.getMap().conflictDialog.warnNumNewConflicts(numberOfConflicts);
     111                }
     112            });
     113        }
     114    }
     115
     116    private RevertChangesetCommand revertChangeset(int changesetId) throws OsmTransferException, UserCancelException {
    75117        progressMonitor.indeterminateSubTask(tr("Downloading changeset"));
    76118        try {
    77             rev = new ChangesetReverter(changesetId, revertType, newLayer,
    78                     progressMonitor.createSubTaskMonitor(0, true));
     119            rev = new ChangesetReverter(changesetId, revertType, newLayer, progressMonitor.createSubTaskMonitor(0, true));
    79120        } catch (final RevertRedactedChangesetException e) {
    80             GuiHelper.runInEDT(new Runnable() {
    81                 @Override
    82                 public void run() {
    83                     new Notification(
    84                             e.getMessage()+"<br>"+
    85                             tr("See {0}", "<a href=\"https://www.openstreetmap.org/redactions\">https://www.openstreetmap.org/redactions</a>"))
    86                     .setIcon(JOptionPane.ERROR_MESSAGE)
    87                     .setDuration(Notification.TIME_LONG)
    88                     .show();
    89                 }
    90             });
     121            GuiHelper.runInEDT(() -> new Notification(
     122                    e.getMessage()+"<br>"+
     123                    tr("See {0}", "<a href=\"https://www.openstreetmap.org/redactions\">https://www.openstreetmap.org/redactions</a>"))
     124            .setIcon(JOptionPane.ERROR_MESSAGE)
     125            .setDuration(Notification.TIME_LONG)
     126            .show());
    91127            progressMonitor.cancel();
    92128        }
    93         if (progressMonitor.isCanceled()) return;
     129        if (progressMonitor.isCanceled())
     130            throw new UserCancelException();
    94131
    95132        // Check missing objects
     
    99136            // If missing created or updated objects, ask user
    100137            rev.checkMissingDeleted();
    101             if (!checkAndDownloadMissing()) return;
     138            if (!checkAndDownloadMissing())
     139                throw new UserCancelException();
    102140        } else {
    103141            // Don't ask user to download primitives going to be undeleted
     
    106144        }
    107145
    108         if (progressMonitor.isCanceled()) return;
     146        if (progressMonitor.isCanceled())
     147            throw new UserCancelException();
    109148        rev.downloadObjectsHistory(progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
    110         if (progressMonitor.isCanceled()) return;
    111         if (!checkAndDownloadMissing()) return;
     149        if (progressMonitor.isCanceled())
     150            throw new UserCancelException();
     151        if (!checkAndDownloadMissing())
     152            throw new UserCancelException();
    112153        rev.fixNodesWithoutCoordinates(progressMonitor);
    113154        List<Command> cmds = rev.getCommands();
    114         final Command cmd = new RevertChangesetCommand(tr(revertType == RevertType.FULL ? "Revert changeset #{0}" :
    115                 "Partially revert changeset #{0}", changesetId), cmds);
    116         int n = 0;
     155        if (cmds.isEmpty()) {
     156            String msg = MessageFormat.format("No revert commands found for changeset {0}", changesetId);
     157            Logging.warn(msg);
     158            throw new OsmTransferException(msg);
     159        }
    117160        for (Command c : cmds) {
    118161            if (c instanceof ConflictAddCommand) {
    119                 n++;
     162                numberOfConflicts++;
    120163            }
    121164        }
    122         final int newConflicts = n;
    123         GuiHelper.runInEDT(new Runnable() {
    124             @Override
    125             public void run() {
    126                 MainApplication.undoRedo.add(cmd);
    127                 if (newConflicts > 0) {
    128                     MainApplication.getMap().conflictDialog.warnNumNewConflicts(newConflicts);
    129                 }
    130             }
    131         });
     165        return new RevertChangesetCommand(tr(revertType == RevertType.FULL ? "Revert changeset #{0}" :
     166                "Partially revert changeset #{0}", changesetId), cmds);
    132167    }
    133168
     
    139174    protected void finish() {
    140175    }
     176
     177    public final int getNumberOfConflicts() {
     178        return numberOfConflicts;
     179    }
    141180}
Note: See TracChangeset for help on using the changeset viewer.