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