[2613] | 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 |
|
---|
[2689] | 6 | import java.awt.Component;
|
---|
[2613] | 7 | import java.io.IOException;
|
---|
[2689] | 8 | import java.lang.reflect.InvocationTargetException;
|
---|
[2613] | 9 | import java.util.Collection;
|
---|
[2689] | 10 | import java.util.Collections;
|
---|
[2613] | 11 | import java.util.HashSet;
|
---|
| 12 | import java.util.Set;
|
---|
| 13 |
|
---|
| 14 | import javax.swing.SwingUtilities;
|
---|
| 15 |
|
---|
[2689] | 16 | import org.openstreetmap.josm.Main;
|
---|
[2613] | 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;
|
---|
[2689] | 23 | import org.openstreetmap.josm.tools.BugReportExceptionHandler;
|
---|
| 24 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
| 25 | import org.openstreetmap.josm.tools.ExceptionUtil;
|
---|
[2613] | 26 | import org.xml.sax.SAXException;
|
---|
| 27 |
|
---|
[2689] | 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{
|
---|
[2613] | 37 |
|
---|
[2689] | 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 |
|
---|
[2613] | 82 | private Set<Integer> idsToDownload;
|
---|
| 83 | private OsmServerChangesetReader reader;
|
---|
[2689] | 84 | private boolean canceled;
|
---|
[2613] | 85 | private Exception lastException;
|
---|
[2689] | 86 | private Set<Changeset> downloadedChangesets;
|
---|
[2613] | 87 |
|
---|
[2689] | 88 | protected void init(Collection<Integer> ids) {
|
---|
| 89 | if (ids == null) {
|
---|
| 90 | ids = Collections.emptyList();
|
---|
| 91 | }
|
---|
[2613] | 92 | idsToDownload = new HashSet<Integer>();
|
---|
| 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 |
|
---|
[2689] | 103 | /**
|
---|
| 104 | * Creates the download task for a collection of changeset ids. Uses a {@see PleaseWaitDialog}
|
---|
| 105 | * whose parent is {@see Main#parent}.
|
---|
| 106 | *
|
---|
| 107 | * Null ids or or ids <= 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 | }
|
---|
| 116 |
|
---|
| 117 | /**
|
---|
| 118 | * Creates the download task for a collection of changeset ids. Uses a {@see PleaseWaitDialog}
|
---|
| 119 | * whose parent is the parent window of <code>dialogParent</code>.
|
---|
| 120 | *
|
---|
| 121 | * Null ids or or ids <= 0 in the id collection are ignored.
|
---|
| 122 | *
|
---|
| 123 | * @param dialogParent the parent reference component for the {@see PleaseWaitDialog}. Must not be null.
|
---|
| 124 | * @param ids the collection of ids. Empty collection assumed if null.
|
---|
| 125 | * @throws IllegalArgumentException thrown if dialogParent is null
|
---|
| 126 | */
|
---|
| 127 | public ChangesetHeaderDownloadTask(Component dialogParent, Collection<Integer> ids) throws IllegalArgumentException{
|
---|
| 128 | super(dialogParent,tr("Download changesets"), false /* don't ignore exceptions */);
|
---|
| 129 | init(ids);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[2613] | 132 | @Override
|
---|
| 133 | protected void cancel() {
|
---|
[2689] | 134 | canceled = true;
|
---|
[2613] | 135 | synchronized (this) {
|
---|
| 136 | if (reader != null) {
|
---|
| 137 | reader.cancel();
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | @Override
|
---|
| 143 | protected void finish() {
|
---|
[2689] | 144 | if (canceled)
|
---|
[2613] | 145 | return;
|
---|
| 146 | if (lastException != null) {
|
---|
| 147 | ExceptionDialogUtil.explainException(lastException);
|
---|
| 148 | }
|
---|
| 149 | Runnable r = new Runnable() {
|
---|
| 150 | public void run() {
|
---|
| 151 | ChangesetCache.getInstance().update(downloadedChangesets);
|
---|
| 152 | }
|
---|
| 153 | };
|
---|
| 154 |
|
---|
| 155 | if (SwingUtilities.isEventDispatchThread()) {
|
---|
| 156 | r.run();
|
---|
| 157 | } else {
|
---|
[2689] | 158 | try {
|
---|
| 159 | SwingUtilities.invokeAndWait(r);
|
---|
| 160 | } catch(InterruptedException e) {
|
---|
| 161 | e.printStackTrace();
|
---|
| 162 | } catch(InvocationTargetException e) {
|
---|
| 163 | Throwable t = e.getTargetException();
|
---|
| 164 | if (t instanceof RuntimeException) {
|
---|
| 165 | BugReportExceptionHandler.handleException(t);
|
---|
| 166 | } else if (t instanceof Exception){
|
---|
| 167 | ExceptionUtil.explainException(e);
|
---|
| 168 | } else {
|
---|
| 169 | BugReportExceptionHandler.handleException(t);
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
[2613] | 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | @Override
|
---|
| 176 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
| 177 | try {
|
---|
| 178 | synchronized (this) {
|
---|
| 179 | reader = new OsmServerChangesetReader();
|
---|
| 180 | }
|
---|
[2689] | 181 | downloadedChangesets = new HashSet<Changeset>();
|
---|
| 182 | downloadedChangesets.addAll(reader.readChangesets(idsToDownload, getProgressMonitor().createSubTaskMonitor(0, false)));
|
---|
| 183 | } catch(OsmTransferException e) {
|
---|
| 184 | if (canceled)
|
---|
[2613] | 185 | // ignore exception if cancelled
|
---|
| 186 | return;
|
---|
[2689] | 187 | // remember other exceptions
|
---|
[2613] | 188 | lastException = e;
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
[2689] | 191 |
|
---|
| 192 | /* ------------------------------------------------------------------------------- */
|
---|
| 193 | /* interface ChangesetDownloadTask */
|
---|
| 194 | /* ------------------------------------------------------------------------------- */
|
---|
| 195 | public Set<Changeset> getDownloadedChangesets() {
|
---|
| 196 | return downloadedChangesets;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | public boolean isCanceled() {
|
---|
| 200 | return canceled;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | public boolean isFailed() {
|
---|
| 204 | return lastException != null;
|
---|
| 205 | }
|
---|
[2613] | 206 | }
|
---|