1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.downloadtasks;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.io.IOException;
|
---|
8 |
|
---|
9 | import javax.swing.JOptionPane;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.data.osm.UserInfo;
|
---|
13 | import org.openstreetmap.josm.gui.JosmUserIdentityManager;
|
---|
14 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
15 | import org.openstreetmap.josm.io.ChangesetQuery;
|
---|
16 | import org.openstreetmap.josm.io.OsmServerUserInfoReader;
|
---|
17 | import org.openstreetmap.josm.io.OsmTransferCanceledException;
|
---|
18 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
20 | import org.openstreetmap.josm.tools.ExceptionUtil;
|
---|
21 | import org.xml.sax.SAXException;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Asynchronous task to send a changeset query to the OSM API.
|
---|
25 | * @since 2689
|
---|
26 | */
|
---|
27 | public class ChangesetQueryTask extends AbstractChangesetDownloadTask {
|
---|
28 |
|
---|
29 | class DownloadTask extends RunnableDownloadTask {
|
---|
30 | /** the changeset query */
|
---|
31 | private ChangesetQuery query;
|
---|
32 | /** the reader object used to read information about the current user from the API */
|
---|
33 | private final OsmServerUserInfoReader userInfoReader = new OsmServerUserInfoReader();
|
---|
34 |
|
---|
35 | DownloadTask(Component parent, ChangesetQuery query) {
|
---|
36 | super(parent, tr("Querying and downloading changesets"));
|
---|
37 | this.query = query;
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Tries to fully identify the current JOSM user
|
---|
42 | *
|
---|
43 | * @throws OsmTransferException if something went wrong
|
---|
44 | */
|
---|
45 | protected void fullyIdentifyCurrentUser() throws OsmTransferException {
|
---|
46 | getProgressMonitor().indeterminateSubTask(tr("Determine user id for current user..."));
|
---|
47 |
|
---|
48 | UserInfo info = userInfoReader.fetchUserInfo(getProgressMonitor().createSubTaskMonitor(1, false));
|
---|
49 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
|
---|
50 | im.setFullyIdentified(im.getUserName(), info);
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
55 | try {
|
---|
56 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
|
---|
57 | if (query.isRestrictedToPartiallyIdentifiedUser() && im.isCurrentUser(query.getUserName())) {
|
---|
58 | // if we query changesets for the current user, make sure we query against
|
---|
59 | // its user id, not its user name. If necessary, determine the user id first.
|
---|
60 | //
|
---|
61 | if (im.isPartiallyIdentified()) {
|
---|
62 | fullyIdentifyCurrentUser();
|
---|
63 | }
|
---|
64 | query = query.forUser(JosmUserIdentityManager.getInstance().getUserId());
|
---|
65 | }
|
---|
66 | if (isCanceled())
|
---|
67 | return;
|
---|
68 | getProgressMonitor().indeterminateSubTask(tr("Query and download changesets ..."));
|
---|
69 | downloadedChangesets.addAll(reader.queryChangesets(query, getProgressMonitor().createSubTaskMonitor(0, false)));
|
---|
70 | } catch (OsmTransferCanceledException e) {
|
---|
71 | // thrown if user cancel the authentication dialog
|
---|
72 | setCanceled(true);
|
---|
73 | Main.trace(e);
|
---|
74 | } catch (OsmTransferException e) {
|
---|
75 | if (isCanceled())
|
---|
76 | return;
|
---|
77 | rememberLastException(e);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | protected void finish() {
|
---|
83 | rememberDownloadedData(downloadedChangesets);
|
---|
84 | if (isCanceled())
|
---|
85 | return;
|
---|
86 | if (lastException != null) {
|
---|
87 | GuiHelper.runInEDTAndWait(new Runnable() {
|
---|
88 | private final Component parent = progressMonitor != null ? progressMonitor.getWindowParent() : null;
|
---|
89 | @Override
|
---|
90 | public void run() {
|
---|
91 | JOptionPane.showMessageDialog(
|
---|
92 | parent != null ? parent : Main.parent,
|
---|
93 | ExceptionUtil.explainException(lastException),
|
---|
94 | tr("Errors during download"),
|
---|
95 | JOptionPane.ERROR_MESSAGE);
|
---|
96 | }
|
---|
97 | });
|
---|
98 | return;
|
---|
99 | }
|
---|
100 | updateChangesets();
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | protected void cancel() {
|
---|
105 | super.cancel();
|
---|
106 | synchronized (this) {
|
---|
107 | if (userInfoReader != null) {
|
---|
108 | userInfoReader.cancel();
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Creates the task.
|
---|
116 | *
|
---|
117 | * @param query the query to submit to the OSM server. Must not be null.
|
---|
118 | * @throws IllegalArgumentException if query is null.
|
---|
119 | */
|
---|
120 | public ChangesetQueryTask(ChangesetQuery query) {
|
---|
121 | this(Main.parent, query);
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Creates the task.
|
---|
126 | *
|
---|
127 | * @param parent the parent component relative to which the {@link org.openstreetmap.josm.gui.PleaseWaitDialog} is displayed.
|
---|
128 | * Must not be null.
|
---|
129 | * @param query the query to submit to the OSM server. Must not be null.
|
---|
130 | * @throws IllegalArgumentException if query is null.
|
---|
131 | * @throws IllegalArgumentException if parent is null
|
---|
132 | */
|
---|
133 | public ChangesetQueryTask(Component parent, ChangesetQuery query) {
|
---|
134 | CheckParameterUtil.ensureParameterNotNull(query, "query");
|
---|
135 | setDownloadTask(new DownloadTask(parent, query));
|
---|
136 | }
|
---|
137 | }
|
---|