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

Last change on this file since 12167 was 10611, checked in by Don-vip, 8 years ago

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

  • Property svn:eol-style set to native
File size: 6.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.Dimension;
7import java.awt.GridBagLayout;
8
9import javax.swing.JCheckBox;
10import javax.swing.JLabel;
11import javax.swing.JList;
12import javax.swing.JOptionPane;
13import javax.swing.JPanel;
14import javax.swing.JSpinner;
15import javax.swing.SpinnerNumberModel;
16import javax.swing.event.ChangeListener;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.gui.HelpAwareOptionPane;
20import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
21import org.openstreetmap.josm.tools.GBC;
22import org.openstreetmap.josm.tools.ImageProvider;
23
24/**
25 * Panel displayed in "Download along..." dialogs
26 * @since 6054
27 */
28public 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."));
64 add(cbDownloadOsmData, GBC.std().insets(1, 5, 1, 5));
65 cbDownloadGpxData = new JCheckBox(tr("Raw GPS data"), Main.pref.getBoolean(prefGps, false));
66 cbDownloadGpxData.setToolTipText(tr("Select to download GPS traces."));
67 add(cbDownloadGpxData, GBC.eol().insets(5, 5, 1, 5));
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));
71 add(buffer, GBC.std().insets(5, 5, 5, 5));
72 add(new JLabel(tr("meters")), GBC.eol());
73
74 add(new JLabel(tr("Maximum area per request:")), GBC.std());
75 maxRect = new JSpinner(new SpinnerNumberModel(20.0, 0.01, 25.0, 1.0)) {
76 @Override
77 public Dimension getPreferredSize() {
78 return buffer.getPreferredSize();
79 }
80 };
81 add(maxRect, GBC.std().insets(5, 5, 5, 5));
82 add(new JLabel("km\u00b2"), GBC.eol());
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
180 addChangeListener(e -> options[0].setEnabled(isDownloadOsmData() || isDownloadGpxData()));
181
182 int ret = HelpAwareOptionPane.showOptionDialog(Main.parent, this, title,
183 JOptionPane.QUESTION_MESSAGE, null, options, options[0], helpTopic);
184 if (0 == ret) {
185 rememberSettings();
186 }
187
188 return ret;
189 }
190}
Note: See TracBrowser for help on using the repository browser.