[8378] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[6973] | 2 | package org.openstreetmap.josm.gui.io;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
| 6 |
|
---|
| 7 | import java.awt.Font;
|
---|
| 8 | import java.awt.GridBagLayout;
|
---|
| 9 | import java.io.IOException;
|
---|
| 10 | import java.util.ArrayList;
|
---|
| 11 | import java.util.HashSet;
|
---|
| 12 | import java.util.List;
|
---|
| 13 | import java.util.Set;
|
---|
| 14 |
|
---|
| 15 | import javax.swing.JLabel;
|
---|
| 16 | import javax.swing.JOptionPane;
|
---|
| 17 | import javax.swing.JPanel;
|
---|
| 18 | import javax.swing.JScrollPane;
|
---|
| 19 |
|
---|
| 20 | import org.openstreetmap.josm.Main;
|
---|
| 21 | import org.openstreetmap.josm.actions.downloadtasks.DownloadReferrersTask;
|
---|
| 22 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 23 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 24 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
| 25 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
[12636] | 26 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[6973] | 27 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
| 28 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
| 29 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
| 30 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
| 31 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
| 32 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
| 33 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
| 34 | import org.openstreetmap.josm.tools.GBC;
|
---|
| 35 | import org.openstreetmap.josm.tools.Utils;
|
---|
| 36 | import org.xml.sax.SAXException;
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * Task for downloading a set of primitives with all referrers.
|
---|
| 40 | */
|
---|
| 41 | public class DownloadPrimitivesWithReferrersTask extends PleaseWaitRunnable {
|
---|
| 42 | /** If true download into a new layer */
|
---|
| 43 | private final boolean newLayer;
|
---|
| 44 | /** List of primitives id to download */
|
---|
| 45 | private final List<PrimitiveId> ids;
|
---|
| 46 | /** If true, download members for relation */
|
---|
| 47 | private final boolean full;
|
---|
| 48 | /** If true, download also referrers */
|
---|
| 49 | private final boolean downloadReferrers;
|
---|
| 50 |
|
---|
| 51 | /** Temporary layer where downloaded primitives are put */
|
---|
[8426] | 52 | private final OsmDataLayer tmpLayer;
|
---|
[6973] | 53 | /** Reference to the task that download requested primitives */
|
---|
| 54 | private DownloadPrimitivesTask mainTask;
|
---|
| 55 | /** Flag indicated that user ask for cancel this task */
|
---|
[8840] | 56 | private boolean canceled;
|
---|
[6973] | 57 | /** Reference to the task currently running */
|
---|
[8840] | 58 | private PleaseWaitRunnable currentTask;
|
---|
[6973] | 59 |
|
---|
| 60 | /**
|
---|
| 61 | * Constructor
|
---|
| 62 | *
|
---|
| 63 | * @param newLayer if the data should be downloaded into a new layer
|
---|
| 64 | * @param ids List of primitive id to download
|
---|
| 65 | * @param downloadReferrers if the referrers of the object should be downloaded as well,
|
---|
| 66 | * i.e., parent relations, and for nodes, additionally, parent ways
|
---|
| 67 | * @param full if the members of a relation should be downloaded as well
|
---|
[8221] | 68 | * @param newLayerName the name to use for the new layer, can be {@code null}.
|
---|
[6973] | 69 | * @param monitor ProgressMonitor to use, or null to create a new one
|
---|
| 70 | */
|
---|
| 71 | public DownloadPrimitivesWithReferrersTask(boolean newLayer, List<PrimitiveId> ids, boolean downloadReferrers,
|
---|
[8221] | 72 | boolean full, String newLayerName, ProgressMonitor monitor) {
|
---|
[6973] | 73 | super(tr("Download objects"), monitor, false);
|
---|
| 74 | this.ids = ids;
|
---|
| 75 | this.downloadReferrers = downloadReferrers;
|
---|
| 76 | this.full = full;
|
---|
| 77 | this.newLayer = newLayer;
|
---|
| 78 | // All downloaded primitives are put in a tmpLayer
|
---|
[8221] | 79 | tmpLayer = new OsmDataLayer(new DataSet(), newLayerName != null ? newLayerName : OsmDataLayer.createNewName(), null);
|
---|
[6973] | 80 | }
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
| 83 | * Cancel recursively the task. Do not call directly
|
---|
[7592] | 84 | * @see DownloadPrimitivesWithReferrersTask#operationCanceled()
|
---|
[6973] | 85 | */
|
---|
| 86 | @Override
|
---|
| 87 | protected void cancel() {
|
---|
[8510] | 88 | synchronized (this) {
|
---|
[6973] | 89 | canceled = true;
|
---|
[8510] | 90 | if (currentTask != null)
|
---|
[6973] | 91 | currentTask.operationCanceled();
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | @Override
|
---|
| 96 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
| 97 | getProgressMonitor().setTicksCount(ids.size()+1);
|
---|
| 98 | // First, download primitives
|
---|
| 99 | mainTask = new DownloadPrimitivesTask(tmpLayer, ids, full, getProgressMonitor().createSubTaskMonitor(1, false));
|
---|
[8510] | 100 | synchronized (this) {
|
---|
[6980] | 101 | currentTask = mainTask;
|
---|
[8510] | 102 | if (canceled) {
|
---|
[6973] | 103 | currentTask = null;
|
---|
| 104 | return;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | currentTask.run();
|
---|
| 108 | // Then, download referrers for each primitive
|
---|
[8510] | 109 | if (downloadReferrers)
|
---|
| 110 | for (PrimitiveId id : ids) {
|
---|
| 111 | synchronized (this) {
|
---|
| 112 | if (canceled) {
|
---|
[6973] | 113 | currentTask = null;
|
---|
| 114 | return;
|
---|
| 115 | }
|
---|
| 116 | currentTask = new DownloadReferrersTask(
|
---|
| 117 | tmpLayer, id, getProgressMonitor().createSubTaskMonitor(1, false));
|
---|
| 118 | }
|
---|
| 119 | currentTask.run();
|
---|
| 120 | }
|
---|
| 121 | currentTask = null;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | @Override
|
---|
| 125 | protected void finish() {
|
---|
[8510] | 126 | synchronized (this) {
|
---|
| 127 | if (canceled)
|
---|
[6980] | 128 | return;
|
---|
| 129 | }
|
---|
[6973] | 130 |
|
---|
| 131 | // Append downloaded data to JOSM
|
---|
[12636] | 132 | OsmDataLayer layer = MainApplication.getLayerManager().getEditLayer();
|
---|
[8510] | 133 | if (layer == null || this.newLayer)
|
---|
[12636] | 134 | MainApplication.getLayerManager().addLayer(tmpLayer);
|
---|
[6973] | 135 | else
|
---|
| 136 | layer.mergeFrom(tmpLayer);
|
---|
| 137 |
|
---|
| 138 | // Warm about missing primitives
|
---|
| 139 | final Set<PrimitiveId> errs = mainTask.getMissingPrimitives();
|
---|
| 140 | if (errs != null && !errs.isEmpty())
|
---|
[10621] | 141 | GuiHelper.runInEDTAndWait(() -> reportProblemDialog(errs,
|
---|
[10611] | 142 | trn("Object could not be downloaded", "Some objects could not be downloaded", errs.size()),
|
---|
| 143 | trn("One object could not be downloaded.<br>",
|
---|
| 144 | "{0} objects could not be downloaded.<br>",
|
---|
| 145 | errs.size(),
|
---|
| 146 | errs.size())
|
---|
| 147 | + tr("The server replied with response code 404.<br>"
|
---|
| 148 | + "This usually means, the server does not know an object with the requested id."),
|
---|
| 149 | tr("missing objects:"),
|
---|
| 150 | JOptionPane.ERROR_MESSAGE
|
---|
[10621] | 151 | ).showDialog());
|
---|
[6973] | 152 |
|
---|
| 153 | // Warm about deleted primitives
|
---|
[7005] | 154 | final Set<PrimitiveId> del = new HashSet<>();
|
---|
[12636] | 155 | DataSet ds = MainApplication.getLayerManager().getEditDataSet();
|
---|
[6973] | 156 | for (PrimitiveId id : ids) {
|
---|
| 157 | OsmPrimitive osm = ds.getPrimitiveById(id);
|
---|
| 158 | if (osm != null && osm.isDeleted()) {
|
---|
| 159 | del.add(id);
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 | if (!del.isEmpty())
|
---|
[10621] | 163 | GuiHelper.runInEDTAndWait(() -> reportProblemDialog(del,
|
---|
[10611] | 164 | trn("Object deleted", "Objects deleted", del.size()),
|
---|
| 165 | trn(
|
---|
| 166 | "One downloaded object is deleted.",
|
---|
| 167 | "{0} downloaded objects are deleted.",
|
---|
| 168 | del.size(),
|
---|
| 169 | del.size()),
|
---|
| 170 | null,
|
---|
| 171 | JOptionPane.WARNING_MESSAGE
|
---|
[10621] | 172 | ).showDialog());
|
---|
[6973] | 173 | }
|
---|
| 174 |
|
---|
| 175 | /**
|
---|
| 176 | * Return id of really downloaded primitives.
|
---|
| 177 | * @return List of primitives id or null if no primitives was downloaded
|
---|
| 178 | */
|
---|
| 179 | public List<PrimitiveId> getDownloadedId() {
|
---|
[8510] | 180 | synchronized (this) {
|
---|
| 181 | if (canceled)
|
---|
[6980] | 182 | return null;
|
---|
| 183 | }
|
---|
[8338] | 184 | List<PrimitiveId> downloaded = new ArrayList<>(ids);
|
---|
[6973] | 185 | downloaded.removeAll(mainTask.getMissingPrimitives());
|
---|
| 186 | return downloaded;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | /**
|
---|
| 190 | * Dialog for report a problem during download.
|
---|
| 191 | * @param errs Primitives involved
|
---|
[6977] | 192 | * @param title Title of dialog
|
---|
| 193 | * @param text Detail message
|
---|
| 194 | * @param listLabel List of primitives description
|
---|
| 195 | * @param msgType Type of message, see {@link JOptionPane}
|
---|
[6973] | 196 | * @return The Dialog object
|
---|
| 197 | */
|
---|
| 198 | private static ExtendedDialog reportProblemDialog(Set<PrimitiveId> errs,
|
---|
[6977] | 199 | String title, String text, String listLabel, int msgType) {
|
---|
[6973] | 200 | JPanel p = new JPanel(new GridBagLayout());
|
---|
[6977] | 201 | p.add(new HtmlPanel(text), GBC.eop());
|
---|
[8426] | 202 | JosmTextArea txt = new JosmTextArea();
|
---|
[6977] | 203 | if (listLabel != null) {
|
---|
| 204 | JLabel missing = new JLabel(listLabel);
|
---|
[6973] | 205 | missing.setFont(missing.getFont().deriveFont(Font.PLAIN));
|
---|
[8426] | 206 | missing.setLabelFor(txt);
|
---|
[6973] | 207 | p.add(missing, GBC.eol());
|
---|
| 208 | }
|
---|
[7896] | 209 | txt.setFont(GuiHelper.getMonospacedFont(txt));
|
---|
[6973] | 210 | txt.setEditable(false);
|
---|
| 211 | txt.setBackground(p.getBackground());
|
---|
| 212 | txt.setColumns(40);
|
---|
| 213 | txt.setRows(1);
|
---|
| 214 | txt.setText(Utils.join(", ", errs));
|
---|
| 215 | JScrollPane scroll = new JScrollPane(txt);
|
---|
| 216 | p.add(scroll, GBC.eop().weight(1.0, 0.0).fill(GBC.HORIZONTAL));
|
---|
| 217 |
|
---|
| 218 | return new ExtendedDialog(
|
---|
| 219 | Main.parent,
|
---|
[6977] | 220 | title,
|
---|
[12279] | 221 | tr("Ok"))
|
---|
| 222 | .setButtonIcons("ok")
|
---|
[6973] | 223 | .setIcon(msgType)
|
---|
| 224 | .setContent(p, false);
|
---|
| 225 | }
|
---|
[6986] | 226 | }
|
---|