1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.download;
|
---|
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.Color;
|
---|
9 | import java.awt.Component;
|
---|
10 | import java.awt.Dimension;
|
---|
11 | import java.awt.FlowLayout;
|
---|
12 | import java.awt.Font;
|
---|
13 | import java.awt.Graphics;
|
---|
14 | import java.awt.GridBagConstraints;
|
---|
15 | import java.awt.GridBagLayout;
|
---|
16 | import java.awt.event.ActionEvent;
|
---|
17 | import java.awt.event.InputEvent;
|
---|
18 | import java.awt.event.KeyEvent;
|
---|
19 | import java.awt.event.WindowAdapter;
|
---|
20 | import java.awt.event.WindowEvent;
|
---|
21 | import java.util.ArrayList;
|
---|
22 | import java.util.List;
|
---|
23 |
|
---|
24 | import javax.swing.AbstractAction;
|
---|
25 | import javax.swing.JCheckBox;
|
---|
26 | import javax.swing.JComponent;
|
---|
27 | import javax.swing.JDialog;
|
---|
28 | import javax.swing.JLabel;
|
---|
29 | import javax.swing.JOptionPane;
|
---|
30 | import javax.swing.JPanel;
|
---|
31 | import javax.swing.JTabbedPane;
|
---|
32 | import javax.swing.KeyStroke;
|
---|
33 |
|
---|
34 | import org.openstreetmap.josm.Main;
|
---|
35 | import org.openstreetmap.josm.data.Bounds;
|
---|
36 | import org.openstreetmap.josm.gui.MapView;
|
---|
37 | import org.openstreetmap.josm.gui.SideButton;
|
---|
38 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
|
---|
39 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
40 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
41 | import org.openstreetmap.josm.tools.GBC;
|
---|
42 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
43 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
44 | import org.openstreetmap.josm.tools.Utils;
|
---|
45 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | *
|
---|
49 | */
|
---|
50 | public class DownloadDialog extends JDialog {
|
---|
51 | /** the unique instance of the download dialog */
|
---|
52 | static private DownloadDialog instance;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Replies the unique instance of the download dialog
|
---|
56 | *
|
---|
57 | * @return the unique instance of the download dialog
|
---|
58 | */
|
---|
59 | static public DownloadDialog getInstance() {
|
---|
60 | if (instance == null) {
|
---|
61 | instance = new DownloadDialog(Main.parent);
|
---|
62 | }
|
---|
63 | return instance;
|
---|
64 | }
|
---|
65 |
|
---|
66 | private final List<DownloadSelection> downloadSelections = new ArrayList<DownloadSelection>();
|
---|
67 | private final JTabbedPane tpDownloadAreaSelectors = new JTabbedPane();
|
---|
68 | private JCheckBox cbNewLayer;
|
---|
69 | private final JLabel sizeCheck = new JLabel();
|
---|
70 | private Bounds currentBounds = null;
|
---|
71 | private boolean canceled;
|
---|
72 |
|
---|
73 | private JCheckBox cbDownloadOsmData;
|
---|
74 | private JCheckBox cbDownloadGpxData;
|
---|
75 | /** the download action and button */
|
---|
76 | private DownloadAction actDownload;
|
---|
77 | private SideButton btnDownload;
|
---|
78 |
|
---|
79 | private void makeCheckBoxRespondToEnter(JCheckBox cb) {
|
---|
80 | cb.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0), "doDownload");
|
---|
81 | cb.getActionMap().put("doDownload", actDownload);
|
---|
82 | }
|
---|
83 |
|
---|
84 | public JPanel buildMainPanel() {
|
---|
85 | // generic constraints used by different components
|
---|
86 | GridBagConstraints gridBagConstraints;
|
---|
87 |
|
---|
88 | JPanel pnl = new JPanel();
|
---|
89 | pnl.setLayout(new GridBagLayout());
|
---|
90 |
|
---|
91 | // adding the download tasks
|
---|
92 | pnl.add(new JLabel(tr("Data Sources and Types:")), GBC.std().insets(5,5,1,5));
|
---|
93 | cbDownloadOsmData = new JCheckBox(tr("OpenStreetMap data"), true);
|
---|
94 | cbDownloadOsmData.setToolTipText(tr("Select to download OSM data in the selected download area."));
|
---|
95 | pnl.add(cbDownloadOsmData, GBC.std().insets(1,5,1,5));
|
---|
96 | cbDownloadGpxData = new JCheckBox(tr("Raw GPS data"));
|
---|
97 | cbDownloadGpxData.setToolTipText(tr("Select to download GPS traces in the selected download area."));
|
---|
98 | pnl.add(cbDownloadGpxData, GBC.eol().insets(5,5,1,5));
|
---|
99 |
|
---|
100 | // predefined download selections
|
---|
101 | downloadSelections.add(new SlippyMapChooser());
|
---|
102 | downloadSelections.add(new BookmarkSelection());
|
---|
103 | downloadSelections.add(new BoundingBoxSelection());
|
---|
104 | downloadSelections.add(new PlaceSelection());
|
---|
105 | downloadSelections.add(new TileSelection());
|
---|
106 |
|
---|
107 | // add selections from plugins
|
---|
108 | PluginHandler.addDownloadSelection(downloadSelections);
|
---|
109 |
|
---|
110 | // now everybody may add their tab to the tabbed pane
|
---|
111 | // (not done right away to allow plugins to remove one of
|
---|
112 | // the default selectors!)
|
---|
113 | for (DownloadSelection s : downloadSelections) {
|
---|
114 | s.addGui(this);
|
---|
115 | }
|
---|
116 |
|
---|
117 | pnl.add(tpDownloadAreaSelectors, GBC.eol().fill());
|
---|
118 |
|
---|
119 | try {
|
---|
120 | tpDownloadAreaSelectors.setSelectedIndex(Main.pref.getInteger("download.tab", 0));
|
---|
121 | } catch (Exception ex) {
|
---|
122 | Main.pref.putInteger("download.tab", 0);
|
---|
123 | }
|
---|
124 |
|
---|
125 | Font labelFont = sizeCheck.getFont();
|
---|
126 | sizeCheck.setFont(labelFont.deriveFont(Font.PLAIN, labelFont.getSize()));
|
---|
127 |
|
---|
128 | cbNewLayer = new JCheckBox(tr("Download as new layer"));
|
---|
129 | cbNewLayer.setToolTipText(tr("<html>Select to download data into a new data layer.<br>"
|
---|
130 | +"Unselect to download into the currently active data layer.</html>"));
|
---|
131 |
|
---|
132 | pnl.add(cbNewLayer, GBC.std().anchor(GBC.WEST).insets(5,5,5,5));
|
---|
133 | pnl.add(sizeCheck, GBC.eol().anchor(GBC.EAST).insets(5,5,5,5));
|
---|
134 |
|
---|
135 | return pnl;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /* This should not be necessary, but if not here, repaint is not always correct in SlippyMap! */
|
---|
139 | public void paint(Graphics g) {
|
---|
140 | tpDownloadAreaSelectors.getSelectedComponent().paint(g);
|
---|
141 | super.paint(g);
|
---|
142 | }
|
---|
143 |
|
---|
144 | protected JPanel buildButtonPanel() {
|
---|
145 | JPanel pnl = new JPanel();
|
---|
146 | pnl.setLayout(new FlowLayout());
|
---|
147 |
|
---|
148 | // -- download button
|
---|
149 | pnl.add(btnDownload = new SideButton(actDownload = new DownloadAction()));
|
---|
150 | btnDownload.setFocusable(true);
|
---|
151 | btnDownload.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0), "download");
|
---|
152 | btnDownload.getActionMap().put("download",actDownload);
|
---|
153 | makeCheckBoxRespondToEnter(cbDownloadGpxData);
|
---|
154 | makeCheckBoxRespondToEnter(cbDownloadOsmData);
|
---|
155 | makeCheckBoxRespondToEnter(cbNewLayer);
|
---|
156 |
|
---|
157 | // -- cancel button
|
---|
158 | SideButton btnCancel;
|
---|
159 | CancelAction actCancel = new CancelAction();
|
---|
160 | pnl.add(btnCancel = new SideButton(actCancel));
|
---|
161 | btnCancel.setFocusable(true);
|
---|
162 | btnCancel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0), "enter");
|
---|
163 | btnCancel.getActionMap().put("enter",actCancel);
|
---|
164 |
|
---|
165 | // -- cancel on ESC
|
---|
166 | getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0), "cancel");
|
---|
167 | getRootPane().getActionMap().put("cancel", actCancel);
|
---|
168 |
|
---|
169 | // -- help button
|
---|
170 | SideButton btnHelp;
|
---|
171 | pnl.add(btnHelp = new SideButton(new ContextSensitiveHelpAction(ht("/Dialog/Download"))));
|
---|
172 | btnHelp.setFocusable(true);
|
---|
173 | btnHelp.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0), "enter");
|
---|
174 | btnHelp.getActionMap().put("enter",btnHelp.getAction());
|
---|
175 |
|
---|
176 | return pnl;
|
---|
177 | }
|
---|
178 |
|
---|
179 | public DownloadDialog(Component parent) {
|
---|
180 | super(JOptionPane.getFrameForComponent(parent),tr("Download"), ModalityType.DOCUMENT_MODAL);
|
---|
181 | getContentPane().setLayout(new BorderLayout());
|
---|
182 | getContentPane().add(buildMainPanel(), BorderLayout.CENTER);
|
---|
183 | getContentPane().add(buildButtonPanel(), BorderLayout.SOUTH);
|
---|
184 |
|
---|
185 | getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
|
---|
186 | KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK), "checkClipboardContents");
|
---|
187 |
|
---|
188 | getRootPane().getActionMap().put("checkClipboardContents", new AbstractAction() {
|
---|
189 | public void actionPerformed(ActionEvent e) {
|
---|
190 | String clip = Utils.getClipboardContent();
|
---|
191 | if (clip == null) {
|
---|
192 | return;
|
---|
193 | }
|
---|
194 | Bounds b = OsmUrlToBounds.parse(clip);
|
---|
195 | if (b != null) {
|
---|
196 | boundingBoxChanged(new Bounds(b), null);
|
---|
197 | }
|
---|
198 | }
|
---|
199 | });
|
---|
200 | HelpUtil.setHelpContext(getRootPane(), ht("/Dialog/Download"));
|
---|
201 | addWindowListener(new WindowEventHandler());
|
---|
202 | restoreSettings();
|
---|
203 | }
|
---|
204 |
|
---|
205 | private void updateSizeCheck() {
|
---|
206 | if (currentBounds == null) {
|
---|
207 | sizeCheck.setText(tr("No area selected yet"));
|
---|
208 | sizeCheck.setForeground(Color.darkGray);
|
---|
209 | } else if (currentBounds.getArea() > Main.pref.getDouble("osm-server.max-request-area", 0.25)) {
|
---|
210 | sizeCheck.setText(tr("Download area too large; will probably be rejected by server"));
|
---|
211 | sizeCheck.setForeground(Color.red);
|
---|
212 | } else {
|
---|
213 | sizeCheck.setText(tr("Download area ok, size probably acceptable to server"));
|
---|
214 | sizeCheck.setForeground(Color.darkGray);
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Distributes a "bounding box changed" from one DownloadSelection
|
---|
220 | * object to the others, so they may update or clear their input
|
---|
221 | * fields.
|
---|
222 | *
|
---|
223 | * @param eventSource - the DownloadSelection object that fired this notification.
|
---|
224 | */
|
---|
225 | public void boundingBoxChanged(Bounds b, DownloadSelection eventSource) {
|
---|
226 | this.currentBounds = b;
|
---|
227 | for (DownloadSelection s : downloadSelections) {
|
---|
228 | if (s != eventSource) {
|
---|
229 | s.setDownloadArea(currentBounds);
|
---|
230 | }
|
---|
231 | }
|
---|
232 | updateSizeCheck();
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Invoked by
|
---|
237 | * @param b
|
---|
238 | */
|
---|
239 | public void startDownload(Bounds b) {
|
---|
240 | this.currentBounds = b;
|
---|
241 | actDownload.run();
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Replies true if the user selected to download OSM data
|
---|
246 | *
|
---|
247 | * @return true if the user selected to download OSM data
|
---|
248 | */
|
---|
249 | public boolean isDownloadOsmData() {
|
---|
250 | return cbDownloadOsmData.isSelected();
|
---|
251 | }
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * Replies true if the user selected to download GPX data
|
---|
255 | *
|
---|
256 | * @return true if the user selected to download GPX data
|
---|
257 | */
|
---|
258 | public boolean isDownloadGpxData() {
|
---|
259 | return cbDownloadGpxData.isSelected();
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Replies true if the user requires to download into a new layer
|
---|
264 | *
|
---|
265 | * @return true if the user requires to download into a new layer
|
---|
266 | */
|
---|
267 | public boolean isNewLayerRequired() {
|
---|
268 | return cbNewLayer.isSelected();
|
---|
269 | }
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Adds a new download area selector to the download dialog
|
---|
273 | *
|
---|
274 | * @param selector the download are selector
|
---|
275 | * @param displayName the display name of the selector
|
---|
276 | */
|
---|
277 | public void addDownloadAreaSelector(JPanel selector, String displayName) {
|
---|
278 | tpDownloadAreaSelectors.add(displayName, selector);
|
---|
279 | }
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Remembers the current settings in the download dialog
|
---|
283 | *
|
---|
284 | */
|
---|
285 | public void rememberSettings() {
|
---|
286 | Main.pref.put("download.tab", Integer.toString(tpDownloadAreaSelectors.getSelectedIndex()));
|
---|
287 | Main.pref.put("download.osm", cbDownloadOsmData.isSelected());
|
---|
288 | Main.pref.put("download.gps", cbDownloadGpxData.isSelected());
|
---|
289 | Main.pref.put("download.newlayer", cbNewLayer.isSelected());
|
---|
290 | if (currentBounds != null) {
|
---|
291 | Main.pref.put("osm-download.bounds", currentBounds.encodeAsString(";"));
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | public void restoreSettings() {
|
---|
296 | cbDownloadOsmData.setSelected(Main.pref.getBoolean("download.osm", true));
|
---|
297 | cbDownloadGpxData.setSelected(Main.pref.getBoolean("download.gps", false));
|
---|
298 | cbNewLayer.setSelected(Main.pref.getBoolean("download.newlayer", false));
|
---|
299 | int idx = Main.pref.getInteger("download.tab", 0);
|
---|
300 | if (idx < 0 || idx > tpDownloadAreaSelectors.getTabCount()) {
|
---|
301 | idx = 0;
|
---|
302 | }
|
---|
303 | tpDownloadAreaSelectors.setSelectedIndex(idx);
|
---|
304 |
|
---|
305 | if (Main.map != null) {
|
---|
306 | MapView mv = Main.map.mapView;
|
---|
307 | currentBounds = new Bounds(
|
---|
308 | mv.getLatLon(0, mv.getHeight()),
|
---|
309 | mv.getLatLon(mv.getWidth(), 0)
|
---|
310 | );
|
---|
311 | boundingBoxChanged(currentBounds,null);
|
---|
312 | }
|
---|
313 | else if (Main.pref.hasKey("osm-download.bounds")) {
|
---|
314 | // read the bounding box from the preferences
|
---|
315 | try {
|
---|
316 | currentBounds = new Bounds(Main.pref.get("osm-download.bounds"), ";");
|
---|
317 | boundingBoxChanged(currentBounds,null);
|
---|
318 | }
|
---|
319 | catch (Exception e) {
|
---|
320 | e.printStackTrace();
|
---|
321 | }
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Replies the currently selected download area. May be null, if no download area is selected
|
---|
327 | * yet.
|
---|
328 | */
|
---|
329 | public Bounds getSelectedDownloadArea() {
|
---|
330 | return currentBounds;
|
---|
331 | }
|
---|
332 |
|
---|
333 | @Override
|
---|
334 | public void setVisible(boolean visible) {
|
---|
335 | if (visible) {
|
---|
336 | new WindowGeometry(
|
---|
337 | getClass().getName() + ".geometry",
|
---|
338 | WindowGeometry.centerInWindow(
|
---|
339 | getParent(),
|
---|
340 | new Dimension(1000,600)
|
---|
341 | )
|
---|
342 | ).applySafe(this);
|
---|
343 | } else if (!visible && isShowing()){
|
---|
344 | new WindowGeometry(this).remember(getClass().getName() + ".geometry");
|
---|
345 | }
|
---|
346 | super.setVisible(visible);
|
---|
347 | }
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Replies true if the dialog was canceled
|
---|
351 | *
|
---|
352 | * @return true if the dialog was canceled
|
---|
353 | */
|
---|
354 | public boolean isCanceled() {
|
---|
355 | return canceled;
|
---|
356 | }
|
---|
357 |
|
---|
358 | protected void setCanceled(boolean canceled) {
|
---|
359 | this.canceled = canceled;
|
---|
360 | }
|
---|
361 |
|
---|
362 | class CancelAction extends AbstractAction {
|
---|
363 | public CancelAction() {
|
---|
364 | putValue(NAME, tr("Cancel"));
|
---|
365 | putValue(SMALL_ICON, ImageProvider.get("cancel"));
|
---|
366 | putValue(SHORT_DESCRIPTION, tr("Click to close the dialog and to abort downloading"));
|
---|
367 | }
|
---|
368 |
|
---|
369 | public void run() {
|
---|
370 | setCanceled(true);
|
---|
371 | setVisible(false);
|
---|
372 | }
|
---|
373 |
|
---|
374 | public void actionPerformed(ActionEvent e) {
|
---|
375 | run();
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | class DownloadAction extends AbstractAction {
|
---|
380 | public DownloadAction() {
|
---|
381 | putValue(NAME, tr("Download"));
|
---|
382 | putValue(SMALL_ICON, ImageProvider.get("download"));
|
---|
383 | putValue(SHORT_DESCRIPTION, tr("Click to download the currently selected area"));
|
---|
384 | }
|
---|
385 |
|
---|
386 | public void run() {
|
---|
387 | if (currentBounds == null) {
|
---|
388 | JOptionPane.showMessageDialog(
|
---|
389 | DownloadDialog.this,
|
---|
390 | tr("Please select a download area first."),
|
---|
391 | tr("Error"),
|
---|
392 | JOptionPane.ERROR_MESSAGE
|
---|
393 | );
|
---|
394 | return;
|
---|
395 | }
|
---|
396 | if (!isDownloadOsmData() && !isDownloadGpxData()) {
|
---|
397 | JOptionPane.showMessageDialog(
|
---|
398 | DownloadDialog.this,
|
---|
399 | tr("<html>Neither <strong>{0}</strong> nor <strong>{1}</strong> is enabled.<br>"
|
---|
400 | + "Please choose to either download OSM data, or GPX data, or both.</html>",
|
---|
401 | cbDownloadOsmData.getText(),
|
---|
402 | cbDownloadGpxData.getText()
|
---|
403 | ),
|
---|
404 | tr("Error"),
|
---|
405 | JOptionPane.ERROR_MESSAGE
|
---|
406 | );
|
---|
407 | return;
|
---|
408 | }
|
---|
409 | setCanceled(false);
|
---|
410 | setVisible(false);
|
---|
411 | }
|
---|
412 |
|
---|
413 | public void actionPerformed(ActionEvent e) {
|
---|
414 | run();
|
---|
415 | }
|
---|
416 | }
|
---|
417 |
|
---|
418 | class WindowEventHandler extends WindowAdapter {
|
---|
419 | @Override
|
---|
420 | public void windowClosing(WindowEvent e) {
|
---|
421 | new CancelAction().run();
|
---|
422 | }
|
---|
423 |
|
---|
424 | @Override
|
---|
425 | public void windowActivated(WindowEvent e) {
|
---|
426 | btnDownload.requestFocusInWindow();
|
---|
427 | }
|
---|
428 | }
|
---|
429 | }
|
---|