Ticket #15167: merge-download-dialog-v4.patch

File merge-download-dialog-v4.patch, 17.5 KB (added by bafonins, 7 years ago)

refactored some code, fixed docs, removed redundant code according to Michaels changes, each download source has its own size

  • src/org/openstreetmap/josm/gui/download/AbstractDownloadSourcePanel.java

     
    9797    public void triggerDownload(DownloadSettings downloadSettings) {
    9898        getDownloadSource().doDownload(getData(), downloadSettings);
    9999    }
     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();
    100109}
  • src/org/openstreetmap/josm/gui/download/DownloadDialog.java

     
    1515import java.awt.event.KeyEvent;
    1616import java.awt.event.WindowAdapter;
    1717import java.awt.event.WindowEvent;
     18import java.beans.PropertyChangeListener;
    1819import java.util.ArrayList;
    1920import java.util.Arrays;
    2021import java.util.List;
     
    3233import javax.swing.JSplitPane;
    3334import javax.swing.JTabbedPane;
    3435import javax.swing.KeyStroke;
     36import javax.swing.event.ChangeEvent;
     37import javax.swing.event.ChangeListener;
    3538
    3639import org.openstreetmap.josm.Main;
    3740import org.openstreetmap.josm.actions.ExpertToggleAction;
     
    4952import org.openstreetmap.josm.tools.GBC;
    5053import org.openstreetmap.josm.tools.ImageProvider;
    5154import org.openstreetmap.josm.tools.InputMapUtils;
     55import org.openstreetmap.josm.tools.JosmRuntimeException;
    5256import org.openstreetmap.josm.tools.Logging;
    5357import org.openstreetmap.josm.tools.OsmUrlToBounds;
    5458import org.openstreetmap.josm.tools.WindowGeometry;
     
    6165    /**
    6266     * Preference properties
    6367     */
     68    private static final String TAB_SPLIT_NAMESPACE = "download.tabsplit.";
    6469    private static final IntegerProperty DOWNLOAD_TAB = new IntegerProperty("download.tab", 0);
    6570    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);
    6771    private static final BooleanProperty DOWNLOAD_AUTORUN = new BooleanProperty("download.autorun", false);
    6872    private static final BooleanProperty DOWNLOAD_NEWLAYER = new BooleanProperty("download.newlayer", false);
    6973    private static final BooleanProperty DOWNLOAD_ZOOMTODATA = new BooleanProperty("download.zoomtodata", true);
     
    114118    protected final JPanel buildMainPanel() {
    115119        mainPanel = new JPanel(new GridBagLayout());
    116120
    117         downloadSources.add(new OSMDownloadSource());
    118         downloadSources.add(new OverpassDownloadSource());
     121        // add default download sources
     122        addDownloadSource(new OSMDownloadSource());
     123        addDownloadSource(new OverpassDownloadSource());
    119124
    120         // register all default download sources
    121         for (int i = 0; i < downloadSources.size(); i++) {
    122             downloadSources.get(i).addGui(this);
    123         }
    124 
    125125        // must be created before hook
    126126        slippyMapChooser = new SlippyMapChooser();
    127127
     
    140140            s.addGui(this);
    141141        }
    142142
    143         // allow to collapse the panes completely
    144         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));
    145145        tpDownloadAreaSelectors.setMinimumSize(new Dimension(0, 0));
    146146
    147147        dialogSplit = new JSplitPane(
     
    148148                JSplitPane.VERTICAL_SPLIT,
    149149                downloadSourcesTab,
    150150                tpDownloadAreaSelectors);
     151        dialogSplit.addPropertyChangeListener(getDividerChangedListener());
    151152
     153        ChangeListener tabChangedListener = getDownloadSourceTabChangeListener();
     154        tabChangedListener.stateChanged(new ChangeEvent(downloadSourcesTab));
     155        downloadSourcesTab.addChangeListener(tabChangedListener);
     156
    152157        mainPanel.add(dialogSplit, GBC.eol().fill());
    153158
    154159        cbNewLayer = new JCheckBox(tr("Download as new layer"));
     
    252257        addWindowListener(new WindowEventHandler());
    253258        ExpertToggleAction.addExpertModeChangeListener(expertListener);
    254259        restoreSettings();
     260
     261        // if no bounding box is selected make sure it is still propagated.
     262        if (currentBounds == null) {
     263            boundingBoxChanged(null, null);
     264        }
    255265    }
    256266
    257267    /**
     
    314324
    315325    /**
    316326     * 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
     327     * @return {@code true} if the download dialog must be open at startup, {@code false} otherwise.
    318328     */
    319329    public static boolean isAutorunEnabled() {
    320330        return DOWNLOAD_AUTORUN.get();
     
    321331    }
    322332
    323333    /**
    324      * Adds a new download area selector to the download dialog
     334     * Adds a new download area selector to the download dialog.
    325335     *
    326      * @param selector the download are selector
    327      * @param displayName the display name of the selector
     336     * @param selector the download are selector.
     337     * @param displayName the display name of the selector.
    328338     */
    329339    public void addDownloadAreaSelector(JPanel selector, String displayName) {
    330340        tpDownloadAreaSelectors.add(displayName, selector);
     
    331341    }
    332342
    333343    /**
    334      * Adds a new download source to the download dialog
     344     * Adds a new download source to the download dialog if it is not added.
    335345     *
    336346     * @param downloadSource The download source to be added.
    337347     * @param <T> The type of the download data.
     348     * @throws JosmRuntimeException If the download source is already added. Note, download sources are
     349     * compared by their reference.
    338350     */
    339351    public <T> void addDownloadSource(DownloadSource<T> downloadSource) {
     352        if (downloadSources.contains(downloadSource)) {
     353            throw new JosmRuntimeException("The download source you are trying to add already exists.");
     354        }
     355
     356        downloadSources.add(downloadSource);
    340357        if ((ExpertToggleAction.isExpert() && downloadSource.onlyExpert()) || !downloadSource.onlyExpert()) {
    341358            addNewDownloadSourceTab(downloadSource);
    342359        }
     
    343360    }
    344361
    345362    /**
    346      * Refreshes the tile sources
     363     * Refreshes the tile sources.
    347364     * @since 6364
    348365     */
    349366    public final void refreshTileSources() {
     
    358375    public void rememberSettings() {
    359376        DOWNLOAD_TAB.put(tpDownloadAreaSelectors.getSelectedIndex());
    360377        DOWNLOAD_SOURCE_TAB.put(downloadSourcesTab.getSelectedIndex());
    361         DIALOG_SPLIT.put(dialogSplit.getDividerLocation());
    362378        DOWNLOAD_NEWLAYER.put(cbNewLayer.isSelected());
    363379        DOWNLOAD_ZOOMTODATA.put(cbZoomToDownloadedData.isSelected());
    364380        if (currentBounds != null) {
     
    373389        cbNewLayer.setSelected(DOWNLOAD_NEWLAYER.get());
    374390        cbStartup.setSelected(isAutorunEnabled());
    375391        cbZoomToDownloadedData.setSelected(DOWNLOAD_ZOOMTODATA.get());
    376         dialogSplit.setDividerLocation(DIALOG_SPLIT.get());
    377392
    378393        try {
    379394            tpDownloadAreaSelectors.setSelectedIndex(DOWNLOAD_TAB.get());
     
    407422
    408423    /**
    409424     * Returns the previously saved bounding box from preferences.
    410      * @return The bounding box saved in preferences if any, {@code null} otherwise
     425     * @return The bounding box saved in preferences if any, {@code null} otherwise.
    411426     * @since 6509
    412427     */
    413428    public static Bounds getSavedDownloadBounds() {
     
    499514     * @param downloadSource The download source to be added.
    500515     * @param <T> The type of the download data.
    501516     */
    502     private <T> void addNewDownloadSourceTab(DownloadSource<T> downloadSource) {
     517    protected <T> void addNewDownloadSourceTab(DownloadSource<T> downloadSource) {
    503518        AbstractDownloadSourcePanel<T> panel = downloadSource.createPanel();
    504519        downloadSourcesTab.add(panel, downloadSource.getLabel());
    505520        Icon icon = panel.getIcon();
     
    535550    }
    536551
    537552    /**
     553     * Creates a listener that reacts on tab switches for {@code downloadSourcesTab} in order
     554     * to adjust proper division of the dialog according to user saved preferences or minimal size
     555     * of the panel.
     556     * @return A listener to adjust dialog division.
     557     */
     558    private ChangeListener getDownloadSourceTabChangeListener() {
     559        return ec -> {
     560            JTabbedPane tabbedPane = (JTabbedPane) ec.getSource();
     561            Component selectedComponent = tabbedPane.getSelectedComponent();
     562            if (selectedComponent instanceof AbstractDownloadSourcePanel) {
     563                AbstractDownloadSourcePanel<?> panel = (AbstractDownloadSourcePanel<?>) selectedComponent;
     564                dialogSplit.setDividerLocation(Main.pref.getInteger(
     565                        TAB_SPLIT_NAMESPACE + panel.getSimpleName(),
     566                        panel.getMinimumSize().height));
     567            }
     568        };
     569    }
     570
     571    /**
     572     * Creates a listener that react on dialog splitters movements to save users preferences.
     573     * @return A listener to save user preferred split of the dialog.
     574     */
     575    private PropertyChangeListener getDividerChangedListener() {
     576        return evt -> {
     577            if (evt.getPropertyName().equalsIgnoreCase(JSplitPane.DIVIDER_LOCATION_PROPERTY)) {
     578                Component selectedComponent = downloadSourcesTab.getSelectedComponent();
     579                if (selectedComponent instanceof AbstractDownloadSourcePanel) {
     580                    AbstractDownloadSourcePanel<?> panel = (AbstractDownloadSourcePanel<?>) selectedComponent;
     581                    Main.pref.put(
     582                            TAB_SPLIT_NAMESPACE + panel.getSimpleName(),
     583                            String.valueOf(dialogSplit.getDividerLocation())
     584                    );
     585                }
     586            }
     587        };
     588    }
     589
     590    /**
    538591     * Action that is executed when the cancel button is pressed.
    539592     */
    540593    class CancelAction extends AbstractAction {
     
    554607
    555608        @Override
    556609        public void actionPerformed(ActionEvent e) {
    557             AbstractDownloadSourcePanel<?> pnl = (AbstractDownloadSourcePanel<?>) downloadSourcesTab.getSelectedComponent();
    558             run();
    559             pnl.checkCancel();
     610            Component panel = downloadSourcesTab.getSelectedComponent();
     611            if (panel instanceof AbstractDownloadSourcePanel) {
     612                AbstractDownloadSourcePanel<?> pnl = (AbstractDownloadSourcePanel<?>) panel;
     613                run();
     614                pnl.checkCancel();
     615            } else {
     616                run();
     617            }
    560618        }
    561619    }
    562620
     
    572630        }
    573631
    574632        /**
    575          * Starts the download, if possible
     633         * Starts the download and closes the dialog, if all requirements for the current download source are met.
     634         * Otherwise the download is not started and the dialog remains visible.
    576635         */
    577636        public void run() {
    578637            Component panel = downloadSourcesTab.getSelectedComponent();
  • src/org/openstreetmap/josm/gui/download/DownloadSettings.java

     
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.download;
    33
     4import org.openstreetmap.josm.data.Bounds;
     5
    46import java.util.Optional;
    57
    6 import org.openstreetmap.josm.data.Bounds;
    7 
    88/**
    99 * The global settings of {@link DownloadDialog}.
    1010 * <p>
     
    5151     * @return The bounds or an empty {@link Optional} if no bounds are selected
    5252     */
    5353    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);
    5955    }
    6056}
  • src/org/openstreetmap/josm/gui/download/DownloadSource.java

     
    3030    String getLabel();
    3131
    3232    /**
    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     /**
    3933     * Defines whether this download source should be visible only in the expert mode.
    4034     * @return Returns {@code true} if the download source should be visible only in the
    4135     * expert mode, {@code false} otherwise.
  • src/org/openstreetmap/josm/gui/download/OSMDownloadSource.java

     
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    66import java.awt.Color;
     7import java.awt.Dimension;
    78import java.awt.Font;
    89import java.awt.GridBagLayout;
    910import java.util.ArrayList;
     
    117118    }
    118119
    119120    @Override
    120     public void addGui(DownloadDialog dialog) {
    121         dialog.addDownloadSource(this);
    122     }
    123 
    124     @Override
    125121    public boolean onlyExpert() {
    126122        return false;
    127123    }
     
    137133        private final JCheckBox cbDownloadNotes;
    138134        private final JLabel sizeCheck = new JLabel();
    139135
     136        private static final String SIMPLE_NAME = "osmdownloadpanel";
    140137        private static final BooleanProperty DOWNLOAD_OSM = new BooleanProperty("download.osm.data", true);
    141138        private static final BooleanProperty DOWNLOAD_GPS = new BooleanProperty("download.osm.gps", false);
    142139        private static final BooleanProperty DOWNLOAD_NOTES = new BooleanProperty("download.osm.notes", false);
    143140
    144141        /**
    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.
    147144         */
    148145        public OSMDownloadSourcePanel(OSMDownloadSource ds) {
    149146            super(ds);
     
    174171            add(cbDownloadGpxData, GBC.std().insets(1, 5, 1, 5));
    175172            add(cbDownloadNotes, GBC.eol().insets(1, 5, 1, 5));
    176173            add(sizeCheck, GBC.eol().anchor(GBC.EAST).insets(5, 5, 5, 2));
     174
     175            setMinimumSize(new Dimension(450, 115));
    177176        }
    178177
    179178        @Override
     
    277276            updateSizeCheck(bbox);
    278277        }
    279278
     279        @Override
     280        public String getSimpleName() {
     281            return SIMPLE_NAME;
     282        }
     283
    280284        private void updateSizeCheck(Bounds bbox) {
    281             boolean isAreaTooLarge = false;
    282285            if (bbox == null) {
    283286                sizeCheck.setText(tr("No area selected yet"));
    284287                sizeCheck.setForeground(Color.darkGray);
    285             } else if (!isDownloadNotes() && !isDownloadOsmData() && !isDownloadGpxData()) {
     288                return;
     289            }
     290
     291            boolean isAreaTooLarge = false;
     292            if (!isDownloadNotes() && !isDownloadOsmData() && !isDownloadGpxData()) {
    286293                isAreaTooLarge = false;
    287294            } else if (isDownloadNotes() && !isDownloadOsmData() && !isDownloadGpxData()) {
    288295                // 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

     
    7070    }
    7171
    7272    @Override
    73     public void addGui(DownloadDialog dialog) {
    74         dialog.addDownloadSource(this);
    75     }
    76 
    77     @Override
    7873    public boolean onlyExpert() {
    7974        return true;
    8075    }
     
    8883        private JosmTextArea overpassQuery;
    8984        private OverpassQueryList overpassQueryList;
    9085
     86        private static final String SIMPLE_NAME = "overpassdownloadpanel";
    9187        private static final BooleanProperty OVERPASS_QUERY_LIST_OPENED =
    9288                new BooleanProperty("download.overpass.query-list.opened", false);
    9389        private static final String ACTION_IMG_SUBDIR = "dialogs";
     
    179175            add(leftPanel, BorderLayout.WEST);
    180176            add(innerPanel, BorderLayout.CENTER);
    181177            add(listPanel, BorderLayout.EAST);
     178
     179            setMinimumSize(new Dimension(450, 240));
    182180        }
    183181
    184182        @Override
     
    274272            return ImageProvider.get("download-overpass");
    275273        }
    276274
     275        @Override
     276        public String getSimpleName() {
     277            return SIMPLE_NAME;
     278        }
     279
    277280        /**
    278281         * Action that delegates snippet creation to {@link OverpassQueryList#createNewItem()}.
    279282         */