View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts.ui.common.tag;
20  
21  import javax.faces.FacesException;
22  import javax.faces.component.UICommand;
23  import javax.faces.component.UIComponent;
24  import javax.faces.el.MethodBinding;
25  import javax.faces.el.ValueBinding;
26  import javax.faces.webapp.UIComponentTag;
27  
28  import org.springframework.extensions.webscripts.ui.common.ConstantMethodBinding;
29  
30  /**
31   * @author Kevin Roast
32   */
33  public abstract class BaseComponentTag extends UIComponentTag
34  {
35     /**
36      * Helper to set an action property into a command component
37      * 
38      * @param command    Command component
39      * @param action     The action method binding or outcome to set
40      */
41     protected void setActionProperty(UICommand command, String action)
42     {
43        if (action != null)
44        {
45           if (isValueReference(action))
46           {
47              MethodBinding vb = getFacesContext().getApplication().createMethodBinding(action, null);
48              command.setAction(vb);
49           }
50           else
51           {
52              MethodBinding vb = new ConstantMethodBinding(action);
53              command.setAction(vb);
54           }
55        }
56     }
57     
58     /**
59      * Helper to set an action listener property into a command component
60      * 
61      * @param command          Command component
62      * @param actionListener   Action listener method binding
63      */
64     protected void setActionListenerProperty(UICommand command, String actionListener)
65     {
66        if (actionListener != null)
67        {
68           if (isValueReference(actionListener))
69           {
70              MethodBinding vb = getFacesContext().getApplication().createMethodBinding(actionListener, ACTION_CLASS_ARGS);
71              command.setActionListener(vb);
72           }
73           else
74           {
75              throw new FacesException("Action listener method binding incorrectly specified: " + actionListener);
76           }
77        }
78     }
79     
80     /**
81      * Helper method to set a String property value into the component.
82      * Respects the possibility that the property value is a Value Binding.
83      * 
84      * @param component  UIComponent
85      * @param name       property string name
86      * @param value      property string value
87      */
88     protected void setStringProperty(UIComponent component, String name, String value)
89     {
90        if (value != null)
91        {
92           if (isValueReference(value))
93           {
94              ValueBinding vb = getFacesContext().getApplication().createValueBinding(value);
95              component.setValueBinding(name, vb);
96           }
97           else
98           {
99              component.getAttributes().put(name, value);
100          }
101       }
102    }
103    
104    /**
105     * Helper method to set a String value property into the component.
106     * Assumes the that the property value can only be a Value Binding.
107     * 
108     * @param component  UIComponent
109     * @param name       property string name
110     * @param value      property string value binding
111     */
112    protected void setStringBindingProperty(UIComponent component, String name, String value)
113    {
114       if (value != null)
115       {
116          if (isValueReference(value))
117          {
118             ValueBinding vb = getFacesContext().getApplication().createValueBinding(value);
119             component.setValueBinding(name, vb);
120          }
121          else
122          {
123             throw new IllegalArgumentException("Property: '" + name + "' must be a value binding expression.");
124          }
125       }
126    }
127    
128    /**
129     * Helper method to set a static String property into the component.
130     * Assumes the that the property value can only be a static string value.
131     * 
132     * @param component  UIComponent
133     * @param name       property string name
134     * @param value      property string static value
135     */
136    protected void setStringStaticProperty(UIComponent component, String name, String value)
137    {
138       if (value != null)
139       {
140          component.getAttributes().put(name, value);
141       }
142    }
143    
144    /**
145     * Helper method to set a String property as an Integer value into the component.
146     * Respects the possibility that the property value is a Value Binding.
147     * 
148     * @param component  UIComponent
149     * @param name       property string name
150     * @param value      property string value (an Integer will be created)
151     */
152    protected void setIntProperty(UIComponent component, String name, String value)
153    {
154       if (value != null)
155       {
156          if (isValueReference(value))
157          {
158             ValueBinding vb = getFacesContext().getApplication().createValueBinding(value);
159             component.setValueBinding(name, vb);
160          }
161          else
162          {
163             try
164             {
165                component.getAttributes().put(name, Integer.valueOf(value));
166             }
167             catch (NumberFormatException ne)
168             {
169                throw new RuntimeException("Was expecting Int value for property '" + name + "' but passed value: " + value); 
170             }
171          }
172       }
173    }
174    
175    protected void setIntStaticProperty(UIComponent component, String name, String value)
176    {
177       if (value != null)
178       {
179          try
180          {
181             component.getAttributes().put(name, Integer.valueOf(value));
182          }
183          catch (NumberFormatException ne)
184          {
185             throw new RuntimeException("Was expecting Int value for property '" + name + "' but passed value: " + value); 
186          }
187       }
188    }
189    
190    /**
191     * Helper method to set a String property as an Boolean value into the component.
192     * Respects the possibility that the property value is a Value Binding.
193     * 
194     * @param component  UIComponent
195     * @param name       property string name
196     * @param value      property string value (a Boolean will be created)
197     */
198    protected void setBooleanProperty(UIComponent component, String name, String value)
199    {
200       if (value != null)
201       {
202          if (isValueReference(value))
203          {
204             ValueBinding vb = getFacesContext().getApplication().createValueBinding(value);
205             component.setValueBinding(name, vb);
206          }
207          else
208          {
209             component.getAttributes().put(name, Boolean.valueOf(value));
210          }
211       }
212    }
213    
214    protected void setBooleanStaticProperty(UIComponent component, String name, String value)
215    {
216       if (value != null)
217       {
218          component.getAttributes().put(name, Boolean.valueOf(value));
219       }
220    }
221    
222    protected final static Class ACTION_CLASS_ARGS[] = {javax.faces.event.ActionEvent.class};
223 }