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

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

see #4429 - Right click menu "undo, cut, copy, paste, delete, select all" for each text component (originally based on patch by NooN)

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