1 | // License: GPL. For details, see LICENSE file.
|
---|
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.BorderLayout;
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.GridLayout;
|
---|
10 | import java.awt.Rectangle;
|
---|
11 | import java.awt.event.ActionEvent;
|
---|
12 | import java.awt.event.ActionListener;
|
---|
13 | import java.awt.event.FocusEvent;
|
---|
14 | import java.awt.event.FocusListener;
|
---|
15 | import java.awt.event.KeyEvent;
|
---|
16 | import java.util.ArrayList;
|
---|
17 | import java.util.Collection;
|
---|
18 | import java.util.Collections;
|
---|
19 | import java.util.Deque;
|
---|
20 | import java.util.LinkedList;
|
---|
21 | import java.util.concurrent.Future;
|
---|
22 |
|
---|
23 | import javax.swing.AbstractAction;
|
---|
24 | import javax.swing.Action;
|
---|
25 | import javax.swing.ActionMap;
|
---|
26 | import javax.swing.JButton;
|
---|
27 | import javax.swing.JComponent;
|
---|
28 | import javax.swing.JLabel;
|
---|
29 | import javax.swing.JMenuItem;
|
---|
30 | import javax.swing.JOptionPane;
|
---|
31 | import javax.swing.JPanel;
|
---|
32 | import javax.swing.JPopupMenu;
|
---|
33 | import javax.swing.JScrollPane;
|
---|
34 | import javax.swing.plaf.basic.BasicArrowButton;
|
---|
35 |
|
---|
36 | import org.openstreetmap.josm.Main;
|
---|
37 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
38 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
|
---|
39 | import org.openstreetmap.josm.data.Bounds;
|
---|
40 | import org.openstreetmap.josm.data.preferences.CollectionProperty;
|
---|
41 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
42 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
43 | import org.openstreetmap.josm.gui.download.DownloadDialog;
|
---|
44 | import org.openstreetmap.josm.gui.preferences.server.OverpassServerPreference;
|
---|
45 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
46 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
|
---|
47 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
48 | import org.openstreetmap.josm.io.OverpassDownloadReader;
|
---|
49 | import org.openstreetmap.josm.tools.GBC;
|
---|
50 | import org.openstreetmap.josm.tools.OverpassTurboQueryWizard;
|
---|
51 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
52 | import org.openstreetmap.josm.tools.UncheckedParseException;
|
---|
53 | import org.openstreetmap.josm.tools.Utils;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Download map data from Overpass API server.
|
---|
57 | * @since 8684
|
---|
58 | */
|
---|
59 | public class OverpassDownloadAction extends JosmAction {
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Constructs a new {@code OverpassDownloadAction}.
|
---|
63 | */
|
---|
64 | public OverpassDownloadAction() {
|
---|
65 | super(tr("Download from Overpass API ..."), "download-overpass", tr("Download map data from Overpass API server."),
|
---|
66 | // CHECKSTYLE.OFF: LineLength
|
---|
67 | Shortcut.registerShortcut("file:download-overpass", tr("File: {0}", tr("Download from Overpass API ...")), KeyEvent.VK_DOWN, Shortcut.ALT_SHIFT),
|
---|
68 | // CHECKSTYLE.ON: LineLength
|
---|
69 | true, "overpassdownload/download", true);
|
---|
70 | putValue("help", ht("/Action/OverpassDownload"));
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public void actionPerformed(ActionEvent e) {
|
---|
75 | OverpassDownloadDialog dialog = OverpassDownloadDialog.getInstance();
|
---|
76 | dialog.restoreSettings();
|
---|
77 | dialog.setVisible(true);
|
---|
78 | if (!dialog.isCanceled()) {
|
---|
79 | dialog.rememberSettings();
|
---|
80 | Bounds area = dialog.getSelectedDownloadArea();
|
---|
81 | DownloadOsmTask task = new DownloadOsmTask();
|
---|
82 | Future<?> future = task.download(
|
---|
83 | new OverpassDownloadReader(area, OverpassServerPreference.getOverpassServer(), dialog.getOverpassQuery()),
|
---|
84 | dialog.isNewLayerRequired(), area, null);
|
---|
85 | Main.worker.submit(new PostDownloadHandler(task, future));
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | private static final class DisableActionsFocusListener implements FocusListener {
|
---|
90 |
|
---|
91 | private final ActionMap actionMap;
|
---|
92 |
|
---|
93 | private DisableActionsFocusListener(ActionMap actionMap) {
|
---|
94 | this.actionMap = actionMap;
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | public void focusGained(FocusEvent e) {
|
---|
99 | enableActions(false);
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Override
|
---|
103 | public void focusLost(FocusEvent e) {
|
---|
104 | enableActions(true);
|
---|
105 | }
|
---|
106 |
|
---|
107 | private void enableActions(boolean enabled) {
|
---|
108 | for (Object key : actionMap.allKeys()) {
|
---|
109 | Action action = actionMap.get(key);
|
---|
110 | if (action != null) {
|
---|
111 | action.setEnabled(enabled);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | private static final class OverpassDownloadDialog extends DownloadDialog {
|
---|
118 |
|
---|
119 | private HistoryComboBox overpassWizard;
|
---|
120 | private JosmTextArea overpassQuery;
|
---|
121 | private static OverpassDownloadDialog instance;
|
---|
122 | private static final CollectionProperty OVERPASS_WIZARD_HISTORY = new CollectionProperty("download.overpass.wizard",
|
---|
123 | new ArrayList<String>());
|
---|
124 |
|
---|
125 | private OverpassDownloadDialog(Component parent) {
|
---|
126 | super(parent, ht("/Action/OverpassDownload"));
|
---|
127 | cbDownloadOsmData.setEnabled(false);
|
---|
128 | cbDownloadOsmData.setSelected(false);
|
---|
129 | cbDownloadGpxData.setVisible(false);
|
---|
130 | cbDownloadNotes.setVisible(false);
|
---|
131 | cbStartup.setVisible(false);
|
---|
132 | }
|
---|
133 |
|
---|
134 | public static OverpassDownloadDialog getInstance() {
|
---|
135 | if (instance == null) {
|
---|
136 | instance = new OverpassDownloadDialog(Main.parent);
|
---|
137 | }
|
---|
138 | return instance;
|
---|
139 | }
|
---|
140 |
|
---|
141 | @Override
|
---|
142 | protected void buildMainPanelAboveDownloadSelections(JPanel pnl) {
|
---|
143 |
|
---|
144 | DisableActionsFocusListener disableActionsFocusListener =
|
---|
145 | new DisableActionsFocusListener(slippyMapChooser.getNavigationComponentActionMap());
|
---|
146 |
|
---|
147 | pnl.add(new JLabel(), GBC.eol()); // needed for the invisible checkboxes cbDownloadGpxData, cbDownloadNotes
|
---|
148 |
|
---|
149 | final String tooltip = tr("Builds an Overpass query using the Overpass Turbo query wizard");
|
---|
150 | overpassWizard = new HistoryComboBox();
|
---|
151 | overpassWizard.setToolTipText(tooltip);
|
---|
152 | overpassWizard.getEditorComponent().addFocusListener(disableActionsFocusListener);
|
---|
153 | final JButton buildQuery = new JButton(tr("Build query"));
|
---|
154 | buildQuery.addActionListener(new AbstractAction() {
|
---|
155 | @Override
|
---|
156 | public void actionPerformed(ActionEvent e) {
|
---|
157 | final String overpassWizardText = overpassWizard.getText();
|
---|
158 | try {
|
---|
159 | overpassQuery.setText(OverpassTurboQueryWizard.getInstance().constructQuery(overpassWizardText));
|
---|
160 | } catch (UncheckedParseException ex) {
|
---|
161 | HelpAwareOptionPane.showOptionDialog(
|
---|
162 | Main.parent,
|
---|
163 | tr("<html>The Overpass wizard could not parse the following query:"
|
---|
164 | + Utils.joinAsHtmlUnorderedList(Collections.singleton(overpassWizardText))),
|
---|
165 | tr("Parse error"),
|
---|
166 | JOptionPane.ERROR_MESSAGE,
|
---|
167 | null
|
---|
168 | );
|
---|
169 | }
|
---|
170 | }
|
---|
171 | });
|
---|
172 | buildQuery.setToolTipText(tooltip);
|
---|
173 | pnl.add(buildQuery, GBC.std().insets(5, 5, 5, 5));
|
---|
174 | pnl.add(overpassWizard, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
175 |
|
---|
176 | overpassQuery = new JosmTextArea("", 8, 80);
|
---|
177 | overpassQuery.setFont(GuiHelper.getMonospacedFont(overpassQuery));
|
---|
178 | overpassQuery.addFocusListener(disableActionsFocusListener);
|
---|
179 | JScrollPane scrollPane = new JScrollPane(overpassQuery);
|
---|
180 | final JPanel pane = new JPanel(new BorderLayout());
|
---|
181 | final BasicArrowButton arrowButton = new BasicArrowButton(BasicArrowButton.SOUTH);
|
---|
182 | arrowButton.addActionListener(new AbstractAction() {
|
---|
183 | @Override
|
---|
184 | public void actionPerformed(ActionEvent e) {
|
---|
185 | OverpassQueryHistoryPopup.show(arrowButton, OverpassDownloadDialog.this);
|
---|
186 | }
|
---|
187 | });
|
---|
188 | pane.add(scrollPane, BorderLayout.CENTER);
|
---|
189 | pane.add(arrowButton, BorderLayout.EAST);
|
---|
190 | pnl.add(new JLabel(tr("Overpass query: ")), GBC.std().insets(5, 5, 5, 5));
|
---|
191 | GBC gbc = GBC.eol().fill(GBC.HORIZONTAL);
|
---|
192 | gbc.ipady = 200;
|
---|
193 | pnl.add(pane, gbc);
|
---|
194 |
|
---|
195 | }
|
---|
196 |
|
---|
197 | public String getOverpassQuery() {
|
---|
198 | return overpassQuery.getText();
|
---|
199 | }
|
---|
200 |
|
---|
201 | public void setOverpassQuery(String text) {
|
---|
202 | overpassQuery.setText(text);
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Override
|
---|
206 | public void restoreSettings() {
|
---|
207 | super.restoreSettings();
|
---|
208 | overpassWizard.setPossibleItems(OVERPASS_WIZARD_HISTORY.get());
|
---|
209 | }
|
---|
210 |
|
---|
211 | @Override
|
---|
212 | public void rememberSettings() {
|
---|
213 | super.rememberSettings();
|
---|
214 | overpassWizard.addCurrentItemToHistory();
|
---|
215 | OVERPASS_WIZARD_HISTORY.put(overpassWizard.getHistory());
|
---|
216 | OverpassQueryHistoryPopup.addToHistory(getOverpassQuery());
|
---|
217 | }
|
---|
218 |
|
---|
219 | }
|
---|
220 |
|
---|
221 | static class OverpassQueryHistoryPopup extends JPopupMenu {
|
---|
222 |
|
---|
223 | static final CollectionProperty OVERPASS_QUERY_HISTORY = new CollectionProperty("download.overpass.query", new ArrayList<String>());
|
---|
224 | static final IntegerProperty OVERPASS_QUERY_HISTORY_SIZE = new IntegerProperty("download.overpass.query.size", 12);
|
---|
225 |
|
---|
226 | OverpassQueryHistoryPopup(final OverpassDownloadDialog dialog) {
|
---|
227 | final Collection<String> history = OVERPASS_QUERY_HISTORY.get();
|
---|
228 | setLayout(new GridLayout((int) Math.ceil(history.size() / 2.), 2));
|
---|
229 | for (final String i : history) {
|
---|
230 | add(new OverpassQueryHistoryItem(i, dialog));
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | static void show(final JComponent parent, final OverpassDownloadDialog dialog) {
|
---|
235 | final OverpassQueryHistoryPopup menu = new OverpassQueryHistoryPopup(dialog);
|
---|
236 | final Rectangle r = parent.getBounds();
|
---|
237 | menu.show(parent.getParent(), r.x + r.width - (int) menu.getPreferredSize().getWidth(), r.y + r.height);
|
---|
238 | }
|
---|
239 |
|
---|
240 | static void addToHistory(final String query) {
|
---|
241 | final Deque<String> history = new LinkedList<>(OVERPASS_QUERY_HISTORY.get());
|
---|
242 | if (!history.contains(query)) {
|
---|
243 | history.add(query);
|
---|
244 | }
|
---|
245 | while (history.size() > OVERPASS_QUERY_HISTORY_SIZE.get()) {
|
---|
246 | history.removeFirst();
|
---|
247 | }
|
---|
248 | OVERPASS_QUERY_HISTORY.put(history);
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | static class OverpassQueryHistoryItem extends JMenuItem implements ActionListener {
|
---|
253 |
|
---|
254 | final String query;
|
---|
255 | final OverpassDownloadDialog dialog;
|
---|
256 |
|
---|
257 | OverpassQueryHistoryItem(final String query, final OverpassDownloadDialog dialog) {
|
---|
258 | this.query = query;
|
---|
259 | this.dialog = dialog;
|
---|
260 | setText("<html><pre style='width:300px;'>" +
|
---|
261 | Utils.escapeReservedCharactersHTML(Utils.restrictStringLines(query, 7)));
|
---|
262 | addActionListener(this);
|
---|
263 | }
|
---|
264 |
|
---|
265 | @Override
|
---|
266 | public void actionPerformed(ActionEvent e) {
|
---|
267 | dialog.setOverpassQuery(query);
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | }
|
---|