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

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

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • 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.actions.downloadtasks;
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.List;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.osm.Changeset;
16import org.openstreetmap.josm.data.osm.ChangesetCache;
17import org.openstreetmap.josm.data.osm.ChangesetDataSet;
18import org.openstreetmap.josm.gui.ExceptionDialogUtil;
19import org.openstreetmap.josm.io.OsmTransferCanceledException;
20import org.openstreetmap.josm.io.OsmTransferException;
21import org.openstreetmap.josm.tools.Logging;
22import org.xml.sax.SAXException;
23
24/**
25 * This is an asynchronous task for downloading the changeset content of a collection of changesets.
26 * @since 2689
27 */
28public class ChangesetContentDownloadTask extends AbstractChangesetDownloadTask {
29
30 class DownloadTask extends RunnableDownloadTask {
31 /** the list of changeset ids to download */
32 private final List<Integer> toDownload = new ArrayList<>();
33
34 DownloadTask(Component parent, Collection<Integer> ids) {
35 super(parent, tr("Downloading changeset content"));
36 for (Integer id: ids != null ? ids : Collections.<Integer>emptyList()) {
37 if (id == null || id <= 0) {
38 continue;
39 }
40 toDownload.add(id);
41 }
42 }
43
44 /**
45 * Downloads the changeset with id <code>changesetId</code> (only "header" information, no content)
46 *
47 * @param changesetId the changeset id
48 * @throws OsmTransferException if something went wrong
49 */
50 protected void downloadChangeset(int changesetId) throws OsmTransferException {
51 Changeset cs = reader.readChangeset(changesetId, false, getProgressMonitor().createSubTaskMonitor(0, false));
52 ChangesetCache.getInstance().update(cs);
53 }
54
55 @Override
56 protected void realRun() throws SAXException, IOException, OsmTransferException {
57 try {
58 getProgressMonitor().setTicksCount(toDownload.size());
59 int i = 0;
60 for (int id: toDownload) {
61 i++;
62 if (!isAvailableLocally(id)) {
63 getProgressMonitor().setCustomText(tr("({0}/{1}) Downloading changeset {2}...", i, toDownload.size(), id));
64 downloadChangeset(id);
65 }
66 if (isCanceled())
67 return;
68 getProgressMonitor().setCustomText(tr("({0}/{1}) Downloading content for changeset {2}...", i, toDownload.size(), id));
69 ChangesetDataSet ds = reader.downloadChangeset(id, getProgressMonitor().createSubTaskMonitor(0, false));
70 Changeset cs = ChangesetCache.getInstance().get(id);
71 cs.setContent(ds);
72 ChangesetCache.getInstance().update(cs);
73 downloadedChangesets.add(cs);
74 getProgressMonitor().worked(1);
75 }
76 } catch (OsmTransferCanceledException e) {
77 // the download was canceled by the user. This exception is caught if the user canceled the authentication dialog.
78 setCanceled(true);
79 Logging.trace(e);
80 return;
81 } catch (OsmTransferException e) {
82 if (isCanceled())
83 return;
84 rememberLastException(e);
85 }
86 }
87
88 @Override
89 protected void finish() {
90 rememberDownloadedData(downloadedChangesets);
91 if (isCanceled())
92 return;
93 if (lastException != null) {
94 ExceptionDialogUtil.explainException(lastException);
95 }
96 }
97 }
98
99 /**
100 * Creates a download task for a single changeset
101 *
102 * @param changesetId the changeset id. &gt; 0 required.
103 * @throws IllegalArgumentException if changesetId &lt;= 0
104 */
105 public ChangesetContentDownloadTask(int changesetId) {
106 this(Main.parent, changesetId);
107 }
108
109 /**
110 * Creates a download task for a collection of changesets. null values and id &lt;=0 in
111 * the collection are silently discarded.
112 *
113 * @param changesetIds the changeset ids. Empty collection assumed, if null.
114 */
115 public ChangesetContentDownloadTask(Collection<Integer> changesetIds) {
116 this(Main.parent, changesetIds);
117 }
118
119 /**
120 * Creates a download task for a single changeset
121 *
122 * @param parent the parent component for the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}. Must not be {@code null}.
123 * @param changesetId the changeset id. {@code >0} required.
124 * @throws IllegalArgumentException if {@code changesetId <= 0}
125 * @throws IllegalArgumentException if parent is {@code null}
126 */
127 public ChangesetContentDownloadTask(Component parent, int changesetId) {
128 if (changesetId <= 0)
129 throw new IllegalArgumentException(
130 MessageFormat.format("Expected integer value > 0 for parameter ''{0}'', got ''{1}''", "changesetId", changesetId));
131 setDownloadTask(new DownloadTask(parent, Collections.singleton(changesetId)));
132 }
133
134 /**
135 * Creates a download task for a collection of changesets. null values and id &lt;=0 in
136 * the collection are sillently discarded.
137 *
138 * @param parent the parent component for the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}. Must not be {@code null}.
139 * @param changesetIds the changeset ids. Empty collection assumed, if {@code null}.
140 * @throws IllegalArgumentException if parent is {@code null}
141 */
142 public ChangesetContentDownloadTask(Component parent, Collection<Integer> changesetIds) {
143 setDownloadTask(new DownloadTask(parent, changesetIds));
144 }
145
146 /**
147 * Replies true if the local {@link ChangesetCache} already includes the changeset with
148 * id <code>changesetId</code>.
149 *
150 * @param changesetId the changeset id
151 * @return true if the local {@link ChangesetCache} already includes the changeset with
152 * id <code>changesetId</code>
153 */
154 protected static boolean isAvailableLocally(int changesetId) {
155 return ChangesetCache.getInstance().get(changesetId) != null;
156 }
157}
Note: See TracBrowser for help on using the repository browser.