[2923] | 1 | // License: GPL. See LICENSE file for details.
|
---|
| 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;
|
---|
[4081] | 6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
[2923] | 7 |
|
---|
[4081] | 8 | import java.awt.Font;
|
---|
[2923] | 9 | import java.awt.GridBagLayout;
|
---|
| 10 | import java.awt.event.ActionEvent;
|
---|
| 11 | import java.awt.event.KeyEvent;
|
---|
[4081] | 12 | import java.lang.reflect.InvocationTargetException;
|
---|
| 13 | import java.util.List;
|
---|
| 14 | import java.util.Set;
|
---|
| 15 | import java.util.TreeSet;
|
---|
[2923] | 16 |
|
---|
| 17 | import javax.swing.JLabel;
|
---|
[3285] | 18 | import javax.swing.JOptionPane;
|
---|
[2923] | 19 | import javax.swing.JPanel;
|
---|
[4081] | 20 | import javax.swing.JScrollPane;
|
---|
| 21 | import javax.swing.SwingUtilities;
|
---|
[2923] | 22 |
|
---|
| 23 | import org.openstreetmap.josm.Main;
|
---|
| 24 | import org.openstreetmap.josm.actions.downloadtasks.DownloadReferrersTask;
|
---|
| 25 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[4081] | 26 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 27 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
[2923] | 28 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
[5765] | 29 | import org.openstreetmap.josm.gui.download.DownloadObjectDialog;
|
---|
[4081] | 30 | import org.openstreetmap.josm.gui.io.DownloadPrimitivesTask;
|
---|
[2923] | 31 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[4081] | 32 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
[5886] | 33 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
[4081] | 34 | import org.openstreetmap.josm.tools.GBC;
|
---|
[2923] | 35 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[4081] | 36 | import org.openstreetmap.josm.tools.Utils;
|
---|
[2923] | 37 |
|
---|
| 38 | /**
|
---|
| 39 | * Download an OsmPrimitive by specifying type and ID
|
---|
| 40 | *
|
---|
| 41 | * @author Matthias Julius
|
---|
| 42 | */
|
---|
| 43 | public class DownloadPrimitiveAction extends JosmAction {
|
---|
| 44 |
|
---|
[5886] | 45 | /**
|
---|
| 46 | * Constructs a new {@code DownloadPrimitiveAction}.
|
---|
| 47 | */
|
---|
[2923] | 48 | public DownloadPrimitiveAction() {
|
---|
| 49 | super(tr("Download object..."), "downloadprimitive", tr("Download OSM object by ID."),
|
---|
[4982] | 50 | Shortcut.registerShortcut("system:download_primitive", tr("File: {0}", tr("Download object...")), KeyEvent.VK_O, Shortcut.CTRL_SHIFT), true);
|
---|
[2923] | 51 | putValue("help", ht("/Action/DownloadObject"));
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[6084] | 54 | @Override
|
---|
[2923] | 55 | public void actionPerformed(ActionEvent e) {
|
---|
[4096] | 56 |
|
---|
[5765] | 57 | DownloadObjectDialog dialog = new DownloadObjectDialog();
|
---|
| 58 | if (dialog.showDialog().getValue() != 1) return;
|
---|
[4096] | 59 |
|
---|
[5765] | 60 | processItems(dialog.isNewLayerRequested(), dialog.getOsmIds(), dialog.isReferrersRequested(), dialog.isFullRelationRequested());
|
---|
[2923] | 61 | }
|
---|
[4096] | 62 |
|
---|
[4796] | 63 | /**
|
---|
| 64 | * @param newLayer if the data should be downloaded into a new layer
|
---|
| 65 | * @param ids
|
---|
| 66 | * @param downloadReferrers if the referrers of the object should be downloaded as well, i.e., parent relations, and for nodes, additionally, parent ways
|
---|
| 67 | * @param full if the members of a relation should be downloaded as well
|
---|
| 68 | */
|
---|
| 69 | public static void processItems(boolean newLayer, final List<PrimitiveId> ids, boolean downloadReferrers, boolean full) {
|
---|
[2923] | 70 | OsmDataLayer layer = getEditLayer();
|
---|
| 71 | if ((layer == null) || newLayer) {
|
---|
| 72 | layer = new OsmDataLayer(new DataSet(), OsmDataLayer.createNewName(), null);
|
---|
| 73 | Main.main.addLayer(layer);
|
---|
| 74 | }
|
---|
[4497] | 75 | final DownloadPrimitivesTask task = new DownloadPrimitivesTask(layer, ids, full);
|
---|
[4081] | 76 | Main.worker.submit(task);
|
---|
[4096] | 77 |
|
---|
[2923] | 78 | if (downloadReferrers) {
|
---|
[4081] | 79 | for (PrimitiveId id : ids) {
|
---|
| 80 | Main.worker.submit(new DownloadReferrersTask(layer, id));
|
---|
| 81 | }
|
---|
[2923] | 82 | }
|
---|
[4096] | 83 |
|
---|
[4081] | 84 | Runnable showErrorsAndWarnings = new Runnable() {
|
---|
| 85 | @Override
|
---|
| 86 | public void run() {
|
---|
[5765] | 87 | final Set<PrimitiveId> errs = task.getMissingPrimitives();
|
---|
[4081] | 88 | if (errs != null && !errs.isEmpty()) {
|
---|
| 89 | try {
|
---|
| 90 | SwingUtilities.invokeAndWait(new Runnable() {
|
---|
| 91 | @Override
|
---|
| 92 | public void run() {
|
---|
[5765] | 93 | reportProblemDialog(errs,
|
---|
| 94 | trn("Object could not be downloaded", "Some objects could not be downloaded", errs.size()),
|
---|
| 95 | trn("One object could not be downloaded.<br>",
|
---|
| 96 | "{0} objects could not be downloaded.<br>",
|
---|
| 97 | errs.size(),
|
---|
| 98 | errs.size())
|
---|
| 99 | + tr("The server replied with response code 404.<br>"
|
---|
| 100 | + "This usually means, the server does not know an object with the requested id."),
|
---|
| 101 | tr("missing objects:"),
|
---|
| 102 | JOptionPane.ERROR_MESSAGE
|
---|
| 103 | ).showDialog();
|
---|
[4081] | 104 | }
|
---|
| 105 | });
|
---|
| 106 | } catch (InterruptedException ex) {
|
---|
| 107 | } catch (InvocationTargetException ex) {
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
[4096] | 110 |
|
---|
[5765] | 111 | final Set<PrimitiveId> del = new TreeSet<PrimitiveId>();
|
---|
[4081] | 112 | DataSet ds = getCurrentDataSet();
|
---|
| 113 | for (PrimitiveId id : ids) {
|
---|
| 114 | OsmPrimitive osm = ds.getPrimitiveById(id);
|
---|
| 115 | if (osm != null && osm.isDeleted()) {
|
---|
| 116 | del.add(id);
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
[4343] | 119 | if (!del.isEmpty()) {
|
---|
[4081] | 120 | SwingUtilities.invokeLater(new Runnable() {
|
---|
| 121 | @Override
|
---|
| 122 | public void run() {
|
---|
[5765] | 123 | reportProblemDialog(del,
|
---|
| 124 | trn("Object deleted", "Objects deleted", del.size()),
|
---|
| 125 | trn(
|
---|
| 126 | "One downloaded object is deleted.",
|
---|
| 127 | "{0} downloaded objects are deleted.",
|
---|
| 128 | del.size(),
|
---|
| 129 | del.size()),
|
---|
| 130 | null,
|
---|
| 131 | JOptionPane.WARNING_MESSAGE
|
---|
| 132 | ).showDialog();
|
---|
[4081] | 133 | }
|
---|
| 134 | });
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | };
|
---|
| 138 | Main.worker.submit(showErrorsAndWarnings);
|
---|
[2923] | 139 | }
|
---|
[4096] | 140 |
|
---|
[4796] | 141 | private static ExtendedDialog reportProblemDialog(Set<PrimitiveId> errs,
|
---|
[4081] | 142 | String TITLE, String TEXT, String LIST_LABEL, int msgType) {
|
---|
| 143 | JPanel p = new JPanel(new GridBagLayout());
|
---|
| 144 | p.add(new HtmlPanel(TEXT), GBC.eop());
|
---|
| 145 | if (LIST_LABEL != null) {
|
---|
| 146 | JLabel missing = new JLabel(LIST_LABEL);
|
---|
| 147 | missing.setFont(missing.getFont().deriveFont(Font.PLAIN));
|
---|
| 148 | p.add(missing, GBC.eol());
|
---|
| 149 | }
|
---|
[5886] | 150 | JosmTextArea txt = new JosmTextArea();
|
---|
[4081] | 151 | txt.setFont(new Font("Monospaced", txt.getFont().getStyle(), txt.getFont().getSize()));
|
---|
| 152 | txt.setEditable(false);
|
---|
| 153 | txt.setBackground(p.getBackground());
|
---|
| 154 | txt.setColumns(40);
|
---|
| 155 | txt.setRows(1);
|
---|
| 156 | txt.setText(Utils.join(", ", errs));
|
---|
| 157 | JScrollPane scroll = new JScrollPane(txt);
|
---|
| 158 | p.add(scroll, GBC.eop().weight(1.0, 0.0).fill(GBC.HORIZONTAL));
|
---|
| 159 |
|
---|
| 160 | return new ExtendedDialog(
|
---|
| 161 | Main.parent,
|
---|
[4096] | 162 | TITLE,
|
---|
[4081] | 163 | new String[] { tr("Ok") })
|
---|
| 164 | .setButtonIcons(new String[] { "ok" })
|
---|
| 165 | .setIcon(msgType)
|
---|
| 166 | .setContent(p, false);
|
---|
| 167 | }
|
---|
[2923] | 168 | }
|
---|