1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.changeset.query;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.lang.reflect.InvocationTargetException;
|
---|
9 | import java.util.HashSet;
|
---|
10 | import java.util.Set;
|
---|
11 |
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 | import javax.swing.SwingUtilities;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
17 | import org.openstreetmap.josm.data.osm.ChangesetCache;
|
---|
18 | import org.openstreetmap.josm.data.osm.UserInfo;
|
---|
19 | import org.openstreetmap.josm.gui.JosmUserIdentityManager;
|
---|
20 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
21 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetDownloadTask;
|
---|
22 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
23 | import org.openstreetmap.josm.io.ChangesetQuery;
|
---|
24 | import org.openstreetmap.josm.io.OsmServerChangesetReader;
|
---|
25 | import org.openstreetmap.josm.io.OsmServerUserInfoReader;
|
---|
26 | import org.openstreetmap.josm.io.OsmTransferCanceledException;
|
---|
27 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
28 | import org.openstreetmap.josm.tools.BugReportExceptionHandler;
|
---|
29 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
30 | import org.openstreetmap.josm.tools.ExceptionUtil;
|
---|
31 | import org.xml.sax.SAXException;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Asynchronous task to send a changeset query to the OSM API.
|
---|
35 | *
|
---|
36 | */
|
---|
37 | public class ChangesetQueryTask extends PleaseWaitRunnable implements ChangesetDownloadTask{
|
---|
38 |
|
---|
39 | /** the changeset query */
|
---|
40 | private ChangesetQuery query;
|
---|
41 | /** true if the task was canceled */
|
---|
42 | private boolean canceled;
|
---|
43 | /** the set of downloaded changesets */
|
---|
44 | private Set<Changeset> downloadedChangesets;
|
---|
45 | /** the last exception remembered, if any */
|
---|
46 | private Exception lastException;
|
---|
47 | /** the reader object used to read information about the current user from the API */
|
---|
48 | private OsmServerUserInfoReader userInfoReader;
|
---|
49 | /** the reader object used to submit the changeset query to the API */
|
---|
50 | private OsmServerChangesetReader changesetReader;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Creates the task.
|
---|
54 | *
|
---|
55 | * @param query the query to submit to the OSM server. Must not be null.
|
---|
56 | * @throws IllegalArgumentException thrown if query is null.
|
---|
57 | */
|
---|
58 | public ChangesetQueryTask(ChangesetQuery query) throws IllegalArgumentException {
|
---|
59 | super(tr("Querying and downloading changesets",false /* don't ignore exceptions */));
|
---|
60 | CheckParameterUtil.ensureParameterNotNull(query, "query");
|
---|
61 | this.query = query;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Creates the task.
|
---|
66 | *
|
---|
67 | * @param parent the parent component relative to which the {@link PleaseWaitDialog} is displayed.
|
---|
68 | * Must not be null.
|
---|
69 | * @param query the query to submit to the OSM server. Must not be null.
|
---|
70 | * @throws IllegalArgumentException thrown if query is null.
|
---|
71 | * @throws IllegalArgumentException thrown if parent is null
|
---|
72 | */
|
---|
73 | public ChangesetQueryTask(Component parent, ChangesetQuery query) throws IllegalArgumentException {
|
---|
74 | super(parent, tr("Querying and downloading changesets"), false /* don't ignore exceptions */);
|
---|
75 | CheckParameterUtil.ensureParameterNotNull(query, "query");
|
---|
76 | this.query = query;
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | protected void cancel() {
|
---|
81 | canceled = true;
|
---|
82 | synchronized(this) {
|
---|
83 | if (userInfoReader != null) {
|
---|
84 | userInfoReader.cancel();
|
---|
85 | }
|
---|
86 | }
|
---|
87 | synchronized(this) {
|
---|
88 | if (changesetReader != null) {
|
---|
89 | changesetReader.cancel();
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Override
|
---|
95 | protected void finish() {
|
---|
96 | if (canceled) return;
|
---|
97 | if (lastException != null) {
|
---|
98 | GuiHelper.runInEDTAndWait(new Runnable() {
|
---|
99 | private final Component parent = progressMonitor != null ? progressMonitor.getWindowParent() : null;
|
---|
100 | @Override
|
---|
101 | public void run() {
|
---|
102 | JOptionPane.showMessageDialog(
|
---|
103 | parent != null ? parent : Main.parent,
|
---|
104 | ExceptionUtil.explainException(lastException),
|
---|
105 | tr("Errors during download"),
|
---|
106 | JOptionPane.ERROR_MESSAGE);
|
---|
107 | }
|
---|
108 | });
|
---|
109 | return;
|
---|
110 | }
|
---|
111 |
|
---|
112 | // update the global changeset cache with the downloaded changesets;
|
---|
113 | // this will trigger change events which views are listening to. They
|
---|
114 | // will update their views accordingly.
|
---|
115 | //
|
---|
116 | // Run on the EDT because UI updates are triggered.
|
---|
117 | //
|
---|
118 | Runnable r = new Runnable() {
|
---|
119 | @Override public void run() {
|
---|
120 | ChangesetCache.getInstance().update(downloadedChangesets);
|
---|
121 | }
|
---|
122 | };
|
---|
123 | if (SwingUtilities.isEventDispatchThread()) {
|
---|
124 | r.run();
|
---|
125 | } else {
|
---|
126 | try {
|
---|
127 | SwingUtilities.invokeAndWait(r);
|
---|
128 | } catch(InterruptedException e) {
|
---|
129 | e.printStackTrace();
|
---|
130 | } catch(InvocationTargetException e) {
|
---|
131 | Throwable t = e.getTargetException();
|
---|
132 | if (t instanceof RuntimeException) {
|
---|
133 | BugReportExceptionHandler.handleException(t);
|
---|
134 | } else if (t instanceof Exception){
|
---|
135 | ExceptionUtil.explainException(e);
|
---|
136 | } else {
|
---|
137 | BugReportExceptionHandler.handleException(t);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Tries to fully identify the current JOSM user
|
---|
145 | *
|
---|
146 | * @throws OsmTransferException thrown if something went wrong
|
---|
147 | */
|
---|
148 | protected void fullyIdentifyCurrentUser() throws OsmTransferException {
|
---|
149 | getProgressMonitor().indeterminateSubTask(tr("Determine user id for current user..."));
|
---|
150 |
|
---|
151 | synchronized(this) {
|
---|
152 | userInfoReader = new OsmServerUserInfoReader();
|
---|
153 | }
|
---|
154 | UserInfo info = userInfoReader.fetchUserInfo(getProgressMonitor().createSubTaskMonitor(1,false));
|
---|
155 | synchronized(this) {
|
---|
156 | userInfoReader = null;
|
---|
157 | }
|
---|
158 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
|
---|
159 | im.setFullyIdentified(im.getUserName(), info);
|
---|
160 | }
|
---|
161 |
|
---|
162 | @Override
|
---|
163 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
164 | try {
|
---|
165 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
|
---|
166 | if (query.isRestrictedToPartiallyIdentifiedUser() && im.isCurrentUser(query.getUserName())) {
|
---|
167 | // if we query changesets for the current user, make sure we query against
|
---|
168 | // its user id, not its user name. If necessary, determine the user id
|
---|
169 | // first.
|
---|
170 | //
|
---|
171 | if (im.isPartiallyIdentified() ) {
|
---|
172 | fullyIdentifyCurrentUser();
|
---|
173 | }
|
---|
174 | query = query.forUser(JosmUserIdentityManager.getInstance().getUserId());
|
---|
175 | }
|
---|
176 | if (canceled) return;
|
---|
177 | getProgressMonitor().indeterminateSubTask(tr("Query and download changesets ..."));
|
---|
178 | synchronized(this) {
|
---|
179 | changesetReader= new OsmServerChangesetReader();
|
---|
180 | }
|
---|
181 | downloadedChangesets = new HashSet<Changeset>();
|
---|
182 | downloadedChangesets.addAll(changesetReader.queryChangesets(query, getProgressMonitor().createSubTaskMonitor(0, false)));
|
---|
183 | synchronized (this) {
|
---|
184 | changesetReader = null;
|
---|
185 | }
|
---|
186 | } catch(OsmTransferCanceledException e) {
|
---|
187 | // thrown if user cancel the authentication dialog
|
---|
188 | canceled = true;
|
---|
189 | } catch(OsmTransferException e) {
|
---|
190 | if (canceled)
|
---|
191 | return;
|
---|
192 | this.lastException = e;
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | /* ------------------------------------------------------------------------------- */
|
---|
197 | /* interface ChangesetDownloadTask */
|
---|
198 | /* ------------------------------------------------------------------------------- */
|
---|
199 | @Override
|
---|
200 | public Set<Changeset> getDownloadedChangesets() {
|
---|
201 | return downloadedChangesets;
|
---|
202 | }
|
---|
203 |
|
---|
204 | @Override
|
---|
205 | public boolean isCanceled() {
|
---|
206 | return canceled;
|
---|
207 | }
|
---|
208 |
|
---|
209 | @Override
|
---|
210 | public boolean isFailed() {
|
---|
211 | return lastException != null;
|
---|
212 | }
|
---|
213 | }
|
---|