[1810] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.event.ActionEvent;
|
---|
| 7 | import java.awt.event.KeyEvent;
|
---|
| 8 | import java.io.IOException;
|
---|
| 9 | import java.util.Collection;
|
---|
| 10 |
|
---|
| 11 | import javax.swing.JOptionPane;
|
---|
| 12 | import javax.swing.SwingUtilities;
|
---|
| 13 |
|
---|
| 14 | import org.openstreetmap.josm.Main;
|
---|
| 15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 17 | import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;
|
---|
[1990] | 18 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
[1810] | 19 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
| 20 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[1811] | 21 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[1810] | 22 | import org.openstreetmap.josm.io.OsmApi;
|
---|
| 23 | import org.openstreetmap.josm.io.OsmServerBackreferenceReader;
|
---|
| 24 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
| 25 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
| 26 | import org.xml.sax.SAXException;
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * This action loads the set of primitives referring to the current selection from the OSM
|
---|
| 30 | * server.
|
---|
| 31 | *
|
---|
[1811] | 32 | *
|
---|
[1810] | 33 | */
|
---|
[1820] | 34 | public class DownloadReferrersAction extends JosmAction{
|
---|
[1810] | 35 |
|
---|
| 36 | public DownloadReferrersAction() {
|
---|
| 37 | super(tr("Download referrers from OSM..."), "downloadreferrers", tr("Download primitives referring to one of the selected primitives"),
|
---|
| 38 | Shortcut.registerShortcut("file:downloadreferrers", tr("File: {0}", tr("Download referrers...")), KeyEvent.VK_D, Shortcut.GROUPS_ALT2+Shortcut.GROUP_HOTKEY), true);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * Downloads the primitives referring to the primitives in <code>primitives</code>.
|
---|
| 43 | * Does nothing if primitives is null or empty.
|
---|
[1811] | 44 | *
|
---|
[1810] | 45 | * @param primitives the collection of primitives.
|
---|
| 46 | */
|
---|
| 47 | public void downloadReferrers(Collection<OsmPrimitive> primitives) {
|
---|
| 48 | if (primitives == null || primitives.isEmpty()) return;
|
---|
| 49 | Main.worker.submit(new DownloadReferrersTask(primitives));
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | public void actionPerformed(ActionEvent e) {
|
---|
| 54 | if (!isEnabled())
|
---|
| 55 | return;
|
---|
| 56 | OsmDataLayer layer = Main.map.mapView.getEditLayer();
|
---|
| 57 | Collection<OsmPrimitive> primitives = layer.data.getSelected();
|
---|
| 58 | downloadReferrers(primitives);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * The asynchronous task for downloading referring primitives
|
---|
[1811] | 63 | *
|
---|
[1810] | 64 | */
|
---|
| 65 | class DownloadReferrersTask extends PleaseWaitRunnable {
|
---|
| 66 | private DataSet ds;
|
---|
| 67 | private boolean cancelled;
|
---|
| 68 | Exception lastException;
|
---|
| 69 | private Collection<OsmPrimitive> primitives;
|
---|
| 70 | private DataSet parents;
|
---|
| 71 |
|
---|
| 72 | public DownloadReferrersTask(Collection<OsmPrimitive> primitives) {
|
---|
| 73 | super("Download referrers", false /* don't ignore exception*/);
|
---|
| 74 | cancelled = false;
|
---|
| 75 | this.primitives = primitives;
|
---|
| 76 | parents = new DataSet();
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | protected void showLastException() {
|
---|
| 80 | String msg = lastException.getMessage();
|
---|
| 81 | if (msg == null) {
|
---|
| 82 | msg = lastException.toString();
|
---|
| 83 | }
|
---|
[2017] | 84 | JOptionPane.showMessageDialog(
|
---|
[1810] | 85 | Main.map,
|
---|
| 86 | msg,
|
---|
| 87 | tr("Error"),
|
---|
| 88 | JOptionPane.ERROR_MESSAGE
|
---|
| 89 | );
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | @Override
|
---|
| 93 | protected void cancel() {
|
---|
| 94 | cancelled = true;
|
---|
| 95 | OsmApi.getOsmApi().cancel();
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | @Override
|
---|
| 99 | protected void finish() {
|
---|
| 100 | if (cancelled)
|
---|
| 101 | return;
|
---|
| 102 | if (lastException != null) {
|
---|
| 103 | showLastException();
|
---|
| 104 | return;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | MergeVisitor visitor = new MergeVisitor(Main.map.mapView.getEditLayer().data, parents);
|
---|
| 108 | visitor.merge();
|
---|
| 109 | SwingUtilities.invokeLater(
|
---|
| 110 | new Runnable() {
|
---|
| 111 | public void run() {
|
---|
| 112 | Main.map.mapView.getEditLayer().fireDataChange();
|
---|
| 113 | Main.map.mapView.repaint();
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | );
|
---|
| 117 | if (visitor.getConflicts().isEmpty())
|
---|
| 118 | return;
|
---|
| 119 | Main.map.mapView.getEditLayer().getConflicts().add(visitor.getConflicts());
|
---|
[2017] | 120 | JOptionPane.showMessageDialog(
|
---|
[1847] | 121 | Main.parent,
|
---|
[1810] | 122 | tr("There were {0} conflicts during import.",
|
---|
[1847] | 123 | visitor.getConflicts().size()
|
---|
| 124 | ),
|
---|
| 125 | tr("Conflicts during download"),
|
---|
| 126 | JOptionPane.WARNING_MESSAGE
|
---|
[1810] | 127 | );
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[1811] | 130 | protected void downloadParents(OsmPrimitive primitive, ProgressMonitor progressMonitor) throws OsmTransferException{
|
---|
[1810] | 131 | OsmServerBackreferenceReader reader = new OsmServerBackreferenceReader(primitive);
|
---|
[1811] | 132 | DataSet ds = reader.parseOsm(progressMonitor);
|
---|
[1810] | 133 | MergeVisitor visitor = new MergeVisitor(parents, ds);
|
---|
| 134 | visitor.merge();
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | @Override
|
---|
| 138 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
| 139 | try {
|
---|
[1811] | 140 | progressMonitor.setTicksCount(primitives.size());
|
---|
[1810] | 141 | int i=1;
|
---|
| 142 | for (OsmPrimitive primitive: primitives) {
|
---|
| 143 | if (cancelled)
|
---|
| 144 | return;
|
---|
[1990] | 145 | progressMonitor.subTask(tr("({0}/{1}) Loading parents of primitive {2}", i+1,primitives.size(), primitive.getDisplayName(DefaultNameFormatter.getInstance())));
|
---|
[1811] | 146 | downloadParents(primitive, progressMonitor.createSubTaskMonitor(1, false));
|
---|
[1810] | 147 | i++;
|
---|
| 148 | }
|
---|
| 149 | } catch(Exception e) {
|
---|
| 150 | if (cancelled)
|
---|
| 151 | return;
|
---|
| 152 | lastException = e;
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /**
|
---|
| 158 | * Refreshes the enabled state
|
---|
[1811] | 159 | *
|
---|
[1810] | 160 | */
|
---|
[1820] | 161 | @Override
|
---|
| 162 | protected void updateEnabledState() {
|
---|
| 163 | setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
|
---|
[1810] | 164 | }
|
---|
| 165 | }
|
---|