source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetContentDownloadTask.java@ 6084

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • Property svn:eol-style set to native
File size: 8.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.changeset;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.io.IOException;
8import java.text.MessageFormat;
9import java.util.ArrayList;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.HashSet;
13import java.util.List;
14import java.util.Set;
15
16import org.openstreetmap.josm.data.osm.Changeset;
17import org.openstreetmap.josm.data.osm.ChangesetCache;
18import org.openstreetmap.josm.data.osm.ChangesetDataSet;
19import org.openstreetmap.josm.gui.ExceptionDialogUtil;
20import org.openstreetmap.josm.gui.PleaseWaitRunnable;
21import org.openstreetmap.josm.io.OsmServerChangesetReader;
22import org.openstreetmap.josm.io.OsmTransferCanceledException;
23import org.openstreetmap.josm.io.OsmTransferException;
24import org.xml.sax.SAXException;
25
26/**
27 * This is an asynchronous task for downloading the changeset content of a collection of
28 * changesets.
29 *
30 */
31public class ChangesetContentDownloadTask extends PleaseWaitRunnable implements ChangesetDownloadTask{
32
33 /** the list of changeset ids to download */
34 private final List<Integer> toDownload = new ArrayList<Integer>();
35 /** true if the task was canceled */
36 private boolean canceled;
37 /** keeps the last exception thrown in the task, if any */
38 private Exception lastException;
39 /** the reader object used to read changesets from the API */
40 private OsmServerChangesetReader reader;
41 /** the set of downloaded changesets */
42 private Set<Changeset> downloadedChangesets;
43
44 /**
45 * Initialize the task with a collection of changeset ids to download
46 *
47 * @param ids the collection of ids. May be null.
48 */
49 protected void init(Collection<Integer> ids) {
50 if (ids == null) {
51 ids = Collections.emptyList();
52 }
53 for (Integer id: ids) {
54 if (id == null || id <= 0) {
55 continue;
56 }
57 toDownload.add(id);
58 }
59 downloadedChangesets = new HashSet<Changeset>();
60 }
61
62 /**
63 * Creates a download task for a single changeset
64 *
65 * @param changesetId the changeset id. >0 required.
66 * @throws IllegalArgumentException thrown if changesetId <= 0
67 */
68 public ChangesetContentDownloadTask(int changesetId) throws IllegalArgumentException{
69 super(tr("Downloading changeset content"), false /* don't ignore exceptions */);
70 if (changesetId <= 0)
71 throw new IllegalArgumentException(MessageFormat.format("Expected integer value > 0 for parameter ''{0}'', got ''{1}''", "changesetId", changesetId));
72 init(Collections.singleton(changesetId));
73 }
74
75 /**
76 * Creates a download task for a collection of changesets. null values and id <=0 in
77 * the collection are sillently discarded.
78 *
79 * @param changesetIds the changeset ids. Empty collection assumed, if null.
80 */
81 public ChangesetContentDownloadTask(Collection<Integer> changesetIds) {
82 super(tr("Downloading changeset content"), false /* don't ignore exceptions */);
83 init(changesetIds);
84 }
85
86 /**
87 * Creates a download task for a single changeset
88 *
89 * @param parent the parent component for the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}. Must not be {@code null}.
90 * @param changesetId the changeset id. {@code >0} required.
91 * @throws IllegalArgumentException thrown if {@code changesetId <= 0}
92 * @throws IllegalArgumentException thrown if parent is {@code null}
93 */
94 public ChangesetContentDownloadTask(Component parent, int changesetId) throws IllegalArgumentException{
95 super(parent, tr("Downloading changeset content"), false /* don't ignore exceptions */);
96 if (changesetId <= 0)
97 throw new IllegalArgumentException(MessageFormat.format("Expected integer value > 0 for parameter ''{0}'', got ''{1}''", "changesetId", changesetId));
98 init(Collections.singleton(changesetId));
99 }
100
101 /**
102 * Creates a download task for a collection of changesets. null values and id <=0 in
103 * the collection are sillently discarded.
104 *
105 * @param parent the parent component for the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}. Must not be {@code null}.
106 * @param changesetIds the changeset ids. Empty collection assumed, if {@code null}.
107 * @throws IllegalArgumentException thrown if parent is {@code null}
108 */
109 public ChangesetContentDownloadTask(Component parent, Collection<Integer> changesetIds) throws IllegalArgumentException {
110 super(parent, tr("Downloading changeset content"), false /* don't ignore exceptions */);
111 init(changesetIds);
112 }
113
114 /**
115 * Replies true if the local {@link ChangesetCache} already includes the changeset with
116 * id <code>changesetId</code>.
117 *
118 * @param changesetId the changeset id
119 * @return true if the local {@link ChangesetCache} already includes the changeset with
120 * id <code>changesetId</code>
121 */
122 protected boolean isAvailableLocally(int changesetId) {
123 return ChangesetCache.getInstance().get(changesetId) != null;
124 }
125
126 /**
127 * Downloads the changeset with id <code>changesetId</code> (only "header"
128 * information, no content)
129 *
130 * @param changesetId the changeset id
131 * @throws OsmTransferException thrown if something went wrong
132 */
133 protected void downloadChangeset(int changesetId) throws OsmTransferException {
134 synchronized(this) {
135 reader = new OsmServerChangesetReader();
136 }
137 Changeset cs = reader.readChangeset(changesetId, getProgressMonitor().createSubTaskMonitor(0, false));
138 synchronized(this) {
139 reader = null;
140 }
141 ChangesetCache.getInstance().update(cs);
142 }
143
144 @Override
145 protected void cancel() {
146 canceled = true;
147 synchronized (this) {
148 if (reader != null) {
149 reader.cancel();
150 }
151 }
152 }
153
154 @Override
155 protected void finish() {
156 if (canceled) return;
157 if (lastException != null) {
158 ExceptionDialogUtil.explainException(lastException);
159 }
160 }
161
162 @Override
163 protected void realRun() throws SAXException, IOException, OsmTransferException {
164 try {
165 getProgressMonitor().setTicksCount(toDownload.size());
166 int i=0;
167 for (int id: toDownload) {
168 i++;
169 if (!isAvailableLocally(id)) {
170 getProgressMonitor().setCustomText(tr("({0}/{1}) Downloading changeset {2}...", i, toDownload.size(), id));
171 downloadChangeset(id);
172 }
173 if (canceled) return;
174 synchronized(this) {
175 reader = new OsmServerChangesetReader();
176 }
177 getProgressMonitor().setCustomText(tr("({0}/{1}) Downloading content for changeset {2}...", i, toDownload.size(), id));
178 ChangesetDataSet ds = reader.downloadChangeset(id, getProgressMonitor().createSubTaskMonitor(0, false));
179 synchronized(this) {
180 reader = null;
181 }
182 Changeset cs = ChangesetCache.getInstance().get(id);
183 cs.setContent(ds);
184 ChangesetCache.getInstance().update(cs);
185 downloadedChangesets.add(cs);
186 getProgressMonitor().worked(1);
187 }
188 } catch(OsmTransferCanceledException e) {
189 // the download was canceled by the user. This exception is caught if the
190 // user canceled the authentication dialog.
191 //
192 canceled = true;
193 return;
194 } catch(OsmTransferException e) {
195 if (canceled)
196 return;
197 lastException = e;
198 } catch(RuntimeException e) {
199 throw e;
200 }
201 }
202
203 /* ------------------------------------------------------------------------------- */
204 /* interface ChangesetDownloadTask */
205 /* ------------------------------------------------------------------------------- */
206 @Override
207 public Set<Changeset> getDownloadedChangesets() {
208 return downloadedChangesets;
209 }
210
211 @Override
212 public boolean isCanceled() {
213 return canceled;
214 }
215
216 @Override
217 public boolean isFailed() {
218 return lastException != null;
219 }
220}
Note: See TracBrowser for help on using the repository browser.