source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/ChangesetHeaderDownloadTask.java@ 11186

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

findbugs - UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR

  • 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.actions.downloadtasks;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.io.IOException;
8import java.util.Collection;
9import java.util.Collections;
10import java.util.HashSet;
11import java.util.Set;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.osm.Changeset;
15import org.openstreetmap.josm.data.osm.ChangesetCache;
16import org.openstreetmap.josm.gui.ExceptionDialogUtil;
17import org.openstreetmap.josm.io.OsmTransferException;
18import org.openstreetmap.josm.tools.CheckParameterUtil;
19import org.xml.sax.SAXException;
20
21/**
22 * This is an asynchronous task for downloading a collection of changests from the OSM server.
23 *
24 * The task only downloads the changeset properties without the changeset content. It
25 * updates the global {@link ChangesetCache}.
26 * @since 2613
27 */
28public class ChangesetHeaderDownloadTask extends AbstractChangesetDownloadTask {
29
30 class DownloadTask extends RunnableDownloadTask {
31 /** the list of changeset ids to download */
32 private final Set<Integer> toDownload = new HashSet<>();
33 /** whether to include discussions or not */
34 private final boolean includeDiscussion;
35
36 DownloadTask(Component parent, Collection<Integer> ids, boolean includeDiscussion) {
37 super(parent, tr("Download changesets"));
38 this.includeDiscussion = includeDiscussion;
39 for (int id: ids != null ? ids : Collections.<Integer>emptyList()) {
40 if (id <= 0) {
41 continue;
42 }
43 toDownload.add(id);
44 }
45 }
46
47 @Override
48 protected void realRun() throws SAXException, IOException, OsmTransferException {
49 try {
50 downloadedChangesets.addAll(reader.readChangesets(toDownload, includeDiscussion,
51 getProgressMonitor().createSubTaskMonitor(0, false)));
52 } catch (OsmTransferException e) {
53 if (isCanceled())
54 // ignore exception if canceled
55 return;
56 // remember other exceptions
57 rememberLastException(e);
58 }
59 }
60
61 @Override
62 protected void finish() {
63 rememberDownloadedData(downloadedChangesets);
64 if (isCanceled())
65 return;
66 if (lastException != null) {
67 ExceptionDialogUtil.explainException(lastException);
68 }
69 updateChangesets();
70 }
71 }
72
73 /**
74 * Creates the download task for a collection of changeset ids. Uses a {@link org.openstreetmap.josm.gui.PleaseWaitDialog}
75 * whose parent is {@link Main#parent}.
76 *
77 * Null ids or or ids &lt;= 0 in the id collection are ignored.
78 *
79 * @param ids the collection of ids. Empty collection assumed if null.
80 */
81 public ChangesetHeaderDownloadTask(Collection<Integer> ids) {
82 this(Main.parent, ids, false);
83 }
84
85 /**
86 * Creates the download task for a collection of changeset ids. Uses a {@link org.openstreetmap.josm.gui.PleaseWaitDialog}
87 * whose parent is the parent window of <code>dialogParent</code>.
88 *
89 * Null ids or or ids &lt;= 0 in the id collection are ignored.
90 *
91 * @param dialogParent the parent reference component for the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}. Must not be null.
92 * @param ids the collection of ids. Empty collection assumed if null.
93 * @throws IllegalArgumentException if dialogParent is null
94 */
95 public ChangesetHeaderDownloadTask(Component dialogParent, Collection<Integer> ids) {
96 this(dialogParent, ids, false);
97 }
98
99 /**
100 * Creates the download task for a collection of changeset ids, with possibility to download changeset discussion.
101 * Uses a {@link org.openstreetmap.josm.gui.PleaseWaitDialog} whose parent is the parent window of <code>dialogParent</code>.
102 *
103 * Null ids or or ids &lt;= 0 in the id collection are ignored.
104 *
105 * @param dialogParent the parent reference component for the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}. Must not be null.
106 * @param ids the collection of ids. Empty collection assumed if null.
107 * @param includeDiscussion determines if discussion comments must be downloaded or not
108 * @throws IllegalArgumentException if dialogParent is null
109 * @since 7704
110 */
111 public ChangesetHeaderDownloadTask(Component dialogParent, Collection<Integer> ids, boolean includeDiscussion) {
112 setDownloadTask(new DownloadTask(dialogParent, ids, includeDiscussion));
113 }
114
115 /**
116 * Builds a download task from for a collection of changesets.
117 *
118 * Ignores null values and changesets with {@link Changeset#isNew()} == true.
119 *
120 * @param changesets the collection of changesets. Assumes an empty collection if null.
121 * @return the download task
122 */
123 public static ChangesetHeaderDownloadTask buildTaskForChangesets(Collection<Changeset> changesets) {
124 return buildTaskForChangesets(Main.parent, changesets);
125 }
126
127 /**
128 * Builds a download task from for a collection of changesets.
129 *
130 * Ignores null values and changesets with {@link Changeset#isNew()} == true.
131 *
132 * @param parent the parent component relative to which the {@link org.openstreetmap.josm.gui.PleaseWaitDialog} is displayed.
133 * Must not be null.
134 * @param changesets the collection of changesets. Assumes an empty collection if null.
135 * @return the download task
136 * @throws IllegalArgumentException if parent is null
137 */
138 public static ChangesetHeaderDownloadTask buildTaskForChangesets(Component parent, Collection<Changeset> changesets) {
139 CheckParameterUtil.ensureParameterNotNull(parent, "parent");
140
141 Set<Integer> ids = new HashSet<>();
142 for (Changeset cs: changesets != null ? changesets : Collections.<Changeset>emptyList()) {
143 if (cs == null || cs.isNew()) {
144 continue;
145 }
146 ids.add(cs.getId());
147 }
148 return new ChangesetHeaderDownloadTask(parent, ids);
149 }
150}
Note: See TracBrowser for help on using the repository browser.