1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import java.awt.event.ActionEvent;
|
---|
5 | import java.awt.event.KeyEvent;
|
---|
6 | import java.io.IOException;
|
---|
7 |
|
---|
8 | import javax.swing.JOptionPane;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
12 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
13 | import org.openstreetmap.josm.io.ChangesetProcessingType;
|
---|
14 | import org.openstreetmap.josm.io.OsmApi;
|
---|
15 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
16 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
17 | import org.xml.sax.SAXException;
|
---|
18 |
|
---|
19 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
20 |
|
---|
21 | public class StopChangesetAction extends JosmAction{
|
---|
22 |
|
---|
23 | public StopChangesetAction() {
|
---|
24 | super(tr("Close current changeset"),
|
---|
25 | "closechangeset",
|
---|
26 | tr("Close the current changeset ..."),
|
---|
27 | Shortcut.registerShortcut(
|
---|
28 | "system:closechangeset",
|
---|
29 | tr("File: {0}", tr("Close the current changeset ...")),
|
---|
30 | KeyEvent.VK_O,
|
---|
31 | Shortcut.GROUP_MENU
|
---|
32 | ),
|
---|
33 | true
|
---|
34 | );
|
---|
35 |
|
---|
36 | }
|
---|
37 | public void actionPerformed(ActionEvent e) {
|
---|
38 | if (OsmApi.getOsmApi().getCurrentChangeset() == null) {
|
---|
39 | JOptionPane.showMessageDialog(
|
---|
40 | Main.parent,
|
---|
41 | tr("There is currently no changeset open."),
|
---|
42 | tr("No open changeset"),
|
---|
43 | JOptionPane.INFORMATION_MESSAGE
|
---|
44 | );
|
---|
45 | return;
|
---|
46 | }
|
---|
47 | Main.worker.submit(new StopChangesetActionTask());
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | protected void updateEnabledState() {
|
---|
52 | setEnabled(Main.map != null && OsmApi.getOsmApi().getCurrentChangeset() != null);
|
---|
53 | }
|
---|
54 |
|
---|
55 | static class StopChangesetActionTask extends PleaseWaitRunnable {
|
---|
56 | private boolean cancelled;
|
---|
57 | private Exception lastException;
|
---|
58 |
|
---|
59 | public StopChangesetActionTask() {
|
---|
60 | super(tr("Closing changeset"), false /* don't ignore exceptions */);
|
---|
61 | }
|
---|
62 | @Override
|
---|
63 | protected void cancel() {
|
---|
64 | this.cancelled = true;
|
---|
65 | OsmApi.getOsmApi().cancel();
|
---|
66 |
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | protected void finish() {
|
---|
71 | if (cancelled)
|
---|
72 | return;
|
---|
73 | if (lastException != null) {
|
---|
74 | ExceptionDialogUtil.explainException(lastException);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
80 | try {
|
---|
81 | OsmApi.getOsmApi().stopChangeset(ChangesetProcessingType.USE_EXISTING_AND_CLOSE, getProgressMonitor().createSubTaskMonitor(1, false));
|
---|
82 | } catch(Exception e) {
|
---|
83 | if (cancelled)
|
---|
84 | return;
|
---|
85 | lastException = e;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|