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

Last change on this file since 13632 was 12743, checked in by Don-vip, 7 years ago

see #15229 - see #15182 - deprecate gui.JosmUserIdentityManager - replaced by data.UserIdentityManager

  • 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.UserIdentityManager;
13import org.openstreetmap.josm.data.osm.UserInfo;
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.openstreetmap.josm.tools.Logging;
22import org.xml.sax.SAXException;
23
24/**
25 * Asynchronous task to send a changeset query to the OSM API.
26 * @since 2689
27 */
28public 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 : Main.parent,
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(Main.parent, 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}
Note: See TracBrowser for help on using the repository browser.