source: josm/trunk/src/org/openstreetmap/josm/gui/io/DownloadOpenChangesetsTask.java@ 11457

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

add more unit tests

  • Property svn:eol-style set to native
File size: 6.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.GraphicsEnvironment;
8import java.io.IOException;
9import java.util.Collections;
10import java.util.List;
11
12import javax.swing.JOptionPane;
13import javax.swing.SwingUtilities;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.osm.Changeset;
17import org.openstreetmap.josm.data.osm.ChangesetCache;
18import org.openstreetmap.josm.data.osm.UserInfo;
19import org.openstreetmap.josm.gui.ExceptionDialogUtil;
20import org.openstreetmap.josm.gui.JosmUserIdentityManager;
21import org.openstreetmap.josm.gui.PleaseWaitRunnable;
22import org.openstreetmap.josm.gui.util.GuiHelper;
23import org.openstreetmap.josm.io.ChangesetQuery;
24import org.openstreetmap.josm.io.OsmServerChangesetReader;
25import org.openstreetmap.josm.io.OsmServerUserInfoReader;
26import org.openstreetmap.josm.io.OsmTransferException;
27import org.xml.sax.SAXException;
28
29/**
30 * This is a task for downloading the open changesets of the current user
31 * from the OSM server.
32 */
33public class DownloadOpenChangesetsTask extends PleaseWaitRunnable {
34
35 private boolean canceled;
36 private OsmServerChangesetReader reader;
37 private List<Changeset> changesets;
38 private Exception lastException;
39 private final Component parent;
40
41 /**
42 * Constructs the task
43 * @param parent is a component to show error messages
44 */
45 public DownloadOpenChangesetsTask(Component parent) {
46 super(parent, tr("Downloading open changesets ..."), false /* don't ignore exceptions */);
47 this.parent = parent;
48 }
49
50 @Override
51 protected void cancel() {
52 this.canceled = true;
53 synchronized (this) {
54 if (reader != null) {
55 reader.cancel();
56 }
57 }
58 }
59
60 @Override
61 protected void finish() {
62 if (JosmUserIdentityManager.getInstance().isAnonymous()) {
63 String msg = tr("Could not retrieve the list of your open changesets because<br>"
64 + "JOSM does not know your identity.<br>"
65 + "You have either chosen to work anonymously or you are not entitled<br>"
66 + "to know the identity of the user on whose behalf you are working.");
67 Main.warn(msg);
68 if (!GraphicsEnvironment.isHeadless()) {
69 JOptionPane.showMessageDialog(GuiHelper.getFrameForComponent(parent),
70 "<html>" + msg + "</html>", tr("Missing user identity"), JOptionPane.ERROR_MESSAGE);
71 }
72 return;
73 }
74 if (canceled) return;
75 if (lastException != null) {
76 ExceptionDialogUtil.explainException(lastException);
77 return;
78 }
79 if (changesets.isEmpty()) {
80 JOptionPane.showMessageDialog(
81 Main.parent,
82 tr("There are no open changesets"),
83 tr("No open changesets"),
84 JOptionPane.INFORMATION_MESSAGE
85 );
86 return;
87 }
88 SwingUtilities.invokeLater(() -> ChangesetCache.getInstance().update(changesets));
89 }
90
91 /**
92 * Refreshes the user info from the server. This is necessary if we don't know the users id yet.
93 */
94 protected void refreshUserIdentity() {
95 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
96 try {
97 OsmServerUserInfoReader infoReader = new OsmServerUserInfoReader();
98 UserInfo info = infoReader.fetchUserInfo(getProgressMonitor().createSubTaskMonitor(1, false));
99 im.setFullyIdentified(info.getDisplayName(), info);
100 } catch (OsmTransferException e) {
101 // retrieving the user info can fail if the current user is not authorised to
102 // retrieve it, i.e. if he is working with an OAuth Access Token which doesn't
103 // have the respective privileges or if he didn't or he can't authenticate with
104 // a username/password-pair.
105 //
106 // Downgrade your knowlege about its identity if we've assumed that he was fully
107 // identified. Otherwise, if he is anonymous or partially identified, keep our level
108 // of knowlege.
109 //
110 if (im.isFullyIdentified()) {
111 im.setPartiallyIdentified(im.getUserName());
112 }
113 Main.warn(e, tr("Failed to retrieve user infos for the current JOSM user. Exception was: {0}", e.toString()));
114 }
115 }
116
117 @Override
118 protected void realRun() throws SAXException, IOException, OsmTransferException {
119 try {
120 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
121 if (im.isAnonymous()) {
122 refreshUserIdentity();
123 } else if (im.isFullyIdentified()) {
124 // do nothing
125 } else if (im.isPartiallyIdentified()) {
126 refreshUserIdentity();
127 }
128 if (canceled) return;
129 synchronized (this) {
130 reader = new OsmServerChangesetReader();
131 }
132 ChangesetQuery query = new ChangesetQuery().beingOpen(true);
133 if (im.isAnonymous())
134 // we still don't know anything about the current user. Can't retrieve
135 // its changesets
136 return;
137 else if (im.isFullyIdentified()) {
138 query = query.forUser(im.getUserId());
139 } else {
140 // we only know the users name, not its id. Nevermind, try to read
141 // its open changesets anyway.
142 //
143 query = query.forUser(im.getUserName());
144 }
145 changesets = reader.queryChangesets(
146 query,
147 getProgressMonitor().createSubTaskMonitor(1, false /* not internal */)
148 );
149 } catch (OsmTransferException e) {
150 if (canceled)
151 return;
152 lastException = e;
153 }
154 }
155
156 /**
157 * Determines if this task has been cancelled.
158 * @return {@code true} if this task has been cancelled
159 */
160 public boolean isCanceled() {
161 return canceled;
162 }
163
164 /**
165 * Returns the changesets.
166 * @return the changesets, or {@code null}
167 * @since 11110
168 */
169 public final List<Changeset> getChangesets() {
170 return changesets != null ? Collections.unmodifiableList(changesets) : null;
171 }
172}
Note: See TracBrowser for help on using the repository browser.