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