Changeset 12652 in josm


Ignore:
Timestamp:
2017-08-26T00:02:12+02:00 (7 years ago)
Author:
michael2402
Message:

Apply #15167: Merge OSM and overpass download dialog. Patch by bafonins

Location:
trunk/src/org/openstreetmap/josm
Files:
5 added
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/DownloadAction.java

    r12635 r12652  
    77import java.awt.event.ActionEvent;
    88import java.awt.event.KeyEvent;
    9 import java.util.ArrayList;
    10 import java.util.List;
    11 import java.util.Optional;
    12 import java.util.concurrent.ExecutionException;
    13 import java.util.concurrent.Future;
    149
    15 import javax.swing.JOptionPane;
    16 
    17 import org.openstreetmap.josm.actions.downloadtasks.AbstractDownloadTask;
    18 import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
    19 import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
    20 import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
    21 import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
    22 import org.openstreetmap.josm.data.Bounds;
    23 import org.openstreetmap.josm.data.ProjectionBounds;
    24 import org.openstreetmap.josm.data.ViewportData;
    25 import org.openstreetmap.josm.gui.MainApplication;
    26 import org.openstreetmap.josm.gui.MapFrame;
    2710import org.openstreetmap.josm.gui.download.DownloadDialog;
    28 import org.openstreetmap.josm.gui.util.GuiHelper;
    29 import org.openstreetmap.josm.tools.Logging;
    30 import org.openstreetmap.josm.tools.Pair;
    3111import org.openstreetmap.josm.tools.Shortcut;
    3212
     
    4525     */
    4626    public DownloadAction() {
    47         super(tr("Download from OSM..."), "download", tr("Download map data from the OSM server."),
    48               Shortcut.registerShortcut("file:download", tr("File: {0}", tr("Download from OSM...")), KeyEvent.VK_DOWN, Shortcut.CTRL_SHIFT),
     27        super(tr("Download data"), "download", tr("Download map data from a server of your choice"),
     28              Shortcut.registerShortcut("file:download", tr("File: {0}", tr("Download data")), KeyEvent.VK_DOWN, Shortcut.CTRL_SHIFT),
    4929              true);
    5030        putValue("help", ht("/Action/Download"));
     
    5636        dialog.restoreSettings();
    5737        dialog.setVisible(true);
    58 
    59         if (dialog.isCanceled()) {
    60             return;
    61         }
    62 
    63         dialog.rememberSettings();
    64 
    65         Optional<Bounds> selectedArea = dialog.getSelectedDownloadArea();
    66         if (!selectedArea.isPresent()) {
    67             JOptionPane.showMessageDialog(
    68                     dialog,
    69                     tr("Please select a download area first."),
    70                     tr("Error"),
    71                     JOptionPane.ERROR_MESSAGE
    72             );
    73             return;
    74         }
    75 
    76         final Bounds area = selectedArea.get();
    77         final boolean zoom = dialog.isZoomToDownloadedDataRequired();
    78         final List<Pair<AbstractDownloadTask<?>, Future<?>>> tasks = new ArrayList<>();
    79 
    80         if (dialog.isDownloadOsmData()) {
    81             DownloadOsmTask task = new DownloadOsmTask();
    82             task.setZoomAfterDownload(zoom && !dialog.isDownloadGpxData() && !dialog.isDownloadNotes());
    83             Future<?> future = task.download(dialog.isNewLayerRequired(), area, null);
    84             MainApplication.worker.submit(new PostDownloadHandler(task, future));
    85             if (zoom) {
    86                 tasks.add(new Pair<>(task, future));
    87             }
    88         }
    89 
    90         if (dialog.isDownloadGpxData()) {
    91             DownloadGpsTask task = new DownloadGpsTask();
    92             task.setZoomAfterDownload(zoom && !dialog.isDownloadOsmData() && !dialog.isDownloadNotes());
    93             Future<?> future = task.download(dialog.isNewLayerRequired(), area, null);
    94             MainApplication.worker.submit(new PostDownloadHandler(task, future));
    95             if (zoom) {
    96                 tasks.add(new Pair<>(task, future));
    97             }
    98         }
    99 
    100         if (dialog.isDownloadNotes()) {
    101             DownloadNotesTask task = new DownloadNotesTask();
    102             task.setZoomAfterDownload(zoom && !dialog.isDownloadOsmData() && !dialog.isDownloadGpxData());
    103             Future<?> future = task.download(false, area, null);
    104             MainApplication.worker.submit(new PostDownloadHandler(task, future));
    105             if (zoom) {
    106                 tasks.add(new Pair<>(task, future));
    107             }
    108         }
    109 
    110         if (zoom && tasks.size() > 1) {
    111             MainApplication.worker.submit(() -> {
    112                 ProjectionBounds bounds = null;
    113                 // Wait for completion of download jobs
    114                 for (Pair<AbstractDownloadTask<?>, Future<?>> p : tasks) {
    115                     try {
    116                         p.b.get();
    117                         ProjectionBounds b = p.a.getDownloadProjectionBounds();
    118                         if (bounds == null) {
    119                             bounds = b;
    120                         } else if (b != null) {
    121                             bounds.extend(b);
    122                         }
    123                     } catch (InterruptedException | ExecutionException ex) {
    124                         Logging.warn(ex);
    125                     }
    126                 }
    127                 // Zoom to the larger download bounds
    128                 MapFrame map = MainApplication.getMap();
    129                 if (map != null && bounds != null) {
    130                     final ProjectionBounds pb = bounds;
    131                     GuiHelper.runInEDTAndWait(() -> map.mapView.zoomTo(new ViewportData(pb)));
    132                 }
    133             });
    134         }
    13538    }
    13639}
  • trunk/src/org/openstreetmap/josm/gui/MainMenu.java

    r12641 r12652  
    7373import org.openstreetmap.josm.actions.OrthogonalizeAction;
    7474import org.openstreetmap.josm.actions.OrthogonalizeAction.Undo;
    75 import org.openstreetmap.josm.actions.OverpassDownloadAction;
    7675import org.openstreetmap.josm.actions.PasteAction;
    7776import org.openstreetmap.josm.actions.PasteAtSourcePositionAction;
     
    160159    /** File / Download in current view **/
    161160    public final DownloadOsmInViewAction downloadInView = new DownloadOsmInViewAction();
    162     /** File / Download from Overpass API... **/
    163     public final OverpassDownloadAction overpassDownload = new OverpassDownloadAction();
    164161    /** File / Download object... **/
    165162    public final DownloadPrimitiveAction downloadPrimitive = new DownloadPrimitiveAction();
     
    658655        add(fileMenu, download);
    659656        add(fileMenu, downloadInView, true);
    660         add(fileMenu, overpassDownload, true);
    661657        add(fileMenu, downloadPrimitive);
    662658        add(fileMenu, searchNotes);
  • trunk/src/org/openstreetmap/josm/gui/download/DownloadDialog.java

    r12643 r12652  
    66
    77import java.awt.BorderLayout;
    8 import java.awt.Color;
    98import java.awt.Component;
    109import java.awt.Dimension;
    1110import java.awt.FlowLayout;
    12 import java.awt.Font;
    1311import java.awt.Graphics;
    1412import java.awt.GridBagLayout;
     
    1917import java.awt.event.WindowEvent;
    2018import java.util.ArrayList;
     19import java.util.Arrays;
    2120import java.util.List;
    2221import java.util.Optional;
     22import java.util.stream.IntStream;
    2323
    2424import javax.swing.AbstractAction;
     25import javax.swing.Icon;
    2526import javax.swing.JButton;
    2627import javax.swing.JCheckBox;
     
    2829import javax.swing.JDialog;
    2930import javax.swing.JLabel;
    30 import javax.swing.JOptionPane;
    3131import javax.swing.JPanel;
     32import javax.swing.JSplitPane;
    3233import javax.swing.JTabbedPane;
    3334import javax.swing.KeyStroke;
    34 import javax.swing.event.ChangeListener;
    3535
    3636import org.openstreetmap.josm.Main;
     
    5252import org.openstreetmap.josm.tools.Logging;
    5353import org.openstreetmap.josm.tools.OsmUrlToBounds;
    54 import org.openstreetmap.josm.tools.Utils;
    5554import org.openstreetmap.josm.tools.WindowGeometry;
    5655
    5756/**
    58  * Dialog displayed to download OSM and/or GPS data from OSM server.
     57 * Dialog displayed to the user to download mapping data.
    5958 */
    6059public class DownloadDialog extends JDialog {
     60
     61    /**
     62     * Preference properties
     63     */
    6164    private static final IntegerProperty DOWNLOAD_TAB = new IntegerProperty("download.tab", 0);
    62 
     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);
    6367    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);
    6768    private static final BooleanProperty DOWNLOAD_NEWLAYER = new BooleanProperty("download.newlayer", false);
    6869    private static final BooleanProperty DOWNLOAD_ZOOMTODATA = new BooleanProperty("download.zoomtodata", true);
     
    8384    }
    8485
    85     protected SlippyMapChooser slippyMapChooser;
     86    protected final transient List<DownloadSource> downloadSources = new ArrayList<>();
    8687    protected final transient List<DownloadSelection> downloadSelections = new ArrayList<>();
    8788    protected final JTabbedPane tpDownloadAreaSelectors = new JTabbedPane();
     89    protected final JTabbedPane downloadSourcesTab = new JTabbedPane();
     90
    8891    protected JCheckBox cbNewLayer;
    8992    protected JCheckBox cbStartup;
    9093    protected JCheckBox cbZoomToDownloadedData;
    91     protected final JLabel sizeCheck = new JLabel();
     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();
    92103    protected transient Bounds currentBounds;
    93104    protected boolean canceled;
    94105
    95     protected JCheckBox cbDownloadOsmData;
    96     protected JCheckBox cbDownloadGpxData;
    97     protected JCheckBox cbDownloadNotes;
    98     /** the download action and button */
    99     private final DownloadAction actDownload = new DownloadAction();
    100     protected final JButton btnDownload = new JButton(actDownload);
    101 
     106    protected JButton btnDownload;
     107    protected JButton btnCancel;
     108    protected JButton btnHelp;
     109
     110    /**
     111     * Builds the main panel of the dialog.
     112     * @return The panel of the dialog.
     113     */
    102114    protected final JPanel buildMainPanel() {
    103         JPanel pnl = new JPanel(new GridBagLayout());
    104 
    105         // size check depends on selected data source
    106         final ChangeListener checkboxChangeListener = e -> updateSizeCheck();
    107 
    108         // adding the download tasks
    109         pnl.add(new JLabel(tr("Data Sources and Types:")), GBC.std().insets(5, 5, 1, 5));
    110         cbDownloadOsmData = new JCheckBox(tr("OpenStreetMap data"), true);
    111         cbDownloadOsmData.setToolTipText(tr("Select to download OSM data in the selected download area."));
    112         cbDownloadOsmData.getModel().addChangeListener(checkboxChangeListener);
    113         pnl.add(cbDownloadOsmData, GBC.std().insets(1, 5, 1, 5));
    114         cbDownloadGpxData = new JCheckBox(tr("Raw GPS data"));
    115         cbDownloadGpxData.setToolTipText(tr("Select to download GPS traces in the selected download area."));
    116         cbDownloadGpxData.getModel().addChangeListener(checkboxChangeListener);
    117         pnl.add(cbDownloadGpxData, GBC.std().insets(5, 5, 1, 5));
    118         cbDownloadNotes = new JCheckBox(tr("Notes"));
    119         cbDownloadNotes.setToolTipText(tr("Select to download notes in the selected download area."));
    120         cbDownloadNotes.getModel().addChangeListener(checkboxChangeListener);
    121         pnl.add(cbDownloadNotes, GBC.eol().insets(50, 5, 1, 5));
     115        mainPanel = new JPanel(new GridBagLayout());
     116
     117        downloadSources.add(new OSMDownloadSource());
     118        downloadSources.add(new OverpassDownloadSource());
     119
     120        // register all default download sources
     121        for (int i = 0; i < downloadSources.size(); i++) {
     122            downloadSources.get(i).addGui(this);
     123        }
    122124
    123125        // must be created before hook
    124126        slippyMapChooser = new SlippyMapChooser();
    125 
    126         // hook for subclasses
    127         buildMainPanelAboveDownloadSelections(pnl);
    128127
    129128        // predefined download selections
     
    137136        PluginHandler.addDownloadSelection(downloadSelections);
    138137
    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         }
    145 
    146         pnl.add(tpDownloadAreaSelectors, GBC.eol().fill());
    147 
    148         try {
    149             tpDownloadAreaSelectors.setSelectedIndex(DOWNLOAD_TAB.get());
    150         } catch (IndexOutOfBoundsException ex) {
    151             Logging.trace(ex);
    152             DOWNLOAD_TAB.put(0);
    153         }
    154 
    155         Font labelFont = sizeCheck.getFont();
    156         sizeCheck.setFont(labelFont.deriveFont(Font.PLAIN, labelFont.getSize()));
     138        // register all default download selections
     139        for (int i = 0; i < downloadSelections.size(); i++) {
     140            downloadSelections.get(i).addGui(this);
     141        }
     142
     143        // allow to collapse the panes completely
     144        downloadSourcesTab.setMinimumSize(new Dimension(0, 0));
     145        tpDownloadAreaSelectors.setMinimumSize(new Dimension(0, 0));
     146
     147        dialogSplit = new JSplitPane(
     148                JSplitPane.VERTICAL_SPLIT,
     149                downloadSourcesTab,
     150                tpDownloadAreaSelectors);
     151
     152        mainPanel.add(dialogSplit, GBC.eol().fill());
    157153
    158154        cbNewLayer = new JCheckBox(tr("Download as new layer"));
     
    169165        cbZoomToDownloadedData.setToolTipText(tr("Select to zoom to entire newly downloaded data."));
    170166
    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));
    173         pnl.add(cbZoomToDownloadedData, GBC.std().anchor(GBC.WEST).insets(15, 5, 5, 5));
     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));
    174170
    175171        ExpertToggleAction.addVisibilitySwitcher(cbZoomToDownloadedData);
    176 
    177         pnl.add(sizeCheck, GBC.eol().anchor(GBC.EAST).insets(5, 5, 5, 2));
    178172
    179173        if (!ExpertToggleAction.isExpert()) {
    180174            JLabel infoLabel = new JLabel(
    181175                    tr("Use left click&drag to select area, arrows or right mouse button to scroll map, wheel or +/- to zoom."));
    182             pnl.add(infoLabel, GBC.eol().anchor(GBC.SOUTH).insets(0, 0, 0, 0));
    183         }
    184         return pnl;
     176            mainPanel.add(infoLabel, GBC.eol().anchor(GBC.SOUTH).insets(0, 0, 0, 0));
     177        }
     178        return mainPanel;
    185179    }
    186180
     
    192186    }
    193187
     188    /**
     189     * Builds the button pane of the dialog.
     190     * @return The button panel of the dialog.
     191     */
    194192    protected final JPanel buildButtonPanel() {
     193        btnDownload = new JButton(new DownloadAction());
     194        btnCancel = new JButton(new CancelAction());
     195        btnHelp = new JButton(
     196                new ContextSensitiveHelpAction(getRootPane().getClientProperty("help").toString()));
     197
    195198        JPanel pnl = new JPanel(new FlowLayout());
    196199
    197         // -- download button
    198200        pnl.add(btnDownload);
     201        pnl.add(btnCancel);
     202        pnl.add(btnHelp);
     203
    199204        InputMapUtils.enableEnter(btnDownload);
    200 
    201         InputMapUtils.addEnterActionWhenAncestor(cbDownloadGpxData, actDownload);
    202         InputMapUtils.addEnterActionWhenAncestor(cbDownloadOsmData, actDownload);
    203         InputMapUtils.addEnterActionWhenAncestor(cbDownloadNotes, actDownload);
    204         InputMapUtils.addEnterActionWhenAncestor(cbNewLayer, actDownload);
    205         InputMapUtils.addEnterActionWhenAncestor(cbStartup, actDownload);
    206         InputMapUtils.addEnterActionWhenAncestor(cbZoomToDownloadedData, actDownload);
    207 
    208         // -- cancel button
    209         JButton btnCancel;
    210         CancelAction actCancel = new CancelAction();
    211         btnCancel = new JButton(actCancel);
    212         pnl.add(btnCancel);
    213205        InputMapUtils.enableEnter(btnCancel);
    214 
    215         // -- cancel on ESC
    216         InputMapUtils.addEscapeAction(getRootPane(), actCancel);
    217 
    218         // -- help button
    219         JButton btnHelp = new JButton(new ContextSensitiveHelpAction(getRootPane().getClientProperty("help").toString()));
    220         pnl.add(btnHelp);
     206        InputMapUtils.addEscapeAction(getRootPane(), btnCancel.getAction());
    221207        InputMapUtils.enableEnter(btnHelp);
     208
     209        InputMapUtils.addEnterActionWhenAncestor(cbNewLayer, btnDownload.getAction());
     210        InputMapUtils.addEnterActionWhenAncestor(cbStartup, btnDownload.getAction());
     211        InputMapUtils.addEnterActionWhenAncestor(cbZoomToDownloadedData, btnDownload.getAction());
    222212
    223213        return pnl;
     
    261251        });
    262252        addWindowListener(new WindowEventHandler());
     253        ExpertToggleAction.addExpertModeChangeListener(expertListener);
    263254        restoreSettings();
    264255    }
    265256
    266     protected void updateSizeCheck() {
    267         boolean isAreaTooLarge = false;
    268         if (currentBounds == null) {
    269             sizeCheck.setText(tr("No area selected yet"));
    270             sizeCheck.setForeground(Color.darkGray);
    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         }
    278         displaySizeCheckResult(isAreaTooLarge);
    279     }
    280 
    281     protected void displaySizeCheckResult(boolean isAreaTooLarge) {
    282         if (isAreaTooLarge) {
    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     }
    290 
    291257    /**
    292258     * Distributes a "bounding box changed" from one DownloadSelection
    293      * object to the others, so they may update or clear their input fields.
     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.
    294261     * @param b new current bounds
    295262     *
     
    303270            }
    304271        }
    305         updateSizeCheck();
     272
     273        for (Component ds : downloadSourcesTab.getComponents()) {
     274            if (ds instanceof AbstractDownloadSourcePanel) {
     275                ((AbstractDownloadSourcePanel) ds).boudingBoxChanged(b);
     276            }
     277        }
    306278    }
    307279
     
    312284    public void startDownload(Bounds b) {
    313285        this.currentBounds = b;
    314         actDownload.run();
    315     }
    316 
    317     /**
    318      * Replies true if the user selected to download OSM data
    319      *
    320      * @return true if the user selected to download OSM data
    321      */
    322     public boolean isDownloadOsmData() {
    323         return cbDownloadOsmData.isSelected();
    324     }
    325 
    326     /**
    327      * Replies true if the user selected to download GPX data
    328      *
    329      * @return true if the user selected to download GPX data
    330      */
    331     public boolean isDownloadGpxData() {
    332         return cbDownloadGpxData.isSelected();
    333     }
    334 
    335     /**
    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();
     286        startDownload();
     287    }
     288
     289    /**
     290     * Starts download.
     291     */
     292    public void startDownload() {
     293        btnDownload.doClick();
    342294    }
    343295
     
    362314
    363315    /**
     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    /**
    364324     * Adds a new download area selector to the download dialog
    365325     *
     
    372332
    373333    /**
     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    /**
    374346     * Refreshes the tile sources
    375347     * @since 6364
     
    386358    public void rememberSettings() {
    387359        DOWNLOAD_TAB.put(tpDownloadAreaSelectors.getSelectedIndex());
    388         DOWNLOAD_OSM.put(cbDownloadOsmData.isSelected());
    389         DOWNLOAD_GPS.put(cbDownloadGpxData.isSelected());
    390         DOWNLOAD_NOTES.put(cbDownloadNotes.isSelected());
     360        DOWNLOAD_SOURCE_TAB.put(downloadSourcesTab.getSelectedIndex());
     361        DIALOG_SPLIT.put(dialogSplit.getDividerLocation());
    391362        DOWNLOAD_NEWLAYER.put(cbNewLayer.isSelected());
    392363        DOWNLOAD_ZOOMTODATA.put(cbZoomToDownloadedData.isSelected());
     
    400371     */
    401372    public void restoreSettings() {
    402         cbDownloadOsmData.setSelected(DOWNLOAD_OSM.get());
    403         cbDownloadGpxData.setSelected(DOWNLOAD_GPS.get());
    404         cbDownloadNotes.setSelected(DOWNLOAD_NOTES.get());
    405373        cbNewLayer.setSelected(DOWNLOAD_NEWLAYER.get());
    406374        cbStartup.setSelected(isAutorunEnabled());
    407375        cbZoomToDownloadedData.setSelected(DOWNLOAD_ZOOMTODATA.get());
    408         int idx = Utils.clamp(DOWNLOAD_TAB.get(), 0, tpDownloadAreaSelectors.getTabCount() - 1);
    409         tpDownloadAreaSelectors.setSelectedIndex(idx);
     376        dialogSplit.setDividerLocation(DIALOG_SPLIT.get());
     377
     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        }
    410391
    411392        if (MainApplication.isDisplayingMapView()) {
     
    443424
    444425    /**
    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      */
    448     public static boolean isAutorunEnabled() {
    449         return DOWNLOAD_AUTORUN.get();
    450     }
    451 
    452     /**
    453426     * Automatically opens the download dialog, if autorun is enabled.
    454427     * @see #isAutorunEnabled
     
    494467    }
    495468
     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
    496478    protected void setCanceled(boolean canceled) {
    497479        this.canceled = canceled;
    498480    }
    499481
    500     protected void buildMainPanelAboveDownloadSelections(JPanel pnl) {
    501         // Do nothing
    502     }
    503 
     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);
     495    }
     496
     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     */
    504540    class CancelAction extends AbstractAction {
    505541        CancelAction() {
     
    516552        @Override
    517553        public void actionPerformed(ActionEvent e) {
     554            AbstractDownloadSourcePanel pnl = (AbstractDownloadSourcePanel) downloadSourcesTab.getSelectedComponent();
    518555            run();
    519         }
    520     }
    521 
     556            pnl.checkCancel();
     557        }
     558    }
     559
     560    /**
     561     * Action that is executed when the download button is pressed.
     562     */
    522563    class DownloadAction extends AbstractAction {
    523564        DownloadAction() {
     
    529570
    530571        public void run() {
    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              */
    536             if (!isDownloadOsmData() && !isDownloadGpxData() && !isDownloadNotes()) {
    537                 JOptionPane.showMessageDialog(
    538                         DownloadDialog.this,
    539                         tr("<html>Neither <strong>{0}</strong> nor <strong>{1}</strong> nor <strong>{2}</strong> is enabled.<br>"
    540                                         + "Please choose to either download OSM data, or GPX data, or Notes, or all.</html>",
    541                                 cbDownloadOsmData.getText(),
    542                                 cbDownloadGpxData.getText(),
    543                                 cbDownloadNotes.getText()
    544                         ),
    545                         tr("Error"),
    546                         JOptionPane.ERROR_MESSAGE
    547                 );
    548                 return;
     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                }
    549582            }
    550 
    551             setCanceled(false);
    552             setVisible(false);
    553583        }
    554584
  • trunk/src/org/openstreetmap/josm/gui/download/OverpassQueryWizardDialog.java

    r12620 r12652  
    2020
    2121import org.openstreetmap.josm.Main;
    22 import org.openstreetmap.josm.actions.OverpassDownloadAction.OverpassDownloadDialog;
    2322import org.openstreetmap.josm.data.preferences.CollectionProperty;
    2423import org.openstreetmap.josm.gui.ExtendedDialog;
     
    6463            + "</style>\n";
    6564
    66     private final OverpassDownloadDialog parentDialog;
     65    private final OverpassDownloadSource.OverpassDownloadSourcePanel dsPanel;
    6766
    6867    /**
    6968     * Create a new {@link OverpassQueryWizardDialog}
    70      * @param parentDialog The parent this dialog should be displayed for
    71      */
    72     public OverpassQueryWizardDialog(OverpassDownloadDialog parentDialog) {
    73         super(parentDialog, tr("Overpass Turbo Query Wizard"),
     69     * @param dsPanel The Overpass download source panel.
     70     */
     71    public OverpassQueryWizardDialog(OverpassDownloadSource.OverpassDownloadSourcePanel dsPanel) {
     72        super(dsPanel.getParent(), tr("Overpass Turbo Query Wizard"),
    7473                tr("Build query"), tr("Build query and execute"), tr("Cancel"));
    75         this.parentDialog = parentDialog;
     74        this.dsPanel = dsPanel;
    7675
    7776        this.queryWizard = new HistoryComboBox();
     
    9190        queryWizard.setPossibleItems(OVERPASS_WIZARD_HISTORY.get());
    9291
    93         setCancelButton(CANCEL);
    94         setDefaultButton(BUILD_AN_EXECUTE_QUERY + 1); // Build and execute button
     92        setCancelButton(CANCEL + 1);
     93        setDefaultButton(BUILD_AN_EXECUTE_QUERY + 1);
    9594        setContent(panel, false);
    9695    }
     
    110109                    super.buttonAction(BUILD_AN_EXECUTE_QUERY, evt);
    111110
    112                     parentDialog.triggerDownload();
     111                    DownloadDialog.getInstance().startDownload();
    113112                }
    114113                break;
     
    143142            Logging.error(ex);
    144143            JOptionPane.showMessageDialog(
    145                     parentDialog,
     144                    dsPanel.getParent(),
    146145                    "<html>" +
    147146                     tr("The Overpass wizard could not parse the following query:") +
     
    166165        if (q.isPresent()) {
    167166            String query = q.get();
    168             parentDialog.setOverpassQuery(query);
     167            dsPanel.setOverpassQuery(query);
    169168
    170169            return true;
Note: See TracChangeset for help on using the changeset viewer.