1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
3 |
|
---|
4 | import javax.swing.JCheckBox;
|
---|
5 | import javax.swing.JTextField;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
8 | import org.openstreetmap.josm.tools.JosmDecimalFormatSymbolsProvider;
|
---|
9 | import org.openstreetmap.josm.tools.Logging;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Abstract base class for {@link PreferenceSetting} implementations.
|
---|
13 | *
|
---|
14 | * Handles the flag that indicates if a PreferenceSetting is and expert option or not.
|
---|
15 | * @since 4968
|
---|
16 | */
|
---|
17 | public abstract class DefaultPreferenceSetting implements PreferenceSetting {
|
---|
18 |
|
---|
19 | private final boolean isExpert;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Constructs a new DefaultPreferenceSetting.
|
---|
23 | *
|
---|
24 | * (Not an expert option by default.)
|
---|
25 | */
|
---|
26 | protected DefaultPreferenceSetting() {
|
---|
27 | this(false);
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Constructs a new DefaultPreferenceSetting.
|
---|
32 | *
|
---|
33 | * @param isExpert true, if it is an expert option
|
---|
34 | */
|
---|
35 | protected DefaultPreferenceSetting(boolean isExpert) {
|
---|
36 | this.isExpert = isExpert;
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public boolean isExpert() {
|
---|
41 | return isExpert;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Saves state from a {@link JCheckBox} to a boolean preference.
|
---|
46 | * @param prefName preference name
|
---|
47 | * @param cb check box
|
---|
48 | * @since 13050
|
---|
49 | */
|
---|
50 | protected static void saveBoolean(String prefName, JCheckBox cb) {
|
---|
51 | Config.getPref().putBoolean(prefName, cb.isSelected());
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Saves text from a {@link JTextField} to a double preference.
|
---|
56 | * @param prefName preference name
|
---|
57 | * @param tf text field
|
---|
58 | * @since 13050
|
---|
59 | */
|
---|
60 | protected static void saveDouble(String prefName, JTextField tf) {
|
---|
61 | String text = tf.getText();
|
---|
62 | try {
|
---|
63 | Config.getPref().putDouble(prefName, JosmDecimalFormatSymbolsProvider.parseDouble(text));
|
---|
64 | } catch (NumberFormatException e) {
|
---|
65 | Logging.warn("Unable to save '" + text + "' as a double value for preference " + prefName);
|
---|
66 | Logging.trace(e);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Saves text from a {@link JTextField} to an integer preference.
|
---|
72 | * @param prefName preference name
|
---|
73 | * @param tf text field
|
---|
74 | * @since 13050
|
---|
75 | */
|
---|
76 | protected static void saveInt(String prefName, JTextField tf) {
|
---|
77 | String text = tf.getText();
|
---|
78 | try {
|
---|
79 | Config.getPref().putInt(prefName, Integer.parseInt(text));
|
---|
80 | } catch (NumberFormatException e) {
|
---|
81 | Logging.warn("Unable to save '" + text + "' as an integer value for preference " + prefName);
|
---|
82 | Logging.trace(e);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|