File Source: ThemeManager.java

         /* 
    P/P   *  Method: com.dmdirc.ui.themes.ThemeManager$1__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  
    23  package com.dmdirc.ui.themes;
    24  
    25  import com.dmdirc.Main;
    26  import com.dmdirc.config.IdentityManager;
    27  import com.dmdirc.interfaces.ConfigChangeListener;
    28  import com.dmdirc.logger.ErrorLevel;
    29  import com.dmdirc.logger.Logger;
    30  
    31  import java.io.File;
    32  import java.util.HashMap;
    33  import java.util.List;
    34  import java.util.Map;
    35  
    36  /**
    37   * Manages available themes.
    38   *
    39   * @author Chris
    40   */
    41  public final class ThemeManager {
    42      
    43      /** The directory to look for themes in. */
             /* 
    P/P       *  Method: com.dmdirc.ui.themes.ThemeManager__static_init
              * 
              *  Presumptions:
              *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@50 != null
              * 
              *  Postconditions:
              *    "themes."._tainted == 0
              *    java.lang.StringBuilder:toString(...)._tainted == 0
              *    THEMES == &new HashMap(ThemeManager__static_init#2)
              *    THEME_DIR == &java.lang.StringBuilder:toString(...)
              *    new HashMap(ThemeManager__static_init#2) num objects == 1
              */
    44      private static final String THEME_DIR = Main.getConfigDir() + "themes/";
    45      
    46      /** Available themes. */
    47      private static final Map<String, Theme> THEMES = new HashMap<String, Theme>();
    48      
    49      static {
    50          IdentityManager.getGlobalConfig().addChangeListener("themes", "enabled",
                         /* 
    P/P                   *  Method: void com.dmdirc.ui.themes.ThemeManager$1()
                          */
    51                  new ConfigChangeListener() {
    52              /** {@inheritDoc} */
    53              @Override
    54              public void configChanged(final String domain, final String key) {
                         /* 
    P/P                   *  Method: void configChanged(String, String)
                          */
    55                  loadThemes();
    56              }
    57          });
    58      }
    59      
    60      /**
    61       * Creates a new instance of theme manager.
    62       */
             /* 
    P/P       *  Method: void com.dmdirc.ui.themes.ThemeManager()
              */
    63      private ThemeManager() {
    64          // Do nothing
    65      }
    66      
    67      /**
    68       * Scans for available themes and loads any themes that the user has enabled.
    69       */
    70      public static void loadThemes() {
                 /* 
    P/P           *  Method: void loadThemes()
                  * 
                  *  Presumptions:
                  *    arr$.length@86 <= 232-1
                  *    arr$[i$]@86 != null
                  *    com.dmdirc.config.ConfigManager:getOptionList(...)@77 != null
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@77 != null
                  *    init'ed(com.dmdirc.logger.ErrorLevel.HIGH)
                  *    ...
                  * 
                  *  Test Vectors:
                  *    java.io.File:exists(...)@73: {1}, {0}
                  *    java.io.File:listFiles(...)@80: Inverse{null}, Addr_Set{null}
                  *    java.io.File:mkdirs(...)@73: {1}, {0}
                  */
    71          final File dir = new File(THEME_DIR);
    72  
    73          if (!dir.exists() && !dir.mkdirs()) {
    74              Logger.userError(ErrorLevel.HIGH, "Could not create themes directory");
    75          }
    76          
    77          final List<String> enabled
    78                  = IdentityManager.getGlobalConfig().getOptionList("themes", "enabled");
    79              
    80          if (dir.listFiles() == null) {
    81              Logger.userError(ErrorLevel.MEDIUM, "Unable to load themes");
    82              return;
    83          }
    84          
    85          synchronized (THEMES) {
    86              for (File file : dir.listFiles()) {
    87                  if (file.isDirectory()) {
    88                      continue;
    89                  }
    90  
    91                  loadTheme(file, enabled.contains(file.getName()));
    92              }
    93          }            
    94      }
    95      
    96      /**
    97       * Attempts to load the theme from the specified file. If the enabled 
    98       * argument is true, the theme will be applied automatically. If it
    99       * has been previously applied and is no longer enabled, it will be 
   100       * unapplied.
   101       * 
   102       * @param file The file pointing to the theme to be loaded
   103       * @param enabled Whether this theme is enabled or not
   104       */
   105      private static void loadTheme(final File file, final boolean enabled) {
   106          Theme theme;
   107  
                 /* 
    P/P           *  Method: void loadTheme(File, bool)
                  * 
                  *  Preconditions:
                  *    file != null
                  * 
                  *  Presumptions:
                  *    java.util.Map:get(...)@109 != null
                  *    theme.file@109 != null
                  *    theme.rm.zipFile@109 != null
                  *    theme.rm.zipFile@114 != null
                  * 
                  *  Postconditions:
                  *    new ArrayList(ConfigFile#1) num objects <= 1
                  *    new ArrayList(ZipResourceManager#2) num objects <= 1
                  *    new ArrayList(readLines#4) num objects == 0
                  *    new ConfigFile(isValidTheme#2) num objects <= 1
                  *    init'ed(new ConfigFile(isValidTheme#2).charset)
                  *    init'ed(new ConfigFile(isValidTheme#2).domains)
                  *    init'ed(new ConfigFile(isValidTheme#2).file)
                  *    init'ed(new ConfigFile(isValidTheme#2).flatdomains)
                  *    init'ed(new ConfigFile(isValidTheme#2).is)
                  *    init'ed(new ConfigFile(isValidTheme#2).keydomains)
                  *    ...
                  * 
                  *  Test Vectors:
                  *    enabled: {0}, {1}
                  *    java.util.Map:containsKey(...)@108: {0}, {1}
                  */
   108          if (THEMES.containsKey(file.getName())) {
   109              theme = THEMES.get(file.getName());
   110          } else {
   111              theme = new Theme(file);
   112  
   113              if (theme.isValidTheme()) {
   114                  THEMES.put(file.getName(), theme);
   115              } else {
   116                  return;
   117              }
   118          }
   119  
   120          if (enabled && !theme.isEnabled()) {
   121              theme.applyTheme();
   122          } else if (theme.isEnabled() && !enabled) {
   123              theme.removeTheme();
   124          }        
   125      }
   126      
   127      /**
   128       * Retrieves a list of available themes.
   129       *
   130       * @return A list of available themes
   131       */    
   132      public static Map<String, Theme> getAvailableThemes() {
                 /* 
    P/P           *  Method: Map getAvailableThemes()
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new HashMap(getAvailableThemes#1)
                  *    new HashMap(getAvailableThemes#1) num objects == 1
                  */
   133          loadThemes();
   134          
   135          synchronized (THEMES) {
   136              return new HashMap<String, Theme>(THEMES);
   137          }
   138      }
   139      
   140      /**
   141       * Retrieves the directory used for storing themes.
   142       * 
   143       * @return The directory used for storing themes
   144       */
   145      public static String getThemeDirectory() {
                 /* 
    P/P           *  Method: String getThemeDirectory()
                  * 
                  *  Postconditions:
                  *    return_value == &amp;java.lang.StringBuilder:toString(...)
                  */
   146          return THEME_DIR;
   147      }
   148  
   149  }








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