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