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

Last change on this file since 6316 was 6316, checked in by Don-vip, 11 years ago

Sonar/FindBugs - Loose coupling

  • Property svn:eol-style set to native
File size: 2.8 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 Collection<Changeset> changesets;
29 private 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<Changeset>();
42 }
43 this.changesets = changesets;
44 this.closedChangesets = new ArrayList<Changeset>();
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(
61 new Runnable() {
62 @Override
63 public void run() {
64 ChangesetCache.getInstance().update(closedChangesets);
65 }
66 }
67 );
68 }
69
70 @Override
71 protected void realRun() throws SAXException, IOException, OsmTransferException {
72 try {
73 for (Changeset cs: changesets) {
74 if (canceled) return;
75 if (cs == null || cs.getId() <= 0 || ! cs.isOpen()) {
76 continue;
77 }
78 getProgressMonitor().subTask(tr("Closing changeset {0}", cs.getId()));
79 OsmApi.getOsmApi().closeChangeset(cs, getProgressMonitor().createSubTaskMonitor(1, false));
80 closedChangesets.add(cs);
81 }
82 } catch(Exception e) {
83 if (canceled)
84 return;
85 lastException = e;
86 }
87 }
88}
Note: See TracBrowser for help on using the repository browser.