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

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

sonar - Immutable Field

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