[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[237] | 2 | package org.openstreetmap.josm.gui.download;
|
---|
| 3 |
|
---|
[2529] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[237] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
[2331] | 7 | import java.awt.BorderLayout;
|
---|
[1038] | 8 | import java.awt.Color;
|
---|
[2331] | 9 | import java.awt.Component;
|
---|
| 10 | import java.awt.Dimension;
|
---|
| 11 | import java.awt.FlowLayout;
|
---|
[1811] | 12 | import java.awt.Font;
|
---|
[4552] | 13 | import java.awt.Graphics;
|
---|
[1811] | 14 | import java.awt.GridBagLayout;
|
---|
[1329] | 15 | import java.awt.event.ActionEvent;
|
---|
| 16 | import java.awt.event.InputEvent;
|
---|
| 17 | import java.awt.event.KeyEvent;
|
---|
[2331] | 18 | import java.awt.event.WindowAdapter;
|
---|
| 19 | import java.awt.event.WindowEvent;
|
---|
[237] | 20 | import java.util.ArrayList;
|
---|
| 21 | import java.util.List;
|
---|
[12574] | 22 | import java.util.Optional;
|
---|
[237] | 23 |
|
---|
[1307] | 24 | import javax.swing.AbstractAction;
|
---|
[10424] | 25 | import javax.swing.JButton;
|
---|
[237] | 26 | import javax.swing.JCheckBox;
|
---|
[2331] | 27 | import javax.swing.JComponent;
|
---|
| 28 | import javax.swing.JDialog;
|
---|
[237] | 29 | import javax.swing.JLabel;
|
---|
[2331] | 30 | import javax.swing.JOptionPane;
|
---|
[237] | 31 | import javax.swing.JPanel;
|
---|
| 32 | import javax.swing.JTabbedPane;
|
---|
[1307] | 33 | import javax.swing.KeyStroke;
|
---|
[8215] | 34 | import javax.swing.event.ChangeListener;
|
---|
[237] | 35 |
|
---|
| 36 | import org.openstreetmap.josm.Main;
|
---|
[4906] | 37 | import org.openstreetmap.josm.actions.ExpertToggleAction;
|
---|
[1307] | 38 | import org.openstreetmap.josm.data.Bounds;
|
---|
[11270] | 39 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
| 40 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
[12630] | 41 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[237] | 42 | import org.openstreetmap.josm.gui.MapView;
|
---|
[10604] | 43 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
[2331] | 44 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
|
---|
| 45 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
[10035] | 46 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[7434] | 47 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
[1326] | 48 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
[237] | 49 | import org.openstreetmap.josm.tools.GBC;
|
---|
[2331] | 50 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[5200] | 51 | import org.openstreetmap.josm.tools.InputMapUtils;
|
---|
[12620] | 52 | import org.openstreetmap.josm.tools.Logging;
|
---|
[1307] | 53 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
[11270] | 54 | import org.openstreetmap.josm.tools.Utils;
|
---|
[2331] | 55 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
[237] | 56 |
|
---|
| 57 | /**
|
---|
[6883] | 58 | * Dialog displayed to download OSM and/or GPS data from OSM server.
|
---|
[237] | 59 | */
|
---|
[10378] | 60 | public class DownloadDialog extends JDialog {
|
---|
[11277] | 61 | private static final IntegerProperty DOWNLOAD_TAB = new IntegerProperty("download.tab", 0);
|
---|
[11270] | 62 |
|
---|
[11277] | 63 | private static final BooleanProperty DOWNLOAD_AUTORUN = new BooleanProperty("download.autorun", false);
|
---|
| 64 | private static final BooleanProperty DOWNLOAD_OSM = new BooleanProperty("download.osm", true);
|
---|
| 65 | private static final BooleanProperty DOWNLOAD_GPS = new BooleanProperty("download.gps", false);
|
---|
| 66 | private static final BooleanProperty DOWNLOAD_NOTES = new BooleanProperty("download.notes", false);
|
---|
| 67 | private static final BooleanProperty DOWNLOAD_NEWLAYER = new BooleanProperty("download.newlayer", false);
|
---|
[11658] | 68 | private static final BooleanProperty DOWNLOAD_ZOOMTODATA = new BooleanProperty("download.zoomtodata", true);
|
---|
[11270] | 69 |
|
---|
[2331] | 70 | /** the unique instance of the download dialog */
|
---|
[6883] | 71 | private static DownloadDialog instance;
|
---|
[2512] | 72 |
|
---|
[2331] | 73 | /**
|
---|
| 74 | * Replies the unique instance of the download dialog
|
---|
[2512] | 75 | *
|
---|
[2331] | 76 | * @return the unique instance of the download dialog
|
---|
| 77 | */
|
---|
[8126] | 78 | public static synchronized DownloadDialog getInstance() {
|
---|
[2529] | 79 | if (instance == null) {
|
---|
[2331] | 80 | instance = new DownloadDialog(Main.parent);
|
---|
[2529] | 81 | }
|
---|
[2331] | 82 | return instance;
|
---|
[2512] | 83 | }
|
---|
[806] | 84 |
|
---|
[6364] | 85 | protected SlippyMapChooser slippyMapChooser;
|
---|
[8308] | 86 | protected final transient List<DownloadSelection> downloadSelections = new ArrayList<>();
|
---|
[5097] | 87 | protected final JTabbedPane tpDownloadAreaSelectors = new JTabbedPane();
|
---|
| 88 | protected JCheckBox cbNewLayer;
|
---|
| 89 | protected JCheckBox cbStartup;
|
---|
[11658] | 90 | protected JCheckBox cbZoomToDownloadedData;
|
---|
[5097] | 91 | protected final JLabel sizeCheck = new JLabel();
|
---|
[8840] | 92 | protected transient Bounds currentBounds;
|
---|
[5097] | 93 | protected boolean canceled;
|
---|
[806] | 94 |
|
---|
[5097] | 95 | protected JCheckBox cbDownloadOsmData;
|
---|
| 96 | protected JCheckBox cbDownloadGpxData;
|
---|
[7722] | 97 | protected JCheckBox cbDownloadNotes;
|
---|
[2344] | 98 | /** the download action and button */
|
---|
[10179] | 99 | private final DownloadAction actDownload = new DownloadAction();
|
---|
[10424] | 100 | protected final JButton btnDownload = new JButton(actDownload);
|
---|
[806] | 101 |
|
---|
[6890] | 102 | protected final JPanel buildMainPanel() {
|
---|
[9543] | 103 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
[806] | 104 |
|
---|
[10611] | 105 | // size check depends on selected data source
|
---|
| 106 | final ChangeListener checkboxChangeListener = e -> updateSizeCheck();
|
---|
[8215] | 107 |
|
---|
[1169] | 108 | // adding the download tasks
|
---|
[8510] | 109 | pnl.add(new JLabel(tr("Data Sources and Types:")), GBC.std().insets(5, 5, 1, 5));
|
---|
[2345] | 110 | cbDownloadOsmData = new JCheckBox(tr("OpenStreetMap data"), true);
|
---|
| 111 | cbDownloadOsmData.setToolTipText(tr("Select to download OSM data in the selected download area."));
|
---|
[8215] | 112 | cbDownloadOsmData.getModel().addChangeListener(checkboxChangeListener);
|
---|
| 113 | pnl.add(cbDownloadOsmData, GBC.std().insets(1, 5, 1, 5));
|
---|
[2345] | 114 | cbDownloadGpxData = new JCheckBox(tr("Raw GPS data"));
|
---|
| 115 | cbDownloadGpxData.setToolTipText(tr("Select to download GPS traces in the selected download area."));
|
---|
[8215] | 116 | cbDownloadGpxData.getModel().addChangeListener(checkboxChangeListener);
|
---|
| 117 | pnl.add(cbDownloadGpxData, GBC.std().insets(5, 5, 1, 5));
|
---|
[7722] | 118 | cbDownloadNotes = new JCheckBox(tr("Notes"));
|
---|
| 119 | cbDownloadNotes.setToolTipText(tr("Select to download notes in the selected download area."));
|
---|
[8215] | 120 | cbDownloadNotes.getModel().addChangeListener(checkboxChangeListener);
|
---|
[8071] | 121 | pnl.add(cbDownloadNotes, GBC.eol().insets(50, 5, 1, 5));
|
---|
[2512] | 122 |
|
---|
[8932] | 123 | // must be created before hook
|
---|
| 124 | slippyMapChooser = new SlippyMapChooser();
|
---|
| 125 |
|
---|
[5113] | 126 | // hook for subclasses
|
---|
[5097] | 127 | buildMainPanelAboveDownloadSelections(pnl);
|
---|
| 128 |
|
---|
[1169] | 129 | // predefined download selections
|
---|
[6365] | 130 | downloadSelections.add(slippyMapChooser);
|
---|
[1392] | 131 | downloadSelections.add(new BookmarkSelection());
|
---|
[1169] | 132 | downloadSelections.add(new BoundingBoxSelection());
|
---|
[1392] | 133 | downloadSelections.add(new PlaceSelection());
|
---|
[1169] | 134 | downloadSelections.add(new TileSelection());
|
---|
[806] | 135 |
|
---|
[1169] | 136 | // add selections from plugins
|
---|
[1326] | 137 | PluginHandler.addDownloadSelection(downloadSelections);
|
---|
[806] | 138 |
|
---|
[1169] | 139 | // now everybody may add their tab to the tabbed pane
|
---|
| 140 | // (not done right away to allow plugins to remove one of
|
---|
| 141 | // the default selectors!)
|
---|
| 142 | for (DownloadSelection s : downloadSelections) {
|
---|
| 143 | s.addGui(this);
|
---|
| 144 | }
|
---|
[2512] | 145 |
|
---|
[2331] | 146 | pnl.add(tpDownloadAreaSelectors, GBC.eol().fill());
|
---|
[806] | 147 |
|
---|
[1169] | 148 | try {
|
---|
[11270] | 149 | tpDownloadAreaSelectors.setSelectedIndex(DOWNLOAD_TAB.get());
|
---|
[10212] | 150 | } catch (IndexOutOfBoundsException ex) {
|
---|
[12620] | 151 | Logging.trace(ex);
|
---|
[11270] | 152 | DOWNLOAD_TAB.put(0);
|
---|
[1169] | 153 | }
|
---|
[1038] | 154 |
|
---|
[1169] | 155 | Font labelFont = sizeCheck.getFont();
|
---|
| 156 | sizeCheck.setFont(labelFont.deriveFont(Font.PLAIN, labelFont.getSize()));
|
---|
[3104] | 157 |
|
---|
[3057] | 158 | cbNewLayer = new JCheckBox(tr("Download as new layer"));
|
---|
| 159 | cbNewLayer.setToolTipText(tr("<html>Select to download data into a new data layer.<br>"
|
---|
| 160 | +"Unselect to download into the currently active data layer.</html>"));
|
---|
| 161 |
|
---|
[4906] | 162 | cbStartup = new JCheckBox(tr("Open this dialog on startup"));
|
---|
[8509] | 163 | cbStartup.setToolTipText(
|
---|
[8510] | 164 | tr("<html>Autostart ''Download from OSM'' dialog every time JOSM is started.<br>" +
|
---|
| 165 | "You can open it manually from File menu or toolbar.</html>"));
|
---|
[11270] | 166 | cbStartup.addActionListener(e -> DOWNLOAD_AUTORUN.put(cbStartup.isSelected()));
|
---|
[4906] | 167 |
|
---|
[11658] | 168 | cbZoomToDownloadedData = new JCheckBox(tr("Zoom to downloaded data"));
|
---|
| 169 | cbZoomToDownloadedData.setToolTipText(tr("Select to zoom to entire newly downloaded data."));
|
---|
| 170 |
|
---|
[8510] | 171 | pnl.add(cbNewLayer, GBC.std().anchor(GBC.WEST).insets(5, 5, 5, 5));
|
---|
| 172 | pnl.add(cbStartup, GBC.std().anchor(GBC.WEST).insets(15, 5, 5, 5));
|
---|
[11658] | 173 | pnl.add(cbZoomToDownloadedData, GBC.std().anchor(GBC.WEST).insets(15, 5, 5, 5));
|
---|
[3057] | 174 |
|
---|
[11658] | 175 | ExpertToggleAction.addVisibilitySwitcher(cbZoomToDownloadedData);
|
---|
| 176 |
|
---|
[10378] | 177 | pnl.add(sizeCheck, GBC.eol().anchor(GBC.EAST).insets(5, 5, 5, 2));
|
---|
[6070] | 178 |
|
---|
[4906] | 179 | if (!ExpertToggleAction.isExpert()) {
|
---|
[10378] | 180 | JLabel infoLabel = new JLabel(
|
---|
[8509] | 181 | tr("Use left click&drag to select area, arrows or right mouse button to scroll map, wheel or +/- to zoom."));
|
---|
[8510] | 182 | pnl.add(infoLabel, GBC.eol().anchor(GBC.SOUTH).insets(0, 0, 0, 0));
|
---|
[4906] | 183 | }
|
---|
[2331] | 184 | return pnl;
|
---|
| 185 | }
|
---|
[2512] | 186 |
|
---|
[4552] | 187 | /* This should not be necessary, but if not here, repaint is not always correct in SlippyMap! */
|
---|
[6084] | 188 | @Override
|
---|
[4552] | 189 | public void paint(Graphics g) {
|
---|
| 190 | tpDownloadAreaSelectors.getSelectedComponent().paint(g);
|
---|
| 191 | super.paint(g);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[6890] | 194 | protected final JPanel buildButtonPanel() {
|
---|
[9543] | 195 | JPanel pnl = new JPanel(new FlowLayout());
|
---|
[2512] | 196 |
|
---|
[2347] | 197 | // -- download button
|
---|
[10179] | 198 | pnl.add(btnDownload);
|
---|
[5200] | 199 | InputMapUtils.enableEnter(btnDownload);
|
---|
[6070] | 200 |
|
---|
[10790] | 201 | InputMapUtils.addEnterActionWhenAncestor(cbDownloadGpxData, actDownload);
|
---|
| 202 | InputMapUtils.addEnterActionWhenAncestor(cbDownloadOsmData, actDownload);
|
---|
| 203 | InputMapUtils.addEnterActionWhenAncestor(cbDownloadNotes, actDownload);
|
---|
| 204 | InputMapUtils.addEnterActionWhenAncestor(cbNewLayer, actDownload);
|
---|
[11658] | 205 | InputMapUtils.addEnterActionWhenAncestor(cbStartup, actDownload);
|
---|
| 206 | InputMapUtils.addEnterActionWhenAncestor(cbZoomToDownloadedData, actDownload);
|
---|
[2347] | 207 |
|
---|
[2512] | 208 | // -- cancel button
|
---|
[10424] | 209 | JButton btnCancel;
|
---|
[2347] | 210 | CancelAction actCancel = new CancelAction();
|
---|
[10424] | 211 | btnCancel = new JButton(actCancel);
|
---|
[10179] | 212 | pnl.add(btnCancel);
|
---|
[5200] | 213 | InputMapUtils.enableEnter(btnCancel);
|
---|
[2347] | 214 |
|
---|
[2512] | 215 | // -- cancel on ESC
|
---|
[10791] | 216 | InputMapUtils.addEscapeAction(getRootPane(), actCancel);
|
---|
[2347] | 217 |
|
---|
[2512] | 218 | // -- help button
|
---|
[10424] | 219 | JButton btnHelp = new JButton(new ContextSensitiveHelpAction(getRootPane().getClientProperty("help").toString()));
|
---|
[10179] | 220 | pnl.add(btnHelp);
|
---|
[5200] | 221 | InputMapUtils.enableEnter(btnHelp);
|
---|
[2512] | 222 |
|
---|
| 223 | return pnl;
|
---|
[2331] | 224 | }
|
---|
[2512] | 225 |
|
---|
[8071] | 226 | /**
|
---|
| 227 | * Constructs a new {@code DownloadDialog}.
|
---|
| 228 | * @param parent the parent component
|
---|
| 229 | */
|
---|
[2331] | 230 | public DownloadDialog(Component parent) {
|
---|
[8713] | 231 | this(parent, ht("/Action/Download"));
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /**
|
---|
| 235 | * Constructs a new {@code DownloadDialog}.
|
---|
| 236 | * @param parent the parent component
|
---|
| 237 | * @param helpTopic the help topic to assign
|
---|
| 238 | */
|
---|
| 239 | public DownloadDialog(Component parent, String helpTopic) {
|
---|
[10035] | 240 | super(GuiHelper.getFrameForComponent(parent), tr("Download"), ModalityType.DOCUMENT_MODAL);
|
---|
[8713] | 241 | HelpUtil.setHelpContext(getRootPane(), helpTopic);
|
---|
[2331] | 242 | getContentPane().setLayout(new BorderLayout());
|
---|
| 243 | getContentPane().add(buildMainPanel(), BorderLayout.CENTER);
|
---|
| 244 | getContentPane().add(buildButtonPanel(), BorderLayout.SOUTH);
|
---|
[2512] | 245 |
|
---|
[2331] | 246 | getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
|
---|
[12523] | 247 | KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_DOWN_MASK), "checkClipboardContents");
|
---|
[1329] | 248 |
|
---|
[2331] | 249 | getRootPane().getActionMap().put("checkClipboardContents", new AbstractAction() {
|
---|
[6084] | 250 | @Override
|
---|
[1329] | 251 | public void actionPerformed(ActionEvent e) {
|
---|
[10604] | 252 | String clip = ClipboardUtils.getClipboardStringContent();
|
---|
[4380] | 253 | if (clip == null) {
|
---|
| 254 | return;
|
---|
| 255 | }
|
---|
| 256 | Bounds b = OsmUrlToBounds.parse(clip);
|
---|
| 257 | if (b != null) {
|
---|
| 258 | boundingBoxChanged(new Bounds(b), null);
|
---|
| 259 | }
|
---|
[1329] | 260 | }
|
---|
| 261 | });
|
---|
[2331] | 262 | addWindowListener(new WindowEventHandler());
|
---|
[2327] | 263 | restoreSettings();
|
---|
[1169] | 264 | }
|
---|
[806] | 265 |
|
---|
[10629] | 266 | protected void updateSizeCheck() {
|
---|
[8215] | 267 | boolean isAreaTooLarge = false;
|
---|
[2327] | 268 | if (currentBounds == null) {
|
---|
| 269 | sizeCheck.setText(tr("No area selected yet"));
|
---|
| 270 | sizeCheck.setForeground(Color.darkGray);
|
---|
[8215] | 271 | } else if (isDownloadNotes() && !isDownloadOsmData() && !isDownloadGpxData()) {
|
---|
| 272 | // see max_note_request_area in https://github.com/openstreetmap/openstreetmap-website/blob/master/config/example.application.yml
|
---|
| 273 | isAreaTooLarge = currentBounds.getArea() > Main.pref.getDouble("osm-server.max-request-area-notes", 25);
|
---|
| 274 | } else {
|
---|
| 275 | // see max_request_area in https://github.com/openstreetmap/openstreetmap-website/blob/master/config/example.application.yml
|
---|
| 276 | isAreaTooLarge = currentBounds.getArea() > Main.pref.getDouble("osm-server.max-request-area", 0.25);
|
---|
| 277 | }
|
---|
[10629] | 278 | displaySizeCheckResult(isAreaTooLarge);
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | protected void displaySizeCheckResult(boolean isAreaTooLarge) {
|
---|
[8215] | 282 | if (isAreaTooLarge) {
|
---|
[1169] | 283 | sizeCheck.setText(tr("Download area too large; will probably be rejected by server"));
|
---|
| 284 | sizeCheck.setForeground(Color.red);
|
---|
| 285 | } else {
|
---|
| 286 | sizeCheck.setText(tr("Download area ok, size probably acceptable to server"));
|
---|
| 287 | sizeCheck.setForeground(Color.darkGray);
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
[1038] | 290 |
|
---|
[1169] | 291 | /**
|
---|
| 292 | * Distributes a "bounding box changed" from one DownloadSelection
|
---|
[8071] | 293 | * object to the others, so they may update or clear their input fields.
|
---|
[9230] | 294 | * @param b new current bounds
|
---|
[1169] | 295 | *
|
---|
| 296 | * @param eventSource - the DownloadSelection object that fired this notification.
|
---|
| 297 | */
|
---|
[2512] | 298 | public void boundingBoxChanged(Bounds b, DownloadSelection eventSource) {
|
---|
[2327] | 299 | this.currentBounds = b;
|
---|
[1169] | 300 | for (DownloadSelection s : downloadSelections) {
|
---|
[2142] | 301 | if (s != eventSource) {
|
---|
[2332] | 302 | s.setDownloadArea(currentBounds);
|
---|
[2142] | 303 | }
|
---|
[1169] | 304 | }
|
---|
| 305 | updateSizeCheck();
|
---|
| 306 | }
|
---|
[2512] | 307 |
|
---|
[2327] | 308 | /**
|
---|
[8470] | 309 | * Starts download for the given bounding box
|
---|
| 310 | * @param b bounding box to download
|
---|
[2344] | 311 | */
|
---|
| 312 | public void startDownload(Bounds b) {
|
---|
| 313 | this.currentBounds = b;
|
---|
| 314 | actDownload.run();
|
---|
| 315 | }
|
---|
[2512] | 316 |
|
---|
[2344] | 317 | /**
|
---|
[2327] | 318 | * Replies true if the user selected to download OSM data
|
---|
[2512] | 319 | *
|
---|
[2327] | 320 | * @return true if the user selected to download OSM data
|
---|
[1169] | 321 | */
|
---|
[2327] | 322 | public boolean isDownloadOsmData() {
|
---|
| 323 | return cbDownloadOsmData.isSelected();
|
---|
[1169] | 324 | }
|
---|
[2512] | 325 |
|
---|
[2327] | 326 | /**
|
---|
| 327 | * Replies true if the user selected to download GPX data
|
---|
[2512] | 328 | *
|
---|
[2327] | 329 | * @return true if the user selected to download GPX data
|
---|
| 330 | */
|
---|
| 331 | public boolean isDownloadGpxData() {
|
---|
| 332 | return cbDownloadGpxData.isSelected();
|
---|
| 333 | }
|
---|
[2512] | 334 |
|
---|
[2327] | 335 | /**
|
---|
[7722] | 336 | * Replies true if user selected to download notes
|
---|
| 337 | *
|
---|
| 338 | * @return true if user selected to download notes
|
---|
| 339 | */
|
---|
| 340 | public boolean isDownloadNotes() {
|
---|
| 341 | return cbDownloadNotes.isSelected();
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | /**
|
---|
[2512] | 345 | * Replies true if the user requires to download into a new layer
|
---|
| 346 | *
|
---|
| 347 | * @return true if the user requires to download into a new layer
|
---|
[2327] | 348 | */
|
---|
| 349 | public boolean isNewLayerRequired() {
|
---|
| 350 | return cbNewLayer.isSelected();
|
---|
| 351 | }
|
---|
[2512] | 352 |
|
---|
[2327] | 353 | /**
|
---|
[11658] | 354 | * Replies true if the user requires to zoom to new downloaded data
|
---|
| 355 | *
|
---|
| 356 | * @return true if the user requires to zoom to new downloaded data
|
---|
| 357 | * @since 11658
|
---|
| 358 | */
|
---|
| 359 | public boolean isZoomToDownloadedDataRequired() {
|
---|
| 360 | return cbZoomToDownloadedData.isSelected();
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | /**
|
---|
[2327] | 364 | * Adds a new download area selector to the download dialog
|
---|
[2512] | 365 | *
|
---|
| 366 | * @param selector the download are selector
|
---|
[2327] | 367 | * @param displayName the display name of the selector
|
---|
| 368 | */
|
---|
| 369 | public void addDownloadAreaSelector(JPanel selector, String displayName) {
|
---|
| 370 | tpDownloadAreaSelectors.add(displayName, selector);
|
---|
| 371 | }
|
---|
[2512] | 372 |
|
---|
[2327] | 373 | /**
|
---|
[6364] | 374 | * Refreshes the tile sources
|
---|
| 375 | * @since 6364
|
---|
| 376 | */
|
---|
| 377 | public final void refreshTileSources() {
|
---|
| 378 | if (slippyMapChooser != null) {
|
---|
| 379 | slippyMapChooser.refreshTileSources();
|
---|
| 380 | }
|
---|
| 381 | }
|
---|
[7434] | 382 |
|
---|
[6364] | 383 | /**
|
---|
[6509] | 384 | * Remembers the current settings in the download dialog.
|
---|
[2327] | 385 | */
|
---|
| 386 | public void rememberSettings() {
|
---|
[11270] | 387 | DOWNLOAD_TAB.put(tpDownloadAreaSelectors.getSelectedIndex());
|
---|
| 388 | DOWNLOAD_OSM.put(cbDownloadOsmData.isSelected());
|
---|
| 389 | DOWNLOAD_GPS.put(cbDownloadGpxData.isSelected());
|
---|
| 390 | DOWNLOAD_NOTES.put(cbDownloadNotes.isSelected());
|
---|
| 391 | DOWNLOAD_NEWLAYER.put(cbNewLayer.isSelected());
|
---|
[11658] | 392 | DOWNLOAD_ZOOMTODATA.put(cbZoomToDownloadedData.isSelected());
|
---|
[2327] | 393 | if (currentBounds != null) {
|
---|
| 394 | Main.pref.put("osm-download.bounds", currentBounds.encodeAsString(";"));
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
[2512] | 397 |
|
---|
[6509] | 398 | /**
|
---|
| 399 | * Restores the previous settings in the download dialog.
|
---|
| 400 | */
|
---|
[2327] | 401 | public void restoreSettings() {
|
---|
[11270] | 402 | cbDownloadOsmData.setSelected(DOWNLOAD_OSM.get());
|
---|
| 403 | cbDownloadGpxData.setSelected(DOWNLOAD_GPS.get());
|
---|
| 404 | cbDownloadNotes.setSelected(DOWNLOAD_NOTES.get());
|
---|
| 405 | cbNewLayer.setSelected(DOWNLOAD_NEWLAYER.get());
|
---|
[8444] | 406 | cbStartup.setSelected(isAutorunEnabled());
|
---|
[11658] | 407 | cbZoomToDownloadedData.setSelected(DOWNLOAD_ZOOMTODATA.get());
|
---|
[11270] | 408 | int idx = Utils.clamp(DOWNLOAD_TAB.get(), 0, tpDownloadAreaSelectors.getTabCount() - 1);
|
---|
[2327] | 409 | tpDownloadAreaSelectors.setSelectedIndex(idx);
|
---|
[2512] | 410 |
|
---|
[12630] | 411 | if (MainApplication.isDisplayingMapView()) {
|
---|
| 412 | MapView mv = MainApplication.getMap().mapView;
|
---|
[2327] | 413 | currentBounds = new Bounds(
|
---|
| 414 | mv.getLatLon(0, mv.getHeight()),
|
---|
[2512] | 415 | mv.getLatLon(mv.getWidth(), 0)
|
---|
[2529] | 416 | );
|
---|
[8510] | 417 | boundingBoxChanged(currentBounds, null);
|
---|
[8342] | 418 | } else {
|
---|
[6509] | 419 | Bounds bounds = getSavedDownloadBounds();
|
---|
| 420 | if (bounds != null) {
|
---|
| 421 | currentBounds = bounds;
|
---|
| 422 | boundingBoxChanged(currentBounds, null);
|
---|
| 423 | }
|
---|
| 424 | }
|
---|
| 425 | }
|
---|
[7434] | 426 |
|
---|
[6509] | 427 | /**
|
---|
| 428 | * Returns the previously saved bounding box from preferences.
|
---|
| 429 | * @return The bounding box saved in preferences if any, {@code null} otherwise
|
---|
| 430 | * @since 6509
|
---|
| 431 | */
|
---|
| 432 | public static Bounds getSavedDownloadBounds() {
|
---|
| 433 | String value = Main.pref.get("osm-download.bounds");
|
---|
| 434 | if (!value.isEmpty()) {
|
---|
[2327] | 435 | try {
|
---|
[6509] | 436 | return new Bounds(value, ";");
|
---|
| 437 | } catch (IllegalArgumentException e) {
|
---|
[12620] | 438 | Logging.warn(e);
|
---|
[2327] | 439 | }
|
---|
| 440 | }
|
---|
[6509] | 441 | return null;
|
---|
[2327] | 442 | }
|
---|
[2512] | 443 |
|
---|
[6509] | 444 | /**
|
---|
| 445 | * Determines if the dialog autorun is enabled in preferences.
|
---|
| 446 | * @return {@code true} if the download dialog must be open at startup, {@code false} otherwise
|
---|
| 447 | */
|
---|
[4906] | 448 | public static boolean isAutorunEnabled() {
|
---|
[11270] | 449 | return DOWNLOAD_AUTORUN.get();
|
---|
[4906] | 450 | }
|
---|
| 451 |
|
---|
[9230] | 452 | /**
|
---|
| 453 | * Automatically opens the download dialog, if autorun is enabled.
|
---|
| 454 | * @see #isAutorunEnabled
|
---|
| 455 | */
|
---|
[4906] | 456 | public static void autostartIfNeeded() {
|
---|
| 457 | if (isAutorunEnabled()) {
|
---|
[12643] | 458 | MainApplication.getMenu().download.actionPerformed(null);
|
---|
[4906] | 459 | }
|
---|
| 460 | }
|
---|
| 461 |
|
---|
[2327] | 462 | /**
|
---|
[12574] | 463 | * Returns an {@link Optional} of the currently selected download area.
|
---|
| 464 | * @return An {@link Optional} of the currently selected download area.
|
---|
| 465 | * @since 12574 Return type changed to optional
|
---|
[2327] | 466 | */
|
---|
[12574] | 467 | public Optional<Bounds> getSelectedDownloadArea() {
|
---|
| 468 | return Optional.ofNullable(currentBounds);
|
---|
[2327] | 469 | }
|
---|
[2512] | 470 |
|
---|
[2331] | 471 | @Override
|
---|
| 472 | public void setVisible(boolean visible) {
|
---|
| 473 | if (visible) {
|
---|
| 474 | new WindowGeometry(
|
---|
| 475 | getClass().getName() + ".geometry",
|
---|
| 476 | WindowGeometry.centerInWindow(
|
---|
| 477 | getParent(),
|
---|
[8510] | 478 | new Dimension(1000, 600)
|
---|
[2331] | 479 | )
|
---|
[2824] | 480 | ).applySafe(this);
|
---|
[5998] | 481 | } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
|
---|
[2331] | 482 | new WindowGeometry(this).remember(getClass().getName() + ".geometry");
|
---|
| 483 | }
|
---|
| 484 | super.setVisible(visible);
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | /**
|
---|
| 488 | * Replies true if the dialog was canceled
|
---|
[2512] | 489 | *
|
---|
[2331] | 490 | * @return true if the dialog was canceled
|
---|
| 491 | */
|
---|
| 492 | public boolean isCanceled() {
|
---|
| 493 | return canceled;
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | protected void setCanceled(boolean canceled) {
|
---|
| 497 | this.canceled = canceled;
|
---|
| 498 | }
|
---|
[2512] | 499 |
|
---|
[5097] | 500 | protected void buildMainPanelAboveDownloadSelections(JPanel pnl) {
|
---|
[10173] | 501 | // Do nothing
|
---|
[5097] | 502 | }
|
---|
| 503 |
|
---|
[2331] | 504 | class CancelAction extends AbstractAction {
|
---|
[8836] | 505 | CancelAction() {
|
---|
[2331] | 506 | putValue(NAME, tr("Cancel"));
|
---|
[10424] | 507 | new ImageProvider("cancel").getResource().attachImageIcon(this);
|
---|
[2512] | 508 | putValue(SHORT_DESCRIPTION, tr("Click to close the dialog and to abort downloading"));
|
---|
[2331] | 509 | }
|
---|
[2512] | 510 |
|
---|
[2331] | 511 | public void run() {
|
---|
| 512 | setCanceled(true);
|
---|
[2512] | 513 | setVisible(false);
|
---|
[2331] | 514 | }
|
---|
[2512] | 515 |
|
---|
[6084] | 516 | @Override
|
---|
[2331] | 517 | public void actionPerformed(ActionEvent e) {
|
---|
| 518 | run();
|
---|
[2512] | 519 | }
|
---|
[2331] | 520 | }
|
---|
| 521 |
|
---|
| 522 | class DownloadAction extends AbstractAction {
|
---|
[8836] | 523 | DownloadAction() {
|
---|
[2331] | 524 | putValue(NAME, tr("Download"));
|
---|
[10424] | 525 | new ImageProvider("download").getResource().attachImageIcon(this);
|
---|
[3976] | 526 | putValue(SHORT_DESCRIPTION, tr("Click to download the currently selected area"));
|
---|
[7434] | 527 | setEnabled(!Main.isOffline(OnlineResource.OSM_API));
|
---|
[2331] | 528 | }
|
---|
[2512] | 529 |
|
---|
[2344] | 530 | public void run() {
|
---|
[12574] | 531 | /*
|
---|
| 532 | * Checks if the user selected the type of data to download. At least one the following
|
---|
| 533 | * must be chosen : raw osm data, gpx data, notes.
|
---|
| 534 | * If none of those are selected, then the corresponding dialog is shown to inform the user.
|
---|
| 535 | */
|
---|
[7808] | 536 | if (!isDownloadOsmData() && !isDownloadGpxData() && !isDownloadNotes()) {
|
---|
[2331] | 537 | JOptionPane.showMessageDialog(
|
---|
| 538 | DownloadDialog.this,
|
---|
[8071] | 539 | tr("<html>Neither <strong>{0}</strong> nor <strong>{1}</strong> nor <strong>{2}</strong> is enabled.<br>"
|
---|
[12574] | 540 | + "Please choose to either download OSM data, or GPX data, or Notes, or all.</html>",
|
---|
[2529] | 541 | cbDownloadOsmData.getText(),
|
---|
[8071] | 542 | cbDownloadGpxData.getText(),
|
---|
| 543 | cbDownloadNotes.getText()
|
---|
[2331] | 544 | ),
|
---|
| 545 | tr("Error"),
|
---|
| 546 | JOptionPane.ERROR_MESSAGE
|
---|
| 547 | );
|
---|
| 548 | return;
|
---|
| 549 | }
|
---|
[12574] | 550 |
|
---|
[2331] | 551 | setCanceled(false);
|
---|
[2512] | 552 | setVisible(false);
|
---|
[2344] | 553 | }
|
---|
[2512] | 554 |
|
---|
[6084] | 555 | @Override
|
---|
[2344] | 556 | public void actionPerformed(ActionEvent e) {
|
---|
| 557 | run();
|
---|
[2512] | 558 | }
|
---|
[2331] | 559 | }
|
---|
[2512] | 560 |
|
---|
[2331] | 561 | class WindowEventHandler extends WindowAdapter {
|
---|
| 562 | @Override
|
---|
| 563 | public void windowClosing(WindowEvent e) {
|
---|
| 564 | new CancelAction().run();
|
---|
[2344] | 565 | }
|
---|
| 566 |
|
---|
| 567 | @Override
|
---|
| 568 | public void windowActivated(WindowEvent e) {
|
---|
| 569 | btnDownload.requestFocusInWindow();
|
---|
[2512] | 570 | }
|
---|
[2331] | 571 | }
|
---|
[12575] | 572 | }
|
---|