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

Last change on this file since 5765 was 5765, checked in by Don-vip, 11 years ago

see #8494 - Refactor of OSM id text fields / changeset id text fields management, add changeset id auto paste in changeset manager, fix EDT violations

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