001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or
005: * without modification, are permitted provided that the following
006: * conditions are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above
012: * copyright notice, this list of conditions and the following
013: * disclaimer in the documentation and/or other materials
014: * provided with the distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
025: * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
026: * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
027: * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
028: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
029: * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
030: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
031: * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
032: * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
033: *
034: * You acknowledge that this software is not designed, licensed or
035: * intended for use in the design, construction, operation or
036: * maintenance of any nuclear facility.
037: */
038:
039: package org.apache.cocoon.faces.samples.carstore;
040:
041: import org.apache.commons.logging.Log;
042: import org.apache.commons.logging.LogFactory;
043:
044: import javax.faces.component.UIComponent;
045: import javax.faces.component.ValueHolder;
046: import javax.faces.context.FacesContext;
047: import javax.faces.convert.Converter;
048:
049: import java.util.Enumeration;
050: import java.util.ResourceBundle;
051:
052: /**
053: * <p>A helper class that customizes a CarBean for a set of options
054: * in a package.</p>
055: *
056: * <p>This class reads its settings from a Properties file</p>
057: */
058:
059: public class CarCustomizer extends Object {
060:
061: protected static final Log log = LogFactory
062: .getLog(CarCustomizer.class);
063:
064: //
065: // Relationship Instance Variables
066: //
067:
068: private ResourceBundle bundle = null;
069:
070: public CarCustomizer() {
071: this .init(CarStore.DEFAULT_PACKAGE_PROPERTIES);
072: }
073:
074: public CarCustomizer(String bundleName) {
075: this .init(bundleName);
076: }
077:
078: private void init(String bundleName) {
079: FacesContext context = FacesContext.getCurrentInstance();
080:
081: if (log.isDebugEnabled()) {
082: log.debug("Loading bundle: " + bundleName + ".");
083: }
084: bundle = ResourceBundle.getBundle(bundleName);
085: }
086:
087: private String buttonStyle = null;
088:
089: public String getButtonStyle() {
090: return buttonStyle;
091: }
092:
093: public void setButtonStyle(String newButtonStyle) {
094: buttonStyle = newButtonStyle;
095: }
096:
097: public void customizeCar(CarBean toCustomize) {
098: FacesContext context = FacesContext.getCurrentInstance();
099: Enumeration keys = bundle.getKeys();
100: String key = null, disabledStr = null, curSetting = null;
101: Boolean disabled = null;
102: UIComponent component = null;
103: Converter converter = null;
104: Object valueToSet = null;
105:
106: while (keys.hasMoreElements()) {
107: key = (String) keys.nextElement();
108: // skip null and secondary keys.
109: if (key == null || -1 != key.indexOf("_")) {
110: continue;
111: }
112: // skip null values
113: if (null == (curSetting = bundle.getString(key))) {
114: continue;
115: }
116:
117: // skip null components
118: if (null == (component = (UIComponent) toCustomize
119: .getComponents().get(key))) {
120: continue;
121: }
122:
123: // handle the disabled setting, if necessary
124: disabled = null;
125: try {
126: if (null != (disabledStr = bundle.getString(key
127: + "_disabled"))) {
128: disabled = Boolean.valueOf(disabledStr);
129: }
130: } catch (Throwable e) {
131: }
132: if (null != disabled) {
133: component.getAttributes().put("disabled", disabled);
134: }
135:
136: // set the value
137: // If the component can and does have a converter
138: if (component instanceof ValueHolder
139: && (null != (converter = ((ValueHolder) component)
140: .getConverter()))) {
141: valueToSet = converter.getAsObject(context, component,
142: curSetting);
143: } else {
144: valueToSet = curSetting;
145: }
146:
147: if (component instanceof ValueHolder) {
148: ((ValueHolder) component).setValue(valueToSet);
149: }
150: }
151: }
152: }
|