source: josm/trunk/src/org/openstreetmap/josm/actions/DownloadPrimitiveAction.java@ 6244

Last change on this file since 6244 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • Property svn:eol-style set to native
File size: 7.1 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6import static org.openstreetmap.josm.tools.I18n.trn;
7
8import java.awt.Font;
9import java.awt.GridBagLayout;
10import java.awt.event.ActionEvent;
11import java.awt.event.KeyEvent;
12import java.lang.reflect.InvocationTargetException;
13import java.util.List;
14import java.util.Set;
15import java.util.TreeSet;
16
17import javax.swing.JLabel;
18import javax.swing.JOptionPane;
19import javax.swing.JPanel;
20import javax.swing.JScrollPane;
21import javax.swing.SwingUtilities;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.actions.downloadtasks.DownloadReferrersTask;
25import org.openstreetmap.josm.data.osm.DataSet;
26import org.openstreetmap.josm.data.osm.OsmPrimitive;
27import org.openstreetmap.josm.data.osm.PrimitiveId;
28import org.openstreetmap.josm.gui.ExtendedDialog;
29import org.openstreetmap.josm.gui.download.DownloadObjectDialog;
30import org.openstreetmap.josm.gui.io.DownloadPrimitivesTask;
31import org.openstreetmap.josm.gui.layer.OsmDataLayer;
32import org.openstreetmap.josm.gui.widgets.HtmlPanel;
33import org.openstreetmap.josm.gui.widgets.JosmTextArea;
34import org.openstreetmap.josm.tools.GBC;
35import org.openstreetmap.josm.tools.Shortcut;
36import org.openstreetmap.josm.tools.Utils;
37
38/**
39 * Download an OsmPrimitive by specifying type and ID
40 *
41 * @author Matthias Julius
42 */
43public 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() != 1) 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 } catch (InvocationTargetException ex) {
108 }
109 }
110
111 final Set<PrimitiveId> del = new TreeSet<PrimitiveId>();
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 }
119 if (!del.isEmpty()) {
120 SwingUtilities.invokeLater(new Runnable() {
121 @Override
122 public void run() {
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();
133 }
134 });
135 }
136 }
137 };
138 Main.worker.submit(showErrorsAndWarnings);
139 }
140
141 private static ExtendedDialog reportProblemDialog(Set<PrimitiveId> errs,
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 }
150 JosmTextArea txt = new JosmTextArea();
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,
162 TITLE,
163 new String[] { tr("Ok") })
164 .setButtonIcons(new String[] { "ok" })
165 .setIcon(msgType)
166 .setContent(p, false);
167 }
168}
Note: See TracBrowser for help on using the repository browser.