1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 | import javax.swing.SwingUtilities;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
18 | import org.openstreetmap.josm.data.osm.ChangesetCache;
|
---|
19 | import org.openstreetmap.josm.data.osm.UserInfo;
|
---|
20 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
21 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
22 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
23 | import org.openstreetmap.josm.gui.io.CloseChangesetDialog;
|
---|
24 | import org.openstreetmap.josm.gui.io.CloseChangesetTask;
|
---|
25 | import org.openstreetmap.josm.io.ChangesetQuery;
|
---|
26 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
27 | import org.openstreetmap.josm.io.OsmServerChangesetReader;
|
---|
28 | import org.openstreetmap.josm.io.OsmServerUserInfoReader;
|
---|
29 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
30 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
31 | import org.xml.sax.SAXException;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * User action to close open changesets.
|
---|
35 | *
|
---|
36 | * The list of open changesets will be downloaded from the server and presented
|
---|
37 | * to the user.
|
---|
38 | */
|
---|
39 | public class CloseChangesetAction extends JosmAction {
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Constructs a new {@code CloseChangesetAction}.
|
---|
43 | */
|
---|
44 | public CloseChangesetAction() {
|
---|
45 | super(tr("Close open changesets"),
|
---|
46 | "closechangeset",
|
---|
47 | tr("Closes open changesets"),
|
---|
48 | Shortcut.registerShortcut("system:closechangeset",
|
---|
49 | tr("File: {0}", tr("Closes open changesets")),
|
---|
50 | KeyEvent.VK_Q, Shortcut.ALT_CTRL),
|
---|
51 | true
|
---|
52 | );
|
---|
53 | putValue("help", ht("/Action/CloseChangeset"));
|
---|
54 | setEnabled(!Main.isOffline(OnlineResource.OSM_API));
|
---|
55 |
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public void actionPerformed(ActionEvent e) {
|
---|
60 | MainApplication.worker.submit(new DownloadOpenChangesetsTask());
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected void onPostDownloadOpenChangesets() {
|
---|
64 | List<Changeset> openChangesets = ChangesetCache.getInstance().getOpenChangesetsForCurrentUser();
|
---|
65 | if (openChangesets.isEmpty()) {
|
---|
66 | JOptionPane.showMessageDialog(
|
---|
67 | Main.parent,
|
---|
68 | tr("There are no open changesets"),
|
---|
69 | tr("No open changesets"),
|
---|
70 | JOptionPane.INFORMATION_MESSAGE
|
---|
71 | );
|
---|
72 | return;
|
---|
73 | }
|
---|
74 |
|
---|
75 | CloseChangesetDialog dialog = new CloseChangesetDialog();
|
---|
76 | dialog.setChangesets(openChangesets);
|
---|
77 | dialog.setVisible(true);
|
---|
78 | if (dialog.isCanceled())
|
---|
79 | return;
|
---|
80 |
|
---|
81 | Collection<Changeset> changesetsToClose = dialog.getSelectedChangesets();
|
---|
82 | CloseChangesetTask closeChangesetTask = new CloseChangesetTask(changesetsToClose);
|
---|
83 | MainApplication.worker.submit(closeChangesetTask);
|
---|
84 | }
|
---|
85 |
|
---|
86 | private final class DownloadOpenChangesetsTask extends PleaseWaitRunnable {
|
---|
87 |
|
---|
88 | private boolean canceled;
|
---|
89 | private OsmServerChangesetReader reader;
|
---|
90 | private List<Changeset> changesets;
|
---|
91 | private Exception lastException;
|
---|
92 |
|
---|
93 | private DownloadOpenChangesetsTask() {
|
---|
94 | super(tr("Downloading open changesets ..."), false /* don't ignore exceptions */);
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | protected void cancel() {
|
---|
99 | this.canceled = true;
|
---|
100 | reader.cancel();
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | protected void finish() {
|
---|
105 | SwingUtilities.invokeLater(() -> {
|
---|
106 | if (lastException != null) {
|
---|
107 | ExceptionDialogUtil.explainException(lastException);
|
---|
108 | }
|
---|
109 | ChangesetCache.getInstance().update(changesets);
|
---|
110 | if (!canceled && lastException == null) {
|
---|
111 | onPostDownloadOpenChangesets();
|
---|
112 | }
|
---|
113 | });
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Fetch the user info from the server. This is necessary if we don't know the users id yet
|
---|
118 | *
|
---|
119 | * @return the user info
|
---|
120 | * @throws OsmTransferException in case of any communication exception
|
---|
121 | */
|
---|
122 | private UserInfo fetchUserInfo() throws OsmTransferException {
|
---|
123 | return new OsmServerUserInfoReader().fetchUserInfo(getProgressMonitor().createSubTaskMonitor(1, false));
|
---|
124 | }
|
---|
125 |
|
---|
126 | @Override
|
---|
127 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
128 | try {
|
---|
129 | UserInfo userInfo = fetchUserInfo();
|
---|
130 | if (canceled)
|
---|
131 | return;
|
---|
132 | reader = new OsmServerChangesetReader();
|
---|
133 | ChangesetQuery query = new ChangesetQuery().forUser(userInfo.getId()).beingOpen(true);
|
---|
134 | changesets = reader.queryChangesets(
|
---|
135 | query,
|
---|
136 | getProgressMonitor().createSubTaskMonitor(1, false /* not internal */)
|
---|
137 | );
|
---|
138 | } catch (OsmTransferException | IllegalArgumentException e) {
|
---|
139 | if (canceled)
|
---|
140 | return;
|
---|
141 | lastException = e;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Determines if the download task has been canceled.
|
---|
147 | * @return {@code true} if the download task has been canceled
|
---|
148 | */
|
---|
149 | public boolean isCanceled() {
|
---|
150 | return canceled;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Returns the last exception that occured.
|
---|
155 | * @return the last exception that occured, or {@code null}
|
---|
156 | */
|
---|
157 | public Exception getLastException() {
|
---|
158 | return lastException;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|