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