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

Last change on this file since 3049 was 3030, checked in by bastiK, 14 years ago

fixed #4583 - "Download object" ignores Enter

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