[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;
|
---|
| 8 | import java.awt.Component;
|
---|
| 9 | import java.awt.Dimension;
|
---|
| 10 | import java.awt.FlowLayout;
|
---|
[4552] | 11 | import java.awt.Graphics;
|
---|
[1811] | 12 | import java.awt.GridBagLayout;
|
---|
[1329] | 13 | import java.awt.event.ActionEvent;
|
---|
| 14 | import java.awt.event.InputEvent;
|
---|
| 15 | import java.awt.event.KeyEvent;
|
---|
[2331] | 16 | import java.awt.event.WindowAdapter;
|
---|
| 17 | import java.awt.event.WindowEvent;
|
---|
[237] | 18 | import java.util.ArrayList;
|
---|
[12652] | 19 | import java.util.Arrays;
|
---|
[237] | 20 | import java.util.List;
|
---|
[12574] | 21 | import java.util.Optional;
|
---|
[12652] | 22 | import java.util.stream.IntStream;
|
---|
[237] | 23 |
|
---|
[1307] | 24 | import javax.swing.AbstractAction;
|
---|
[12652] | 25 | import javax.swing.Icon;
|
---|
[10424] | 26 | import javax.swing.JButton;
|
---|
[237] | 27 | import javax.swing.JCheckBox;
|
---|
[2331] | 28 | import javax.swing.JComponent;
|
---|
| 29 | import javax.swing.JDialog;
|
---|
[237] | 30 | import javax.swing.JLabel;
|
---|
| 31 | import javax.swing.JPanel;
|
---|
[12652] | 32 | import javax.swing.JSplitPane;
|
---|
[237] | 33 | import javax.swing.JTabbedPane;
|
---|
[1307] | 34 | import javax.swing.KeyStroke;
|
---|
[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;
|
---|
[2331] | 54 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
[237] | 55 |
|
---|
| 56 | /**
|
---|
[12652] | 57 | * Dialog displayed to the user to download mapping data.
|
---|
[237] | 58 | */
|
---|
[10378] | 59 | public class DownloadDialog extends JDialog {
|
---|
[12652] | 60 |
|
---|
| 61 | /**
|
---|
| 62 | * Preference properties
|
---|
| 63 | */
|
---|
[11277] | 64 | private static final IntegerProperty DOWNLOAD_TAB = new IntegerProperty("download.tab", 0);
|
---|
[12652] | 65 | private static final IntegerProperty DOWNLOAD_SOURCE_TAB = new IntegerProperty("download-source.tab", 0);
|
---|
| 66 | private static final IntegerProperty DIALOG_SPLIT = new IntegerProperty("download.split", 200);
|
---|
[11277] | 67 | private static final BooleanProperty DOWNLOAD_AUTORUN = new BooleanProperty("download.autorun", false);
|
---|
| 68 | private static final BooleanProperty DOWNLOAD_NEWLAYER = new BooleanProperty("download.newlayer", false);
|
---|
[11658] | 69 | private static final BooleanProperty DOWNLOAD_ZOOMTODATA = new BooleanProperty("download.zoomtodata", true);
|
---|
[11270] | 70 |
|
---|
[2331] | 71 | /** the unique instance of the download dialog */
|
---|
[6883] | 72 | private static DownloadDialog instance;
|
---|
[2512] | 73 |
|
---|
[2331] | 74 | /**
|
---|
| 75 | * Replies the unique instance of the download dialog
|
---|
[2512] | 76 | *
|
---|
[2331] | 77 | * @return the unique instance of the download dialog
|
---|
| 78 | */
|
---|
[8126] | 79 | public static synchronized DownloadDialog getInstance() {
|
---|
[2529] | 80 | if (instance == null) {
|
---|
[2331] | 81 | instance = new DownloadDialog(Main.parent);
|
---|
[2529] | 82 | }
|
---|
[2331] | 83 | return instance;
|
---|
[2512] | 84 | }
|
---|
[806] | 85 |
|
---|
[12652] | 86 | protected final transient List<DownloadSource> downloadSources = new ArrayList<>();
|
---|
[8308] | 87 | protected final transient List<DownloadSelection> downloadSelections = new ArrayList<>();
|
---|
[5097] | 88 | protected final JTabbedPane tpDownloadAreaSelectors = new JTabbedPane();
|
---|
[12652] | 89 | protected final JTabbedPane downloadSourcesTab = new JTabbedPane();
|
---|
| 90 |
|
---|
[5097] | 91 | protected JCheckBox cbNewLayer;
|
---|
| 92 | protected JCheckBox cbStartup;
|
---|
[11658] | 93 | protected JCheckBox cbZoomToDownloadedData;
|
---|
[12652] | 94 | protected SlippyMapChooser slippyMapChooser;
|
---|
| 95 | protected JPanel mainPanel;
|
---|
| 96 | protected JSplitPane dialogSplit;
|
---|
| 97 |
|
---|
| 98 | /*
|
---|
| 99 | * Keep the reference globally to avoid having it garbage collected
|
---|
| 100 | */
|
---|
| 101 | protected final transient ExpertToggleAction.ExpertModeChangeListener expertListener =
|
---|
| 102 | getExpertModeListenerForDownloadSources();
|
---|
[8840] | 103 | protected transient Bounds currentBounds;
|
---|
[5097] | 104 | protected boolean canceled;
|
---|
[806] | 105 |
|
---|
[12652] | 106 | protected JButton btnDownload;
|
---|
| 107 | protected JButton btnCancel;
|
---|
| 108 | protected JButton btnHelp;
|
---|
[806] | 109 |
|
---|
[12652] | 110 | /**
|
---|
| 111 | * Builds the main panel of the dialog.
|
---|
| 112 | * @return The panel of the dialog.
|
---|
| 113 | */
|
---|
[6890] | 114 | protected final JPanel buildMainPanel() {
|
---|
[12652] | 115 | mainPanel = new JPanel(new GridBagLayout());
|
---|
[806] | 116 |
|
---|
[12652] | 117 | downloadSources.add(new OSMDownloadSource());
|
---|
| 118 | downloadSources.add(new OverpassDownloadSource());
|
---|
[8215] | 119 |
|
---|
[12652] | 120 | // register all default download sources
|
---|
| 121 | for (int i = 0; i < downloadSources.size(); i++) {
|
---|
| 122 | downloadSources.get(i).addGui(this);
|
---|
| 123 | }
|
---|
[2512] | 124 |
|
---|
[8932] | 125 | // must be created before hook
|
---|
| 126 | slippyMapChooser = new SlippyMapChooser();
|
---|
| 127 |
|
---|
[1169] | 128 | // predefined download selections
|
---|
[6365] | 129 | downloadSelections.add(slippyMapChooser);
|
---|
[1392] | 130 | downloadSelections.add(new BookmarkSelection());
|
---|
[1169] | 131 | downloadSelections.add(new BoundingBoxSelection());
|
---|
[1392] | 132 | downloadSelections.add(new PlaceSelection());
|
---|
[1169] | 133 | downloadSelections.add(new TileSelection());
|
---|
[806] | 134 |
|
---|
[1169] | 135 | // add selections from plugins
|
---|
[1326] | 136 | PluginHandler.addDownloadSelection(downloadSelections);
|
---|
[806] | 137 |
|
---|
[12652] | 138 | // register all default download selections
|
---|
| 139 | for (int i = 0; i < downloadSelections.size(); i++) {
|
---|
| 140 | downloadSelections.get(i).addGui(this);
|
---|
[1169] | 141 | }
|
---|
[2512] | 142 |
|
---|
[12652] | 143 | // allow to collapse the panes completely
|
---|
| 144 | downloadSourcesTab.setMinimumSize(new Dimension(0, 0));
|
---|
| 145 | tpDownloadAreaSelectors.setMinimumSize(new Dimension(0, 0));
|
---|
[806] | 146 |
|
---|
[12652] | 147 | dialogSplit = new JSplitPane(
|
---|
| 148 | JSplitPane.VERTICAL_SPLIT,
|
---|
| 149 | downloadSourcesTab,
|
---|
| 150 | tpDownloadAreaSelectors);
|
---|
[1038] | 151 |
|
---|
[12652] | 152 | mainPanel.add(dialogSplit, GBC.eol().fill());
|
---|
[3104] | 153 |
|
---|
[3057] | 154 | cbNewLayer = new JCheckBox(tr("Download as new layer"));
|
---|
| 155 | cbNewLayer.setToolTipText(tr("<html>Select to download data into a new data layer.<br>"
|
---|
| 156 | +"Unselect to download into the currently active data layer.</html>"));
|
---|
| 157 |
|
---|
[4906] | 158 | cbStartup = new JCheckBox(tr("Open this dialog on startup"));
|
---|
[8509] | 159 | cbStartup.setToolTipText(
|
---|
[8510] | 160 | tr("<html>Autostart ''Download from OSM'' dialog every time JOSM is started.<br>" +
|
---|
| 161 | "You can open it manually from File menu or toolbar.</html>"));
|
---|
[11270] | 162 | cbStartup.addActionListener(e -> DOWNLOAD_AUTORUN.put(cbStartup.isSelected()));
|
---|
[4906] | 163 |
|
---|
[11658] | 164 | cbZoomToDownloadedData = new JCheckBox(tr("Zoom to downloaded data"));
|
---|
| 165 | cbZoomToDownloadedData.setToolTipText(tr("Select to zoom to entire newly downloaded data."));
|
---|
| 166 |
|
---|
[12652] | 167 | mainPanel.add(cbNewLayer, GBC.std().anchor(GBC.WEST).insets(5, 5, 5, 5));
|
---|
| 168 | mainPanel.add(cbStartup, GBC.std().anchor(GBC.WEST).insets(15, 5, 5, 5));
|
---|
| 169 | mainPanel.add(cbZoomToDownloadedData, GBC.std().anchor(GBC.WEST).insets(15, 5, 5, 5));
|
---|
[3057] | 170 |
|
---|
[11658] | 171 | ExpertToggleAction.addVisibilitySwitcher(cbZoomToDownloadedData);
|
---|
| 172 |
|
---|
[4906] | 173 | if (!ExpertToggleAction.isExpert()) {
|
---|
[10378] | 174 | JLabel infoLabel = new JLabel(
|
---|
[8509] | 175 | tr("Use left click&drag to select area, arrows or right mouse button to scroll map, wheel or +/- to zoom."));
|
---|
[12652] | 176 | mainPanel.add(infoLabel, GBC.eol().anchor(GBC.SOUTH).insets(0, 0, 0, 0));
|
---|
[4906] | 177 | }
|
---|
[12652] | 178 | return mainPanel;
|
---|
[2331] | 179 | }
|
---|
[2512] | 180 |
|
---|
[4552] | 181 | /* This should not be necessary, but if not here, repaint is not always correct in SlippyMap! */
|
---|
[6084] | 182 | @Override
|
---|
[4552] | 183 | public void paint(Graphics g) {
|
---|
| 184 | tpDownloadAreaSelectors.getSelectedComponent().paint(g);
|
---|
| 185 | super.paint(g);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[12652] | 188 | /**
|
---|
| 189 | * Builds the button pane of the dialog.
|
---|
| 190 | * @return The button panel of the dialog.
|
---|
| 191 | */
|
---|
[6890] | 192 | protected final JPanel buildButtonPanel() {
|
---|
[12652] | 193 | btnDownload = new JButton(new DownloadAction());
|
---|
| 194 | btnCancel = new JButton(new CancelAction());
|
---|
| 195 | btnHelp = new JButton(
|
---|
| 196 | new ContextSensitiveHelpAction(getRootPane().getClientProperty("help").toString()));
|
---|
| 197 |
|
---|
[9543] | 198 | JPanel pnl = new JPanel(new FlowLayout());
|
---|
[2512] | 199 |
|
---|
[10179] | 200 | pnl.add(btnDownload);
|
---|
[12652] | 201 | pnl.add(btnCancel);
|
---|
| 202 | pnl.add(btnHelp);
|
---|
| 203 |
|
---|
[5200] | 204 | InputMapUtils.enableEnter(btnDownload);
|
---|
| 205 | InputMapUtils.enableEnter(btnCancel);
|
---|
[12652] | 206 | InputMapUtils.addEscapeAction(getRootPane(), btnCancel.getAction());
|
---|
| 207 | InputMapUtils.enableEnter(btnHelp);
|
---|
[2347] | 208 |
|
---|
[12652] | 209 | InputMapUtils.addEnterActionWhenAncestor(cbNewLayer, btnDownload.getAction());
|
---|
| 210 | InputMapUtils.addEnterActionWhenAncestor(cbStartup, btnDownload.getAction());
|
---|
| 211 | InputMapUtils.addEnterActionWhenAncestor(cbZoomToDownloadedData, btnDownload.getAction());
|
---|
[2347] | 212 |
|
---|
[2512] | 213 | return pnl;
|
---|
[2331] | 214 | }
|
---|
[2512] | 215 |
|
---|
[8071] | 216 | /**
|
---|
| 217 | * Constructs a new {@code DownloadDialog}.
|
---|
| 218 | * @param parent the parent component
|
---|
| 219 | */
|
---|
[2331] | 220 | public DownloadDialog(Component parent) {
|
---|
[8713] | 221 | this(parent, ht("/Action/Download"));
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | /**
|
---|
| 225 | * Constructs a new {@code DownloadDialog}.
|
---|
| 226 | * @param parent the parent component
|
---|
| 227 | * @param helpTopic the help topic to assign
|
---|
| 228 | */
|
---|
| 229 | public DownloadDialog(Component parent, String helpTopic) {
|
---|
[10035] | 230 | super(GuiHelper.getFrameForComponent(parent), tr("Download"), ModalityType.DOCUMENT_MODAL);
|
---|
[8713] | 231 | HelpUtil.setHelpContext(getRootPane(), helpTopic);
|
---|
[2331] | 232 | getContentPane().setLayout(new BorderLayout());
|
---|
| 233 | getContentPane().add(buildMainPanel(), BorderLayout.CENTER);
|
---|
| 234 | getContentPane().add(buildButtonPanel(), BorderLayout.SOUTH);
|
---|
[2512] | 235 |
|
---|
[2331] | 236 | getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
|
---|
[12523] | 237 | KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_DOWN_MASK), "checkClipboardContents");
|
---|
[1329] | 238 |
|
---|
[2331] | 239 | getRootPane().getActionMap().put("checkClipboardContents", new AbstractAction() {
|
---|
[6084] | 240 | @Override
|
---|
[1329] | 241 | public void actionPerformed(ActionEvent e) {
|
---|
[10604] | 242 | String clip = ClipboardUtils.getClipboardStringContent();
|
---|
[4380] | 243 | if (clip == null) {
|
---|
| 244 | return;
|
---|
| 245 | }
|
---|
| 246 | Bounds b = OsmUrlToBounds.parse(clip);
|
---|
| 247 | if (b != null) {
|
---|
| 248 | boundingBoxChanged(new Bounds(b), null);
|
---|
| 249 | }
|
---|
[1329] | 250 | }
|
---|
| 251 | });
|
---|
[2331] | 252 | addWindowListener(new WindowEventHandler());
|
---|
[12652] | 253 | ExpertToggleAction.addExpertModeChangeListener(expertListener);
|
---|
[2327] | 254 | restoreSettings();
|
---|
[1169] | 255 | }
|
---|
[806] | 256 |
|
---|
[1169] | 257 | /**
|
---|
| 258 | * Distributes a "bounding box changed" from one DownloadSelection
|
---|
[12652] | 259 | * object to the others, so they may update or clear their input fields. Also informs
|
---|
| 260 | * download sources about the change, so they can react on it.
|
---|
[9230] | 261 | * @param b new current bounds
|
---|
[1169] | 262 | *
|
---|
| 263 | * @param eventSource - the DownloadSelection object that fired this notification.
|
---|
| 264 | */
|
---|
[2512] | 265 | public void boundingBoxChanged(Bounds b, DownloadSelection eventSource) {
|
---|
[2327] | 266 | this.currentBounds = b;
|
---|
[1169] | 267 | for (DownloadSelection s : downloadSelections) {
|
---|
[2142] | 268 | if (s != eventSource) {
|
---|
[2332] | 269 | s.setDownloadArea(currentBounds);
|
---|
[2142] | 270 | }
|
---|
[1169] | 271 | }
|
---|
[12652] | 272 |
|
---|
| 273 | for (Component ds : downloadSourcesTab.getComponents()) {
|
---|
| 274 | if (ds instanceof AbstractDownloadSourcePanel) {
|
---|
| 275 | ((AbstractDownloadSourcePanel) ds).boudingBoxChanged(b);
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
[1169] | 278 | }
|
---|
[2512] | 279 |
|
---|
[2327] | 280 | /**
|
---|
[8470] | 281 | * Starts download for the given bounding box
|
---|
| 282 | * @param b bounding box to download
|
---|
[2344] | 283 | */
|
---|
| 284 | public void startDownload(Bounds b) {
|
---|
| 285 | this.currentBounds = b;
|
---|
[12652] | 286 | startDownload();
|
---|
[2344] | 287 | }
|
---|
[2512] | 288 |
|
---|
[2344] | 289 | /**
|
---|
[12652] | 290 | * Starts download.
|
---|
[1169] | 291 | */
|
---|
[12652] | 292 | public void startDownload() {
|
---|
| 293 | btnDownload.doClick();
|
---|
[1169] | 294 | }
|
---|
[2512] | 295 |
|
---|
[2327] | 296 | /**
|
---|
[2512] | 297 | * Replies true if the user requires to download into a new layer
|
---|
| 298 | *
|
---|
| 299 | * @return true if the user requires to download into a new layer
|
---|
[2327] | 300 | */
|
---|
| 301 | public boolean isNewLayerRequired() {
|
---|
| 302 | return cbNewLayer.isSelected();
|
---|
| 303 | }
|
---|
[2512] | 304 |
|
---|
[2327] | 305 | /**
|
---|
[11658] | 306 | * Replies true if the user requires to zoom to new downloaded data
|
---|
| 307 | *
|
---|
| 308 | * @return true if the user requires to zoom to new downloaded data
|
---|
| 309 | * @since 11658
|
---|
| 310 | */
|
---|
| 311 | public boolean isZoomToDownloadedDataRequired() {
|
---|
| 312 | return cbZoomToDownloadedData.isSelected();
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | /**
|
---|
[12652] | 316 | * Determines if the dialog autorun is enabled in preferences.
|
---|
| 317 | * @return {@code true} if the download dialog must be open at startup, {@code false} otherwise
|
---|
| 318 | */
|
---|
| 319 | public static boolean isAutorunEnabled() {
|
---|
| 320 | return DOWNLOAD_AUTORUN.get();
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | /**
|
---|
[2327] | 324 | * Adds a new download area selector to the download dialog
|
---|
[2512] | 325 | *
|
---|
| 326 | * @param selector the download are selector
|
---|
[2327] | 327 | * @param displayName the display name of the selector
|
---|
| 328 | */
|
---|
| 329 | public void addDownloadAreaSelector(JPanel selector, String displayName) {
|
---|
| 330 | tpDownloadAreaSelectors.add(displayName, selector);
|
---|
| 331 | }
|
---|
[2512] | 332 |
|
---|
[2327] | 333 | /**
|
---|
[12652] | 334 | * Adds a new download source to the download dialog
|
---|
| 335 | *
|
---|
| 336 | * @param downloadSource The download source to be added.
|
---|
| 337 | * @param <T> The type of the download data.
|
---|
| 338 | */
|
---|
| 339 | public <T> void addDownloadSource(DownloadSource<T> downloadSource) {
|
---|
| 340 | if ((ExpertToggleAction.isExpert() && downloadSource.onlyExpert()) || !downloadSource.onlyExpert()) {
|
---|
| 341 | addNewDownloadSourceTab(downloadSource);
|
---|
| 342 | }
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | /**
|
---|
[6364] | 346 | * Refreshes the tile sources
|
---|
| 347 | * @since 6364
|
---|
| 348 | */
|
---|
| 349 | public final void refreshTileSources() {
|
---|
| 350 | if (slippyMapChooser != null) {
|
---|
| 351 | slippyMapChooser.refreshTileSources();
|
---|
| 352 | }
|
---|
| 353 | }
|
---|
[7434] | 354 |
|
---|
[6364] | 355 | /**
|
---|
[6509] | 356 | * Remembers the current settings in the download dialog.
|
---|
[2327] | 357 | */
|
---|
| 358 | public void rememberSettings() {
|
---|
[11270] | 359 | DOWNLOAD_TAB.put(tpDownloadAreaSelectors.getSelectedIndex());
|
---|
[12652] | 360 | DOWNLOAD_SOURCE_TAB.put(downloadSourcesTab.getSelectedIndex());
|
---|
| 361 | DIALOG_SPLIT.put(dialogSplit.getDividerLocation());
|
---|
[11270] | 362 | DOWNLOAD_NEWLAYER.put(cbNewLayer.isSelected());
|
---|
[11658] | 363 | DOWNLOAD_ZOOMTODATA.put(cbZoomToDownloadedData.isSelected());
|
---|
[2327] | 364 | if (currentBounds != null) {
|
---|
| 365 | Main.pref.put("osm-download.bounds", currentBounds.encodeAsString(";"));
|
---|
| 366 | }
|
---|
| 367 | }
|
---|
[2512] | 368 |
|
---|
[6509] | 369 | /**
|
---|
| 370 | * Restores the previous settings in the download dialog.
|
---|
| 371 | */
|
---|
[2327] | 372 | public void restoreSettings() {
|
---|
[11270] | 373 | cbNewLayer.setSelected(DOWNLOAD_NEWLAYER.get());
|
---|
[8444] | 374 | cbStartup.setSelected(isAutorunEnabled());
|
---|
[11658] | 375 | cbZoomToDownloadedData.setSelected(DOWNLOAD_ZOOMTODATA.get());
|
---|
[12652] | 376 | dialogSplit.setDividerLocation(DIALOG_SPLIT.get());
|
---|
[2512] | 377 |
|
---|
[12652] | 378 | try {
|
---|
| 379 | tpDownloadAreaSelectors.setSelectedIndex(DOWNLOAD_TAB.get());
|
---|
| 380 | } catch (IndexOutOfBoundsException e) {
|
---|
| 381 | Main.trace(e);
|
---|
| 382 | tpDownloadAreaSelectors.setSelectedIndex(0);
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | try {
|
---|
| 386 | downloadSourcesTab.setSelectedIndex(DOWNLOAD_SOURCE_TAB.get());
|
---|
| 387 | } catch (IndexOutOfBoundsException e) {
|
---|
| 388 | Main.trace(e);
|
---|
| 389 | downloadSourcesTab.setSelectedIndex(0);
|
---|
| 390 | }
|
---|
| 391 |
|
---|
[12630] | 392 | if (MainApplication.isDisplayingMapView()) {
|
---|
| 393 | MapView mv = MainApplication.getMap().mapView;
|
---|
[2327] | 394 | currentBounds = new Bounds(
|
---|
| 395 | mv.getLatLon(0, mv.getHeight()),
|
---|
[2512] | 396 | mv.getLatLon(mv.getWidth(), 0)
|
---|
[2529] | 397 | );
|
---|
[8510] | 398 | boundingBoxChanged(currentBounds, null);
|
---|
[8342] | 399 | } else {
|
---|
[6509] | 400 | Bounds bounds = getSavedDownloadBounds();
|
---|
| 401 | if (bounds != null) {
|
---|
| 402 | currentBounds = bounds;
|
---|
| 403 | boundingBoxChanged(currentBounds, null);
|
---|
| 404 | }
|
---|
| 405 | }
|
---|
| 406 | }
|
---|
[7434] | 407 |
|
---|
[6509] | 408 | /**
|
---|
| 409 | * Returns the previously saved bounding box from preferences.
|
---|
| 410 | * @return The bounding box saved in preferences if any, {@code null} otherwise
|
---|
| 411 | * @since 6509
|
---|
| 412 | */
|
---|
| 413 | public static Bounds getSavedDownloadBounds() {
|
---|
| 414 | String value = Main.pref.get("osm-download.bounds");
|
---|
| 415 | if (!value.isEmpty()) {
|
---|
[2327] | 416 | try {
|
---|
[6509] | 417 | return new Bounds(value, ";");
|
---|
| 418 | } catch (IllegalArgumentException e) {
|
---|
[12620] | 419 | Logging.warn(e);
|
---|
[2327] | 420 | }
|
---|
| 421 | }
|
---|
[6509] | 422 | return null;
|
---|
[2327] | 423 | }
|
---|
[2512] | 424 |
|
---|
[6509] | 425 | /**
|
---|
[9230] | 426 | * Automatically opens the download dialog, if autorun is enabled.
|
---|
| 427 | * @see #isAutorunEnabled
|
---|
| 428 | */
|
---|
[4906] | 429 | public static void autostartIfNeeded() {
|
---|
| 430 | if (isAutorunEnabled()) {
|
---|
[12643] | 431 | MainApplication.getMenu().download.actionPerformed(null);
|
---|
[4906] | 432 | }
|
---|
| 433 | }
|
---|
| 434 |
|
---|
[2327] | 435 | /**
|
---|
[12574] | 436 | * Returns an {@link Optional} of the currently selected download area.
|
---|
| 437 | * @return An {@link Optional} of the currently selected download area.
|
---|
| 438 | * @since 12574 Return type changed to optional
|
---|
[2327] | 439 | */
|
---|
[12574] | 440 | public Optional<Bounds> getSelectedDownloadArea() {
|
---|
| 441 | return Optional.ofNullable(currentBounds);
|
---|
[2327] | 442 | }
|
---|
[2512] | 443 |
|
---|
[2331] | 444 | @Override
|
---|
| 445 | public void setVisible(boolean visible) {
|
---|
| 446 | if (visible) {
|
---|
| 447 | new WindowGeometry(
|
---|
| 448 | getClass().getName() + ".geometry",
|
---|
| 449 | WindowGeometry.centerInWindow(
|
---|
| 450 | getParent(),
|
---|
[8510] | 451 | new Dimension(1000, 600)
|
---|
[2331] | 452 | )
|
---|
[2824] | 453 | ).applySafe(this);
|
---|
[5998] | 454 | } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
|
---|
[2331] | 455 | new WindowGeometry(this).remember(getClass().getName() + ".geometry");
|
---|
| 456 | }
|
---|
| 457 | super.setVisible(visible);
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | /**
|
---|
| 461 | * Replies true if the dialog was canceled
|
---|
[2512] | 462 | *
|
---|
[2331] | 463 | * @return true if the dialog was canceled
|
---|
| 464 | */
|
---|
| 465 | public boolean isCanceled() {
|
---|
| 466 | return canceled;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
[12652] | 469 | /**
|
---|
| 470 | * Gets the global settings of the download dialog.
|
---|
| 471 | * @return The {@link DownloadSettings} object that describes the current state of
|
---|
| 472 | * the download dialog.
|
---|
| 473 | */
|
---|
| 474 | public DownloadSettings getDownloadSettings() {
|
---|
| 475 | return new DownloadSettings(isNewLayerRequired(), isZoomToDownloadedDataRequired());
|
---|
| 476 | }
|
---|
| 477 |
|
---|
[2331] | 478 | protected void setCanceled(boolean canceled) {
|
---|
| 479 | this.canceled = canceled;
|
---|
| 480 | }
|
---|
[2512] | 481 |
|
---|
[12652] | 482 | /**
|
---|
| 483 | * Returns position of the download source in the tabbed pane.
|
---|
| 484 | * @param downloadSource The download source.
|
---|
| 485 | * @return The index of the download source, or -1 if it not in the pane.
|
---|
| 486 | */
|
---|
| 487 | protected int getDownloadSourceIndex(DownloadSource downloadSource) {
|
---|
| 488 | return Arrays.stream(downloadSourcesTab.getComponents())
|
---|
| 489 | .filter(it -> it instanceof AbstractDownloadSourcePanel)
|
---|
| 490 | .map(it -> (AbstractDownloadSourcePanel) it)
|
---|
| 491 | .filter(it -> it.getDownloadSource().equals(downloadSource))
|
---|
| 492 | .findAny()
|
---|
| 493 | .map(downloadSourcesTab::indexOfComponent)
|
---|
| 494 | .orElse(-1);
|
---|
[5097] | 495 | }
|
---|
| 496 |
|
---|
[12652] | 497 | /**
|
---|
| 498 | * Adds the download source to the download sources tab.
|
---|
| 499 | * @param downloadSource The download source to be added.
|
---|
| 500 | * @param <T> The type of the download data.
|
---|
| 501 | */
|
---|
| 502 | private <T> void addNewDownloadSourceTab(DownloadSource<T> downloadSource) {
|
---|
| 503 | AbstractDownloadSourcePanel<T> panel = downloadSource.createPanel();
|
---|
| 504 | downloadSourcesTab.add(panel, downloadSource.getLabel());
|
---|
| 505 | Icon icon = panel.getIcon();
|
---|
| 506 | if (icon != null) {
|
---|
| 507 | int idx = getDownloadSourceIndex(downloadSource);
|
---|
| 508 | downloadSourcesTab.setIconAt(
|
---|
| 509 | idx != -1 ? idx : downloadSourcesTab.getTabCount() - 1,
|
---|
| 510 | icon);
|
---|
| 511 | }
|
---|
| 512 | }
|
---|
| 513 |
|
---|
| 514 | /**
|
---|
| 515 | * Creates listener that removes/adds download sources from/to {@code downloadSourcesTab}
|
---|
| 516 | * depending on the current mode.
|
---|
| 517 | * @return The expert mode listener.
|
---|
| 518 | */
|
---|
| 519 | private ExpertToggleAction.ExpertModeChangeListener getExpertModeListenerForDownloadSources() {
|
---|
| 520 | return isExpert -> {
|
---|
| 521 | if (isExpert) {
|
---|
| 522 | downloadSources.stream()
|
---|
| 523 | .filter(DownloadSource::onlyExpert)
|
---|
| 524 | .filter(it -> getDownloadSourceIndex(it) == -1)
|
---|
| 525 | .forEach(this::addNewDownloadSourceTab);
|
---|
| 526 | } else {
|
---|
| 527 | IntStream.range(0, downloadSourcesTab.getTabCount())
|
---|
| 528 | .mapToObj(downloadSourcesTab::getComponentAt)
|
---|
| 529 | .filter(it -> it instanceof AbstractDownloadSourcePanel)
|
---|
| 530 | .map(it -> (AbstractDownloadSourcePanel) it)
|
---|
| 531 | .filter(it -> it.getDownloadSource().onlyExpert())
|
---|
| 532 | .forEach(downloadSourcesTab::remove);
|
---|
| 533 | }
|
---|
| 534 | };
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | /**
|
---|
| 538 | * Action that is executed when the cancel button is pressed.
|
---|
| 539 | */
|
---|
[2331] | 540 | class CancelAction extends AbstractAction {
|
---|
[8836] | 541 | CancelAction() {
|
---|
[2331] | 542 | putValue(NAME, tr("Cancel"));
|
---|
[10424] | 543 | new ImageProvider("cancel").getResource().attachImageIcon(this);
|
---|
[2512] | 544 | putValue(SHORT_DESCRIPTION, tr("Click to close the dialog and to abort downloading"));
|
---|
[2331] | 545 | }
|
---|
[2512] | 546 |
|
---|
[2331] | 547 | public void run() {
|
---|
| 548 | setCanceled(true);
|
---|
[2512] | 549 | setVisible(false);
|
---|
[2331] | 550 | }
|
---|
[2512] | 551 |
|
---|
[6084] | 552 | @Override
|
---|
[2331] | 553 | public void actionPerformed(ActionEvent e) {
|
---|
[12652] | 554 | AbstractDownloadSourcePanel pnl = (AbstractDownloadSourcePanel) downloadSourcesTab.getSelectedComponent();
|
---|
[2331] | 555 | run();
|
---|
[12652] | 556 | pnl.checkCancel();
|
---|
[2512] | 557 | }
|
---|
[2331] | 558 | }
|
---|
| 559 |
|
---|
[12652] | 560 | /**
|
---|
| 561 | * Action that is executed when the download button is pressed.
|
---|
| 562 | */
|
---|
[2331] | 563 | class DownloadAction extends AbstractAction {
|
---|
[8836] | 564 | DownloadAction() {
|
---|
[2331] | 565 | putValue(NAME, tr("Download"));
|
---|
[10424] | 566 | new ImageProvider("download").getResource().attachImageIcon(this);
|
---|
[3976] | 567 | putValue(SHORT_DESCRIPTION, tr("Click to download the currently selected area"));
|
---|
[7434] | 568 | setEnabled(!Main.isOffline(OnlineResource.OSM_API));
|
---|
[2331] | 569 | }
|
---|
[2512] | 570 |
|
---|
[2344] | 571 | public void run() {
|
---|
[12652] | 572 | Component panel = downloadSourcesTab.getSelectedComponent();
|
---|
| 573 | if (panel instanceof AbstractDownloadSourcePanel) {
|
---|
| 574 | AbstractDownloadSourcePanel pnl = (AbstractDownloadSourcePanel) panel;
|
---|
| 575 | DownloadSettings downloadSettings = getDownloadSettings();
|
---|
| 576 | if (pnl.checkDownload(currentBounds, downloadSettings)) {
|
---|
| 577 | rememberSettings();
|
---|
| 578 | setCanceled(false);
|
---|
| 579 | setVisible(false);
|
---|
| 580 | pnl.getDownloadSource().doDownload(currentBounds, pnl.getData(), downloadSettings);
|
---|
| 581 | }
|
---|
[2331] | 582 | }
|
---|
[2344] | 583 | }
|
---|
[2512] | 584 |
|
---|
[6084] | 585 | @Override
|
---|
[2344] | 586 | public void actionPerformed(ActionEvent e) {
|
---|
| 587 | run();
|
---|
[2512] | 588 | }
|
---|
[2331] | 589 | }
|
---|
[2512] | 590 |
|
---|
[2331] | 591 | class WindowEventHandler extends WindowAdapter {
|
---|
| 592 | @Override
|
---|
| 593 | public void windowClosing(WindowEvent e) {
|
---|
| 594 | new CancelAction().run();
|
---|
[2344] | 595 | }
|
---|
| 596 |
|
---|
| 597 | @Override
|
---|
| 598 | public void windowActivated(WindowEvent e) {
|
---|
| 599 | btnDownload.requestFocusInWindow();
|
---|
[2512] | 600 | }
|
---|
[2331] | 601 | }
|
---|
[12575] | 602 | }
|
---|