01: package org.andromda.cartridges.jsf.utils;
02:
03: import javax.faces.component.UICommand;
04: import javax.faces.component.UIComponent;
05: import javax.faces.component.UIGraphic;
06: import javax.faces.component.UIParameter;
07: import javax.faces.component.UISelectBoolean;
08: import javax.faces.component.ValueHolder;
09: import javax.faces.context.FacesContext;
10: import javax.faces.webapp.UIComponentTag;
11:
12: /**
13: * Utilities for dealing with the JSF components.
14: *
15: * @author Chad Brandon
16: */
17: public class ComponentUtils {
18: /**
19: * Sets the value property of a component.
20: *
21: * @param context the current faces context.
22: * @param component the component.
23: * @param value the value to set.
24: */
25: public static void setValueProperty(final FacesContext context,
26: final UIComponent component, final String value) {
27: if (value != null) {
28: if (UIComponentTag.isValueReference(value)) {
29: final javax.faces.el.ValueBinding binding = context
30: .getApplication().createValueBinding(value);
31: component.setValueBinding("value", binding);
32: } else if (component instanceof UICommand) {
33: ((UICommand) component).setValue(value);
34: } else if (component instanceof UIParameter) {
35: ((UIParameter) component).setValue(value);
36: } else if (component instanceof UISelectBoolean) {
37: ((UISelectBoolean) component).setValue(Boolean
38: .valueOf(value));
39: } else if (component instanceof UIGraphic) {
40: ((UIGraphic) component).setValue(value);
41: } else if (component instanceof ValueHolder) {
42: ((ValueHolder) component).setValue(value);
43: }
44: }
45: }
46:
47: /**
48: * Sets the property with the given <code>name</code> of a component.
49: * @param name the name of the component to set.
50: * @param context the current faces context.
51: * @param component the component.
52: * @param value the value to set.
53: */
54: public static void setStringProperty(final String name,
55: final FacesContext context, final UIComponent component,
56: final String value) {
57: if (value != null) {
58: if (UIComponentTag.isValueReference(value)) {
59: final javax.faces.el.ValueBinding binding = context
60: .getApplication().createValueBinding(value);
61: component.setValueBinding(name, binding);
62: } else {
63: component.getAttributes().put(name, value);
64: }
65: }
66: }
67:
68: /**
69: * Sets the boolean value of property with the given <code>name</code> of a component.
70: * @param name the name of the component to set.
71: * @param context the current faces context.
72: * @param component the component.
73: * @param value the value to set.
74: */
75: public static void setBooleanProperty(final String name,
76: final FacesContext context, final UIComponent component,
77: final String value) {
78: if (value != null) {
79: if (UIComponentTag.isValueReference(value)) {
80: final javax.faces.el.ValueBinding binding = context
81: .getApplication().createValueBinding(value);
82: component.setValueBinding(name, binding);
83: } else {
84: component.getAttributes().put(name,
85: Boolean.valueOf(value));
86: }
87: }
88: }
89: }
|