| 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.List;
|
|---|
| 8 |
|
|---|
| 9 | import javax.swing.JOptionPane;
|
|---|
| 10 | import javax.swing.SwingUtilities;
|
|---|
| 11 |
|
|---|
| 12 | import org.openstreetmap.josm.Main;
|
|---|
| 13 | import org.openstreetmap.josm.data.osm.Changeset;
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.ChangesetCache;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.UserInfo;
|
|---|
| 16 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
|---|
| 17 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
|---|
| 18 | import org.openstreetmap.josm.io.ChangesetQuery;
|
|---|
| 19 | import org.openstreetmap.josm.io.OsmServerChangesetReader;
|
|---|
| 20 | import org.openstreetmap.josm.io.OsmServerUserInfoReader;
|
|---|
| 21 | import org.openstreetmap.josm.io.OsmTransferException;
|
|---|
| 22 | import org.xml.sax.SAXException;
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * This is a task for downloading the open changesets of the current user
|
|---|
| 26 | * from the OSM server.
|
|---|
| 27 | *
|
|---|
| 28 | */
|
|---|
| 29 | public class DownloadOpenChangesetsTask extends PleaseWaitRunnable {
|
|---|
| 30 |
|
|---|
| 31 | private boolean cancelled;
|
|---|
| 32 | private OsmServerChangesetReader reader;
|
|---|
| 33 | private List<Changeset> changesets;
|
|---|
| 34 | private OpenChangesetComboBoxModel model;
|
|---|
| 35 | private Exception lastException;
|
|---|
| 36 | private UserInfo userInfo;
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | *
|
|---|
| 40 | * @param model provides the user id of the current user and accepts the changesets
|
|---|
| 41 | * after download
|
|---|
| 42 | */
|
|---|
| 43 | public DownloadOpenChangesetsTask(OpenChangesetComboBoxModel model) {
|
|---|
| 44 | super(tr("Downloading open changesets ...", false /* don't ignore exceptions */));
|
|---|
| 45 | this.model = model;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | @Override
|
|---|
| 49 | protected void cancel() {
|
|---|
| 50 | this.cancelled = true;
|
|---|
| 51 | reader.cancel();
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | @Override
|
|---|
| 55 | protected void finish() {
|
|---|
| 56 | if (cancelled)
|
|---|
| 57 | return;
|
|---|
| 58 | if (lastException != null) {
|
|---|
| 59 | ExceptionDialogUtil.explainException(lastException);
|
|---|
| 60 | return;
|
|---|
| 61 | }
|
|---|
| 62 | if (changesets.isEmpty()) {
|
|---|
| 63 | JOptionPane.showMessageDialog(
|
|---|
| 64 | Main.parent,
|
|---|
| 65 | tr("There are no open changesets"),
|
|---|
| 66 | tr("No open changesets"),
|
|---|
| 67 | JOptionPane.INFORMATION_MESSAGE
|
|---|
| 68 | );
|
|---|
| 69 | }
|
|---|
| 70 | SwingUtilities.invokeLater(
|
|---|
| 71 | new Runnable() {
|
|---|
| 72 | public void run() {
|
|---|
| 73 | ChangesetCache.getInstance().update(changesets);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | );
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * Fetch the user info from the server. This is necessary if we don't know
|
|---|
| 81 | * the users id yet
|
|---|
| 82 | *
|
|---|
| 83 | * @return the user info
|
|---|
| 84 | * @throws OsmTransferException thrown in case of any communication exception
|
|---|
| 85 | */
|
|---|
| 86 | protected UserInfo fetchUserInfo() throws OsmTransferException {
|
|---|
| 87 | OsmServerUserInfoReader reader = new OsmServerUserInfoReader();
|
|---|
| 88 | return reader.fetchUserInfo(getProgressMonitor().createSubTaskMonitor(1, false));
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | @Override
|
|---|
| 92 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
|---|
| 93 | try {
|
|---|
| 94 | if (model.getUserId()== 0) {
|
|---|
| 95 | userInfo = fetchUserInfo();
|
|---|
| 96 | model.setUserId(userInfo.getId());
|
|---|
| 97 | }
|
|---|
| 98 | if (cancelled)
|
|---|
| 99 | return;
|
|---|
| 100 | reader = new OsmServerChangesetReader();
|
|---|
| 101 | ChangesetQuery query = new ChangesetQuery().forUser((int)model.getUserId()).beingOpen(true);
|
|---|
| 102 | changesets = reader.queryChangesets(
|
|---|
| 103 | query,
|
|---|
| 104 | getProgressMonitor().createSubTaskMonitor(1, false /* not internal */)
|
|---|
| 105 | );
|
|---|
| 106 | } catch(Exception e) {
|
|---|
| 107 | if (cancelled)
|
|---|
| 108 | return;
|
|---|
| 109 | lastException = e;
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | public boolean isCancelled() {
|
|---|
| 114 | return cancelled;
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|