1 16 package org.apache.myfaces.taglib; 17 18 import org.apache.myfaces.el.SimpleActionMethodBinding; 19 import org.apache.myfaces.renderkit.JSFAttr; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import javax.faces.component.*; 25 import javax.faces.context.FacesContext; 26 import javax.faces.convert.Converter; 27 import javax.faces.el.MethodBinding; 28 import javax.faces.el.ValueBinding; 29 import javax.faces.event.ActionEvent; 30 import javax.faces.event.ValueChangeEvent; 31 import javax.faces.webapp.UIComponentTag; 32 33 56 public class UIComponentTagUtils 57 { 58 private static final Log log = LogFactory.getLog(UIComponentTagUtils.class); 59 60 private static final Class [] VALIDATOR_ARGS = {FacesContext.class, 61 UIComponent.class, 62 Object .class}; 63 private static final Class [] ACTION_LISTENER_ARGS = {ActionEvent.class}; 64 private static final Class [] VALUE_LISTENER_ARGS = {ValueChangeEvent.class}; 65 66 private UIComponentTagUtils() {} 68 69 public static boolean isValueReference(String v) 70 { 71 return UIComponentTag.isValueReference(v); 72 } 73 74 75 public static void setIntegerProperty(FacesContext context, 76 UIComponent component, 77 String propName, 78 String value) 79 { 80 if (value != null) 81 { 82 if (isValueReference(value)) 83 { 84 ValueBinding vb = context.getApplication().createValueBinding(value); 85 component.setValueBinding(propName, vb); 86 } 87 else 88 { 89 component.getAttributes().put(propName, Integer.valueOf(value)); 91 } 92 } 93 } 94 95 public static void setStringProperty(FacesContext context, 96 UIComponent component, 97 String propName, 98 String value) 99 { 100 if (value != null) 101 { 102 if (isValueReference(value)) 103 { 104 ValueBinding vb = context.getApplication().createValueBinding(value); 105 component.setValueBinding(propName, vb); 106 } 107 else 108 { 109 component.getAttributes().put(propName, value); 111 } 112 } 113 } 114 115 116 public static void setBooleanProperty(FacesContext context, 117 UIComponent component, 118 String propName, 119 String value) 120 { 121 if (value != null) 122 { 123 if (isValueReference(value)) 124 { 125 ValueBinding vb = context.getApplication().createValueBinding(value); 126 component.setValueBinding(propName, vb); 127 } 128 else 129 { 130 component.getAttributes().put(propName, Boolean.valueOf(value)); 132 } 133 } 134 } 135 136 137 public static void setValueProperty(FacesContext context, 138 UIComponent component, 139 String value) 140 { 141 if (value != null) 142 { 143 if (isValueReference(value)) 144 { 145 ValueBinding vb = context.getApplication().createValueBinding(value); 146 component.setValueBinding(JSFAttr.VALUE_ATTR, vb); 147 } 148 else if (component instanceof UICommand) 149 { 150 ((UICommand)component).setValue(value); 151 } 152 else if (component instanceof UIParameter) 153 { 154 ((UIParameter)component).setValue(value); 155 } 156 else if (component instanceof UISelectBoolean) 157 { 158 ((UISelectBoolean)component).setValue(Boolean.valueOf(value)); 159 } 160 else if (component instanceof UIGraphic) 161 { 162 ((UIGraphic)component).setValue(value); 163 } 164 else if (component instanceof ValueHolder) 167 { 168 ((ValueHolder)component).setValue(value); 169 } 170 else 171 { 172 log.error("Component " + component.getClass().getName() + " is no ValueHolder, cannot set value."); 173 } 174 } 175 } 176 177 178 public static void setConverterProperty(FacesContext context, 179 UIComponent component, 180 String value) 181 { 182 if (value != null) 183 { 184 if (component instanceof ValueHolder) 185 { 186 if (isValueReference(value)) 187 { 188 ValueBinding vb = context.getApplication().createValueBinding(value); 189 component.setValueBinding(JSFAttr.CONVERTER_ATTR, vb); 190 } 191 else 192 { 193 FacesContext facesContext = FacesContext.getCurrentInstance(); 194 Converter converter = facesContext.getApplication().createConverter(value); 195 ((ValueHolder)component).setConverter(converter); 196 } 197 } 198 else 199 { 200 log.error("Component " + component.getClass().getName() + " is no ValueHolder, cannot set value."); 201 } 202 } 203 } 204 205 206 public static void setValidatorProperty(FacesContext context, 207 UIComponent component, 208 String validator) 209 { 210 if (validator != null) 211 { 212 if (!(component instanceof EditableValueHolder)) 213 { 214 throw new IllegalArgumentException ("Component " + component.getClientId(context) + " is no EditableValueHolder"); 215 } 216 if (isValueReference(validator)) 217 { 218 MethodBinding mb = context.getApplication().createMethodBinding(validator, 219 VALIDATOR_ARGS); 220 ((EditableValueHolder)component).setValidator(mb); 221 } 222 else 223 { 224 log.error("Invalid expression " + validator); 225 } 226 } 227 } 228 229 public static void setValueBinding(FacesContext context, 230 UIComponent component, 231 String propName, 232 String value) 233 { 234 if (value != null) 235 { 236 if (isValueReference(value)) 237 { 238 ValueBinding vb = context.getApplication().createValueBinding(value); 239 component.setValueBinding(propName, vb); 240 } 241 else 242 { 243 throw new IllegalArgumentException ("Attribute " + propName + " must be a value reference"); 244 } 245 } 246 } 247 248 public static void setActionProperty(FacesContext context, 249 UIComponent component, 250 String action) 251 { 252 if (action != null) 253 { 254 if (!(component instanceof UICommand)) 255 { 256 throw new IllegalArgumentException ("Component " + component.getClientId(context) + " is no UICommand"); 257 } 258 MethodBinding mb; 259 if (isValueReference(action)) 260 { 261 mb = context.getApplication().createMethodBinding(action, null); 262 } 263 else 264 { 265 mb = new SimpleActionMethodBinding(action); 266 } 267 ((UICommand)component).setAction(mb); 268 } 269 } 270 271 public static void setActionListenerProperty(FacesContext context, 272 UIComponent component, 273 String actionListener) 274 { 275 if (actionListener != null) 276 { 277 if (!(component instanceof ActionSource)) 278 { 279 throw new IllegalArgumentException ("Component " + component.getClientId(context) + " is no ActionSource"); 280 } 281 if (isValueReference(actionListener)) 282 { 283 MethodBinding mb = context.getApplication().createMethodBinding(actionListener, 284 ACTION_LISTENER_ARGS); 285 ((ActionSource)component).setActionListener(mb); 286 } 287 else 288 { 289 log.error("Invalid expression " + actionListener); 290 } 291 } 292 } 293 294 public static void setValueChangedListenerProperty(FacesContext context, 295 UIComponent component, 296 String valueChangedListener) 297 { 298 if (valueChangedListener != null) 299 { 300 if (!(component instanceof EditableValueHolder)) 301 { 302 throw new IllegalArgumentException ("Component " + component.getClientId(context) + " is no EditableValueHolder"); 303 } 304 if (isValueReference(valueChangedListener)) 305 { 306 MethodBinding mb = context.getApplication().createMethodBinding(valueChangedListener, 307 VALUE_LISTENER_ARGS); 308 ((EditableValueHolder)component).setValueChangeListener(mb); 309 } 310 else 311 { 312 log.error("Invalid expression " + valueChangedListener); 313 } 314 } 315 } 316 317 } 318 | Popular Tags |