source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/ChangesetQueryTask.java@ 10250

Last change on this file since 10250 was 10250, checked in by Don-vip, 8 years ago

findbugs - UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.downloadtasks;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.io.IOException;
8
9import javax.swing.JOptionPane;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.osm.UserInfo;
13import org.openstreetmap.josm.gui.JosmUserIdentityManager;
14import org.openstreetmap.josm.gui.util.GuiHelper;
15import org.openstreetmap.josm.io.ChangesetQuery;
16import org.openstreetmap.josm.io.OsmServerUserInfoReader;
17import org.openstreetmap.josm.io.OsmTransferCanceledException;
18import org.openstreetmap.josm.io.OsmTransferException;
19import org.openstreetmap.josm.tools.CheckParameterUtil;
20import org.openstreetmap.josm.tools.ExceptionUtil;
21import org.xml.sax.SAXException;
22
23/**
24 * Asynchronous task to send a changeset query to the OSM API.
25 * @since 2689
26 */
27public 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 } catch (OsmTransferException e) {
74 if (isCanceled())
75 return;
76 rememberLastException(e);
77 }
78 }
79
80 @Override
81 protected void finish() {
82 rememberDownloadedData(downloadedChangesets);
83 if (isCanceled())
84 return;
85 if (lastException != null) {
86 GuiHelper.runInEDTAndWait(new Runnable() {
87 private final Component parent = progressMonitor != null ? progressMonitor.getWindowParent() : null;
88 @Override
89 public void run() {
90 JOptionPane.showMessageDialog(
91 parent != null ? parent : Main.parent,
92 ExceptionUtil.explainException(lastException),
93 tr("Errors during download"),
94 JOptionPane.ERROR_MESSAGE);
95 }
96 });
97 return;
98 }
99 updateChangesets();
100 }
101
102 @Override
103 protected void cancel() {
104 super.cancel();
105 synchronized (this) {
106 if (userInfoReader != null) {
107 userInfoReader.cancel();
108 }
109 }
110 }
111 }
112
113 /**
114 * Creates the task.
115 *
116 * @param query the query to submit to the OSM server. Must not be null.
117 * @throws IllegalArgumentException if query is null.
118 */
119 public ChangesetQueryTask(ChangesetQuery query) {
120 this(Main.parent, query);
121 }
122
123 /**
124 * Creates the task.
125 *
126 * @param parent the parent component relative to which the {@link org.openstreetmap.josm.gui.PleaseWaitDialog} is displayed.
127 * Must not be null.
128 * @param query the query to submit to the OSM server. Must not be null.
129 * @throws IllegalArgumentException if query is null.
130 * @throws IllegalArgumentException if parent is null
131 */
132 public ChangesetQueryTask(Component parent, ChangesetQuery query) {
133 CheckParameterUtil.ensureParameterNotNull(query, "query");
134 setDownloadTask(new DownloadTask(parent, query));
135 }
136}
Note: See TracBrowser for help on using the repository browser.