1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import javax.swing.SwingUtilities;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
14 | import org.openstreetmap.josm.data.osm.ChangesetCache;
|
---|
15 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
16 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
17 | import org.openstreetmap.josm.io.OsmApi;
|
---|
18 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
19 | import org.xml.sax.SAXException;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * A task for closing a collection of changesets.
|
---|
23 | *
|
---|
24 | */
|
---|
25 | public class CloseChangesetTask extends PleaseWaitRunnable {
|
---|
26 | private boolean canceled;
|
---|
27 | private Exception lastException;
|
---|
28 | private final Collection<Changeset> changesets;
|
---|
29 | private final List<Changeset> closedChangesets;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Closes all changesets in <code>changesets</code> if they are not null, if they
|
---|
33 | * are still open and if they have an id > 0. Other changesets in the collection
|
---|
34 | * are ignored.
|
---|
35 | *
|
---|
36 | * @param changesets the collection of changesets. Empty collection assumes, if null.
|
---|
37 | */
|
---|
38 | public CloseChangesetTask(Collection<Changeset> changesets) {
|
---|
39 | super(tr("Closing changeset"), false /* don't ignore exceptions */);
|
---|
40 | if (changesets == null) {
|
---|
41 | changesets = new ArrayList<>();
|
---|
42 | }
|
---|
43 | this.changesets = changesets;
|
---|
44 | this.closedChangesets = new ArrayList<>();
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | protected void cancel() {
|
---|
49 | this.canceled = true;
|
---|
50 | OsmApi.getOsmApi().cancel();
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | protected void finish() {
|
---|
55 | if (canceled)
|
---|
56 | return;
|
---|
57 | if (lastException != null) {
|
---|
58 | ExceptionDialogUtil.explainException(lastException);
|
---|
59 | }
|
---|
60 | SwingUtilities.invokeLater(() -> ChangesetCache.getInstance().update(closedChangesets));
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
65 | try {
|
---|
66 | for (Changeset cs: changesets) {
|
---|
67 | if (canceled) return;
|
---|
68 | if (cs == null || cs.getId() <= 0 || !cs.isOpen()) {
|
---|
69 | continue;
|
---|
70 | }
|
---|
71 | getProgressMonitor().subTask(tr("Closing changeset {0}", cs.getId()));
|
---|
72 | OsmApi.getOsmApi().closeChangeset(cs, getProgressMonitor().createSubTaskMonitor(1, false));
|
---|
73 | closedChangesets.add(cs);
|
---|
74 | }
|
---|
75 | } catch (OsmTransferException e) {
|
---|
76 | if (canceled)
|
---|
77 | return;
|
---|
78 | lastException = e;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|