001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.preferences.display;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005import static org.openstreetmap.josm.tools.I18n.trc;
006
007import java.awt.Component;
008import java.awt.GridBagLayout;
009import java.awt.event.ActionEvent;
010import java.awt.event.ActionListener;
011
012import javax.swing.BorderFactory;
013import javax.swing.Box;
014import javax.swing.ButtonGroup;
015import javax.swing.JCheckBox;
016import javax.swing.JLabel;
017import javax.swing.JOptionPane;
018import javax.swing.JPanel;
019import javax.swing.JRadioButton;
020import javax.swing.event.ChangeEvent;
021import javax.swing.event.ChangeListener;
022
023import org.openstreetmap.josm.Main;
024import org.openstreetmap.josm.actions.ExpertToggleAction;
025import org.openstreetmap.josm.gui.layer.markerlayer.Marker;
026import org.openstreetmap.josm.gui.layer.markerlayer.Marker.TemplateEntryProperty;
027import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane.ValidationListener;
028import org.openstreetmap.josm.gui.widgets.JosmComboBox;
029import org.openstreetmap.josm.tools.GBC;
030import org.openstreetmap.josm.tools.template_engine.ParseError;
031import org.openstreetmap.josm.tools.template_engine.TemplateParser;
032import org.openstreetmap.josm.gui.widgets.JosmTextField;
033
034public class GPXSettingsPanel extends JPanel implements ValidationListener {
035
036    private static final int WAYPOINT_LABEL_CUSTOM = 6;
037    private static final String[] LABEL_PATTERN_TEMPLATE = new String[] {Marker.LABEL_PATTERN_AUTO, Marker.LABEL_PATTERN_NAME,
038        Marker.LABEL_PATTERN_DESC, "{special:everything}", "?{ '{name}' | '{desc}' | '{formattedWaypointOffset}' }", ""};
039    private static final String[] LABEL_PATTERN_DESC = new String[] {tr("Auto"), /* gpx data field name */ trc("gpx_field", "Name"),
040        /* gpx data field name */ trc("gpx_field", "Desc(ription)"), tr("Everything"), tr("Name or offset"), tr("None"), tr("Custom")};
041
042
043    private JRadioButton drawRawGpsLinesGlobal = new JRadioButton(tr("Use global settings"));
044    private JRadioButton drawRawGpsLinesAll = new JRadioButton(tr("All"));
045    private JRadioButton drawRawGpsLinesLocal = new JRadioButton(tr("Local files"));
046    private JRadioButton drawRawGpsLinesNone = new JRadioButton(tr("None"));
047    private ActionListener drawRawGpsLinesActionListener;
048    private JosmTextField drawRawGpsMaxLineLength = new JosmTextField(8);
049    private JosmTextField drawRawGpsMaxLineLengthLocal = new JosmTextField(8);
050    private JosmTextField drawLineWidth = new JosmTextField(2);
051    private JCheckBox forceRawGpsLines = new JCheckBox(tr("Force lines if no segments imported"));
052    private JCheckBox largeGpsPoints = new JCheckBox(tr("Draw large GPS points"));
053    private JCheckBox hdopCircleGpsPoints = new JCheckBox(tr("Draw a circle from HDOP value"));
054    private JRadioButton colorTypeVelocity = new JRadioButton(tr("Velocity (red = slow, green = fast)"));
055    private JRadioButton colorTypeDirection = new JRadioButton(tr("Direction (red = west, yellow = north, green = east, blue = south)"));
056    private JRadioButton colorTypeDilution = new JRadioButton(tr("Dilution of Position (red = high, green = low, if available)"));
057    private JRadioButton colorTypeTime = new JRadioButton(tr("Track date"));
058    private JRadioButton colorTypeNone = new JRadioButton(tr("Single Color (can be customized for named layers)"));
059    private JRadioButton colorTypeGlobal  = new JRadioButton(tr("Use global settings"));
060    private JosmComboBox colorTypeVelocityTune = new JosmComboBox(new String[] {tr("Car"), tr("Bicycle"), tr("Foot")});
061    private JCheckBox makeAutoMarkers = new JCheckBox(tr("Create markers when reading GPX"));
062    private JCheckBox drawGpsArrows = new JCheckBox(tr("Draw Direction Arrows"));
063    private JCheckBox drawGpsArrowsFast = new JCheckBox(tr("Fast drawing (looks uglier)"));
064    private JosmTextField drawGpsArrowsMinDist = new JosmTextField(8);
065    private JCheckBox colorDynamic = new JCheckBox(tr("Dynamic color range based on data limits"));
066    private JosmComboBox waypointLabel = new JosmComboBox(LABEL_PATTERN_DESC);
067    private JosmTextField waypointLabelPattern = new JosmTextField();
068    private JosmComboBox audioWaypointLabel = new JosmComboBox(LABEL_PATTERN_DESC);
069    private JosmTextField audioWaypointLabelPattern = new JosmTextField();
070    private JCheckBox useGpsAntialiasing = new JCheckBox(tr("Smooth GPX graphics (antialiasing)"));
071
072    private String layerName;
073    private boolean local; // flag to display LocalOnly checkbox
074    private boolean nonlocal; // flag to display AllLines checkbox
075
076    public GPXSettingsPanel(String layerName, boolean local, boolean nonlocal) {
077        super(new GridBagLayout());
078        this.local=local; this.nonlocal=nonlocal;
079        this.layerName = "layer "+layerName;
080        initComponents();
081        loadPreferences();
082    }
083
084    public GPXSettingsPanel() {
085        super(new GridBagLayout());
086        initComponents();
087        local=false; nonlocal=false;
088        loadPreferences(); // preferences -> controls
089    }
090
091    private void initComponents() {
092        setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
093
094        // makeAutoMarkers
095        makeAutoMarkers.setToolTipText(tr("Automatically make a marker layer from any waypoints when opening a GPX layer."));
096        ExpertToggleAction.addVisibilitySwitcher(makeAutoMarkers);
097        add(makeAutoMarkers, GBC.eol().insets(20,0,0,5));
098
099        // drawRawGpsLines
100        ButtonGroup gpsLinesGroup = new ButtonGroup();
101        if (layerName!=null) {
102            gpsLinesGroup.add(drawRawGpsLinesGlobal);
103        }
104        gpsLinesGroup.add(drawRawGpsLinesNone);
105        gpsLinesGroup.add(drawRawGpsLinesLocal);
106        gpsLinesGroup.add(drawRawGpsLinesAll);
107
108        /* ensure that default is in data base */
109
110        JLabel label = new JLabel(tr("Draw lines between raw GPS points"));
111        add(label, GBC.eol().insets(20,0,0,0));
112        if (layerName!=null) {
113            add(drawRawGpsLinesGlobal, GBC.eol().insets(40,0,0,0));
114        }
115        add(drawRawGpsLinesNone, GBC.eol().insets(40,0,0,0));
116        if (layerName==null || local) {
117            add(drawRawGpsLinesLocal, GBC.eol().insets(40,0,0,0));
118        }
119        if (layerName==null || nonlocal) {
120            add(drawRawGpsLinesAll, GBC.eol().insets(40,0,0,0));
121        }
122        ExpertToggleAction.addVisibilitySwitcher(label);
123        ExpertToggleAction.addVisibilitySwitcher(drawRawGpsLinesGlobal);
124        ExpertToggleAction.addVisibilitySwitcher(drawRawGpsLinesNone);
125        ExpertToggleAction.addVisibilitySwitcher(drawRawGpsLinesLocal);
126        ExpertToggleAction.addVisibilitySwitcher(drawRawGpsLinesAll);
127
128        drawRawGpsLinesActionListener = new ActionListener(){
129            @Override
130            public void actionPerformed(ActionEvent e) {
131                boolean f=drawRawGpsLinesNone.isSelected()||drawRawGpsLinesGlobal.isSelected();
132                forceRawGpsLines.setEnabled(!f);
133                drawRawGpsMaxLineLength.setEnabled(!(f || drawRawGpsLinesLocal.isSelected()));
134                drawRawGpsMaxLineLengthLocal.setEnabled(!f);
135                drawGpsArrows.setEnabled(!f);
136                drawGpsArrowsFast.setEnabled(drawGpsArrows.isSelected() && drawGpsArrows.isEnabled());
137                drawGpsArrowsMinDist.setEnabled(drawGpsArrows.isSelected() && drawGpsArrows.isEnabled());
138            }
139        };
140
141        drawRawGpsLinesGlobal.addActionListener(drawRawGpsLinesActionListener);
142        drawRawGpsLinesNone.addActionListener(drawRawGpsLinesActionListener);
143        drawRawGpsLinesLocal.addActionListener(drawRawGpsLinesActionListener);
144        drawRawGpsLinesAll.addActionListener(drawRawGpsLinesActionListener);
145
146        // drawRawGpsMaxLineLengthLocal
147        drawRawGpsMaxLineLengthLocal.setToolTipText(tr("Maximum length (in meters) to draw lines for local files. Set to ''-1'' to draw all lines."));
148        label = new JLabel(tr("Maximum length for local files (meters)"));
149        add(label, GBC.std().insets(40,0,0,0));
150        add(drawRawGpsMaxLineLengthLocal, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
151        ExpertToggleAction.addVisibilitySwitcher(label);
152        ExpertToggleAction.addVisibilitySwitcher(drawRawGpsMaxLineLengthLocal);
153
154        // drawRawGpsMaxLineLength
155        drawRawGpsMaxLineLength.setToolTipText(tr("Maximum length (in meters) to draw lines. Set to ''-1'' to draw all lines."));
156        label = new JLabel(tr("Maximum length (meters)"));
157        add(label, GBC.std().insets(40,0,0,0));
158        add(drawRawGpsMaxLineLength, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
159        ExpertToggleAction.addVisibilitySwitcher(label);
160        ExpertToggleAction.addVisibilitySwitcher(drawRawGpsMaxLineLength);
161
162        // forceRawGpsLines
163        forceRawGpsLines.setToolTipText(tr("Force drawing of lines if the imported data contain no line information."));
164        add(forceRawGpsLines, GBC.eop().insets(40,0,0,0));
165        ExpertToggleAction.addVisibilitySwitcher(forceRawGpsLines);
166
167        // drawGpsArrows
168        drawGpsArrows.addActionListener(new ActionListener(){
169            @Override
170            public void actionPerformed(ActionEvent e) {
171                drawGpsArrowsFast.setEnabled(drawGpsArrows.isSelected() && drawGpsArrows.isEnabled());
172                drawGpsArrowsMinDist.setEnabled(drawGpsArrows.isSelected() && drawGpsArrows.isEnabled());
173            }
174        });
175        drawGpsArrows.setToolTipText(tr("Draw direction arrows for lines, connecting GPS points."));
176        add(drawGpsArrows, GBC.eop().insets(40,0,0,0));
177
178        // drawGpsArrowsFast
179        drawGpsArrowsFast.setToolTipText(tr("Draw the direction arrows using table lookups instead of complex math."));
180        add(drawGpsArrowsFast, GBC.eop().insets(60,0,0,0));
181        ExpertToggleAction.addVisibilitySwitcher(drawGpsArrowsFast);
182
183        // drawGpsArrowsMinDist
184        drawGpsArrowsMinDist.setToolTipText(tr("Do not draw arrows if they are not at least this distance away from the last one."));
185        add(new JLabel(tr("Minimum distance (pixels)")), GBC.std().insets(60,0,0,0));
186        add(drawGpsArrowsMinDist, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
187
188        // hdopCircleGpsPoints
189        hdopCircleGpsPoints.setToolTipText(tr("Draw a circle from HDOP value."));
190        add(hdopCircleGpsPoints, GBC.eop().insets(20,0,0,0));
191        ExpertToggleAction.addVisibilitySwitcher(hdopCircleGpsPoints);
192
193        // largeGpsPoints
194        largeGpsPoints.setToolTipText(tr("Draw larger dots for the GPS points."));
195        add(largeGpsPoints, GBC.eop().insets(20,0,0,0));
196
197        // drawLineWidth
198        drawLineWidth.setToolTipText(tr("Width of drawn GPX line (0 for default)"));
199        add(new JLabel(tr("Drawing width of GPX lines")), GBC.std().insets(20,0,0,0));
200        add(drawLineWidth, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
201
202        // antialiasing
203        useGpsAntialiasing.setToolTipText(tr("Apply antialiasing to the GPX lines resulting in a smoother appearance."));
204        add(useGpsAntialiasing, GBC.eop().insets(20, 0, 0, 0));
205        ExpertToggleAction.addVisibilitySwitcher(useGpsAntialiasing);
206
207        // colorTracks
208        ButtonGroup colorGroup = new ButtonGroup();
209        if (layerName!=null) {
210            colorGroup.add(colorTypeGlobal);
211        }
212        colorGroup.add(colorTypeNone);
213        colorGroup.add(colorTypeVelocity);
214        colorGroup.add(colorTypeDirection);
215        colorGroup.add(colorTypeDilution);
216        colorGroup.add(colorTypeTime);
217
218        colorTypeVelocity.addChangeListener(new ChangeListener(){
219            @Override
220            public void stateChanged(ChangeEvent e) {
221                colorTypeVelocityTune.setEnabled(colorTypeVelocity.isSelected());
222                colorDynamic.setEnabled(colorTypeVelocity.isSelected() || colorTypeDilution.isSelected());
223            }
224        });
225        colorTypeDilution.addChangeListener(new ChangeListener(){
226            @Override
227            public void stateChanged(ChangeEvent e) {
228                colorDynamic.setEnabled(colorTypeVelocity.isSelected() || colorTypeDilution.isSelected());
229            }
230        });
231
232        colorTypeNone.setToolTipText(tr("All points and track segments will have the same color. Can be customized in Layer Manager."));
233        colorTypeVelocity.setToolTipText(tr("Colors points and track segments by velocity."));
234        colorTypeDirection.setToolTipText(tr("Colors points and track segments by direction."));
235        colorTypeDilution.setToolTipText(tr("Colors points and track segments by dilution of position (HDOP). Your capture device needs to log that information."));
236        colorTypeTime.setToolTipText(tr("Colors points and track segments by its timestamp."));
237
238        // color Tracks by Velocity Tune
239        colorTypeVelocityTune.setToolTipText(tr("Allows to tune the track coloring for different average speeds."));
240
241        add(Box.createVerticalGlue(), GBC.eol().insets(0, 20, 0, 0));
242
243        add(new JLabel(tr("Track and Point Coloring")), GBC.eol().insets(20,0,0,0));
244        if (layerName!=null) {
245            add(colorTypeGlobal, GBC.eol().insets(40,0,0,0));
246        }
247        add(colorTypeNone, GBC.eol().insets(40,0,0,0));
248        add(colorTypeVelocity, GBC.std().insets(40,0,0,0));
249        add(colorTypeVelocityTune, GBC.eop().insets(5,0,0,5));
250        add(colorTypeDirection, GBC.eol().insets(40,0,0,0));
251        add(colorTypeDilution, GBC.eol().insets(40,0,0,0));
252        add(colorTypeTime, GBC.eol().insets(40,0,0,0));
253        ExpertToggleAction.addVisibilitySwitcher(colorTypeDirection);
254        ExpertToggleAction.addVisibilitySwitcher(colorTypeDilution);
255
256        colorDynamic.setToolTipText(tr("Colors points and track segments by data limits."));
257        add(colorDynamic, GBC.eop().insets(40,0,0,0));
258        ExpertToggleAction.addVisibilitySwitcher(colorDynamic);
259
260        if (layerName == null) {
261            // Setting waypoints for gpx layer doesn't make sense - waypoints are shown in marker layer that has different name - so show
262            // this only for global config
263
264            // waypointLabel
265            label = new JLabel(tr("Waypoint labelling"));
266            add(label, GBC.std().insets(20,0,0,0));
267            add(waypointLabel, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
268            waypointLabel.addActionListener(new ActionListener() {
269                @Override
270                public void actionPerformed(ActionEvent e) {
271                    updateWaypointPattern(waypointLabel, waypointLabelPattern);
272                }
273            });
274            updateWaypointLabelCombobox(waypointLabel, waypointLabelPattern, TemplateEntryProperty.forMarker(layerName));
275            add(waypointLabelPattern, GBC.eol().fill(GBC.HORIZONTAL).insets(20,0,0,5));
276            ExpertToggleAction.addVisibilitySwitcher(label);
277            ExpertToggleAction.addVisibilitySwitcher(waypointLabel);
278            ExpertToggleAction.addVisibilitySwitcher(waypointLabelPattern);
279
280            // audioWaypointLabel
281            Component glue = Box.createVerticalGlue();
282            add(glue, GBC.eol().insets(0, 20, 0, 0));
283            ExpertToggleAction.addVisibilitySwitcher(glue);
284
285            label = new JLabel(tr("Audio waypoint labelling"));
286            add(label, GBC.std().insets(20,0,0,0));
287            add(audioWaypointLabel, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
288            audioWaypointLabel.addActionListener(new ActionListener() {
289                @Override
290                public void actionPerformed(ActionEvent e) {
291                    updateWaypointPattern(audioWaypointLabel, audioWaypointLabelPattern);
292                }
293            });
294            updateWaypointLabelCombobox(audioWaypointLabel, audioWaypointLabelPattern, TemplateEntryProperty.forAudioMarker(layerName));
295            add(audioWaypointLabelPattern, GBC.eol().fill(GBC.HORIZONTAL).insets(20,0,0,5));
296            ExpertToggleAction.addVisibilitySwitcher(label);
297            ExpertToggleAction.addVisibilitySwitcher(audioWaypointLabel);
298            ExpertToggleAction.addVisibilitySwitcher(audioWaypointLabelPattern);
299        }
300
301        add(Box.createVerticalGlue(), GBC.eol().fill(GBC.BOTH));
302    }
303
304    /**
305     * Loads preferences to UI controls
306     */
307    public void loadPreferences () {
308        makeAutoMarkers.setSelected(Main.pref.getBoolean("marker.makeautomarkers", true));
309        if(layerName!=null && Main.pref.get("draw.rawgps.lines."+layerName).isEmpty()
310                && Main.pref.get("draw.rawgps.lines.local."+layerName).isEmpty()){
311            // no line preferences for layer is found
312            drawRawGpsLinesGlobal.setSelected(true);
313        } else {
314            Boolean lf = Main.pref.getBoolean("draw.rawgps.lines.local",layerName, true);
315            if(Main.pref.getBoolean("draw.rawgps.lines",layerName, true)) {
316                drawRawGpsLinesAll.setSelected(true);
317            } else if (lf) {
318                drawRawGpsLinesLocal.setSelected(true);
319            } else {
320                drawRawGpsLinesNone.setSelected(true);
321            }
322        }
323
324        drawRawGpsMaxLineLengthLocal.setText(Integer.toString(Main.pref.getInteger("draw.rawgps.max-line-length.local",layerName, -1)));
325        drawRawGpsMaxLineLength.setText(Integer.toString(Main.pref.getInteger("draw.rawgps.max-line-length",layerName, 200)));
326        drawLineWidth.setText(Integer.toString(Main.pref.getInteger("draw.rawgps.linewidth",layerName, 0)));
327        forceRawGpsLines.setSelected(Main.pref.getBoolean("draw.rawgps.lines.force",layerName, false));
328        drawGpsArrows.setSelected(Main.pref.getBoolean("draw.rawgps.direction",layerName, false));
329        drawGpsArrowsFast.setSelected(Main.pref.getBoolean("draw.rawgps.alternatedirection",layerName, false));
330        drawGpsArrowsMinDist.setText(Integer.toString(Main.pref.getInteger("draw.rawgps.min-arrow-distance",layerName, 40)));
331        hdopCircleGpsPoints.setSelected(Main.pref.getBoolean("draw.rawgps.hdopcircle",layerName, false));
332        largeGpsPoints.setSelected(Main.pref.getBoolean("draw.rawgps.large",layerName, false));
333        useGpsAntialiasing.setSelected(Main.pref.getBoolean("mappaint.gpx.use-antialiasing", false));
334        drawRawGpsLinesActionListener.actionPerformed(null);
335
336        if(layerName!=null && Main.pref.get("draw.rawgps.colors."+layerName).isEmpty()) {
337            colorTypeGlobal.setSelected(true);
338            colorDynamic.setSelected(false);
339            colorDynamic.setEnabled(false);
340        } else {
341            switch(Main.pref.getInteger("draw.rawgps.colors",layerName, 0)) {
342            case 0: colorTypeNone.setSelected(true);   break;
343            case 1: colorTypeVelocity.setSelected(true);  break;
344            case 2: colorTypeDilution.setSelected(true);  break;
345            case 3: colorTypeDirection.setSelected(true); break;
346            case 4: colorTypeTime.setSelected(true);  break;
347            }
348            int ccts = Main.pref.getInteger("draw.rawgps.colorTracksTune",layerName, 45);
349            colorTypeVelocityTune.setSelectedIndex(ccts==10 ? 2 : (ccts==20 ? 1 : 0));
350            colorTypeVelocityTune.setEnabled(colorTypeVelocity.isSelected() && colorTypeVelocity.isEnabled());
351            colorDynamic.setSelected(Main.pref.getBoolean("draw.rawgps.colors.dynamic",layerName, false));
352            colorDynamic.setEnabled(colorTypeVelocity.isSelected() || colorTypeDilution.isSelected());
353        }
354    }
355
356
357    /**
358     * Save preferences from UI controls for specified layer
359     * if layerName==null, global preferences are written
360     */
361    public boolean savePreferences (String layerName, boolean locLayer) {
362        String layerNameDot = ".layer "+layerName;
363        if (layerName==null) {
364            layerNameDot="";
365        }
366        Main.pref.put("marker.makeautomarkers"+layerNameDot, makeAutoMarkers.isSelected());
367        if (drawRawGpsLinesGlobal.isSelected()) {
368            Main.pref.put("draw.rawgps.lines" + layerNameDot, null);
369            Main.pref.put("draw.rawgps.max-line-length" + layerNameDot, null);
370            Main.pref.put("draw.rawgps.lines.local" + layerNameDot, null);
371            Main.pref.put("draw.rawgps.max-line-length.local" + layerNameDot, null);
372            Main.pref.put("draw.rawgps.lines.force"+layerNameDot, null);
373            Main.pref.put("draw.rawgps.direction"+layerNameDot, null);
374            Main.pref.put("draw.rawgps.alternatedirection"+layerNameDot, null);
375            Main.pref.put("draw.rawgps.min-arrow-distance"+layerNameDot, null);
376        } else {
377            if (layerName==null || !locLayer) {
378                Main.pref.put("draw.rawgps.lines" +  layerNameDot, drawRawGpsLinesAll.isSelected());
379                Main.pref.put("draw.rawgps.max-line-length" + layerNameDot, drawRawGpsMaxLineLength.getText());
380            }
381            if (layerName==null || locLayer) {
382                Main.pref.put("draw.rawgps.lines.local" + layerNameDot, drawRawGpsLinesAll.isSelected() || drawRawGpsLinesLocal.isSelected());
383                Main.pref.put("draw.rawgps.max-line-length.local" + layerNameDot, drawRawGpsMaxLineLengthLocal.getText());
384            }
385            Main.pref.put("draw.rawgps.lines.force"+layerNameDot, forceRawGpsLines.isSelected());
386            Main.pref.put("draw.rawgps.direction"+layerNameDot, drawGpsArrows.isSelected());
387            Main.pref.put("draw.rawgps.alternatedirection"+layerNameDot, drawGpsArrowsFast.isSelected());
388            Main.pref.put("draw.rawgps.min-arrow-distance"+layerNameDot, drawGpsArrowsMinDist.getText());
389        }
390
391        Main.pref.put("draw.rawgps.hdopcircle"+layerNameDot, hdopCircleGpsPoints.isSelected());
392        Main.pref.put("draw.rawgps.large"+layerNameDot, largeGpsPoints.isSelected());
393        Main.pref.put("draw.rawgps.linewidth"+layerNameDot, drawLineWidth.getText());
394        Main.pref.put("mappaint.gpx.use-antialiasing", useGpsAntialiasing.isSelected());
395
396        TemplateEntryProperty.forMarker(layerName).put(waypointLabelPattern.getText());
397        TemplateEntryProperty.forAudioMarker(layerName).put(audioWaypointLabelPattern.getText());
398
399        if(colorTypeGlobal.isSelected()) {
400            Main.pref.put("draw.rawgps.colors"+layerNameDot, null);
401            Main.pref.put("draw.rawgps.colors.dynamic"+layerNameDot, null);
402            Main.pref.put("draw.rawgps.colorTracksTunec"+layerNameDot, null);
403            return false;
404        } else if(colorTypeVelocity.isSelected()) {
405            Main.pref.putInteger("draw.rawgps.colors"+layerNameDot, 1);
406        } else if(colorTypeDilution.isSelected()) {
407            Main.pref.putInteger("draw.rawgps.colors"+layerNameDot, 2);
408        } else if(colorTypeDirection.isSelected()) {
409            Main.pref.putInteger("draw.rawgps.colors"+layerNameDot, 3);
410        } else if(colorTypeTime.isSelected()) {
411            Main.pref.putInteger("draw.rawgps.colors"+layerNameDot, 4);
412        } else {
413            Main.pref.putInteger("draw.rawgps.colors"+layerNameDot, 0);
414        }
415        Main.pref.put("draw.rawgps.colors.dynamic"+layerNameDot, colorDynamic.isSelected());
416        int ccti=colorTypeVelocityTune.getSelectedIndex();
417        Main.pref.putInteger("draw.rawgps.colorTracksTune"+layerNameDot, ccti==2 ? 10 : (ccti==1 ? 20 : 45));
418        return false;
419    }
420
421    /**
422     * Save preferences from UI controls for initial layer or globally
423     */
424    public void savePreferences() {
425        savePreferences(null, false);
426    }
427
428    private void updateWaypointLabelCombobox(JosmComboBox cb, JosmTextField tf, TemplateEntryProperty property) {
429        String labelPattern = property.getAsString();
430        boolean found = false;
431        for (int i=0; i<LABEL_PATTERN_TEMPLATE.length; i++) {
432            if (LABEL_PATTERN_TEMPLATE[i].equals(labelPattern)) {
433                cb.setSelectedIndex(i);
434                found = true;
435                break;
436            }
437        }
438        if (!found) {
439            cb.setSelectedIndex(WAYPOINT_LABEL_CUSTOM);
440            tf.setEnabled(true);
441            tf.setText(labelPattern);
442        }
443    }
444
445    private void updateWaypointPattern(JosmComboBox cb, JosmTextField tf) {
446        if (cb.getSelectedIndex() == WAYPOINT_LABEL_CUSTOM) {
447            tf.setEnabled(true);
448        } else {
449            tf.setEnabled(false);
450            tf.setText(LABEL_PATTERN_TEMPLATE[cb.getSelectedIndex()]);
451        }
452    }
453
454    @Override
455    public boolean validatePreferences() {
456        TemplateParser parser = new TemplateParser(waypointLabelPattern.getText());
457        try {
458            parser.parse();
459        } catch (ParseError e) {
460            JOptionPane.showMessageDialog(Main.parent, tr("Incorrect waypoint label pattern: {0}", e.getMessage()), tr("Incorrect pattern"), JOptionPane.ERROR_MESSAGE);
461            waypointLabelPattern.requestFocus();
462            return false;
463        }
464        parser = new TemplateParser(audioWaypointLabelPattern.getText());
465        try {
466            parser.parse();
467        } catch (ParseError e) {
468            JOptionPane.showMessageDialog(Main.parent, tr("Incorrect audio waypoint label pattern: {0}", e.getMessage()), tr("Incorrect pattern"), JOptionPane.ERROR_MESSAGE);
469            audioWaypointLabelPattern.requestFocus();
470            return false;
471        }
472        return true;
473    }
474
475}