source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetHeaderDownloadTask.java@ 7730

Last change on this file since 7730 was 7704, checked in by Don-vip, 9 years ago

see #10701 - parsing support of changeset discussions

  • Property svn:eol-style set to native
File size: 8.5 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.lang.reflect.InvocationTargetException;
9import java.util.Collection;
10import java.util.Collections;
11import java.util.HashSet;
12import java.util.Set;
13
14import javax.swing.SwingUtilities;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.data.osm.Changeset;
18import org.openstreetmap.josm.data.osm.ChangesetCache;
19import org.openstreetmap.josm.gui.ExceptionDialogUtil;
20import org.openstreetmap.josm.gui.PleaseWaitRunnable;
21import org.openstreetmap.josm.io.OsmServerChangesetReader;
22import org.openstreetmap.josm.io.OsmTransferException;
23import org.openstreetmap.josm.tools.BugReportExceptionHandler;
24import org.openstreetmap.josm.tools.CheckParameterUtil;
25import org.openstreetmap.josm.tools.ExceptionUtil;
26import org.xml.sax.SAXException;
27
28/**
29 * This is an asynchronous task for downloading a collection of changests from the OSM
30 * server.
31 *
32 * The task only downloads the changeset properties without the changeset content. It
33 * updates the global {@link ChangesetCache}.
34 *
35 */
36public class ChangesetHeaderDownloadTask extends PleaseWaitRunnable implements ChangesetDownloadTask{
37
38 /**
39 * Builds a download task from for a collection of changesets.
40 *
41 * Ignores null values and changesets with {@link Changeset#isNew()} == true.
42 *
43 * @param changesets the collection of changesets. Assumes an empty collection if null.
44 * @return the download task
45 */
46 public static ChangesetHeaderDownloadTask buildTaskForChangesets(Collection<Changeset> changesets) {
47 return buildTaskForChangesets(Main.parent, changesets);
48 }
49
50 /**
51 * Builds a download task from for a collection of changesets.
52 *
53 * Ignores null values and changesets with {@link Changeset#isNew()} == true.
54 *
55 * @param parent the parent component relative to which the {@link org.openstreetmap.josm.gui.PleaseWaitDialog} is displayed.
56 * Must not be null.
57 * @param changesets the collection of changesets. Assumes an empty collection if null.
58 * @return the download task
59 * @throws IllegalArgumentException thrown if parent is null
60 */
61 public static ChangesetHeaderDownloadTask buildTaskForChangesets(Component parent, Collection<Changeset> changesets) {
62 CheckParameterUtil.ensureParameterNotNull(parent, "parent");
63 if (changesets == null) {
64 changesets = Collections.emptyList();
65 }
66
67 HashSet<Integer> ids = new HashSet<>();
68 for (Changeset cs: changesets) {
69 if (cs == null || cs.isNew()) {
70 continue;
71 }
72 ids.add(cs.getId());
73 }
74 if (parent == null)
75 return new ChangesetHeaderDownloadTask(ids);
76 else
77 return new ChangesetHeaderDownloadTask(parent, ids);
78
79 }
80
81 private Set<Integer> idsToDownload;
82 private OsmServerChangesetReader reader;
83 private boolean canceled;
84 private Exception lastException;
85 private Set<Changeset> downloadedChangesets;
86 private final boolean includeDiscussion;
87
88 protected void init(Collection<Integer> ids) {
89 if (ids == null) {
90 ids = Collections.emptyList();
91 }
92 idsToDownload = new HashSet<>();
93 if (ids == null || ids.isEmpty())
94 return;
95 for (int id: ids) {
96 if (id <= 0) {
97 continue;
98 }
99 idsToDownload.add(id);
100 }
101 }
102
103 /**
104 * Creates the download task for a collection of changeset ids. Uses a {@link org.openstreetmap.josm.gui.PleaseWaitDialog}
105 * whose parent is {@link Main#parent}.
106 *
107 * Null ids or or ids &lt;= 0 in the id collection are ignored.
108 *
109 * @param ids the collection of ids. Empty collection assumed if null.
110 */
111 public ChangesetHeaderDownloadTask(Collection<Integer> ids) {
112 // parent for dialog is Main.parent
113 super(tr("Download changesets"), false /* don't ignore exceptions */);
114 init(ids);
115 this.includeDiscussion = false;
116 }
117
118 /**
119 * Creates the download task for a collection of changeset ids. Uses a {@link org.openstreetmap.josm.gui.PleaseWaitDialog}
120 * whose parent is the parent window of <code>dialogParent</code>.
121 *
122 * Null ids or or ids &lt;= 0 in the id collection are ignored.
123 *
124 * @param dialogParent the parent reference component for the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}. Must not be null.
125 * @param ids the collection of ids. Empty collection assumed if null.
126 * @throws IllegalArgumentException thrown if dialogParent is null
127 */
128 public ChangesetHeaderDownloadTask(Component dialogParent, Collection<Integer> ids) throws IllegalArgumentException{
129 this(dialogParent, ids, false);
130 }
131
132 /**
133 * Creates the download task for a collection of changeset ids, with possibility to download changeset discussion.
134 * Uses a {@link org.openstreetmap.josm.gui.PleaseWaitDialog} whose parent is the parent window of <code>dialogParent</code>.
135 *
136 * Null ids or or ids &lt;= 0 in the id collection are ignored.
137 *
138 * @param dialogParent the parent reference component for the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}. Must not be null.
139 * @param ids the collection of ids. Empty collection assumed if null.
140 * @param includeDiscussion determines if discussion comments must be downloaded or not
141 * @throws IllegalArgumentException thrown if dialogParent is null
142 * @since 7704
143 */
144 public ChangesetHeaderDownloadTask(Component dialogParent, Collection<Integer> ids, boolean includeDiscussion)
145 throws IllegalArgumentException {
146 super(dialogParent, tr("Download changesets"), false /* don't ignore exceptions */);
147 init(ids);
148 this.includeDiscussion = includeDiscussion;
149 }
150
151 @Override
152 protected void cancel() {
153 canceled = true;
154 synchronized (this) {
155 if (reader != null) {
156 reader.cancel();
157 }
158 }
159 }
160
161 @Override
162 protected void finish() {
163 if (canceled)
164 return;
165 if (lastException != null) {
166 ExceptionDialogUtil.explainException(lastException);
167 }
168 Runnable r = new Runnable() {
169 @Override
170 public void run() {
171 ChangesetCache.getInstance().update(downloadedChangesets);
172 }
173 };
174
175 if (SwingUtilities.isEventDispatchThread()) {
176 r.run();
177 } else {
178 try {
179 SwingUtilities.invokeAndWait(r);
180 } catch(InterruptedException e) {
181 Main.warn("InterruptedException in "+getClass().getSimpleName()+" while updating changeset cache");
182 } catch(InvocationTargetException e) {
183 Throwable t = e.getTargetException();
184 if (t instanceof RuntimeException) {
185 BugReportExceptionHandler.handleException(t);
186 } else if (t instanceof Exception){
187 ExceptionUtil.explainException(e);
188 } else {
189 BugReportExceptionHandler.handleException(t);
190 }
191 }
192 }
193 }
194
195 @Override
196 protected void realRun() throws SAXException, IOException, OsmTransferException {
197 try {
198 synchronized (this) {
199 reader = new OsmServerChangesetReader();
200 }
201 downloadedChangesets = new HashSet<>();
202 downloadedChangesets.addAll(reader.readChangesets(idsToDownload, includeDiscussion,
203 getProgressMonitor().createSubTaskMonitor(0, false)));
204 } catch(OsmTransferException e) {
205 if (canceled)
206 // ignore exception if canceled
207 return;
208 // remember other exceptions
209 lastException = e;
210 }
211 }
212
213 /* ------------------------------------------------------------------------------- */
214 /* interface ChangesetDownloadTask */
215 /* ------------------------------------------------------------------------------- */
216 @Override
217 public Set<Changeset> getDownloadedChangesets() {
218 return downloadedChangesets;
219 }
220
221 @Override
222 public boolean isCanceled() {
223 return canceled;
224 }
225
226 @Override
227 public boolean isFailed() {
228 return lastException != null;
229 }
230}
Note: See TracBrowser for help on using the repository browser.