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.controller;
12  
13  import java.util.List;
14  import java.util.prefs.Preferences;
15  import javax.swing.DefaultListModel;
16  import javax.swing.JList;
17  import com.timjohnstondev.unitconverter.Launcher;
18  import com.timjohnstondev.unitconverter.logic.ConversionFactorParser;
19  import com.timjohnstondev.unitconverter.logic.ConversionFactorParserHandler;
20  import com.timjohnstondev.unitconverter.model.PropertyList;
21  
22  /**
23   * The controller in the MVC pattern for this application. Acts and the intermediary between the view components and the
24   * model components.
25   */
26  public class ModelController
27  {
28    private PropertyList properties;
29    private String unitSeparator;
30  
31    /**
32     * Constructs and initializes a {@code ModelController}, pulling all of the model data into a
33     * {@link com.timjohnstondev.unitconverter.model.PropertyList}.
34     */
35    public ModelController()
36    {
37      properties = loadPropertyList();
38    }
39  
40    /**
41     * Returns the {@link com.timjohnstondev.unitconverter.model.Conversion Conversion} factor or formula for the given
42     * {@link com.timjohnstondev.unitconverter.model.Property Property} name for going from one set of units to another.
43     * 
44     * @param property the name of the {@code Property} that will be converted, such as temperature, pressure or length
45     * @param from the original units
46     * @param to the desired units
47     * @return the factor or formula to perform the conversion
48     */
49    public final String getConversion(final String property, final String from, final String to)
50    {
51      return properties.getConversion(property, from, to);
52    }
53  
54    /**
55     * Returns a list of all the {@link com.timjohnstondev.unitconverter.model.Property Property} names in the data
56     * source. The order of the names is determined by the underlying {@link PropertyList}. This list order was initially
57     * based on the order the properties appear in the data source.
58     * 
59     * @return a list of the {@code Property} names
60     */
61    public final List <String> getPropertyNames()
62    {
63      return properties.getPropertyNames();
64    }
65  
66    /**
67     * Returns a list of all the symbols for this {@link com.timjohnstondev.unitconverter.model.Property Property} in the
68     * data source. The order of the symbols is determined by the underlying {@link PropertyList}. This list order was
69     * initially based on the order the symbols appear in the data source for this {@code Property}.
70     * 
71     * @param property the name of the {@code Property} that is the basis for the units
72     * @return a list of the symbols
73     */
74    public final List <String> getSymbols(final String property)
75    {
76      return properties.getSymbols(property);
77    }
78  
79    /**
80     * Returns a list, minus the symbol provided, of the symbols for this
81     * {@link com.timjohnstondev.unitconverter.model.Property Property} in the data source. The order of the symbols is
82     * determined by the underlying {@code Property}. This list order was initially based on the order the symbols appear
83     * in the data source for this {@code Property}.
84     * 
85     * @param property the name of the {@code Property} that is the basis for the units
86     * @param fromUnit the symbol to leave out of the list
87     * @return a list of the symbols
88     */
89    public final List <String> getSymbols(final String property, final String fromUnit)
90    {
91      return properties.getSymbols(property, fromUnit);
92    }
93  
94    /**
95     * Returns this symbol's name in unabbreviated words for this {@link com.timjohnstondev.unitconverter.model.Property
96     * Property}.
97     * 
98     * @param property the name of the {@code Property} that is the basis for this unit
99     * @param unitSymbol the symbol that represents the returned name
100    * @return the name for this symbol
101    */
102   public final String getUnitName(final String property, final String unitSymbol)
103   {
104     return properties.getUnitName(property, unitSymbol);
105   }
106 
107   /**
108    * Returns the unit separator symbol or the default (*) if the value has not been set
109    * 
110    * @return the unit separator symbol or the default (*) if the value has not been set
111    */
112   public final String getUnitSeparator()
113   {
114     return unitSeparator != null ? unitSeparator : "*";
115   }
116 
117   /**
118    * Updates the unit separator symbol
119    * 
120    * @param symbol the new unit separator symbol
121    */
122   public final void setUnitSeparator(final String symbol)
123   {
124     properties = loadPropertyList();
125     properties.setUnitSeparator(symbol);
126     unitSeparator = symbol;
127   }
128 
129   /**
130    * Returns {@code true} if this {@code Property} can do mole to mass and mass to mole unit conversions, else {@code
131    * false}
132    * 
133    * @param propertyName name of the property
134    * @return {@code true} if this {@code Property} can do mole to mass and mass to mole unit conversions, else {@code
135    *         false}
136    */
137   public final boolean usesMoles(final String propertyName)
138   {
139     return properties.usesMoles(propertyName);
140   }
141 
142   private PropertyList loadPropertyList()
143   {
144     final ConversionFactorParserHandler parserHandler = new ConversionFactorParserHandler();
145     (new ConversionFactorParser(parserHandler)).parse();
146     return parserHandler.getPropertyList();
147   }
148 
149   /**
150    * Updates the preference that orders the {@code Property}s in the application.
151    * 
152    * @param list {@code Property} list
153    */
154   public final void setPropertyOrderPreferences(final JList list)
155   {
156     final DefaultListModel model = (DefaultListModel) list.getModel();
157     final Preferences userPref = Preferences.userNodeForPackage(Launcher.class);
158 
159     for (int i = 0; i < model.size(); i++)
160     {
161       final String key = "sortorder." + ((String) model.get(i)).replaceAll(" ", "").toLowerCase();
162       userPref.putInt(key, i + 1);
163     }
164   }
165 }