source: josm/trunk/src/org/openstreetmap/josm/gui/layer/gpx/DownloadAlongPanel.java@ 6070

Last change on this file since 6070 was 6070, checked in by stoecker, 11 years ago

see #8853 remove tabs, trailing spaces, windows line ends, strange characters

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