View Javadoc

1   package org.fckfaces.util;
2   
3   import java.io.Serializable;
4   import javax.faces.application.Application;
5   import javax.faces.component.UIComponent;
6   import javax.faces.context.FacesContext;
7   import javax.faces.el.MethodBinding;
8   import javax.faces.el.ValueBinding;
9   import javax.faces.event.ActionEvent;
10  import javax.faces.event.ValueChangeEvent;
11  import javax.faces.webapp.UIComponentTag;
12  
13  public class Tags {
14     public static void setString(UIComponent component, String attributeName,
15           String attributeValue) {
16        if (attributeValue == null)
17           return;
18        if (UIComponentTag.isValueReference(attributeValue))
19           setValueBinding(component, attributeName, attributeValue);
20        else
21           component.getAttributes().put(attributeName, attributeValue);
22     }
23  
24     public static void setInteger(UIComponent component, 
25           String attributeName, String attributeValue) {
26        if (attributeValue == null) return;
27        if (UIComponentTag.isValueReference(attributeValue))
28           setValueBinding(component, attributeName, attributeValue);
29        else 
30           component.getAttributes().put(attributeName, 
31                 new Integer(attributeValue));
32     }
33  
34     public static void setBoolean(UIComponent component, 
35           String attributeName, String attributeValue) {
36        if (attributeValue == null) return;
37        if (UIComponentTag.isValueReference(attributeValue))
38           setValueBinding(component, attributeName, attributeValue);
39        else 
40           component.getAttributes().put(attributeName,Boolean.valueOf(attributeValue));
41     }
42     
43     public static void setValueBinding(UIComponent component, String attributeName,
44           String attributeValue) {
45        FacesContext context = FacesContext.getCurrentInstance();
46        Application app = context.getApplication();
47        ValueBinding vb = app.createValueBinding(attributeValue);
48        component.setValueBinding(attributeName, vb);
49     }
50  
51     public static void setActionListener(UIComponent component, String attributeValue) {
52        setMethodBinding(component, "actionListener", attributeValue,
53              new Class[] { ActionEvent.class });      
54     }
55  
56     public static void setValueChangeListener(UIComponent component, 
57           String attributeValue) {
58        setMethodBinding(component, "valueChangeListener", attributeValue,
59              new Class[] { ValueChangeEvent.class });      
60     }
61  
62     public static void setValidator(UIComponent component, 
63           String attributeValue) {
64        setMethodBinding(component, "validator", attributeValue,
65              new Class[] { FacesContext.class, UIComponent.class, Object.class });      
66     }
67  
68     public static void setAction(UIComponent component, String attributeValue) {
69        if (attributeValue == null) return;
70        if (UIComponentTag.isValueReference(attributeValue))
71        {
72           setMethodBinding(component, "action", attributeValue,
73                 new Class[] {});
74        }
75        else
76        {
77           MethodBinding mb = new ActionMethodBinding(attributeValue);
78           component.getAttributes().put("action", mb);         
79        }
80     }
81        
82     public static void setMethodBinding(UIComponent component, String attributeName,
83           String attributeValue, Class[] paramTypes) {
84        if (attributeValue == null)
85           return;
86        if (UIComponentTag.isValueReference(attributeValue)) {
87           FacesContext context = FacesContext.getCurrentInstance();
88           Application app = context.getApplication();
89           MethodBinding mb = app.createMethodBinding(attributeValue, paramTypes);
90           component.getAttributes().put(attributeName, mb);
91        }
92     }     
93     
94     private static class ActionMethodBinding 
95           extends MethodBinding 
96           implements Serializable 
97     {      
98  	   /**
99  	    * 
100 	    */
101 	   private static final long serialVersionUID = 6027374557161316454L;
102 	   private String result;
103    
104       public ActionMethodBinding(String result) { this.result = result; }      
105       public Object invoke(FacesContext context, Object params[]) {
106          return result;
107       }
108       public String getExpressionString() { return result; }
109       public Class getType(FacesContext context) { return String.class; }
110    }
111 }