1 | package reverter;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.awt.event.ActionEvent;
|
---|
6 | import java.awt.event.KeyEvent;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import javax.swing.JOptionPane;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
13 | import org.openstreetmap.josm.command.Command;
|
---|
14 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
15 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
16 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
17 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
18 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
19 |
|
---|
20 | import reverter.ChangesetReverter.RevertType;
|
---|
21 |
|
---|
22 | @SuppressWarnings("serial")
|
---|
23 | public class RevertChangesetAction extends JosmAction {
|
---|
24 |
|
---|
25 | public RevertChangesetAction() {
|
---|
26 | super(tr("Revert changeset"),"revert-changeset",tr("Revert changeset"),
|
---|
27 | Shortcut.registerShortcut("tool:revert",
|
---|
28 | tr("Tool: {0}", tr("Revert changeset")),
|
---|
29 | KeyEvent.VK_T, Shortcut.GROUP_EDIT,
|
---|
30 | Shortcut.SHIFT_DEFAULT),
|
---|
31 | true);
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | protected void updateEnabledState() {
|
---|
36 | setEnabled(getCurrentDataSet() != null);
|
---|
37 | }
|
---|
38 |
|
---|
39 | public void actionPerformed(ActionEvent arg0) {
|
---|
40 | if (getCurrentDataSet() == null)
|
---|
41 | return;
|
---|
42 | final ChangesetIdQuery dlg = new ChangesetIdQuery();
|
---|
43 | dlg.setVisible(true);
|
---|
44 | if (dlg.getValue() != 1) return;
|
---|
45 | final int changesetId = dlg.getChangesetId();
|
---|
46 | final RevertType revertType = dlg.getRevertType();
|
---|
47 | if (changesetId == 0) return;
|
---|
48 | if (revertType == null) return;
|
---|
49 |
|
---|
50 | Main.worker.submit(new PleaseWaitRunnable(tr("Reverting...")) {
|
---|
51 | private ChangesetReverter rev;
|
---|
52 | private boolean downloadConfirmed = false;
|
---|
53 |
|
---|
54 | private boolean checkAndDownloadMissing() throws OsmTransferException {
|
---|
55 | if (!rev.hasMissingObjects()) return true;
|
---|
56 | if (!downloadConfirmed) {
|
---|
57 | downloadConfirmed = JOptionPane.showConfirmDialog(Main.parent,
|
---|
58 | tr("This changeset has objects that are not present in current dataset.\n" +
|
---|
59 | "It is needed to download them before reverting. Do you want to continue?"),
|
---|
60 | tr("Confirm"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
|
---|
61 | if (!downloadConfirmed) return false;
|
---|
62 | }
|
---|
63 | final PleaseWaitProgressMonitor monitor =
|
---|
64 | new PleaseWaitProgressMonitor(tr("Fetching missing primitives"));
|
---|
65 | try {
|
---|
66 | rev.downloadMissingPrimitives(monitor);
|
---|
67 | } finally {
|
---|
68 | monitor.close();
|
---|
69 | }
|
---|
70 | return !monitor.isCancelled();
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | protected void realRun() throws OsmTransferException {
|
---|
75 | progressMonitor.indeterminateSubTask(tr("Downloading changeset"));
|
---|
76 | rev = new ChangesetReverter(changesetId, revertType,
|
---|
77 | progressMonitor.createSubTaskMonitor(0, true));
|
---|
78 | if (progressMonitor.isCancelled()) return;
|
---|
79 |
|
---|
80 | // Check missing objects
|
---|
81 | rev.checkMissingCreated();
|
---|
82 | rev.checkMissingUpdated();
|
---|
83 | if (rev.hasMissingObjects()) {
|
---|
84 | // If missing created or updated objects, ask user
|
---|
85 | rev.checkMissingDeleted();
|
---|
86 | if (!checkAndDownloadMissing()) return;
|
---|
87 | } else {
|
---|
88 | // Don't ask user to download primitives going to be undeleted
|
---|
89 | rev.checkMissingDeleted();
|
---|
90 | rev.downloadMissingPrimitives(progressMonitor.createSubTaskMonitor(0, false));
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (progressMonitor.isCancelled()) return;
|
---|
94 | rev.downloadObjectsHistory(progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
|
---|
95 | if (progressMonitor.isCancelled()) return;
|
---|
96 | if (!checkAndDownloadMissing()) return;
|
---|
97 | List<Command> cmds = rev.getCommands();
|
---|
98 | Command cmd = new RevertChangesetCommand(tr(revertType == RevertType.FULL ? "Revert changeset #{0}" :
|
---|
99 | "Partially revert changeset #{0}",changesetId),cmds);
|
---|
100 | Main.main.undoRedo.add(cmd);
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | protected void cancel() {
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Override
|
---|
108 | protected void finish() {
|
---|
109 | }
|
---|
110 | });
|
---|
111 | }
|
---|
112 | }
|
---|