| 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;
|
|---|
| 18 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
|---|
| 19 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
|---|
| 20 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 21 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
|---|
| 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 | *
|
|---|
| 32 | *
|
|---|
| 33 | */
|
|---|
| 34 | public class DownloadReferrersAction extends JosmAction{
|
|---|
| 35 |
|
|---|
| 36 | public DownloadReferrersAction() {
|
|---|
| 37 | super(tr("Download parent ways/relations..."), "downloadreferrers", tr("Download primitives referring to one of the selected primitives"),
|
|---|
| 38 | Shortcut.registerShortcut("file:downloadreferrers", tr("File: {0}", tr("Download parent ways/relations...")), 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.
|
|---|
| 44 | *
|
|---|
| 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() || Main.map == null || Main.map.mapView == null)
|
|---|
| 55 | return;
|
|---|
| 56 | OsmDataLayer layer = Main.map.mapView.getEditLayer();
|
|---|
| 57 | if (layer == null)
|
|---|
| 58 | return;
|
|---|
| 59 | Collection<OsmPrimitive> primitives = layer.data.getSelected();
|
|---|
| 60 | downloadReferrers(primitives);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * The asynchronous task for downloading referring primitives
|
|---|
| 65 | *
|
|---|
| 66 | */
|
|---|
| 67 | class DownloadReferrersTask extends PleaseWaitRunnable {
|
|---|
| 68 | private DataSet ds;
|
|---|
| 69 | private boolean cancelled;
|
|---|
| 70 | Exception lastException;
|
|---|
| 71 | private Collection<OsmPrimitive> primitives;
|
|---|
| 72 | private DataSet parents;
|
|---|
| 73 |
|
|---|
| 74 | public DownloadReferrersTask(Collection<OsmPrimitive> primitives) {
|
|---|
| 75 | super("Download referrers", false /* don't ignore exception*/);
|
|---|
| 76 | cancelled = false;
|
|---|
| 77 | this.primitives = primitives;
|
|---|
| 78 | parents = new DataSet();
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | protected void showLastException() {
|
|---|
| 82 | String msg = lastException.getMessage();
|
|---|
| 83 | if (msg == null) {
|
|---|
| 84 | msg = lastException.toString();
|
|---|
| 85 | }
|
|---|
| 86 | JOptionPane.showMessageDialog(
|
|---|
| 87 | Main.map,
|
|---|
| 88 | msg,
|
|---|
| 89 | tr("Error"),
|
|---|
| 90 | JOptionPane.ERROR_MESSAGE
|
|---|
| 91 | );
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | @Override
|
|---|
| 95 | protected void cancel() {
|
|---|
| 96 | cancelled = true;
|
|---|
| 97 | OsmApi.getOsmApi().cancel();
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | @Override
|
|---|
| 101 | protected void finish() {
|
|---|
| 102 | if (cancelled)
|
|---|
| 103 | return;
|
|---|
| 104 | if (lastException != null) {
|
|---|
| 105 | showLastException();
|
|---|
| 106 | return;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | MergeVisitor visitor = new MergeVisitor(Main.map.mapView.getEditLayer().data, parents);
|
|---|
| 110 | visitor.merge();
|
|---|
| 111 | SwingUtilities.invokeLater(
|
|---|
| 112 | new Runnable() {
|
|---|
| 113 | public void run() {
|
|---|
| 114 | Main.map.mapView.getEditLayer().fireDataChange();
|
|---|
| 115 | Main.map.mapView.repaint();
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 | );
|
|---|
| 119 | if (visitor.getConflicts().isEmpty())
|
|---|
| 120 | return;
|
|---|
| 121 | Main.map.mapView.getEditLayer().getConflicts().add(visitor.getConflicts());
|
|---|
| 122 | JOptionPane.showMessageDialog(
|
|---|
| 123 | Main.parent,
|
|---|
| 124 | tr("There were {0} conflicts during import.",
|
|---|
| 125 | visitor.getConflicts().size()
|
|---|
| 126 | ),
|
|---|
| 127 | tr("Conflicts during download"),
|
|---|
| 128 | JOptionPane.WARNING_MESSAGE
|
|---|
| 129 | );
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | protected void downloadParents(OsmPrimitive primitive, ProgressMonitor progressMonitor) throws OsmTransferException{
|
|---|
| 133 | OsmServerBackreferenceReader reader = new OsmServerBackreferenceReader(primitive);
|
|---|
| 134 | DataSet ds = reader.parseOsm(progressMonitor);
|
|---|
| 135 | MergeVisitor visitor = new MergeVisitor(parents, ds);
|
|---|
| 136 | visitor.merge();
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | @Override
|
|---|
| 140 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
|---|
| 141 | try {
|
|---|
| 142 | progressMonitor.setTicksCount(primitives.size());
|
|---|
| 143 | int i=1;
|
|---|
| 144 | for (OsmPrimitive primitive: primitives) {
|
|---|
| 145 | if (cancelled)
|
|---|
| 146 | return;
|
|---|
| 147 | progressMonitor.subTask(tr("({0}/{1}) Loading parents of primitive {2}", i+1,primitives.size(), primitive.getDisplayName(DefaultNameFormatter.getInstance())));
|
|---|
| 148 | downloadParents(primitive, progressMonitor.createSubTaskMonitor(1, false));
|
|---|
| 149 | i++;
|
|---|
| 150 | }
|
|---|
| 151 | } catch(Exception e) {
|
|---|
| 152 | if (cancelled)
|
|---|
| 153 | return;
|
|---|
| 154 | lastException = e;
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | @Override
|
|---|
| 160 | protected void updateEnabledState() {
|
|---|
| 161 | if (getCurrentDataSet() == null) {
|
|---|
| 162 | setEnabled(false);
|
|---|
| 163 | } else {
|
|---|
| 164 | updateEnabledState(getCurrentDataSet().getSelected());
|
|---|
| 165 | }
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | @Override
|
|---|
| 169 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
|---|
| 170 | setEnabled(selection != null && !selection.isEmpty());
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|