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