1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.changeset;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.lang.reflect.InvocationTargetException;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.HashSet;
|
---|
12 | import java.util.Set;
|
---|
13 |
|
---|
14 | import javax.swing.SwingUtilities;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
18 | import org.openstreetmap.josm.data.osm.ChangesetCache;
|
---|
19 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
20 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
21 | import org.openstreetmap.josm.io.OsmServerChangesetReader;
|
---|
22 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
23 | import org.openstreetmap.josm.tools.BugReportExceptionHandler;
|
---|
24 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
25 | import org.openstreetmap.josm.tools.ExceptionUtil;
|
---|
26 | import 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 {@see ChangesetCache}.
|
---|
34 | *
|
---|
35 | */
|
---|
36 | public 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 {@see Changeset#isNew()} == true.
|
---|
42 | *
|
---|
43 | * @param changesets the collection of changesets. Assumes an empty collection if null.
|
---|
44 | * @return the download task
|
---|
45 | */
|
---|
46 | static public 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 {@see Changeset#isNew()} == true.
|
---|
54 | *
|
---|
55 | * @param parent the parent component relative to which the {@see 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 | static public 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<Integer>();
|
---|
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 |
|
---|
87 | protected void init(Collection<Integer> ids) {
|
---|
88 | if (ids == null) {
|
---|
89 | ids = Collections.emptyList();
|
---|
90 | }
|
---|
91 | idsToDownload = new HashSet<Integer>();
|
---|
92 | if (ids == null || ids.isEmpty())
|
---|
93 | return;
|
---|
94 | for (int id: ids) {
|
---|
95 | if (id <= 0) {
|
---|
96 | continue;
|
---|
97 | }
|
---|
98 | idsToDownload.add(id);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Creates the download task for a collection of changeset ids. Uses a {@see PleaseWaitDialog}
|
---|
104 | * whose parent is {@see Main#parent}.
|
---|
105 | *
|
---|
106 | * Null ids or or ids <= 0 in the id collection are ignored.
|
---|
107 | *
|
---|
108 | * @param ids the collection of ids. Empty collection assumed if null.
|
---|
109 | */
|
---|
110 | public ChangesetHeaderDownloadTask(Collection<Integer> ids) {
|
---|
111 | // parent for dialog is Main.parent
|
---|
112 | super(tr("Download changesets"), false /* don't ignore exceptions */);
|
---|
113 | init(ids);
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Creates the download task for a collection of changeset ids. Uses a {@see PleaseWaitDialog}
|
---|
118 | * whose parent is the parent window of <code>dialogParent</code>.
|
---|
119 | *
|
---|
120 | * Null ids or or ids <= 0 in the id collection are ignored.
|
---|
121 | *
|
---|
122 | * @param dialogParent the parent reference component for the {@see PleaseWaitDialog}. Must not be null.
|
---|
123 | * @param ids the collection of ids. Empty collection assumed if null.
|
---|
124 | * @throws IllegalArgumentException thrown if dialogParent is null
|
---|
125 | */
|
---|
126 | public ChangesetHeaderDownloadTask(Component dialogParent, Collection<Integer> ids) throws IllegalArgumentException{
|
---|
127 | super(dialogParent,tr("Download changesets"), false /* don't ignore exceptions */);
|
---|
128 | init(ids);
|
---|
129 | }
|
---|
130 |
|
---|
131 | @Override
|
---|
132 | protected void cancel() {
|
---|
133 | canceled = true;
|
---|
134 | synchronized (this) {
|
---|
135 | if (reader != null) {
|
---|
136 | reader.cancel();
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | @Override
|
---|
142 | protected void finish() {
|
---|
143 | if (canceled)
|
---|
144 | return;
|
---|
145 | if (lastException != null) {
|
---|
146 | ExceptionDialogUtil.explainException(lastException);
|
---|
147 | }
|
---|
148 | Runnable r = new Runnable() {
|
---|
149 | public void run() {
|
---|
150 | ChangesetCache.getInstance().update(downloadedChangesets);
|
---|
151 | }
|
---|
152 | };
|
---|
153 |
|
---|
154 | if (SwingUtilities.isEventDispatchThread()) {
|
---|
155 | r.run();
|
---|
156 | } else {
|
---|
157 | try {
|
---|
158 | SwingUtilities.invokeAndWait(r);
|
---|
159 | } catch(InterruptedException e) {
|
---|
160 | e.printStackTrace();
|
---|
161 | } catch(InvocationTargetException e) {
|
---|
162 | Throwable t = e.getTargetException();
|
---|
163 | if (t instanceof RuntimeException) {
|
---|
164 | BugReportExceptionHandler.handleException(t);
|
---|
165 | } else if (t instanceof Exception){
|
---|
166 | ExceptionUtil.explainException(e);
|
---|
167 | } else {
|
---|
168 | BugReportExceptionHandler.handleException(t);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | @Override
|
---|
175 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
176 | try {
|
---|
177 | synchronized (this) {
|
---|
178 | reader = new OsmServerChangesetReader();
|
---|
179 | }
|
---|
180 | downloadedChangesets = new HashSet<Changeset>();
|
---|
181 | downloadedChangesets.addAll(reader.readChangesets(idsToDownload, getProgressMonitor().createSubTaskMonitor(0, false)));
|
---|
182 | } catch(OsmTransferException e) {
|
---|
183 | if (canceled)
|
---|
184 | // ignore exception if cancelled
|
---|
185 | return;
|
---|
186 | // remember other exceptions
|
---|
187 | lastException = e;
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | /* ------------------------------------------------------------------------------- */
|
---|
192 | /* interface ChangesetDownloadTask */
|
---|
193 | /* ------------------------------------------------------------------------------- */
|
---|
194 | public Set<Changeset> getDownloadedChangesets() {
|
---|
195 | return downloadedChangesets;
|
---|
196 | }
|
---|
197 |
|
---|
198 | public boolean isCanceled() {
|
---|
199 | return canceled;
|
---|
200 | }
|
---|
201 |
|
---|
202 | public boolean isFailed() {
|
---|
203 | return lastException != null;
|
---|
204 | }
|
---|
205 | }
|
---|