001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.io;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.io.IOException;
007import java.util.ArrayList;
008import java.util.Collection;
009import java.util.List;
010
011import javax.swing.SwingUtilities;
012
013import org.openstreetmap.josm.data.osm.Changeset;
014import org.openstreetmap.josm.data.osm.ChangesetCache;
015import org.openstreetmap.josm.gui.ExceptionDialogUtil;
016import org.openstreetmap.josm.gui.PleaseWaitRunnable;
017import org.openstreetmap.josm.io.OsmApi;
018import org.openstreetmap.josm.io.OsmTransferException;
019import org.xml.sax.SAXException;
020
021/**
022 * A task for closing a collection of changesets.
023 *
024 */
025public class CloseChangesetTask extends PleaseWaitRunnable {
026    private boolean canceled;
027    private Exception lastException;
028    private Collection<Changeset> changesets;
029    private List<Changeset> closedChangesets;
030
031    /**
032     * Closes all changesets in <code>changesets</code> if they are not null, if they
033     * are still open and if they have an id > 0. Other changesets in the collection
034     * are ignored.
035     *
036     * @param changesets  the collection of changesets. Empty collection assumes, if null.
037     */
038    public CloseChangesetTask(Collection<Changeset> changesets) {
039        super(tr("Closing changeset"), false /* don't ignore exceptions */);
040        if (changesets == null) {
041            changesets = new ArrayList<Changeset>();
042        }
043        this.changesets = changesets;
044        this.closedChangesets = new ArrayList<Changeset>();
045    }
046
047    @Override
048    protected void cancel() {
049        this.canceled = true;
050        OsmApi.getOsmApi().cancel();
051    }
052
053    @Override
054    protected void finish() {
055        if (canceled)
056            return;
057        if (lastException != null) {
058            ExceptionDialogUtil.explainException(lastException);
059        }
060        SwingUtilities.invokeLater(
061                new Runnable() {
062                    @Override
063                    public void run() {
064                        ChangesetCache.getInstance().update(closedChangesets);
065                    }
066                }
067        );
068    }
069
070    @Override
071    protected void realRun() throws SAXException, IOException, OsmTransferException {
072        try {
073            for (Changeset cs: changesets) {
074                if (canceled) return;
075                if (cs == null || cs.getId() <= 0 || ! cs.isOpen()) {
076                    continue;
077                }
078                getProgressMonitor().subTask(tr("Closing changeset {0}", cs.getId()));
079                OsmApi.getOsmApi().closeChangeset(cs, getProgressMonitor().createSubTaskMonitor(1, false));
080                closedChangesets.add(cs);
081            }
082        } catch(Exception e) {
083            if (canceled)
084                return;
085            lastException = e;
086        }
087    }
088}