| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.actions; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht; |
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 6 | |
|---|
| 7 | import java.awt.event.ActionEvent; |
|---|
| 8 | import java.awt.event.KeyEvent; |
|---|
| 9 | import java.io.IOException; |
|---|
| 10 | import java.util.Collection; |
|---|
| 11 | import java.util.List; |
|---|
| 12 | |
|---|
| 13 | import javax.swing.JOptionPane; |
|---|
| 14 | import javax.swing.SwingUtilities; |
|---|
| 15 | |
|---|
| 16 | import org.openstreetmap.josm.Main; |
|---|
| 17 | import org.openstreetmap.josm.data.osm.Changeset; |
|---|
| 18 | import org.openstreetmap.josm.data.osm.ChangesetCache; |
|---|
| 19 | import org.openstreetmap.josm.data.osm.UserInfo; |
|---|
| 20 | import org.openstreetmap.josm.gui.ExceptionDialogUtil; |
|---|
| 21 | import org.openstreetmap.josm.gui.PleaseWaitRunnable; |
|---|
| 22 | import org.openstreetmap.josm.gui.io.CloseChangesetDialog; |
|---|
| 23 | import org.openstreetmap.josm.gui.io.CloseChangesetTask; |
|---|
| 24 | import org.openstreetmap.josm.io.ChangesetQuery; |
|---|
| 25 | import org.openstreetmap.josm.io.OsmServerChangesetReader; |
|---|
| 26 | import org.openstreetmap.josm.io.OsmServerUserInfoReader; |
|---|
| 27 | import org.openstreetmap.josm.io.OsmTransferException; |
|---|
| 28 | import org.openstreetmap.josm.tools.Shortcut; |
|---|
| 29 | import org.xml.sax.SAXException; |
|---|
| 30 | |
|---|
| 31 | public class CloseChangesetAction extends JosmAction{ |
|---|
| 32 | |
|---|
| 33 | public CloseChangesetAction() { |
|---|
| 34 | super(tr("Close open changesets"), |
|---|
| 35 | "closechangeset", |
|---|
| 36 | tr("Closes open changesets"), |
|---|
| 37 | Shortcut.registerShortcut( |
|---|
| 38 | "system:closechangeset", |
|---|
| 39 | tr("File: {0}", tr("Closes open changesets")), |
|---|
| 40 | KeyEvent.VK_Q, |
|---|
| 41 | Shortcut.GROUP_HOTKEY + Shortcut.GROUPS_ALT2 |
|---|
| 42 | ), |
|---|
| 43 | true |
|---|
| 44 | ); |
|---|
| 45 | putValue("help", ht("/Action/CloseChangeset")); |
|---|
| 46 | |
|---|
| 47 | } |
|---|
| 48 | public void actionPerformed(ActionEvent e) { |
|---|
| 49 | Main.worker.submit(new DownloadOpenChangesetsTask()); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | protected void onPostDownloadOpenChangesets() { |
|---|
| 53 | List<Changeset> openChangesets = ChangesetCache.getInstance().getOpenChangesets(); |
|---|
| 54 | if (openChangesets.isEmpty()) { |
|---|
| 55 | JOptionPane.showMessageDialog( |
|---|
| 56 | Main.parent, |
|---|
| 57 | tr("There are no open changesets"), |
|---|
| 58 | tr("No open changesets"), |
|---|
| 59 | JOptionPane.INFORMATION_MESSAGE |
|---|
| 60 | ); |
|---|
| 61 | return; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | CloseChangesetDialog dialog = new CloseChangesetDialog(); |
|---|
| 65 | dialog.setChangesets(openChangesets); |
|---|
| 66 | dialog.setVisible(true); |
|---|
| 67 | if (dialog.isCanceled()) |
|---|
| 68 | return; |
|---|
| 69 | |
|---|
| 70 | Collection<Changeset> changesetsToClose = dialog.getSelectedChangesets(); |
|---|
| 71 | CloseChangesetTask closeChangesetTask = new CloseChangesetTask(changesetsToClose); |
|---|
| 72 | Main.worker.submit(closeChangesetTask); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | private class DownloadOpenChangesetsTask extends PleaseWaitRunnable { |
|---|
| 76 | |
|---|
| 77 | private boolean cancelled; |
|---|
| 78 | private OsmServerChangesetReader reader; |
|---|
| 79 | private List<Changeset> changesets; |
|---|
| 80 | private Exception lastException; |
|---|
| 81 | private UserInfo userInfo; |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | * |
|---|
| 85 | * @param model provides the user id of the current user and accepts the changesets |
|---|
| 86 | * after download |
|---|
| 87 | */ |
|---|
| 88 | public DownloadOpenChangesetsTask() { |
|---|
| 89 | super(tr("Downloading open changesets ...", false /* don't ignore exceptions */)); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | @Override |
|---|
| 93 | protected void cancel() { |
|---|
| 94 | this.cancelled = true; |
|---|
| 95 | reader.cancel(); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | @Override |
|---|
| 99 | protected void finish() { |
|---|
| 100 | SwingUtilities.invokeLater( |
|---|
| 101 | new Runnable() { |
|---|
| 102 | public void run() { |
|---|
| 103 | if (lastException != null) { |
|---|
| 104 | ExceptionDialogUtil.explainException(lastException); |
|---|
| 105 | } |
|---|
| 106 | ChangesetCache.getInstance().update(changesets); |
|---|
| 107 | if (!cancelled && lastException == null) { |
|---|
| 108 | onPostDownloadOpenChangesets(); |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | ); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | * Fetch the user info from the server. This is necessary if we don't know |
|---|
| 117 | * the users id yet |
|---|
| 118 | * |
|---|
| 119 | * @return the user info |
|---|
| 120 | * @throws OsmTransferException thrown in case of any communication exception |
|---|
| 121 | */ |
|---|
| 122 | protected UserInfo fetchUserInfo() throws OsmTransferException { |
|---|
| 123 | OsmServerUserInfoReader reader = new OsmServerUserInfoReader(); |
|---|
| 124 | return reader.fetchUserInfo(getProgressMonitor().createSubTaskMonitor(1, false)); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | @Override |
|---|
| 128 | protected void realRun() throws SAXException, IOException, OsmTransferException { |
|---|
| 129 | try { |
|---|
| 130 | userInfo = fetchUserInfo(); |
|---|
| 131 | if (cancelled) |
|---|
| 132 | return; |
|---|
| 133 | reader = new OsmServerChangesetReader(); |
|---|
| 134 | ChangesetQuery query = new ChangesetQuery().forUser(userInfo.getId()).beingOpen(true); |
|---|
| 135 | changesets = reader.queryChangesets( |
|---|
| 136 | query, |
|---|
| 137 | getProgressMonitor().createSubTaskMonitor(1, false /* not internal */) |
|---|
| 138 | ); |
|---|
| 139 | } catch(Exception e) { |
|---|
| 140 | if (cancelled) |
|---|
| 141 | return; |
|---|
| 142 | lastException = e; |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | public boolean isCancelled() { |
|---|
| 147 | return cancelled; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | public Exception getLastException() { |
|---|
| 151 | return lastException; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | } |
|---|