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 |
|
---|
7 | import java.awt.GridBagConstraints;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 |
|
---|
12 | import javax.swing.JCheckBox;
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 | import javax.swing.KeyStroke;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.actions.downloadtasks.DownloadPrimitiveTask;
|
---|
20 | import org.openstreetmap.josm.actions.downloadtasks.DownloadReferrersTask;
|
---|
21 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
22 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
23 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
24 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
25 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
26 | import org.openstreetmap.josm.gui.widgets.OsmIdTextField;
|
---|
27 | import org.openstreetmap.josm.gui.widgets.OsmPrimitiveTypesComboBox;
|
---|
28 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Download an OsmPrimitive by specifying type and ID
|
---|
32 | *
|
---|
33 | * @author Matthias Julius
|
---|
34 | */
|
---|
35 | public class DownloadPrimitiveAction extends JosmAction {
|
---|
36 |
|
---|
37 | public DownloadPrimitiveAction() {
|
---|
38 | super(tr("Download object..."), "downloadprimitive", tr("Download OSM object by ID."),
|
---|
39 | Shortcut.registerShortcut("system:download_primitive", tr("File: {0}", tr("Download Object...")), KeyEvent.VK_O, Shortcut.GROUP_MENU + Shortcut.GROUPS_ALT1), true);
|
---|
40 | putValue("help", ht("/Action/DownloadObject"));
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void actionPerformed(ActionEvent e) {
|
---|
44 | JCheckBox layer = new JCheckBox(tr("Separate Layer"));
|
---|
45 | layer.setToolTipText(tr("Select if the data should be downloaded into a new layer"));
|
---|
46 | layer.setSelected(Main.pref.getBoolean("download.newlayer"));
|
---|
47 | JCheckBox referrers = new JCheckBox(tr("Download referrers"));
|
---|
48 | referrers.setToolTipText(tr("Select if the referrers of the object should be downloaded as well"));
|
---|
49 | referrers.setSelected(Main.pref.getBoolean("downloadprimitive.referrers"));
|
---|
50 | JPanel all = new JPanel(new GridBagLayout());
|
---|
51 | GridBagConstraints gc = new GridBagConstraints();
|
---|
52 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
53 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
54 | gc.gridy = 0;
|
---|
55 | gc.weightx = 0;
|
---|
56 | all.add(new JLabel(tr("Object type:")), gc);
|
---|
57 | OsmPrimitiveTypesComboBox cbType = new OsmPrimitiveTypesComboBox();
|
---|
58 | cbType.setToolTipText(tr("Choose the OSM object type"));
|
---|
59 | gc.weightx = 1;
|
---|
60 | all.add(cbType, gc);
|
---|
61 | gc.gridy = 1;
|
---|
62 | gc.weightx = 0;
|
---|
63 | all.add(new JLabel(tr("Object ID:")), gc);
|
---|
64 | OsmIdTextField tfId = new OsmIdTextField();
|
---|
65 | tfId.setToolTipText(tr("Enter the ID of the object that should be downloaded"));
|
---|
66 | // forward the enter key stroke to the download button
|
---|
67 | tfId.getKeymap().removeKeyStrokeBinding(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false));
|
---|
68 | gc.weightx = 1;
|
---|
69 | all.add(tfId, gc);
|
---|
70 | gc.gridy = 2;
|
---|
71 | gc.fill = GridBagConstraints.BOTH;
|
---|
72 | gc.weighty = 1.0;
|
---|
73 | gc.weightx = 0;
|
---|
74 | all.add(referrers, gc);
|
---|
75 | gc.gridy = 3;
|
---|
76 | all.add(layer, gc);
|
---|
77 | ExtendedDialog dialog = new ExtendedDialog(Main.parent,
|
---|
78 | tr("Download Object"),
|
---|
79 | new String[] {tr("Download object"), tr("Cancel")}
|
---|
80 | );
|
---|
81 | dialog.setContent(all, false);
|
---|
82 | dialog.setButtonIcons(new String[] {"download.png", "cancel.png"});
|
---|
83 | dialog.setToolTipTexts(new String[] {
|
---|
84 | tr("Start downloading"),
|
---|
85 | tr("Close dialog and cancel downloading")
|
---|
86 | });
|
---|
87 | dialog.setDefaultButton(1);
|
---|
88 | dialog.configureContextsensitiveHelp("/Action/DownloadObject", true /* show help button */);
|
---|
89 | cbType.setSelectedIndex(Main.pref.getInteger("downloadprimitive.lasttype", 0));
|
---|
90 | dialog.showDialog();
|
---|
91 | if (dialog.getValue() != 1) return;
|
---|
92 | Main.pref.putInteger("downloadprimitive.lasttype", cbType.getSelectedIndex());
|
---|
93 | Main.pref.put("downloadprimitive.referrers", referrers.isSelected());
|
---|
94 | Main.pref.put("download.newlayer", layer.isSelected());
|
---|
95 | int id = tfId.getOsmId();
|
---|
96 | if(id <= 0)
|
---|
97 | JOptionPane.showMessageDialog(
|
---|
98 | Main.parent,
|
---|
99 | tr("Invalid ID specified. Cannot download object."),
|
---|
100 | tr("Information"),
|
---|
101 | JOptionPane.INFORMATION_MESSAGE
|
---|
102 | );
|
---|
103 | else
|
---|
104 | download(layer.isSelected(), cbType.getType(), id, referrers.isSelected());
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Download the given primitive.
|
---|
109 | */
|
---|
110 | public void download(boolean newLayer, OsmPrimitiveType type, int id, boolean downloadReferrers) {
|
---|
111 | OsmDataLayer layer = getEditLayer();
|
---|
112 | if ((layer == null) || newLayer) {
|
---|
113 | layer = new OsmDataLayer(new DataSet(), OsmDataLayer.createNewName(), null);
|
---|
114 | Main.main.addLayer(layer);
|
---|
115 | }
|
---|
116 | Main.worker.submit(new DownloadPrimitiveTask(new SimplePrimitiveId(id, type), layer));
|
---|
117 | if (downloadReferrers) {
|
---|
118 | Main.worker.submit(new DownloadReferrersTask(layer, id, type));
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|