File Source: PreferencesSetting.java

         /* 
    P/P   *  Method: com.dmdirc.config.prefs.PreferencesSetting__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  package com.dmdirc.config.prefs;
    23  
    24  import com.dmdirc.config.IdentityManager;
    25  import com.dmdirc.config.prefs.validator.PermissiveValidator;
    26  import com.dmdirc.config.prefs.validator.Validator;
    27  
    28  import java.util.ArrayList;
    29  import java.util.HashMap;
    30  import java.util.List;
    31  import java.util.Map;
    32  
    33  /**
    34   * Represents a single setting.
    35   * 
    36   * @author chris
    37   */
    38  public class PreferencesSetting {
    39      
    40      /** The type of this setting. */
    41      protected final PreferencesType type;
    42      
    43      /** The possible options for a multichoice setting. */
    44      protected final Map<String, String> combooptions;
    45  
    46      /** The validator to use to validate this setting. */
    47      protected final Validator<String> validator;
    48      
    49      /** The domain of the setting. */
    50      protected final String domain;
    51      
    52      /** The option name of the setting. */
    53      protected final String option;
    54      
    55      /** The title of this setting. */
    56      protected final String title;
    57      
    58      /** Text to inform the user what the setting is for. */
    59      protected final String helptext;
    60      
    61      /** The current value of the setting. */
    62      protected String current;
    63      
    64      /** The original value of this vsetting. */
    65      private String original;
    66      
    67      /** Whether or not we need a restart. */
    68      protected boolean restartNeeded;
    69      
    70      /** A list of change listeners. */
    71      private final List<SettingChangeListener> listeners = new ArrayList<SettingChangeListener>();
    72  
    73      /**
    74       * Creates a new preferences setting for any type except multi-choice.
    75       * 
    76       * @param type The type of the setting to create
    77       * @param validator A validator to validate the setting's value
    78       * @param domain The domain of the setting
    79       * @param option The option name of the setting
    80       * @param title The title of this setting
    81       * @param helptext Text to display to help the user
    82       */
    83      public PreferencesSetting(final PreferencesType type,
    84              final Validator<String> validator, final String domain,
                     /* 
    P/P               *  Method: void com.dmdirc.config.prefs.PreferencesSetting(PreferencesType, Validator, String, String, String, String)
                      * 
                      *  Preconditions:
                      *    init'ed(com/dmdirc/config/IdentityManager.globalconfig)
                      *    (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
                      * 
                      *  Presumptions:
                      *    com.dmdirc.config.prefs.PreferencesType:equals(...)@86 == 0
                      *    getGlobalConfig(...).sources != null
                      * 
                      *  Postconditions:
                      *    com/dmdirc/config/IdentityManager.globalconfig == One-of{old com/dmdirc/config/IdentityManager.globalconfig, &amp;new ConfigManager(getGlobalConfig#1)}
                      *    com/dmdirc/config/IdentityManager.globalconfig != null
                      *    java.lang.StringBuilder:toString(...)._tainted == 0
                      *    this.combooptions == null
                      *    init'ed(this.current)
                      *    this.original == this.current
                      *    this.domain == domain
                      *    init'ed(this.domain)
                      *    this.helptext == helptext
                      *    init'ed(this.helptext)
                      *    ...
                      */
    85              final String option, final String title, final String helptext) {
    86          if (PreferencesType.MULTICHOICE.equals(type)) {
    87              throw new IllegalArgumentException("Multi-choice preferences must " +
    88                      "have their options specified.");
    89          }
    90          
    91          this.type = type;
    92          this.combooptions = null;
    93          this.validator = validator;
    94          this.domain = domain;
    95          this.option = option;
    96          this.title = title;
    97          this.helptext = helptext;
    98          
    99          current = IdentityManager.getGlobalConfig().getOption(domain, option);
   100          original = current;
   101      }
   102      
   103      /**
   104       * Creates a new preferences setting for any type except multi-choice, with
   105       * a default permissive validator.
   106       * 
   107       * @param type The type of the setting to create
   108       * @param domain The domain of the setting
   109       * @param option The option name of the setting
   110       * @param title The title of this setting
   111       * @param helptext Text to display to help the user
   112       */
   113      public PreferencesSetting(final PreferencesType type, final String domain,
                     /* 
    P/P               *  Method: void com.dmdirc.config.prefs.PreferencesSetting(PreferencesType, String, String, String, String)
                      * 
                      *  Preconditions:
                      *    init'ed(com/dmdirc/config/IdentityManager.globalconfig)
                      *    (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
                      * 
                      *  Presumptions:
                      *    com.dmdirc.config.prefs.PreferencesType:equals(...)@115 == 0
                      *    getGlobalConfig(...).sources != null
                      * 
                      *  Postconditions:
                      *    com/dmdirc/config/IdentityManager.globalconfig == One-of{old com/dmdirc/config/IdentityManager.globalconfig, &amp;new ConfigManager(getGlobalConfig#1)}
                      *    com/dmdirc/config/IdentityManager.globalconfig != null
                      *    java.lang.StringBuilder:toString(...)._tainted == 0
                      *    this.combooptions == null
                      *    init'ed(this.current)
                      *    this.original == this.current
                      *    this.domain == domain
                      *    init'ed(this.domain)
                      *    this.helptext == helptext
                      *    init'ed(this.helptext)
                      *    ...
                      */
   114              final String option, final String title, final String helptext) {
   115          if (PreferencesType.MULTICHOICE.equals(type)) {
   116              throw new IllegalArgumentException("Multi-choice preferences must " +
   117                      "have their options specified.");
   118          }
   119          
   120          this.type = type;
   121          this.combooptions = null;
   122          this.validator = new PermissiveValidator<String>();
   123          this.domain = domain;
   124          this.option = option;
   125          this.title = title;
   126          this.helptext = helptext;
   127          
   128          current = IdentityManager.getGlobalConfig().getOption(domain, option);
   129          original = current;
   130      }    
   131      
   132      /**
   133       * Creates a new preferences setting for multi-choice preferences.
   134       * 
   135       * @param domain The domain of the setting
   136       * @param option The option name of the setting
   137       * @param options A map of setting values to display names for this setting
   138       * @param title The title of this setting
   139       * @param helptext Text to display to help the user
   140       */
   141      public PreferencesSetting(final String domain, final String option,
   142              final String title, final String helptext,
                     /* 
    P/P               *  Method: void com.dmdirc.config.prefs.PreferencesSetting(String, String, String, String, Map)
                      * 
                      *  Preconditions:
                      *    init'ed(com/dmdirc/config/IdentityManager.globalconfig)
                      *    (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
                      * 
                      *  Presumptions:
                      *    getGlobalConfig(...).sources != null
                      * 
                      *  Postconditions:
                      *    com/dmdirc/config/IdentityManager.globalconfig == One-of{old com/dmdirc/config/IdentityManager.globalconfig, &amp;new ConfigManager(getGlobalConfig#1)}
                      *    com/dmdirc/config/IdentityManager.globalconfig != null
                      *    java.lang.StringBuilder:toString(...)._tainted == 0
                      *    this.combooptions == &amp;new HashMap(PreferencesSetting#2)
                      *    init'ed(this.current)
                      *    this.original == this.current
                      *    this.domain == domain
                      *    init'ed(this.domain)
                      *    this.helptext == helptext
                      *    init'ed(this.helptext)
                      *    ...
                      * 
                      *  Test Vectors:
                      *    java.util.Map:containsKey(...)@155: {1}, {0}
                      */
   143              final Map<String, String> options) {
   144          this.type = PreferencesType.MULTICHOICE;
   145          this.combooptions = new HashMap<String, String>(options);
   146          this.validator = new PermissiveValidator<String>();
   147          this.domain = domain;
   148          this.option = option;
   149          this.title = title;
   150          this.helptext = helptext;
   151          
   152          current = IdentityManager.getGlobalConfig().getOption(domain, option);
   153          original = current;
   154          
   155          if (!combooptions.containsKey(current)) {
   156              combooptions.put(current, "Current (" + current + ")");
   157          }
   158      }    
   159  
   160      /**
   161       * Retrieves the possible options for use in a multi-choice preference.
   162       * 
   163       * @return A map of setting values to display names, representing the
   164       * possible options for this setting.
   165       */
   166      public Map<String, String> getComboOptions() {
                 /* 
    P/P           *  Method: Map getComboOptions()
                  * 
                  *  Postconditions:
                  *    return_value == this.combooptions
                  *    init'ed(return_value)
                  */
   167          return combooptions;
   168      }
   169  
   170      /**
   171       * Retrieves the help text for this setting.
   172       * 
   173       * @return This setting's help text.
   174       */
   175      public String getHelptext() {
                 /* 
    P/P           *  Method: String getHelptext()
                  * 
                  *  Postconditions:
                  *    return_value == this.helptext
                  *    init'ed(return_value)
                  */
   176          return helptext;
   177      }
   178  
   179      /**
   180       * Retrieves the title of this setting.
   181       * 
   182       * @return This setting's title.
   183       */
   184      public String getTitle() {
                 /* 
    P/P           *  Method: String getTitle()
                  * 
                  *  Postconditions:
                  *    return_value == this.title
                  *    init'ed(return_value)
                  */
   185          return title;
   186      }
   187  
   188      /**
   189       * Retrieves the current value of this setting.
   190       * 
   191       * @return The current value of this setting.
   192       */
   193      public String getValue() {
                 /* 
    P/P           *  Method: String getValue()
                  * 
                  *  Preconditions:
                  *    init'ed(this.current)
                  * 
                  *  Postconditions:
                  *    return_value == this.current
                  *    init'ed(return_value)
                  */
   194          return current;
   195      }
   196  
   197      /**
   198       * Retieves the type of this setting.
   199       * 
   200       * @return This setting's type.
   201       */
   202      public PreferencesType getType() {
                 /* 
    P/P           *  Method: PreferencesType getType()
                  * 
                  *  Postconditions:
                  *    return_value == this.type
                  *    init'ed(return_value)
                  */
   203          return type;
   204      }
   205  
   206      /**
   207       * Returns a validator that can validate this setting.
   208       * 
   209       * @return This setting's validator.
   210       */
   211      public Validator<String> getValidator() {
                 /* 
    P/P           *  Method: Validator getValidator()
                  * 
                  *  Postconditions:
                  *    return_value == this.validator
                  *    init'ed(return_value)
                  */
   212          return validator;
   213      }
   214      
   215      /**
   216       * Sets the current value of this setting. Note that the setting is not
   217       * saved to the configuration file until the save method is called.
   218       * 
   219       * @param newValue The new value of the setting
   220       */
   221      public void setValue(final String newValue) {
                 /* 
    P/P           *  Method: void setValue(String)
                  * 
                  *  Preconditions:
                  *    this.listeners != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@224 != null
                  * 
                  *  Postconditions:
                  *    this.current == newValue
                  *    init'ed(this.current)
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@224: {0}, {1}
                  */
   222          current = newValue;
   223          
   224          for (SettingChangeListener listener : listeners) {
   225              listener.settingChanged(this);
   226          }
   227      }
   228  
   229      /**
   230       * Determines whether or not this setting needs a restart when it's changed.
   231       * 
   232       * @return True if this setting needs a restart, false otherwise
   233       */
   234      public boolean isRestartNeeded() {
                 /* 
    P/P           *  Method: bool isRestartNeeded()
                  * 
                  *  Preconditions:
                  *    init'ed(this.restartNeeded)
                  * 
                  *  Postconditions:
                  *    return_value == this.restartNeeded
                  *    init'ed(return_value)
                  */
   235          return restartNeeded;
   236      }
   237  
   238      /**
   239       * Sets the "restart needed" flag for this setting, indicating a client
   240       * restart is needed before the setting takes effect.
   241       * 
   242       * @return A reference to this setting, for convenience
   243       */
   244      public PreferencesSetting setRestartNeeded() {
                 /* 
    P/P           *  Method: PreferencesSetting setRestartNeeded()
                  * 
                  *  Postconditions:
                  *    return_value == this
                  *    return_value != null
                  *    this.restartNeeded == 1
                  */
   245          restartNeeded = true;
   246          return this;
   247      }
   248      
   249      /**
   250       * Registers the specified setting change listener.
   251       * 
   252       * @param listener The listener to be registered
   253       * @return A reference to this setting, for convenience
   254       */
   255      public PreferencesSetting registerChangeListener(final SettingChangeListener listener) {
                 /* 
    P/P           *  Method: PreferencesSetting registerChangeListener(SettingChangeListener)
                  * 
                  *  Preconditions:
                  *    this.listeners != null
                  * 
                  *  Postconditions:
                  *    return_value == this
                  *    return_value != null
                  */
   256          listeners.add(listener);
   257          return this;
   258      }    
   259      
   260      /**
   261       * Saves the current value of this setting to the global configuration.
   262       * 
   263       * @return True if the setting has changed, false otherwise
   264       */
   265      public boolean save() {
                 /* 
    P/P           *  Method: bool save()
                  * 
                  *  Preconditions:
                  *    init'ed(this.current)
                  *    (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
                  *    (soft) com/dmdirc/config/IdentityManager.config != null
                  *    (soft) com/dmdirc/config/IdentityManager.config.file != null
                  *    (soft) com/dmdirc/config/IdentityManager.config.listeners != null
                  *    (soft) com/dmdirc/config/IdentityManager.config.myTarget != null
                  *    (soft) init'ed(com/dmdirc/config/IdentityManager.config.myTarget.type)
                  *    (soft) init'ed(com/dmdirc/config/IdentityManager.config.globalConfig)
                  *    (soft) init'ed(this.original)
                  * 
                  *  Postconditions:
                  *    com/dmdirc/config/IdentityManager.config.globalConfig == One-of{old com/dmdirc/config/IdentityManager.config.globalConfig, &amp;new ConfigManager(setOption#2)}
                  *    init'ed(com/dmdirc/config/IdentityManager.config.globalConfig)
                  *    possibly_updated(com/dmdirc/config/IdentityManager.config.needSave)
                  *    java.lang.StringBuilder:toString(...)._tainted == 0
                  *    init'ed(return_value)
                  *    this.original == One-of{old this.original, this.current}
                  *    init'ed(this.original)
                  *    new ArrayList(getSources#1) num objects <= 1
                  *    new ConfigManager(setOption#2) num objects <= 1
                  *    init'ed(new ConfigManager(setOption#2).channel)
                  *    ...
                  * 
                  *  Test Vectors:
                  *    this.current: Inverse{null}, Addr_Set{null}
                  */
   266          if (!needsSaving()) {
   267              return false;
   268          }
   269          
   270          if (current == null) {
   271              IdentityManager.getConfigIdentity().unsetOption(domain, option);
   272          } else {
   273              IdentityManager.getConfigIdentity().setOption(domain, option, current);
   274          }
   275  
   276          original = current;
   277          return true;
   278      }
   279      
   280      /**
   281       * Dismisses changes to this setting.
   282       */
   283      public void dismiss() {
                 /* 
    P/P           *  Method: void dismiss()
                  * 
                  *  Preconditions:
                  *    init'ed(this.original)
                  *    (soft) init'ed(this.current)
                  *    (soft) this.listeners != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@291 != null
                  * 
                  *  Postconditions:
                  *    this.current == One-of{old this.current, this.original}
                  *    init'ed(this.current)
                  * 
                  *  Test Vectors:
                  *    this.current: Inverse{null}, Addr_Set{null}
                  *    this.original: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:equals(...)@284: {1}, {0}
                  *    java.util.Iterator:hasNext(...)@291: {0}, {1}
                  */
   284          if ((original != null && original.equals(current))
   285                  || (original == null && current == null)) {
   286              return;
   287          }
   288          
   289          current = original;
   290          
   291          for (SettingChangeListener listener : listeners) {
   292              listener.settingChanged(this);
   293          }
   294      }
   295      
   296      /**
   297       * Does the setting need saving?
   298       * 
   299       * @return true iif the setting will be changed if saved
   300       */
   301      public boolean needsSaving() {
                 /* 
    P/P           *  Method: bool needsSaving()
                  * 
                  *  Preconditions:
                  *    init'ed(this.current)
                  *    (soft) init'ed(this.original)
                  * 
                  *  Presumptions:
                  *    validate(...)@302 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    this.current: Addr_Set{null}, Inverse{null}
                  */
   302          return (current == null || !current.equals(original))
   303                  && (current != null || original != null)
   304                  && (validator == null || !validator.validate(current).isFailure());
   305      }
   306  
   307  }








SofCheck Inspector Build Version : 2.17854
PreferencesSetting.java 2009-Jun-25 01:54:24
PreferencesSetting.class 2009-Sep-02 17:04:13