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