View Javadoc

1   /**
2    * Copyright 2009 Timothy Johnston Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
3    * file except in compliance with the License. You may obtain a copy of the License at
4    * 
5    * http://www.apache.org/licenses/LICENSE-2.0
6    * 
7    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8    * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9    * specific language governing permissions and limitations under the License.
10   */
11  package com.timjohnstondev.unitconverter.logic;
12  
13  import java.util.ResourceBundle;
14  import java.util.prefs.Preferences;
15  import javax.swing.ListModel;
16  import com.timjohnstondev.unitconverter.Launcher;
17  
18  /**
19   * Utility class that contains all of the logic for maintaining the user preferences.
20   */
21  public final class ConfigurationLogic
22  {
23    private static final ResourceBundle RESOURCES = ResourceBundle
24        .getBundle("com.timjohnstondev.unitconverter.view.View");
25  
26    private ConfigurationLogic()
27    {}
28  
29    /**
30     * Returns the index of the item provided in the {@code ListModel}.
31     * 
32     * @param model list
33     * @param unitSeparator separator
34     * @return index
35     */
36    public static int getPreferenceIndex(final ListModel model, final String unitSeparator)
37    {
38      int sepIndex = 0;
39      for (int i = 0; i < model.getSize(); i++)
40      {
41        if (model.getElementAt(i).equals(unitSeparator))
42        {
43          sepIndex = i;
44        }
45      }
46      return sepIndex;
47    }
48  
49    /**
50     * Looks into the JVM preference storage location for the unit separator. If the unit separator symbol setting is
51     * found, it is used, else the default is used.
52     * 
53     * @return the unit separator symbol stored in the JVM preference storage or the default
54     */
55    public static String getUnitSeparatorPreference()
56    {
57      final Preferences userPref = Preferences.userNodeForPackage(Launcher.class);
58      final String prefValue = userPref.get(RESOURCES.getString("UnitConverter.prefKeyUnitSeparator"), "");
59      final String unitSeparator = prefValue != null ? prefValue : "\"*\"";
60      return unitSeparator;
61    }
62  
63    /**
64     * Puts a unit separator symbol preference into the JVM preference storage location.
65     * 
66     * @param symbol unit separator symbol
67     */
68    public static void setUnitSeparatorPreference(final String symbol)
69    {
70      final Preferences userPref = Preferences.userNodeForPackage(Launcher.class);
71      userPref.put(RESOURCES.getString("UnitConverter.prefKeyUnitSeparator"), symbol);
72    }
73  
74    /**
75     * Looks into the JVM preference storage location for the {@code Font} size. If the {@code Font} size setting is
76     * found, it is used, else the system default is used.
77     * 
78     * @param defaultFontSize the default font size
79     * @return the {@code Font} size stored in the JVM preference storage or the system default
80     */
81    public static int getFontSizePreference(final int defaultFontSize)
82    {
83      final Preferences userPref = Preferences.userNodeForPackage(Launcher.class);
84      final String prefValue = userPref.get(RESOURCES.getString("UnitConverter.prefKeyFontSize"), "");
85      int fontSize = defaultFontSize;
86  
87      try
88      {
89        fontSize = Integer.parseInt(prefValue);
90      }
91      catch (NumberFormatException e)
92      {
93        e.getMessage();
94      }
95      return fontSize;
96    }
97  
98    /**
99     * Puts a font size preference into the JVM preference storage location.
100    * 
101    * @param fontSize the font size
102    */
103   public static void setFontSizePreference(final int fontSize)
104   {
105     final Preferences userPref = Preferences.userNodeForPackage(Launcher.class);
106     userPref.put(RESOURCES.getString("UnitConverter.prefKeyFontSize"), String.valueOf(fontSize));
107   }
108 }