001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.preferences; 003 004import javax.swing.JCheckBox; 005import javax.swing.JTextField; 006 007import org.openstreetmap.josm.spi.preferences.Config; 008import org.openstreetmap.josm.tools.JosmDecimalFormatSymbolsProvider; 009import org.openstreetmap.josm.tools.Logging; 010 011/** 012 * Abstract base class for {@link PreferenceSetting} implementations. 013 * 014 * Handles the flag that indicates if a PreferenceSetting is and expert option or not. 015 * @since 4968 016 */ 017public abstract class DefaultPreferenceSetting implements PreferenceSetting { 018 019 private final boolean isExpert; 020 021 /** 022 * Constructs a new DefaultPreferenceSetting. 023 * 024 * (Not an expert option by default.) 025 */ 026 public DefaultPreferenceSetting() { 027 this(false); 028 } 029 030 /** 031 * Constructs a new DefaultPreferenceSetting. 032 * 033 * @param isExpert true, if it is an expert option 034 */ 035 public DefaultPreferenceSetting(boolean isExpert) { 036 this.isExpert = isExpert; 037 } 038 039 @Override 040 public boolean isExpert() { 041 return isExpert; 042 } 043 044 /** 045 * Saves state from a {@link JCheckBox} to a boolean preference. 046 * @param prefName preference name 047 * @param cb check box 048 * @since 13050 049 */ 050 protected static void saveBoolean(String prefName, JCheckBox cb) { 051 Config.getPref().putBoolean(prefName, cb.isSelected()); 052 } 053 054 /** 055 * Saves text from a {@link JTextField} to a double preference. 056 * @param prefName preference name 057 * @param tf text field 058 * @since 13050 059 */ 060 protected static void saveDouble(String prefName, JTextField tf) { 061 String text = tf.getText(); 062 try { 063 Config.getPref().putDouble(prefName, JosmDecimalFormatSymbolsProvider.parseDouble(text)); 064 } catch (NumberFormatException e) { 065 Logging.warn("Unable to save '" + text + "' as a double value for preference " + prefName); 066 Logging.trace(e); 067 } 068 } 069 070 /** 071 * Saves text from a {@link JTextField} to an integer preference. 072 * @param prefName preference name 073 * @param tf text field 074 * @since 13050 075 */ 076 protected static void saveInt(String prefName, JTextField tf) { 077 String text = tf.getText(); 078 try { 079 Config.getPref().putInt(prefName, Integer.parseInt(text)); 080 } catch (NumberFormatException e) { 081 Logging.warn("Unable to save '" + text + "' as an integer value for preference " + prefName); 082 Logging.trace(e); 083 } 084 } 085}