source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/display/GPXSettingsPanel.java @ 12620

Last change on this file since 12620 was 12620, checked in by Don-vip, 2 months ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 31.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.display;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trc;
6
7import java.awt.Color;
8import java.awt.Component;
9import java.awt.Dimension;
10import java.awt.GridBagLayout;
11import java.awt.event.ActionListener;
12import java.util.Enumeration;
13
14import javax.swing.AbstractButton;
15import javax.swing.BorderFactory;
16import javax.swing.Box;
17import javax.swing.ButtonGroup;
18import javax.swing.JCheckBox;
19import javax.swing.JLabel;
20import javax.swing.JOptionPane;
21import javax.swing.JPanel;
22import javax.swing.JRadioButton;
23import javax.swing.JSlider;
24
25import org.openstreetmap.josm.Main;
26import org.openstreetmap.josm.actions.ExpertToggleAction;
27import org.openstreetmap.josm.gui.layer.gpx.GpxDrawHelper;
28import org.openstreetmap.josm.gui.layer.markerlayer.Marker;
29import org.openstreetmap.josm.gui.layer.markerlayer.Marker.TemplateEntryProperty;
30import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane.ValidationListener;
31import org.openstreetmap.josm.gui.widgets.JosmComboBox;
32import org.openstreetmap.josm.gui.widgets.JosmTextField;
33import org.openstreetmap.josm.tools.GBC;
34import org.openstreetmap.josm.tools.Logging;
35import org.openstreetmap.josm.tools.template_engine.ParseError;
36import org.openstreetmap.josm.tools.template_engine.TemplateParser;
37
38/**
39 * Panel for GPX settings.
40 */
41public class GPXSettingsPanel extends JPanel implements ValidationListener {
42
43    private static final int WAYPOINT_LABEL_CUSTOM = 6;
44    private static final String[] LABEL_PATTERN_TEMPLATE = new String[] {Marker.LABEL_PATTERN_AUTO, Marker.LABEL_PATTERN_NAME,
45        Marker.LABEL_PATTERN_DESC, "{special:everything}", "?{ '{name}' | '{desc}' | '{formattedWaypointOffset}' }", " "};
46    private static final String[] LABEL_PATTERN_DESC = new String[] {tr("Auto"), /* gpx data field name */ trc("gpx_field", "Name"),
47        /* gpx data field name */ trc("gpx_field", "Desc(ription)"), tr("Everything"), tr("Name or offset"), tr("None"), tr("Custom")};
48
49
50    private final JRadioButton drawRawGpsLinesGlobal = new JRadioButton(tr("Use global settings"));
51    private final JRadioButton drawRawGpsLinesAll = new JRadioButton(tr("All"));
52    private final JRadioButton drawRawGpsLinesLocal = new JRadioButton(tr("Local files"));
53    private final JRadioButton drawRawGpsLinesNone = new JRadioButton(tr("None"));
54    private transient ActionListener drawRawGpsLinesActionListener;
55    private final JosmTextField drawRawGpsMaxLineLength = new JosmTextField(8);
56    private final JosmTextField drawRawGpsMaxLineLengthLocal = new JosmTextField(8);
57    private final JosmTextField drawLineWidth = new JosmTextField(2);
58    private final JCheckBox forceRawGpsLines = new JCheckBox(tr("Force lines if no segments imported"));
59    private final JCheckBox largeGpsPoints = new JCheckBox(tr("Draw large GPS points"));
60    private final JCheckBox hdopCircleGpsPoints = new JCheckBox(tr("Draw a circle from HDOP value"));
61    private final JRadioButton colorTypeVelocity = new JRadioButton(tr("Velocity (red = slow, green = fast)"));
62    private final JRadioButton colorTypeDirection = new JRadioButton(tr("Direction (red = west, yellow = north, green = east, blue = south)"));
63    private final JRadioButton colorTypeDilution = new JRadioButton(tr("Dilution of Position (red = high, green = low, if available)"));
64    private final JRadioButton colorTypeTime = new JRadioButton(tr("Track date"));
65    private final JRadioButton colorTypeHeatMap = new JRadioButton(tr("Heat Map (dark = few, bright = many)"));
66    private final JRadioButton colorTypeNone = new JRadioButton(tr("Single Color (can be customized for named layers)"));
67    private final JRadioButton colorTypeGlobal = new JRadioButton(tr("Use global settings"));
68    private final JosmComboBox<String> colorTypeVelocityTune = new JosmComboBox<>(new String[] {tr("Car"), tr("Bicycle"), tr("Foot")});
69    private final JosmComboBox<String> colorTypeHeatMapTune = new JosmComboBox<>(new String[] {
70        trc("Heat map", "User Normal"),
71        trc("Heat map", "User Light"),
72        trc("Heat map", "Traffic Lights"),
73        trc("Heat map", "Inferno"),
74        trc("Heat map", "Viridis"),
75        trc("Heat map", "Wood"),
76        trc("Heat map", "Heat")});
77    private final JCheckBox colorTypeHeatMapPoints = new JCheckBox(tr("Use points instead of lines for heat map"));
78    private final JSlider colorTypeHeatMapGain = new JSlider();
79    private final JSlider colorTypeHeatMapLowerLimit = new JSlider();
80    private final JCheckBox makeAutoMarkers = new JCheckBox(tr("Create markers when reading GPX"));
81    private final JCheckBox drawGpsArrows = new JCheckBox(tr("Draw Direction Arrows"));
82    private final JCheckBox drawGpsArrowsFast = new JCheckBox(tr("Fast drawing (looks uglier)"));
83    private final JosmTextField drawGpsArrowsMinDist = new JosmTextField(8);
84    private final JCheckBox colorDynamic = new JCheckBox(tr("Dynamic color range based on data limits"));
85    private final JosmComboBox<String> waypointLabel = new JosmComboBox<>(LABEL_PATTERN_DESC);
86    private final JosmTextField waypointLabelPattern = new JosmTextField();
87    private final JosmComboBox<String> audioWaypointLabel = new JosmComboBox<>(LABEL_PATTERN_DESC);
88    private final JosmTextField audioWaypointLabelPattern = new JosmTextField();
89    private final JCheckBox useGpsAntialiasing = new JCheckBox(tr("Smooth GPX graphics (antialiasing)"));
90    private final JCheckBox drawLineWithAlpha = new JCheckBox(tr("Draw with Opacity (alpha blending) "));
91
92    private String layerName;
93    private final boolean local; // flag to display LocalOnly checkbox
94    private final boolean nonlocal; // flag to display AllLines checkbox
95
96    /**
97     * Constructs a new {@code GPXSettingsPanel} for a given layer name.
98     * @param layerName The GPX layer name
99     * @param local flag to display LocalOnly checkbox
100     * @param nonlocal flag to display AllLines checkbox
101     */
102    public GPXSettingsPanel(String layerName, boolean local, boolean nonlocal) {
103        super(new GridBagLayout());
104        this.local = local;
105        this.nonlocal = nonlocal;
106        this.layerName = "layer "+layerName;
107        initComponents();
108        loadPreferences();
109    }
110
111    /**
112     * Constructs a new {@code GPXSettingsPanel}.
113     */
114    public GPXSettingsPanel() {
115        super(new GridBagLayout());
116        initComponents();
117        local = false;
118        nonlocal = false;
119        loadPreferences(); // preferences -> controls
120    }
121
122    // CHECKSTYLE.OFF: ExecutableStatementCountCheck
123    private void initComponents() {
124        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
125
126        // makeAutoMarkers
127        makeAutoMarkers.setToolTipText(tr("Automatically make a marker layer from any waypoints when opening a GPX layer."));
128        ExpertToggleAction.addVisibilitySwitcher(makeAutoMarkers);
129        add(makeAutoMarkers, GBC.eol().insets(20, 0, 0, 5));
130
131        // drawRawGpsLines
132        ButtonGroup gpsLinesGroup = new ButtonGroup();
133        if (layerName != null) {
134            gpsLinesGroup.add(drawRawGpsLinesGlobal);
135        }
136        gpsLinesGroup.add(drawRawGpsLinesNone);
137        gpsLinesGroup.add(drawRawGpsLinesLocal);
138        gpsLinesGroup.add(drawRawGpsLinesAll);
139
140        /* ensure that default is in data base */
141
142        JLabel label = new JLabel(tr("Draw lines between raw GPS points"));
143        add(label, GBC.eol().insets(20, 0, 0, 0));
144        if (layerName != null) {
145            add(drawRawGpsLinesGlobal, GBC.eol().insets(40, 0, 0, 0));
146        }
147        add(drawRawGpsLinesNone, GBC.eol().insets(40, 0, 0, 0));
148        if (layerName == null || local) {
149            add(drawRawGpsLinesLocal, GBC.eol().insets(40, 0, 0, 0));
150        }
151        if (layerName == null || nonlocal) {
152            add(drawRawGpsLinesAll, GBC.eol().insets(40, 0, 0, 0));
153        }
154        ExpertToggleAction.addVisibilitySwitcher(label);
155        ExpertToggleAction.addVisibilitySwitcher(drawRawGpsLinesGlobal);
156        ExpertToggleAction.addVisibilitySwitcher(drawRawGpsLinesNone);
157        ExpertToggleAction.addVisibilitySwitcher(drawRawGpsLinesLocal);
158        ExpertToggleAction.addVisibilitySwitcher(drawRawGpsLinesAll);
159
160        drawRawGpsLinesActionListener = e -> {
161            boolean f = drawRawGpsLinesNone.isSelected() || drawRawGpsLinesGlobal.isSelected();
162            forceRawGpsLines.setEnabled(!f);
163            drawRawGpsMaxLineLength.setEnabled(!(f || drawRawGpsLinesLocal.isSelected()));
164            drawRawGpsMaxLineLengthLocal.setEnabled(!f);
165            drawGpsArrows.setEnabled(!f);
166            drawGpsArrowsFast.setEnabled(drawGpsArrows.isSelected() && drawGpsArrows.isEnabled());
167            drawGpsArrowsMinDist.setEnabled(drawGpsArrows.isSelected() && drawGpsArrows.isEnabled());
168        };
169
170        drawRawGpsLinesGlobal.addActionListener(drawRawGpsLinesActionListener);
171        drawRawGpsLinesNone.addActionListener(drawRawGpsLinesActionListener);
172        drawRawGpsLinesLocal.addActionListener(drawRawGpsLinesActionListener);
173        drawRawGpsLinesAll.addActionListener(drawRawGpsLinesActionListener);
174
175        // drawRawGpsMaxLineLengthLocal
176        drawRawGpsMaxLineLengthLocal.setToolTipText(
177                tr("Maximum length (in meters) to draw lines for local files. Set to ''-1'' to draw all lines."));
178        label = new JLabel(tr("Maximum length for local files (meters)"));
179        add(label, GBC.std().insets(40, 0, 0, 0));
180        add(drawRawGpsMaxLineLengthLocal, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5));
181        ExpertToggleAction.addVisibilitySwitcher(label);
182        ExpertToggleAction.addVisibilitySwitcher(drawRawGpsMaxLineLengthLocal);
183
184        // drawRawGpsMaxLineLength
185        drawRawGpsMaxLineLength.setToolTipText(tr("Maximum length (in meters) to draw lines. Set to ''-1'' to draw all lines."));
186        label = new JLabel(tr("Maximum length (meters)"));
187        add(label, GBC.std().insets(40, 0, 0, 0));
188        add(drawRawGpsMaxLineLength, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5));
189        ExpertToggleAction.addVisibilitySwitcher(label);
190        ExpertToggleAction.addVisibilitySwitcher(drawRawGpsMaxLineLength);
191
192        // forceRawGpsLines
193        forceRawGpsLines.setToolTipText(tr("Force drawing of lines if the imported data contain no line information."));
194        add(forceRawGpsLines, GBC.eop().insets(40, 0, 0, 0));
195        ExpertToggleAction.addVisibilitySwitcher(forceRawGpsLines);
196
197        // drawGpsArrows
198        drawGpsArrows.addActionListener(e -> {
199            drawGpsArrowsFast.setEnabled(drawGpsArrows.isSelected() && drawGpsArrows.isEnabled());
200            drawGpsArrowsMinDist.setEnabled(drawGpsArrows.isSelected() && drawGpsArrows.isEnabled());
201        });
202        drawGpsArrows.setToolTipText(tr("Draw direction arrows for lines, connecting GPS points."));
203        add(drawGpsArrows, GBC.eop().insets(20, 0, 0, 0));
204
205        // drawGpsArrowsFast
206        drawGpsArrowsFast.setToolTipText(tr("Draw the direction arrows using table lookups instead of complex math."));
207        add(drawGpsArrowsFast, GBC.eop().insets(40, 0, 0, 0));
208        ExpertToggleAction.addVisibilitySwitcher(drawGpsArrowsFast);
209
210        // drawGpsArrowsMinDist
211        drawGpsArrowsMinDist.setToolTipText(tr("Do not draw arrows if they are not at least this distance away from the last one."));
212        add(new JLabel(tr("Minimum distance (pixels)")), GBC.std().insets(40, 0, 0, 0));
213        add(drawGpsArrowsMinDist, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5));
214
215        // hdopCircleGpsPoints
216        hdopCircleGpsPoints.setToolTipText(tr("Draw a circle from HDOP value."));
217        add(hdopCircleGpsPoints, GBC.eop().insets(20, 0, 0, 0));
218        ExpertToggleAction.addVisibilitySwitcher(hdopCircleGpsPoints);
219
220        // largeGpsPoints
221        largeGpsPoints.setToolTipText(tr("Draw larger dots for the GPS points."));
222        add(largeGpsPoints, GBC.eop().insets(20, 0, 0, 0));
223
224        // drawLineWidth
225        drawLineWidth.setToolTipText(tr("Width of drawn GPX line (0 for default)"));
226        add(new JLabel(tr("Drawing width of GPX lines")), GBC.std().insets(20, 0, 0, 0));
227        add(drawLineWidth, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5));
228
229        // antialiasing
230        useGpsAntialiasing.setToolTipText(tr("Apply antialiasing to the GPX lines resulting in a smoother appearance."));
231        add(useGpsAntialiasing, GBC.eop().insets(20, 0, 0, 0));
232        ExpertToggleAction.addVisibilitySwitcher(useGpsAntialiasing);
233
234        // alpha blending
235        drawLineWithAlpha.setToolTipText(tr("Apply dynamic alpha-blending and adjust width based on zoom level for all GPX lines."));
236        add(drawLineWithAlpha, GBC.eop().insets(20, 0, 0, 0));
237        ExpertToggleAction.addVisibilitySwitcher(drawLineWithAlpha);
238
239        // colorTracks
240        ButtonGroup colorGroup = new ButtonGroup();
241        if (layerName != null) {
242            colorGroup.add(colorTypeGlobal);
243        }
244        colorGroup.add(colorTypeNone);
245        colorGroup.add(colorTypeVelocity);
246        colorGroup.add(colorTypeDirection);
247        colorGroup.add(colorTypeDilution);
248        colorGroup.add(colorTypeTime);
249        colorGroup.add(colorTypeHeatMap);
250
251        colorTypeNone.setToolTipText(tr("All points and track segments will have the same color. Can be customized in Layer Manager."));
252        colorTypeVelocity.setToolTipText(tr("Colors points and track segments by velocity."));
253        colorTypeDirection.setToolTipText(tr("Colors points and track segments by direction."));
254        colorTypeDilution.setToolTipText(
255                tr("Colors points and track segments by dilution of position (HDOP). Your capture device needs to log that information."));
256        colorTypeTime.setToolTipText(tr("Colors points and track segments by its timestamp."));
257        colorTypeHeatMap.setToolTipText(tr("Collected points and track segments for a position and displayed as heat map."));
258
259        // color Tracks by Velocity Tune
260        colorTypeVelocityTune.setToolTipText(tr("Allows to tune the track coloring for different average speeds."));
261
262        colorTypeHeatMapTune.setToolTipText(tr("Selects the color schema for heat map."));
263        JLabel colorTypeHeatIconLabel = new JLabel();
264
265        add(Box.createVerticalGlue(), GBC.eol().insets(0, 20, 0, 0));
266
267        add(new JLabel(tr("Track and Point Coloring")), GBC.eol().insets(20, 0, 0, 0));
268        if (layerName != null) {
269            add(colorTypeGlobal, GBC.eol().insets(40, 0, 0, 0));
270        }
271        add(colorTypeNone, GBC.eol().insets(40, 0, 0, 0));
272        add(colorTypeVelocity, GBC.std().insets(40, 0, 0, 0));
273        add(colorTypeVelocityTune, GBC.eop().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5));
274        add(colorTypeDirection, GBC.eol().insets(40, 0, 0, 0));
275        add(colorTypeDilution, GBC.eol().insets(40, 0, 0, 0));
276        add(colorTypeTime, GBC.eol().insets(40, 0, 0, 0));
277        add(colorTypeHeatMap, GBC.std().insets(40, 0, 0, 0));
278        add(colorTypeHeatIconLabel, GBC.std().insets(5, 0, 0, 5));
279        add(colorTypeHeatMapTune, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5));
280
281        JLabel colorTypeHeatMapGainLabel = new JLabel(tr("Overlay gain adjustment"));
282        JLabel colorTypeHeatMapLowerLimitLabel = new JLabel(tr("Lower limit of visibility"));
283        add(colorTypeHeatMapGainLabel, GBC.std().insets(80, 0, 0, 0));
284        add(colorTypeHeatMapGain, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5));
285        add(colorTypeHeatMapLowerLimitLabel, GBC.std().insets(80, 0, 0, 0));
286        add(colorTypeHeatMapLowerLimit, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5));
287        add(colorTypeHeatMapPoints, GBC.eol().insets(60, 0, 0, 0));
288
289        colorTypeHeatMapGain.setToolTipText(tr("Adjust the gain of overlay blending."));
290        colorTypeHeatMapGain.setOrientation(JSlider.HORIZONTAL);
291        colorTypeHeatMapGain.setPaintLabels(true);
292        colorTypeHeatMapGain.setMinimum(-10);
293        colorTypeHeatMapGain.setMaximum(+10);
294        colorTypeHeatMapGain.setMinorTickSpacing(1);
295        colorTypeHeatMapGain.setMajorTickSpacing(5);
296
297        colorTypeHeatMapLowerLimit.setToolTipText(tr("Draw all GPX traces that exceed this threshold."));
298        colorTypeHeatMapLowerLimit.setOrientation(JSlider.HORIZONTAL);
299        colorTypeHeatMapLowerLimit.setMinimum(0);
300        colorTypeHeatMapLowerLimit.setMaximum(254);
301        colorTypeHeatMapLowerLimit.setPaintLabels(true);
302        colorTypeHeatMapLowerLimit.setMinorTickSpacing(10);
303        colorTypeHeatMapLowerLimit.setMajorTickSpacing(100);
304
305        colorTypeHeatMapPoints.setToolTipText(tr("Render engine uses points with simulated position error instead of lines. "));
306
307        // iterate over the buttons, add change listener to any change event
308        for (Enumeration<AbstractButton> button = colorGroup.getElements(); button.hasMoreElements();) {
309            (button.nextElement()).addChangeListener(e -> {
310                colorTypeVelocityTune.setEnabled(colorTypeVelocity.isSelected());
311                colorTypeHeatMapTune.setEnabled(colorTypeHeatMap.isSelected());
312                colorTypeHeatMapPoints.setEnabled(colorTypeHeatMap.isSelected());
313                colorTypeHeatMapGain.setEnabled(colorTypeHeatMap.isSelected());
314                colorTypeHeatMapLowerLimit.setEnabled(colorTypeHeatMap.isSelected());
315                colorTypeHeatMapGainLabel.setEnabled(colorTypeHeatMap.isSelected());
316                colorTypeHeatMapLowerLimitLabel.setEnabled(colorTypeHeatMap.isSelected());
317                colorDynamic.setEnabled(colorTypeVelocity.isSelected() || colorTypeDilution.isSelected());
318            });
319        }
320
321        colorTypeHeatMapTune.addActionListener(e -> {
322            final Dimension dim = colorTypeHeatMapTune.getPreferredSize();
323            if (null != dim) {
324                // get image size of environment
325                final int iconSize = (int) dim.getHeight();
326                final Color color;
327                // ask the GPX draw for the correct color of that layer ( if there is one )
328                if (null != layerName) {
329                    color = GpxDrawHelper.DEFAULT_COLOR.getChildColor(layerName).get();
330                } else {
331                    color = GpxDrawHelper.DEFAULT_COLOR.getDefaultValue();
332                }
333                colorTypeHeatIconLabel.setIcon(GpxDrawHelper.getColorMapImageIcon(color, colorTypeHeatMapTune.getSelectedIndex(), iconSize));
334            }
335        });
336
337        ExpertToggleAction.addVisibilitySwitcher(colorTypeDirection);
338        ExpertToggleAction.addVisibilitySwitcher(colorTypeDilution);
339        ExpertToggleAction.addVisibilitySwitcher(colorTypeHeatMapLowerLimit);
340        ExpertToggleAction.addVisibilitySwitcher(colorTypeHeatMapLowerLimitLabel);
341
342        colorDynamic.setToolTipText(tr("Colors points and track segments by data limits."));
343        add(colorDynamic, GBC.eop().insets(40, 0, 0, 0));
344        ExpertToggleAction.addVisibilitySwitcher(colorDynamic);
345
346        if (layerName == null) {
347            // Setting waypoints for gpx layer doesn't make sense - waypoints are shown in marker layer that has different name - so show
348            // this only for global config
349
350            // waypointLabel
351            label = new JLabel(tr("Waypoint labelling"));
352            add(label, GBC.std().insets(20, 0, 0, 0));
353            label.setLabelFor(waypointLabel);
354            add(waypointLabel, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5));
355            waypointLabel.addActionListener(e -> updateWaypointPattern(waypointLabel, waypointLabelPattern));
356            updateWaypointLabelCombobox(waypointLabel, waypointLabelPattern, TemplateEntryProperty.forMarker(layerName));
357            add(waypointLabelPattern, GBC.eol().fill(GBC.HORIZONTAL).insets(20, 0, 0, 5));
358            ExpertToggleAction.addVisibilitySwitcher(label);
359            ExpertToggleAction.addVisibilitySwitcher(waypointLabel);
360            ExpertToggleAction.addVisibilitySwitcher(waypointLabelPattern);
361
362            // audioWaypointLabel
363            Component glue = Box.createVerticalGlue();
364            add(glue, GBC.eol().insets(0, 20, 0, 0));
365            ExpertToggleAction.addVisibilitySwitcher(glue);
366
367            label = new JLabel(tr("Audio waypoint labelling"));
368            add(label, GBC.std().insets(20, 0, 0, 0));
369            label.setLabelFor(audioWaypointLabel);
370            add(audioWaypointLabel, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5));
371            audioWaypointLabel.addActionListener(e -> updateWaypointPattern(audioWaypointLabel, audioWaypointLabelPattern));
372            updateWaypointLabelCombobox(audioWaypointLabel, audioWaypointLabelPattern, TemplateEntryProperty.forAudioMarker(layerName));
373            add(audioWaypointLabelPattern, GBC.eol().fill(GBC.HORIZONTAL).insets(20, 0, 0, 5));
374            ExpertToggleAction.addVisibilitySwitcher(label);
375            ExpertToggleAction.addVisibilitySwitcher(audioWaypointLabel);
376            ExpertToggleAction.addVisibilitySwitcher(audioWaypointLabelPattern);
377        }
378
379        add(Box.createVerticalGlue(), GBC.eol().fill(GBC.BOTH));
380    }
381    // CHECKSTYLE.ON: ExecutableStatementCountCheck
382
383    /**
384     * Loads preferences to UI controls
385     */
386    public final void loadPreferences() {
387        makeAutoMarkers.setSelected(Main.pref.getBoolean("marker.makeautomarkers", true));
388        if (layerName != null && Main.pref.get("draw.rawgps.lines."+layerName).isEmpty()
389                && Main.pref.get("draw.rawgps.lines.local."+layerName).isEmpty()) {
390            // no line preferences for layer is found
391            drawRawGpsLinesGlobal.setSelected(true);
392        } else {
393            Boolean lf = Main.pref.getBoolean("draw.rawgps.lines.local", layerName, true);
394            if (Main.pref.getBoolean("draw.rawgps.lines", layerName, true)) {
395                drawRawGpsLinesAll.setSelected(true);
396            } else if (lf) {
397                drawRawGpsLinesLocal.setSelected(true);
398            } else {
399                drawRawGpsLinesNone.setSelected(true);
400            }
401        }
402
403        drawRawGpsMaxLineLengthLocal.setText(Integer.toString(Main.pref.getInteger("draw.rawgps.max-line-length.local", layerName, -1)));
404        drawRawGpsMaxLineLength.setText(Integer.toString(Main.pref.getInteger("draw.rawgps.max-line-length", layerName, 200)));
405        drawLineWidth.setText(Integer.toString(Main.pref.getInteger("draw.rawgps.linewidth", layerName, 0)));
406        drawLineWithAlpha.setSelected(Main.pref.getBoolean("draw.rawgps.lines.alpha-blend", layerName, false));
407        forceRawGpsLines.setSelected(Main.pref.getBoolean("draw.rawgps.lines.force", layerName, false));
408        drawGpsArrows.setSelected(Main.pref.getBoolean("draw.rawgps.direction", layerName, false));
409        drawGpsArrowsFast.setSelected(Main.pref.getBoolean("draw.rawgps.alternatedirection", layerName, false));
410        drawGpsArrowsMinDist.setText(Integer.toString(Main.pref.getInteger("draw.rawgps.min-arrow-distance", layerName, 40)));
411        hdopCircleGpsPoints.setSelected(Main.pref.getBoolean("draw.rawgps.hdopcircle", layerName, false));
412        largeGpsPoints.setSelected(Main.pref.getBoolean("draw.rawgps.large", layerName, false));
413        useGpsAntialiasing.setSelected(Main.pref.getBoolean("mappaint.gpx.use-antialiasing", false));
414
415        drawRawGpsLinesActionListener.actionPerformed(null);
416
417        if (layerName != null && Main.pref.get("draw.rawgps.colors."+layerName).isEmpty()) {
418            colorTypeGlobal.setSelected(true);
419            colorDynamic.setSelected(false);
420            colorDynamic.setEnabled(false);
421            colorTypeHeatMapPoints.setSelected(false);
422            colorTypeHeatMapGain.setValue(0);
423            colorTypeHeatMapLowerLimit.setValue(0);
424        } else {
425            int colorType = Main.pref.getInteger("draw.rawgps.colors", layerName, 0);
426            switch (colorType) {
427            case 0: colorTypeNone.setSelected(true); break;
428            case 1: colorTypeVelocity.setSelected(true); break;
429            case 2: colorTypeDilution.setSelected(true); break;
430            case 3: colorTypeDirection.setSelected(true); break;
431            case 4: colorTypeTime.setSelected(true); break;
432            case 5: colorTypeHeatMap.setSelected(true); break;
433            default: Logging.warn("Unknown color type: " + colorType);
434            }
435            int ccts = Main.pref.getInteger("draw.rawgps.colorTracksTune", layerName, 45);
436            colorTypeVelocityTune.setSelectedIndex(ccts == 10 ? 2 : (ccts == 20 ? 1 : 0));
437            colorTypeHeatMapTune.setSelectedIndex(Main.pref.getInteger("draw.rawgps.heatmap.colormap", layerName, 0));
438            colorDynamic.setSelected(Main.pref.getBoolean("draw.rawgps.colors.dynamic", layerName, false));
439            colorTypeHeatMapPoints.setSelected(Main.pref.getBoolean("draw.rawgps.heatmap.use-points", layerName, false));
440            colorTypeHeatMapGain.setValue(Main.pref.getInteger("draw.rawgps.heatmap.gain", layerName, 0));
441            colorTypeHeatMapLowerLimit.setValue(Main.pref.getInteger("draw.rawgps.heatmap.lower-limit", layerName, 0));
442        }
443    }
444
445    /**
446     * Save preferences from UI controls, globally or for a specified layer.
447     * @param layerName The GPX layer name. Can be {@code null}, in that case, global preferences are written
448     * @param locLayer {@code true} if the GPX layer is a local one. Ignored if {@code layerName} is null
449     * @return {@code true} when restart is required, {@code false} otherwise
450     */
451    public boolean savePreferences(String layerName, boolean locLayer) {
452        String layerNameDot = ".layer "+layerName;
453        if (layerName == null) {
454            layerNameDot = "";
455        }
456        Main.pref.put("marker.makeautomarkers"+layerNameDot, makeAutoMarkers.isSelected());
457        if (drawRawGpsLinesGlobal.isSelected()) {
458            Main.pref.put("draw.rawgps.lines" + layerNameDot, null);
459            Main.pref.put("draw.rawgps.max-line-length" + layerNameDot, null);
460            Main.pref.put("draw.rawgps.lines.local" + layerNameDot, null);
461            Main.pref.put("draw.rawgps.max-line-length.local" + layerNameDot, null);
462            Main.pref.put("draw.rawgps.lines.force"+layerNameDot, null);
463            Main.pref.put("draw.rawgps.direction"+layerNameDot, null);
464            Main.pref.put("draw.rawgps.alternatedirection"+layerNameDot, null);
465            Main.pref.put("draw.rawgps.min-arrow-distance"+layerNameDot, null);
466        } else {
467            if (layerName == null || !locLayer) {
468                Main.pref.put("draw.rawgps.lines" + layerNameDot, drawRawGpsLinesAll.isSelected());
469                Main.pref.put("draw.rawgps.max-line-length" + layerNameDot, drawRawGpsMaxLineLength.getText());
470            }
471            if (layerName == null || locLayer) {
472                Main.pref.put("draw.rawgps.lines.local" + layerNameDot, drawRawGpsLinesAll.isSelected() || drawRawGpsLinesLocal.isSelected());
473                Main.pref.put("draw.rawgps.max-line-length.local" + layerNameDot, drawRawGpsMaxLineLengthLocal.getText());
474            }
475            Main.pref.put("draw.rawgps.lines.force"+layerNameDot, forceRawGpsLines.isSelected());
476            Main.pref.put("draw.rawgps.direction"+layerNameDot, drawGpsArrows.isSelected());
477            Main.pref.put("draw.rawgps.alternatedirection"+layerNameDot, drawGpsArrowsFast.isSelected());
478            Main.pref.put("draw.rawgps.min-arrow-distance"+layerNameDot, drawGpsArrowsMinDist.getText());
479        }
480
481        Main.pref.put("draw.rawgps.hdopcircle"+layerNameDot, hdopCircleGpsPoints.isSelected());
482        Main.pref.put("draw.rawgps.large"+layerNameDot, largeGpsPoints.isSelected());
483        Main.pref.put("draw.rawgps.linewidth"+layerNameDot, drawLineWidth.getText());
484        Main.pref.put("draw.rawgps.lines.alpha-blend"+layerNameDot, drawLineWithAlpha.isSelected());
485
486        Main.pref.put("mappaint.gpx.use-antialiasing", useGpsAntialiasing.isSelected());
487
488        TemplateEntryProperty.forMarker(layerName).put(waypointLabelPattern.getText());
489        TemplateEntryProperty.forAudioMarker(layerName).put(audioWaypointLabelPattern.getText());
490
491        if (colorTypeGlobal.isSelected()) {
492            Main.pref.put("draw.rawgps.colors"+layerNameDot, null);
493            Main.pref.put("draw.rawgps.colors.dynamic"+layerNameDot, null);
494            Main.pref.put("draw.rawgps.colorTracksTunec"+layerNameDot, null);
495            return false;
496        } else if (colorTypeVelocity.isSelected()) {
497            Main.pref.putInteger("draw.rawgps.colors"+layerNameDot, 1);
498        } else if (colorTypeDilution.isSelected()) {
499            Main.pref.putInteger("draw.rawgps.colors"+layerNameDot, 2);
500        } else if (colorTypeDirection.isSelected()) {
501            Main.pref.putInteger("draw.rawgps.colors"+layerNameDot, 3);
502        } else if (colorTypeTime.isSelected()) {
503            Main.pref.putInteger("draw.rawgps.colors"+layerNameDot, 4);
504        } else if (colorTypeHeatMap.isSelected()) {
505            Main.pref.putInteger("draw.rawgps.colors"+layerNameDot, 5);
506        } else {
507            Main.pref.putInteger("draw.rawgps.colors"+layerNameDot, 0);
508        }
509        Main.pref.put("draw.rawgps.colors.dynamic"+layerNameDot, colorDynamic.isSelected());
510        int ccti = colorTypeVelocityTune.getSelectedIndex();
511        Main.pref.putInteger("draw.rawgps.colorTracksTune"+layerNameDot, ccti == 2 ? 10 : (ccti == 1 ? 20 : 45));
512        Main.pref.putInteger("draw.rawgps.heatmap.colormap"+layerNameDot, colorTypeHeatMapTune.getSelectedIndex());
513        Main.pref.put("draw.rawgps.heatmap.use-points"+layerNameDot, colorTypeHeatMapPoints.isSelected());
514        Main.pref.putInteger("draw.rawgps.heatmap.gain"+layerNameDot, colorTypeHeatMapGain.getValue());
515        Main.pref.putInteger("draw.rawgps.heatmap.lower-limit"+layerNameDot, colorTypeHeatMapLowerLimit.getValue());
516
517        return false;
518    }
519
520    /**
521     * Save preferences from UI controls for initial layer or globally
522     * @return {@code true} when restart is required, {@code false} otherwise
523     */
524    public boolean savePreferences() {
525        return savePreferences(null, false);
526    }
527
528    private static void updateWaypointLabelCombobox(JosmComboBox<String> cb, JosmTextField tf, TemplateEntryProperty property) {
529        String labelPattern = property.getAsString();
530        boolean found = false;
531        for (int i = 0; i < LABEL_PATTERN_TEMPLATE.length; i++) {
532            if (LABEL_PATTERN_TEMPLATE[i].equals(labelPattern)) {
533                cb.setSelectedIndex(i);
534                found = true;
535                break;
536            }
537        }
538        if (!found) {
539            cb.setSelectedIndex(WAYPOINT_LABEL_CUSTOM);
540            tf.setEnabled(true);
541            tf.setText(labelPattern);
542        }
543    }
544
545    private static void updateWaypointPattern(JosmComboBox<String> cb, JosmTextField tf) {
546        if (cb.getSelectedIndex() == WAYPOINT_LABEL_CUSTOM) {
547            tf.setEnabled(true);
548        } else {
549            tf.setEnabled(false);
550            tf.setText(LABEL_PATTERN_TEMPLATE[cb.getSelectedIndex()]);
551        }
552    }
553
554    @Override
555    public boolean validatePreferences() {
556        TemplateParser parser = new TemplateParser(waypointLabelPattern.getText());
557        try {
558            parser.parse();
559        } catch (ParseError e) {
560            Logging.warn(e);
561            JOptionPane.showMessageDialog(Main.parent,
562                    tr("Incorrect waypoint label pattern: {0}", e.getMessage()), tr("Incorrect pattern"), JOptionPane.ERROR_MESSAGE);
563            waypointLabelPattern.requestFocus();
564            return false;
565        }
566        parser = new TemplateParser(audioWaypointLabelPattern.getText());
567        try {
568            parser.parse();
569        } catch (ParseError e) {
570            Logging.warn(e);
571            JOptionPane.showMessageDialog(Main.parent,
572                    tr("Incorrect audio waypoint label pattern: {0}", e.getMessage()), tr("Incorrect pattern"), JOptionPane.ERROR_MESSAGE);
573            audioWaypointLabelPattern.requestFocus();
574            return false;
575        }
576        return true;
577    }
578}
Note: See TracBrowser for help on using the repository browser.