source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/DownloadChangesetsTask.java@ 2613

Last change on this file since 2613 was 2613, checked in by Gubaer, 17 years ago

new: global in-memory cache for downloaded changesets
new: toggle dialog for changesets
new: downloading of changesets (currently without changeset content, will follow later)

File size: 2.7 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.io.IOException;
7import java.util.Collection;
8import java.util.HashSet;
9import java.util.List;
10import java.util.Set;
11
12import javax.swing.SwingUtilities;
13
14import org.openstreetmap.josm.data.osm.Changeset;
15import org.openstreetmap.josm.data.osm.ChangesetCache;
16import org.openstreetmap.josm.gui.ExceptionDialogUtil;
17import org.openstreetmap.josm.gui.PleaseWaitRunnable;
18import org.openstreetmap.josm.io.OsmServerChangesetReader;
19import org.openstreetmap.josm.io.OsmTransferException;
20import org.xml.sax.SAXException;
21
22public class DownloadChangesetsTask extends PleaseWaitRunnable{
23
24 private Set<Integer> idsToDownload;
25 private OsmServerChangesetReader reader;
26 private boolean cancelled;
27 private Exception lastException;
28 private List<Changeset> downloadedChangesets;
29
30 public DownloadChangesetsTask(Collection<Integer> ids) {
31 super(tr("Download changesets"));
32 idsToDownload = new HashSet<Integer>();
33 if (ids == null || ids.isEmpty())
34 return;
35 for (int id: ids) {
36 if (id <= 0) {
37 continue;
38 }
39 idsToDownload.add(id);
40 }
41 }
42
43 @Override
44 protected void cancel() {
45 cancelled = true;
46 synchronized (this) {
47 if (reader != null) {
48 reader.cancel();
49 }
50 }
51 }
52
53 @Override
54 protected void finish() {
55 if (cancelled)
56 return;
57 if (lastException != null) {
58 ExceptionDialogUtil.explainException(lastException);
59 }
60 Runnable r = new Runnable() {
61 public void run() {
62 ChangesetCache.getInstance().update(downloadedChangesets);
63 }
64 };
65
66 if (SwingUtilities.isEventDispatchThread()) {
67 r.run();
68 } else {
69 SwingUtilities.invokeLater(r);
70 }
71 }
72
73 @Override
74 protected void realRun() throws SAXException, IOException, OsmTransferException {
75 try {
76 synchronized (this) {
77 reader = new OsmServerChangesetReader();
78 }
79 downloadedChangesets = reader.readChangesets(idsToDownload, getProgressMonitor().createSubTaskMonitor(0, false));
80 } catch(Exception e) {
81 if (cancelled)
82 // ignore exception if cancelled
83 return;
84 if (e instanceof RuntimeException)
85 throw (RuntimeException)e;
86 lastException = e;
87 }
88 }
89}
Note: See TracBrowser for help on using the repository browser.