Changeset 12706 in josm
- Timestamp:
- 2017-09-01T01:51:07+02:00 (7 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui/download
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/download/DownloadDialog.java
r12705 r12706 16 16 import java.awt.event.WindowEvent; 17 17 import java.util.ArrayList; 18 import java.util.Arrays;19 18 import java.util.List; 20 19 import java.util.Optional; 20 import java.util.stream.Collectors; 21 21 import java.util.stream.IntStream; 22 22 … … 40 40 import org.openstreetmap.josm.data.preferences.BooleanProperty; 41 41 import org.openstreetmap.josm.data.preferences.IntegerProperty; 42 import org.openstreetmap.josm.data.preferences.StringProperty; 42 43 import org.openstreetmap.josm.gui.MainApplication; 43 44 import org.openstreetmap.josm.gui.MapView; … … 62 63 63 64 private static final IntegerProperty DOWNLOAD_TAB = new IntegerProperty("download.tab", 0); 64 private static final IntegerProperty DOWNLOAD_SOURCE_TAB = new IntegerProperty("download-source.tab", 0);65 private static final StringProperty DOWNLOAD_SOURCE_TAB = new StringProperty("download.source.tab", OSMDownloadSource.SIMPLE_NAME); 65 66 private static final BooleanProperty DOWNLOAD_AUTORUN = new BooleanProperty("download.autorun", false); 66 67 private static final BooleanProperty DOWNLOAD_NEWLAYER = new BooleanProperty("download.newlayer", false); … … 85 86 protected final transient List<DownloadSelection> downloadSelections = new ArrayList<>(); 86 87 protected final JTabbedPane tpDownloadAreaSelectors = new JTabbedPane(); 87 protected final JTabbedPane downloadSourcesTab = new JTabbedPane();88 protected final DownloadSourceTabs downloadSourcesTab = new DownloadSourceTabs(); 88 89 89 90 protected JCheckBox cbNewLayer; … … 268 269 } 269 270 270 for (Component ds : downloadSourcesTab.getComponents()) { 271 if (ds instanceof AbstractDownloadSourcePanel) { 272 ((AbstractDownloadSourcePanel<?>) ds).boudingBoxChanged(b); 273 } 271 for (AbstractDownloadSourcePanel<?> ds : downloadSourcesTab.getAllPanels()) { 272 ds.boudingBoxChanged(b); 274 273 } 275 274 } … … 342 341 343 342 downloadSources.add(downloadSource); 344 if ((ExpertToggleAction.isExpert() && downloadSource.onlyExpert()) || !downloadSource.onlyExpert()) { 345 addNewDownloadSourceTab(downloadSource); 346 } 343 addNewDownloadSourceTab(downloadSource); 347 344 } 348 345 … … 362 359 public void rememberSettings() { 363 360 DOWNLOAD_TAB.put(tpDownloadAreaSelectors.getSelectedIndex()); 364 DOWNLOAD_SOURCE_TAB.put(downloadSourcesTab.getSelectedIndex());361 downloadSourcesTab.getSelectedPanel().ifPresent(panel -> DOWNLOAD_SOURCE_TAB.put(panel.getSimpleName())); 365 362 DOWNLOAD_NEWLAYER.put(cbNewLayer.isSelected()); 366 363 DOWNLOAD_ZOOMTODATA.put(cbZoomToDownloadedData.isSelected()); … … 385 382 } 386 383 387 try { 388 downloadSourcesTab.setSelectedIndex(DOWNLOAD_SOURCE_TAB.get()); 389 } catch (IndexOutOfBoundsException e) { 390 Logging.trace(e); 391 downloadSourcesTab.setSelectedIndex(0); 392 } 384 downloadSourcesTab.setSelected(DOWNLOAD_SOURCE_TAB.get()); 393 385 394 386 if (MainApplication.isDisplayingMapView()) { … … 483 475 484 476 /** 485 * Returns position of the download source in the tabbed pane.486 * @param downloadSource The download source.487 * @return The index of the download source, or -1 if it not in the pane.488 */489 protected int getDownloadSourceIndex(DownloadSource<?> downloadSource) {490 return Arrays.stream(downloadSourcesTab.getComponents())491 .filter(it -> it instanceof AbstractDownloadSourcePanel)492 .map(it -> (AbstractDownloadSourcePanel<?>) it)493 .filter(it -> it.getDownloadSource().equals(downloadSource))494 .findAny()495 .map(downloadSourcesTab::indexOfComponent)496 .orElse(-1);497 }498 499 /**500 477 * Adds the download source to the download sources tab. 501 478 * @param downloadSource The download source to be added. … … 503 480 */ 504 481 protected <T> void addNewDownloadSourceTab(DownloadSource<T> downloadSource) { 505 AbstractDownloadSourcePanel<T> panel = downloadSource.createPanel(); 506 downloadSourcesTab.add(panel, downloadSource.getLabel()); 507 Icon icon = panel.getIcon(); 508 if (icon != null) { 509 int idx = getDownloadSourceIndex(downloadSource); 510 downloadSourcesTab.setIconAt( 511 idx != -1 ? idx : downloadSourcesTab.getTabCount() - 1, 512 icon); 513 } 482 downloadSourcesTab.addPanel(downloadSource.createPanel()); 514 483 } 515 484 … … 520 489 */ 521 490 private ExpertToggleAction.ExpertModeChangeListener getExpertModeListenerForDownloadSources() { 522 return isExpert -> { 523 if (isExpert) { 524 downloadSources.stream() 525 .filter(DownloadSource::onlyExpert) 526 .filter(it -> getDownloadSourceIndex(it) == -1) 527 .forEach(this::addNewDownloadSourceTab); 528 } else { 529 IntStream.range(0, downloadSourcesTab.getTabCount()) 530 .mapToObj(downloadSourcesTab::getComponentAt) 531 .filter(it -> it instanceof AbstractDownloadSourcePanel) 532 .map(it -> (AbstractDownloadSourcePanel<?>) it) 533 .filter(it -> it.getDownloadSource().onlyExpert()) 534 .forEach(downloadSourcesTab::remove); 535 } 536 }; 491 return downloadSourcesTab::updateExpert; 537 492 } 538 493 … … 544 499 */ 545 500 private ChangeListener getDownloadSourceTabChangeListener() { 546 return ec -> { 547 JTabbedPane tabbedPane = (JTabbedPane) ec.getSource(); 548 Component selectedComponent = tabbedPane.getSelectedComponent(); 549 if (selectedComponent instanceof AbstractDownloadSourcePanel) { 550 AbstractDownloadSourcePanel<?> panel = (AbstractDownloadSourcePanel<?>) selectedComponent; 551 dialogSplit.setPolicy(panel.getSizingPolicy()); 552 } 553 }; 501 return ec -> downloadSourcesTab.getSelectedPanel().ifPresent( 502 panel -> dialogSplit.setPolicy(panel.getSizingPolicy())); 554 503 } 555 504 … … 568 517 */ 569 518 public void run() { 519 rememberSettings(); 570 520 setCanceled(true); 571 521 setVisible(false); … … 574 524 @Override 575 525 public void actionPerformed(ActionEvent e) { 576 Component panel = downloadSourcesTab.getSelectedComponent(); 577 if (panel instanceof AbstractDownloadSourcePanel) { 578 AbstractDownloadSourcePanel<?> pnl = (AbstractDownloadSourcePanel<?>) panel; 579 run(); 580 pnl.checkCancel(); 581 } else { 582 run(); 583 } 526 Optional<AbstractDownloadSourcePanel<?>> panel = downloadSourcesTab.getSelectedPanel(); 527 run(); 528 panel.ifPresent(AbstractDownloadSourcePanel::checkCancel); 584 529 } 585 530 } … … 601 546 */ 602 547 public void run() { 603 Component panel = downloadSourcesTab.getSelectedComponent(); 604 if (panel instanceof AbstractDownloadSourcePanel) { 605 AbstractDownloadSourcePanel<?> pnl = (AbstractDownloadSourcePanel<?>) panel; 548 rememberSettings(); 549 downloadSourcesTab.getSelectedPanel().ifPresent(panel -> { 606 550 DownloadSettings downloadSettings = getDownloadSettings(); 607 if (pnl.checkDownload(downloadSettings)) { 608 rememberSettings(); 551 if (panel.checkDownload(downloadSettings)) { 609 552 setCanceled(false); 610 553 setVisible(false); 611 p nl.triggerDownload(downloadSettings);554 panel.triggerDownload(downloadSettings); 612 555 } 613 } 556 }); 614 557 } 615 558 … … 629 572 public void windowActivated(WindowEvent e) { 630 573 btnDownload.requestFocusInWindow(); 574 } 575 } 576 577 /** 578 * A special tabbed pane for {@link AbstractDownloadSourcePanel}s 579 * @author Michael Zangl 580 * @since 12706 581 */ 582 private static class DownloadSourceTabs extends JTabbedPane { 583 private final List<AbstractDownloadSourcePanel<?>> allPanels = new ArrayList<>(); 584 585 List<AbstractDownloadSourcePanel<?>> getAllPanels() { 586 return allPanels; 587 } 588 589 List<AbstractDownloadSourcePanel<?>> getVisiblePanels() { 590 return IntStream.range(0, getTabCount()) 591 .mapToObj(this::getComponentAt) 592 .map(p -> (AbstractDownloadSourcePanel<?>) p) 593 .collect(Collectors.toList()); 594 } 595 596 void setSelected(String simpleName) { 597 getVisiblePanels().stream() 598 .filter(panel -> simpleName.equals(panel.getSimpleName())) 599 .findFirst() 600 .ifPresent(this::setSelectedComponent); 601 } 602 603 void updateExpert(boolean isExpert) { 604 updateTabs(); 605 } 606 607 void addPanel(AbstractDownloadSourcePanel<?> panel) { 608 allPanels.add(panel); 609 updateTabs(); 610 } 611 612 private void updateTabs() { 613 // Not the best performance, but we don't do it often 614 removeAll(); 615 616 boolean isExpert = ExpertToggleAction.isExpert(); 617 allPanels.stream() 618 .filter(panel -> isExpert || !panel.getDownloadSource().onlyExpert()) 619 .forEach(panel -> addTab(panel.getDownloadSource().getLabel(), panel.getIcon(), panel)); 620 } 621 622 Optional<AbstractDownloadSourcePanel<?>> getSelectedPanel() { 623 return Optional.ofNullable((AbstractDownloadSourcePanel<?>) getSelectedComponent()); 624 } 625 626 @Override 627 public void insertTab(String title, Icon icon, Component component, String tip, int index) { 628 if (!(component instanceof AbstractDownloadSourcePanel)) { 629 throw new IllegalArgumentException("Can only add AbstractDownloadSourcePanels"); 630 } 631 super.insertTab(title, icon, component, tip, index); 631 632 } 632 633 } -
trunk/src/org/openstreetmap/josm/gui/download/OSMDownloadSource.java
r12684 r12706 42 42 */ 43 43 public class OSMDownloadSource implements DownloadSource<OSMDownloadSource.OSMDownloadData> { 44 /** 45 * The simple name for the {@link OSMDownloadSourcePanel} 46 * @since 12706 47 */ 48 public static final String SIMPLE_NAME = "osmdownloadpanel"; 44 49 45 50 @Override … … 134 139 private final JLabel sizeCheck = new JLabel(); 135 140 136 private static final String SIMPLE_NAME = "osmdownloadpanel";137 141 private static final BooleanProperty DOWNLOAD_OSM = new BooleanProperty("download.osm.data", true); 138 142 private static final BooleanProperty DOWNLOAD_GPS = new BooleanProperty("download.osm.gps", false);
Note:
See TracChangeset
for help on using the changeset viewer.