📄 carbean.java
字号:
/* * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */package carstore;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import javax.faces.application.Application;import javax.faces.application.FacesMessage;import javax.faces.component.UIComponent;import javax.faces.component.UIInput;import javax.faces.component.UISelectItems;import javax.faces.component.ValueHolder;import javax.faces.context.FacesContext;import javax.faces.convert.Converter;import javax.faces.model.SelectItem;import java.util.ArrayList;import java.util.Enumeration;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.MissingResourceException;import java.util.ResourceBundle;import java.util.StringTokenizer;/** * <p>This bean encapsulates a car model, including pricing and package * choices. The system allows the user to customize the properties of * this bean with the help of the {@link CarCustomizer}.</p> * * <h3>Data Access</h3> * * <p>This is the only bean in the system that has complicated access to * the persistent store of data. In the present implementation, this * persistent store is in <code>ResourceBundle</code> instances.</p> * * <p>There are three data source <code>ResourceBundle</code> files * used:</p> * * <ol> * * <li><p><code><ModelName></code></p> * * <p>This contains the localized content for this model. There * is a variant of this file for each supported locale, for * example, <code>Jalopy_de.properties</code></p> * * </li> * * <li><p><code><Common_properties></code></p> * * <p>This contains the localized content common to all * models.</p> * * </li> * * <li><p><code><ModelName_options></code></p> * * <p>This contains the non-localized content for this model, * including the non-localized options. There is only one * variant of this file for all locales for example, * <code>Jalopy_options.properties</code></p> * * </li> * * </ol> * * <p>All files conform to the following convention:</p> * * <code><pre> * key * key_componentType * key_valueType * </pre></code> * * <p>Where <code>key</code> is the name of an attribute of this car. * For example, <code>basePrice</code>, or <code>description</code>. * <code>key_componentType</code> is the component type of the * <code>UIComponent</code> subclass to be used to represent this * attribute in the page, for example <code>SelectManyMenu</code>. * <code>key_valueType</code> is the data type of the value of the * <code>UIComponent</code>, for example <code>java.lang.Integer</code>. * For all non-String valueTypes.</p> * * <p>When the bean is instantiated, we load both of the above * properties files and iterate over the keys in each one. For each * key, we look at the <code>componentType</code> and ask the * <code>Application</code> to create a component of that type. We * store that <code>UIComponent</code> instance in our * <code>components</code> <code>Map</code> under the name * <code>key</code>. We look at the <code>valueType</code> for the * <code>key</code>. For non <code>java.lang.String</code> types, we * ask the <code>Application</code> for a <code>Converter</code> * instance for that class. If found, we use it to convert the value * for the <code>key</code> to the appropriate type and store that as * the <code>value</code> of the <code>UIComponent</code> instance.</p> */public class CarBean extends Object { protected static final Log log = LogFactory.getLog(CarBean.class); /** * <p>The message identifier of the Message to be created if * the conversion fails. The message format string for this * message may optionally include a <code>{0}</code> * placeholder, which will be replaced by the object and value.</p> */ public static final String CONVERTER_ERROR_MESSAGE_ID = "carstore.Converter_Error"; // // Relationship Instance Variables // /** * Localized labels */ private ResourceBundle resources = null; /** * Price data */ private ResourceBundle priceData = null; /** * Keys: String attribute name, such as engine. Values: UIComponent * for the attribute */ private Map components = null; /** * Keys: String attribute name, such as engine. Values: String value * of the component named by key in our components Map. */ private Map attributes = null; // // Constructors // public CarBean() { this.init(CarStore.DEFAULT_MODEL_PROPERTIES); } public CarBean(String bundleName) { this.init(bundleName); } /** * <p>Initialize our components <code>Map</code> as described in the * class documentation.</p> * * <p>Create a wrapper <code>Map</code> around the components * <code>Map</code> that exposes the String converted value of each * component.</p> */ private void init(String bundleName) { FacesContext context = FacesContext.getCurrentInstance(); ResourceBundle data = null; Enumeration keys = null; components = new HashMap(); // load the labels resources = ResourceBundle.getBundle(CarStore.CARSTORE_PREFIX + ".bundles.Resources", context.getViewRoot().getLocale()); // load the prices priceData = ResourceBundle.getBundle(CarStore.CARSTORE_PREFIX + ".bundles.OptionPrices"); // populate the locale-specific information if (log.isDebugEnabled()) { log.debug("Loading bundle: " + bundleName + "."); } data = ResourceBundle.getBundle(bundleName, context.getViewRoot().getLocale()); if (log.isDebugEnabled()) { log.debug("Bundle " + bundleName + " loaded. Reading properties..."); } initComponentsFromProperties(context, data); if (log.isDebugEnabled()) { log.debug("done."); } // populate the non-locale-specific information common to all cars if (log.isDebugEnabled()) { log.debug("Loading bundle: Common_options."); } data = ResourceBundle.getBundle(CarStore.CARSTORE_PREFIX + ".bundles.Common_options"); if (log.isDebugEnabled()) { log.debug("Bundle Common_options loaded. Reading properties..."); } initComponentsFromProperties(context, data); if (log.isDebugEnabled()) { log.debug("done."); } // populate the non-locale-specific information specific to each car if (log.isDebugEnabled()) { log.debug("Loading bundle: " + bundleName + "_options."); } data = ResourceBundle.getBundle(bundleName + "_options"); if (log.isDebugEnabled()) { log.debug("Bundle " + bundleName + "_options loaded. Reading properties..."); } initComponentsFromProperties(context, data); if (log.isDebugEnabled()) { log.debug("done."); } // create a read-only Map exposing the values of all of our // components. attributes = new Map() { public void clear() { CarBean.this.components.clear(); } public boolean containsKey(Object key) { return CarBean.this.components.containsKey(key); } public boolean containsValue(Object value) { throw new UnsupportedOperationException(); } public java.util.Set entrySet() { throw new UnsupportedOperationException(); } public boolean equals(Object o) { throw new UnsupportedOperationException(); } public Object get(Object key) { UIComponent component = null; Converter converter = null; Object result = null; if (null == key) { return null; } if (null != (component = (UIComponent) CarBean.this.components.get(key))) { // if this component can have a Converter if (component instanceof ValueHolder) { // try to get it converter = ((ValueHolder) component). getConverter(); result = ((ValueHolder) component).getValue(); } // if we do have a value if (null != result) { // and we do have a converter if (null != converter) { // convert the value to String
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -