[7937] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.layer.gpx;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.Dimension;
|
---|
| 7 | import java.awt.GridBagLayout;
|
---|
| 8 |
|
---|
| 9 | import javax.swing.JCheckBox;
|
---|
| 10 | import javax.swing.JLabel;
|
---|
| 11 | import javax.swing.JList;
|
---|
| 12 | import javax.swing.JOptionPane;
|
---|
| 13 | import javax.swing.JPanel;
|
---|
| 14 | import javax.swing.JSpinner;
|
---|
| 15 | import javax.swing.SpinnerNumberModel;
|
---|
| 16 | import javax.swing.event.ChangeListener;
|
---|
| 17 |
|
---|
| 18 | import org.openstreetmap.josm.Main;
|
---|
| 19 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
| 20 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
| 21 | import org.openstreetmap.josm.tools.GBC;
|
---|
| 22 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * Panel displayed in "Download along..." dialogs
|
---|
| 26 | * @since 6054
|
---|
| 27 | */
|
---|
| 28 | public class DownloadAlongPanel extends JPanel {
|
---|
| 29 |
|
---|
| 30 | // Preferences keys
|
---|
| 31 | private final String prefOsm;
|
---|
| 32 | private final String prefGps;
|
---|
| 33 | private final String prefDist;
|
---|
| 34 | private final String prefArea;
|
---|
| 35 | private final String prefNear;
|
---|
| 36 |
|
---|
| 37 | // Data types to download
|
---|
| 38 | private final JCheckBox cbDownloadOsmData;
|
---|
| 39 | private final JCheckBox cbDownloadGpxData;
|
---|
| 40 |
|
---|
| 41 | private final JSpinner buffer;
|
---|
| 42 | private final JSpinner maxRect;
|
---|
| 43 | private final JList<String> downloadNear;
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
| 46 | * Constructs a new {@code DownloadPanel}.
|
---|
| 47 | * @param prefOsm Preference key determining if OSM data should be downloaded
|
---|
| 48 | * @param prefGps Preference key determining if GPS data should be downloaded
|
---|
| 49 | * @param prefDist Preference key determining maximum distance
|
---|
| 50 | * @param prefArea Preference key determining maximum area
|
---|
| 51 | * @param prefNear Preference key determining "near" parameter. Can be {@code null}
|
---|
| 52 | */
|
---|
| 53 | public DownloadAlongPanel(String prefOsm, String prefGps, String prefDist, String prefArea, String prefNear) {
|
---|
| 54 | super(new GridBagLayout());
|
---|
| 55 |
|
---|
| 56 | this.prefOsm = prefOsm;
|
---|
| 57 | this.prefGps = prefGps;
|
---|
| 58 | this.prefDist = prefDist;
|
---|
| 59 | this.prefArea = prefArea;
|
---|
| 60 | this.prefNear = prefNear;
|
---|
| 61 |
|
---|
| 62 | cbDownloadOsmData = new JCheckBox(tr("OpenStreetMap data"), Main.pref.getBoolean(prefOsm, true));
|
---|
| 63 | cbDownloadOsmData.setToolTipText(tr("Select to download OSM data."));
|
---|
[8510] | 64 | add(cbDownloadOsmData, GBC.std().insets(1, 5, 1, 5));
|
---|
[7937] | 65 | cbDownloadGpxData = new JCheckBox(tr("Raw GPS data"), Main.pref.getBoolean(prefGps, false));
|
---|
| 66 | cbDownloadGpxData.setToolTipText(tr("Select to download GPS traces."));
|
---|
[8510] | 67 | add(cbDownloadGpxData, GBC.eol().insets(5, 5, 1, 5));
|
---|
[7937] | 68 |
|
---|
| 69 | add(new JLabel(tr("Download everything within:")), GBC.std());
|
---|
| 70 | buffer = new JSpinner(new SpinnerNumberModel(50.0, 10.0, 5000.0, 1.0));
|
---|
[8510] | 71 | add(buffer, GBC.std().insets(5, 5, 5, 5));
|
---|
[7937] | 72 | add(new JLabel(tr("meters")), GBC.eol());
|
---|
| 73 |
|
---|
| 74 | add(new JLabel(tr("Maximum area per request:")), GBC.std());
|
---|
[8672] | 75 | maxRect = new JSpinner(new SpinnerNumberModel(20.0, 0.01, 25.0, 1.0)) {
|
---|
[7937] | 76 | @Override
|
---|
| 77 | public Dimension getPreferredSize() {
|
---|
| 78 | return buffer.getPreferredSize();
|
---|
| 79 | }
|
---|
| 80 | };
|
---|
[8510] | 81 | add(maxRect, GBC.std().insets(5, 5, 5, 5));
|
---|
[8672] | 82 | add(new JLabel("km\u00b2"), GBC.eol());
|
---|
[7937] | 83 |
|
---|
| 84 | if (prefNear != null) {
|
---|
| 85 | add(new JLabel(tr("Download near:")), GBC.eol());
|
---|
| 86 | downloadNear = new JList<>(new String[]{tr("track only"), tr("waypoints only"), tr("track and waypoints")});
|
---|
| 87 | downloadNear.setSelectedIndex(Main.pref.getInteger(prefNear, 0));
|
---|
| 88 | add(downloadNear, GBC.eol());
|
---|
| 89 | } else {
|
---|
| 90 | downloadNear = null;
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
| 95 | * Gets the maximum distance in meters
|
---|
| 96 | * @return The maximum distance, in meters
|
---|
| 97 | */
|
---|
| 98 | public final double getDistance() {
|
---|
| 99 | return (double) buffer.getValue();
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
| 103 | * Gets the maximum area in squared kilometers
|
---|
| 104 | * @return The maximum distance, in squared kilometers
|
---|
| 105 | */
|
---|
| 106 | public final double getArea() {
|
---|
| 107 | return (double) maxRect.getValue();
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /**
|
---|
| 111 | * Gets the "download near" choosen value
|
---|
| 112 | * @return the "download near" choosen value (0: track only, 1: waypoints only, 2: both)
|
---|
| 113 | */
|
---|
| 114 | public final int getNear() {
|
---|
| 115 | return downloadNear.getSelectedIndex();
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /**
|
---|
| 119 | * Replies true if the user selected to download OSM data
|
---|
| 120 | *
|
---|
| 121 | * @return true if the user selected to download OSM data
|
---|
| 122 | */
|
---|
| 123 | public boolean isDownloadOsmData() {
|
---|
| 124 | return cbDownloadOsmData.isSelected();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /**
|
---|
| 128 | * Replies true if the user selected to download GPX data
|
---|
| 129 | *
|
---|
| 130 | * @return true if the user selected to download GPX data
|
---|
| 131 | */
|
---|
| 132 | public boolean isDownloadGpxData() {
|
---|
| 133 | return cbDownloadGpxData.isSelected();
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * Remembers the current settings in the download panel
|
---|
| 138 | */
|
---|
| 139 | protected final void rememberSettings() {
|
---|
| 140 | Main.pref.put(prefOsm, isDownloadOsmData());
|
---|
| 141 | Main.pref.put(prefGps, isDownloadGpxData());
|
---|
| 142 | Main.pref.putDouble(prefDist, getDistance());
|
---|
| 143 | Main.pref.putDouble(prefArea, getArea());
|
---|
| 144 | if (prefNear != null) {
|
---|
| 145 | Main.pref.putInteger(prefNear, getNear());
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /**
|
---|
| 150 | * Adds a change listener to comboboxes
|
---|
| 151 | * @param listener The listener that will be notified of each combobox change
|
---|
| 152 | */
|
---|
| 153 | protected final void addChangeListener(ChangeListener listener) {
|
---|
| 154 | cbDownloadGpxData.addChangeListener(listener);
|
---|
| 155 | cbDownloadOsmData.addChangeListener(listener);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | /**
|
---|
| 159 | * Show this panel in a new "Download along" help-aware dialog
|
---|
| 160 | * @param title The dialog title
|
---|
| 161 | * @param helpTopic The dialog help topic
|
---|
| 162 | * @return The selected button index (0 for download, 1 for cancel, 2 for dialog closure)
|
---|
| 163 | */
|
---|
| 164 | public int showInDownloadDialog(String title, String helpTopic) {
|
---|
| 165 | final ButtonSpec[] options = new ButtonSpec[] {
|
---|
| 166 | new ButtonSpec(
|
---|
| 167 | tr("Download"),
|
---|
| 168 | ImageProvider.get("download"),
|
---|
| 169 | tr("Click to download"),
|
---|
| 170 | null // no specific help text
|
---|
| 171 | ),
|
---|
| 172 | new ButtonSpec(
|
---|
| 173 | tr("Cancel"),
|
---|
| 174 | ImageProvider.get("cancel"),
|
---|
| 175 | tr("Click to cancel"),
|
---|
| 176 | null // no specific help text
|
---|
| 177 | )
|
---|
| 178 | };
|
---|
| 179 |
|
---|
[10611] | 180 | addChangeListener(e -> options[0].setEnabled(isDownloadOsmData() || isDownloadGpxData()));
|
---|
[7937] | 181 |
|
---|
[9917] | 182 | int ret = HelpAwareOptionPane.showOptionDialog(Main.parent, this, title,
|
---|
[9804] | 183 | JOptionPane.QUESTION_MESSAGE, null, options, options[0], helpTopic);
|
---|
[7937] | 184 | if (0 == ret) {
|
---|
| 185 | rememberSettings();
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | return ret;
|
---|
| 189 | }
|
---|
| 190 | }
|
---|