Ticket #15167: merge-download-dialog-v5.patch
| File merge-download-dialog-v5.patch, 18.4 KB (added by , 9 years ago) |
|---|
-
src/org/openstreetmap/josm/gui/download/AbstractDownloadSourcePanel.java
97 97 public void triggerDownload(DownloadSettings downloadSettings) { 98 98 getDownloadSource().doDownload(getData(), downloadSettings); 99 99 } 100 101 /** 102 * Returns a simple name describing this panel. This string can be used from other GUI parts 103 * of JOSM to save the user preferences related to the GUI settings. For example, the panel for downloading 104 * the OSM data can be named 'downloadosmpanel'. Note, choose the name such that it is unique to avoid 105 * collisions with other names. 106 * @return A simple name describing this panel. 107 */ 108 public abstract String getSimpleName(); 100 109 } -
src/org/openstreetmap/josm/gui/download/DownloadDialog.java
15 15 import java.awt.event.KeyEvent; 16 16 import java.awt.event.WindowAdapter; 17 17 import java.awt.event.WindowEvent; 18 import java.beans.PropertyChangeListener; 18 19 import java.util.ArrayList; 19 20 import java.util.Arrays; 20 21 import java.util.List; … … 32 33 import javax.swing.JSplitPane; 33 34 import javax.swing.JTabbedPane; 34 35 import javax.swing.KeyStroke; 36 import javax.swing.event.ChangeEvent; 37 import javax.swing.event.ChangeListener; 35 38 36 39 import org.openstreetmap.josm.Main; 37 40 import org.openstreetmap.josm.actions.ExpertToggleAction; … … 49 52 import org.openstreetmap.josm.tools.GBC; 50 53 import org.openstreetmap.josm.tools.ImageProvider; 51 54 import org.openstreetmap.josm.tools.InputMapUtils; 55 import org.openstreetmap.josm.tools.JosmRuntimeException; 52 56 import org.openstreetmap.josm.tools.Logging; 53 57 import org.openstreetmap.josm.tools.OsmUrlToBounds; 54 58 import org.openstreetmap.josm.tools.WindowGeometry; … … 61 65 /** 62 66 * Preference properties 63 67 */ 68 private static final String TAB_SPLIT_NAMESPACE = "download.tabsplit."; 64 69 private static final IntegerProperty DOWNLOAD_TAB = new IntegerProperty("download.tab", 0); 65 70 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);67 71 private static final BooleanProperty DOWNLOAD_AUTORUN = new BooleanProperty("download.autorun", false); 68 72 private static final BooleanProperty DOWNLOAD_NEWLAYER = new BooleanProperty("download.newlayer", false); 69 73 private static final BooleanProperty DOWNLOAD_ZOOMTODATA = new BooleanProperty("download.zoomtodata", true); … … 114 118 protected final JPanel buildMainPanel() { 115 119 mainPanel = new JPanel(new GridBagLayout()); 116 120 117 downloadSources.add(new OSMDownloadSource()); 118 downloadSources.add(new OverpassDownloadSource()); 121 // add default download sources 122 addDownloadSource(new OSMDownloadSource()); 123 addDownloadSource(new OverpassDownloadSource()); 119 124 120 // register all default download sources121 for (int i = 0; i < downloadSources.size(); i++) {122 downloadSources.get(i).addGui(this);123 }124 125 125 // must be created before hook 126 126 slippyMapChooser = new SlippyMapChooser(); 127 127 … … 140 140 s.addGui(this); 141 141 } 142 142 143 // allow to collapse the panes completely144 downloadSourcesTab.setMinimumSize(new Dimension(0, 0));143 // allow to collapse the panes, but reserve some space for tabs 144 downloadSourcesTab.setMinimumSize(new Dimension(0, 25)); 145 145 tpDownloadAreaSelectors.setMinimumSize(new Dimension(0, 0)); 146 146 147 147 dialogSplit = new JSplitPane( … … 148 148 JSplitPane.VERTICAL_SPLIT, 149 149 downloadSourcesTab, 150 150 tpDownloadAreaSelectors); 151 dialogSplit.addPropertyChangeListener(getDividerChangedListener()); 151 152 153 ChangeListener tabChangedListener = getDownloadSourceTabChangeListener(); 154 tabChangedListener.stateChanged(new ChangeEvent(downloadSourcesTab)); 155 downloadSourcesTab.addChangeListener(tabChangedListener); 156 152 157 mainPanel.add(dialogSplit, GBC.eol().fill()); 153 158 154 159 cbNewLayer = new JCheckBox(tr("Download as new layer")); … … 170 175 171 176 ExpertToggleAction.addVisibilitySwitcher(cbZoomToDownloadedData); 172 177 173 if (!ExpertToggleAction.isExpert()) { 174 JLabel infoLabel = new JLabel( 175 tr("Use left click&drag to select area, arrows or right mouse button to scroll map, wheel or +/- to zoom.")); 176 mainPanel.add(infoLabel, GBC.eol().anchor(GBC.SOUTH).insets(0, 0, 0, 0)); 177 } 178 mainPanel.add(new JLabel(), GBC.eol()); // place info label at a new line 179 JLabel infoLabel = new JLabel( 180 tr("Use left click&drag to select area, arrows or right mouse button to scroll map, wheel or +/- to zoom.")); 181 mainPanel.add(infoLabel, GBC.eol().anchor(GBC.CENTER).insets(0, 0, 0, 0)); 182 183 ExpertToggleAction.addExpertModeChangeListener(isExpert -> infoLabel.setVisible(!isExpert), true); 184 178 185 return mainPanel; 179 186 } 180 187 … … 252 259 addWindowListener(new WindowEventHandler()); 253 260 ExpertToggleAction.addExpertModeChangeListener(expertListener); 254 261 restoreSettings(); 262 263 // if no bounding box is selected make sure it is still propagated. 264 if (currentBounds == null) { 265 boundingBoxChanged(null, null); 266 } 255 267 } 256 268 257 269 /** … … 314 326 315 327 /** 316 328 * 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 329 * @return {@code true} if the download dialog must be open at startup, {@code false} otherwise. 318 330 */ 319 331 public static boolean isAutorunEnabled() { 320 332 return DOWNLOAD_AUTORUN.get(); … … 321 333 } 322 334 323 335 /** 324 * Adds a new download area selector to the download dialog 336 * Adds a new download area selector to the download dialog. 325 337 * 326 * @param selector the download are selector 327 * @param displayName the display name of the selector 338 * @param selector the download are selector. 339 * @param displayName the display name of the selector. 328 340 */ 329 341 public void addDownloadAreaSelector(JPanel selector, String displayName) { 330 342 tpDownloadAreaSelectors.add(displayName, selector); … … 331 343 } 332 344 333 345 /** 334 * Adds a new download source to the download dialog 346 * Adds a new download source to the download dialog if it is not added. 335 347 * 336 348 * @param downloadSource The download source to be added. 337 349 * @param <T> The type of the download data. 350 * @throws JosmRuntimeException If the download source is already added. Note, download sources are 351 * compared by their reference. 338 352 */ 339 353 public <T> void addDownloadSource(DownloadSource<T> downloadSource) { 354 if (downloadSources.contains(downloadSource)) { 355 throw new JosmRuntimeException("The download source you are trying to add already exists."); 356 } 357 358 downloadSources.add(downloadSource); 340 359 if ((ExpertToggleAction.isExpert() && downloadSource.onlyExpert()) || !downloadSource.onlyExpert()) { 341 360 addNewDownloadSourceTab(downloadSource); 342 361 } … … 343 362 } 344 363 345 364 /** 346 * Refreshes the tile sources 365 * Refreshes the tile sources. 347 366 * @since 6364 348 367 */ 349 368 public final void refreshTileSources() { … … 358 377 public void rememberSettings() { 359 378 DOWNLOAD_TAB.put(tpDownloadAreaSelectors.getSelectedIndex()); 360 379 DOWNLOAD_SOURCE_TAB.put(downloadSourcesTab.getSelectedIndex()); 361 DIALOG_SPLIT.put(dialogSplit.getDividerLocation());362 380 DOWNLOAD_NEWLAYER.put(cbNewLayer.isSelected()); 363 381 DOWNLOAD_ZOOMTODATA.put(cbZoomToDownloadedData.isSelected()); 364 382 if (currentBounds != null) { … … 373 391 cbNewLayer.setSelected(DOWNLOAD_NEWLAYER.get()); 374 392 cbStartup.setSelected(isAutorunEnabled()); 375 393 cbZoomToDownloadedData.setSelected(DOWNLOAD_ZOOMTODATA.get()); 376 dialogSplit.setDividerLocation(DIALOG_SPLIT.get());377 394 378 395 try { 379 396 tpDownloadAreaSelectors.setSelectedIndex(DOWNLOAD_TAB.get()); … … 407 424 408 425 /** 409 426 * Returns the previously saved bounding box from preferences. 410 * @return The bounding box saved in preferences if any, {@code null} otherwise 427 * @return The bounding box saved in preferences if any, {@code null} otherwise. 411 428 * @since 6509 412 429 */ 413 430 public static Bounds getSavedDownloadBounds() { … … 499 516 * @param downloadSource The download source to be added. 500 517 * @param <T> The type of the download data. 501 518 */ 502 pr ivate<T> void addNewDownloadSourceTab(DownloadSource<T> downloadSource) {519 protected <T> void addNewDownloadSourceTab(DownloadSource<T> downloadSource) { 503 520 AbstractDownloadSourcePanel<T> panel = downloadSource.createPanel(); 504 521 downloadSourcesTab.add(panel, downloadSource.getLabel()); 505 522 Icon icon = panel.getIcon(); … … 535 552 } 536 553 537 554 /** 555 * Creates a listener that reacts on tab switches for {@code downloadSourcesTab} in order 556 * to adjust proper division of the dialog according to user saved preferences or minimal size 557 * of the panel. 558 * @return A listener to adjust dialog division. 559 */ 560 private ChangeListener getDownloadSourceTabChangeListener() { 561 return ec -> { 562 JTabbedPane tabbedPane = (JTabbedPane) ec.getSource(); 563 Component selectedComponent = tabbedPane.getSelectedComponent(); 564 if (selectedComponent instanceof AbstractDownloadSourcePanel) { 565 AbstractDownloadSourcePanel<?> panel = (AbstractDownloadSourcePanel<?>) selectedComponent; 566 dialogSplit.setDividerLocation(Main.pref.getInteger( 567 TAB_SPLIT_NAMESPACE + panel.getSimpleName(), 568 panel.getMinimumSize().height)); 569 } 570 }; 571 } 572 573 /** 574 * Creates a listener that react on dialog splitters movements to save users preferences. 575 * @return A listener to save user preferred split of the dialog. 576 */ 577 private PropertyChangeListener getDividerChangedListener() { 578 return evt -> { 579 if (evt.getPropertyName().equalsIgnoreCase(JSplitPane.DIVIDER_LOCATION_PROPERTY)) { 580 Component selectedComponent = downloadSourcesTab.getSelectedComponent(); 581 if (selectedComponent instanceof AbstractDownloadSourcePanel) { 582 AbstractDownloadSourcePanel<?> panel = (AbstractDownloadSourcePanel<?>) selectedComponent; 583 Main.pref.put( 584 TAB_SPLIT_NAMESPACE + panel.getSimpleName(), 585 String.valueOf(dialogSplit.getDividerLocation()) 586 ); 587 } 588 } 589 }; 590 } 591 592 /** 538 593 * Action that is executed when the cancel button is pressed. 539 594 */ 540 595 class CancelAction extends AbstractAction { … … 554 609 555 610 @Override 556 611 public void actionPerformed(ActionEvent e) { 557 AbstractDownloadSourcePanel<?> pnl = (AbstractDownloadSourcePanel<?>) downloadSourcesTab.getSelectedComponent(); 558 run(); 559 pnl.checkCancel(); 612 Component panel = downloadSourcesTab.getSelectedComponent(); 613 if (panel instanceof AbstractDownloadSourcePanel) { 614 AbstractDownloadSourcePanel<?> pnl = (AbstractDownloadSourcePanel<?>) panel; 615 run(); 616 pnl.checkCancel(); 617 } else { 618 run(); 619 } 560 620 } 561 621 } 562 622 … … 572 632 } 573 633 574 634 /** 575 * Starts the download, if possible 635 * Starts the download and closes the dialog, if all requirements for the current download source are met. 636 * Otherwise the download is not started and the dialog remains visible. 576 637 */ 577 638 public void run() { 578 639 Component panel = downloadSourcesTab.getSelectedComponent(); -
src/org/openstreetmap/josm/gui/download/DownloadSettings.java
1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.gui.download; 3 3 4 import org.openstreetmap.josm.data.Bounds; 5 4 6 import java.util.Optional; 5 7 6 import org.openstreetmap.josm.data.Bounds;7 8 8 /** 9 9 * The global settings of {@link DownloadDialog}. 10 10 * <p> … … 51 51 * @return The bounds or an empty {@link Optional} if no bounds are selected 52 52 */ 53 53 public Optional<Bounds> getDownloadBounds() { 54 if (downloadBounds == null) { 55 return Optional.empty(); 56 } else { 57 return Optional.of(downloadBounds); 58 } 54 return Optional.ofNullable(downloadBounds); 59 55 } 60 56 } -
src/org/openstreetmap/josm/gui/download/DownloadSource.java
30 30 String getLabel(); 31 31 32 32 /** 33 * Add a download source to the dialog, see {@link DownloadDialog}.34 * @param dialog The download dialog.35 */36 void addGui(DownloadDialog dialog);37 38 /**39 33 * Defines whether this download source should be visible only in the expert mode. 40 34 * @return Returns {@code true} if the download source should be visible only in the 41 35 * expert mode, {@code false} otherwise. -
src/org/openstreetmap/josm/gui/download/OSMDownloadSource.java
4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 6 import java.awt.Color; 7 import java.awt.Dimension; 7 8 import java.awt.Font; 8 9 import java.awt.GridBagLayout; 9 10 import java.util.ArrayList; … … 117 118 } 118 119 119 120 @Override 120 public void addGui(DownloadDialog dialog) {121 dialog.addDownloadSource(this);122 }123 124 @Override125 121 public boolean onlyExpert() { 126 122 return false; 127 123 } … … 137 133 private final JCheckBox cbDownloadNotes; 138 134 private final JLabel sizeCheck = new JLabel(); 139 135 136 private static final String SIMPLE_NAME = "osmdownloadpanel"; 140 137 private static final BooleanProperty DOWNLOAD_OSM = new BooleanProperty("download.osm.data", true); 141 138 private static final BooleanProperty DOWNLOAD_GPS = new BooleanProperty("download.osm.gps", false); 142 139 private static final BooleanProperty DOWNLOAD_NOTES = new BooleanProperty("download.osm.notes", false); 143 140 144 141 /** 145 * Creates a new {@link OSMDownloadSourcePanel} 146 * @param ds The osm download source the panel is for 142 * Creates a new {@link OSMDownloadSourcePanel}. 143 * @param ds The osm download source the panel is for. 147 144 */ 148 145 public OSMDownloadSourcePanel(OSMDownloadSource ds) { 149 146 super(ds); … … 174 171 add(cbDownloadGpxData, GBC.std().insets(1, 5, 1, 5)); 175 172 add(cbDownloadNotes, GBC.eol().insets(1, 5, 1, 5)); 176 173 add(sizeCheck, GBC.eol().anchor(GBC.EAST).insets(5, 5, 5, 2)); 174 175 setMinimumSize(new Dimension(450, 115)); 177 176 } 178 177 179 178 @Override … … 277 276 updateSizeCheck(bbox); 278 277 } 279 278 279 @Override 280 public String getSimpleName() { 281 return SIMPLE_NAME; 282 } 283 280 284 private void updateSizeCheck(Bounds bbox) { 281 boolean isAreaTooLarge = false;282 285 if (bbox == null) { 283 286 sizeCheck.setText(tr("No area selected yet")); 284 287 sizeCheck.setForeground(Color.darkGray); 285 } else if (!isDownloadNotes() && !isDownloadOsmData() && !isDownloadGpxData()) { 288 return; 289 } 290 291 boolean isAreaTooLarge = false; 292 if (!isDownloadNotes() && !isDownloadOsmData() && !isDownloadGpxData()) { 286 293 isAreaTooLarge = false; 287 294 } else if (isDownloadNotes() && !isDownloadOsmData() && !isDownloadGpxData()) { 288 295 // see max_note_request_area in https://github.com/openstreetmap/openstreetmap-website/blob/master/config/example.application.yml -
src/org/openstreetmap/josm/gui/download/OverpassDownloadSource.java
70 70 } 71 71 72 72 @Override 73 public void addGui(DownloadDialog dialog) {74 dialog.addDownloadSource(this);75 }76 77 @Override78 73 public boolean onlyExpert() { 79 74 return true; 80 75 } … … 88 83 private JosmTextArea overpassQuery; 89 84 private OverpassQueryList overpassQueryList; 90 85 86 private static final String SIMPLE_NAME = "overpassdownloadpanel"; 91 87 private static final BooleanProperty OVERPASS_QUERY_LIST_OPENED = 92 88 new BooleanProperty("download.overpass.query-list.opened", false); 93 89 private static final String ACTION_IMG_SUBDIR = "dialogs"; … … 179 175 add(leftPanel, BorderLayout.WEST); 180 176 add(innerPanel, BorderLayout.CENTER); 181 177 add(listPanel, BorderLayout.EAST); 178 179 setMinimumSize(new Dimension(450, 240)); 182 180 } 183 181 184 182 @Override … … 274 272 return ImageProvider.get("download-overpass"); 275 273 } 276 274 275 @Override 276 public String getSimpleName() { 277 return SIMPLE_NAME; 278 } 279 277 280 /** 278 281 * Action that delegates snippet creation to {@link OverpassQueryList#createNewItem()}. 279 282 */
