source: josm/trunk/src/org/openstreetmap/josm/gui/io/CloseChangesetTask.java@ 10680

Last change on this file since 10680 was 10611, checked in by Don-vip, 8 years ago

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

  • Property svn:eol-style set to native
File size: 2.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.util.ArrayList;
8import java.util.Collection;
9import java.util.List;
10
11import javax.swing.SwingUtilities;
12
13import org.openstreetmap.josm.data.osm.Changeset;
14import org.openstreetmap.josm.data.osm.ChangesetCache;
15import org.openstreetmap.josm.gui.ExceptionDialogUtil;
16import org.openstreetmap.josm.gui.PleaseWaitRunnable;
17import org.openstreetmap.josm.io.OsmApi;
18import org.openstreetmap.josm.io.OsmTransferException;
19import org.xml.sax.SAXException;
20
21/**
22 * A task for closing a collection of changesets.
23 *
24 */
25public 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 &gt; 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}
Note: See TracBrowser for help on using the repository browser.