1 38 39 40 package org.apache.cocoon.faces.samples.carstore; 41 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 45 import javax.faces.component.UIComponent; 46 import javax.faces.component.ValueHolder; 47 import javax.faces.context.FacesContext; 48 import javax.faces.convert.Converter; 49 50 import java.util.Enumeration ; 51 import java.util.ResourceBundle ; 52 53 59 60 public class CarCustomizer extends Object { 61 62 protected static final Log log = LogFactory.getLog(CarCustomizer.class); 63 64 68 private ResourceBundle bundle = null; 69 70 71 public CarCustomizer() { 72 this.init(CarStore.DEFAULT_PACKAGE_PROPERTIES); 73 } 74 75 76 public CarCustomizer(String bundleName) { 77 this.init(bundleName); 78 } 79 80 81 private void init(String bundleName) { 82 FacesContext context = FacesContext.getCurrentInstance(); 83 84 if (log.isDebugEnabled()) { 85 log.debug("Loading bundle: " + bundleName + "."); 86 } 87 bundle = ResourceBundle.getBundle(bundleName); 88 } 89 90 91 private String buttonStyle = null; 92 93 94 public String getButtonStyle() { 95 return buttonStyle; 96 } 97 98 99 public void setButtonStyle(String newButtonStyle) { 100 buttonStyle = newButtonStyle; 101 } 102 103 104 public void customizeCar(CarBean toCustomize) { 105 FacesContext context = FacesContext.getCurrentInstance(); 106 Enumeration keys = bundle.getKeys(); 107 String 108 key = null, 109 disabledStr = null, 110 curSetting = null; 111 Boolean disabled = null; 112 UIComponent component = null; 113 Converter converter = null; 114 Object valueToSet = null; 115 116 while (keys.hasMoreElements()) { 117 key = (String ) keys.nextElement(); 118 if (key == null || -1 != key.indexOf("_")) { 120 continue; 121 } 122 if (null == (curSetting = bundle.getString(key))) { 124 continue; 125 } 126 127 if (null == 129 (component = 130 (UIComponent) toCustomize.getComponents().get(key))) { 131 continue; 132 } 133 134 disabled = null; 136 try { 137 if (null != 138 (disabledStr = bundle.getString(key + "_disabled"))) { 139 disabled = Boolean.valueOf(disabledStr); 140 } 141 } catch (Throwable e) { 142 } 143 if (null != disabled) { 144 component.getAttributes().put("disabled", disabled); 145 } 146 147 if (component instanceof ValueHolder && 150 (null != (converter = 151 ((ValueHolder) component).getConverter()))) { 152 valueToSet = converter.getAsObject(context, component, 153 curSetting); 154 } else { 155 valueToSet = curSetting; 156 } 157 158 if (component instanceof ValueHolder) { 159 ((ValueHolder) component).setValue(valueToSet); 160 } 161 } 162 } 163 } 164 165 166 167 | Popular Tags |