KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > common > tag > BaseComponentTag


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.ui.common.tag;
18
19 import javax.faces.FacesException;
20 import javax.faces.component.UICommand;
21 import javax.faces.component.UIComponent;
22 import javax.faces.el.MethodBinding;
23 import javax.faces.el.ValueBinding;
24 import javax.faces.webapp.UIComponentTag;
25
26 import org.alfresco.web.ui.common.ConstantMethodBinding;
27
28 /**
29  * @author Kevin Roast
30  */

31 public abstract class BaseComponentTag extends UIComponentTag
32 {
33    /**
34     * Helper to set an action property into a command component
35     *
36     * @param command Command component
37     * @param action The action method binding or outcome to set
38     */

39    protected void setActionProperty(UICommand command, String JavaDoc action)
40    {
41       if (action != null)
42       {
43          if (isValueReference(action))
44          {
45             MethodBinding vb = getFacesContext().getApplication().createMethodBinding(action, null);
46             command.setAction(vb);
47          }
48          else
49          {
50             MethodBinding vb = new ConstantMethodBinding(action);
51             command.setAction(vb);
52          }
53       }
54    }
55    
56    /**
57     * Helper to set an action listener property into a command component
58     *
59     * @param command Command component
60     * @param actionListener Action listener method binding
61     */

62    protected void setActionListenerProperty(UICommand command, String JavaDoc actionListener)
63    {
64       if (actionListener != null)
65       {
66          if (isValueReference(actionListener))
67          {
68             MethodBinding vb = getFacesContext().getApplication().createMethodBinding(actionListener, ACTION_CLASS_ARGS);
69             command.setActionListener(vb);
70          }
71          else
72          {
73             throw new FacesException("Action listener method binding incorrectly specified: " + actionListener);
74          }
75       }
76    }
77    
78    /**
79     * Helper method to set a String property value into the component.
80     * Respects the possibility that the property value is a Value Binding.
81     *
82     * @param component UIComponent
83     * @param name property string name
84     * @param value property string value
85     */

86    protected void setStringProperty(UIComponent component, String JavaDoc name, String JavaDoc value)
87    {
88       if (value != null)
89       {
90          if (isValueReference(value))
91          {
92             ValueBinding vb = getFacesContext().getApplication().createValueBinding(value);
93             component.setValueBinding(name, vb);
94          }
95          else
96          {
97             component.getAttributes().put(name, value);
98          }
99       }
100    }
101    
102    /**
103     * Helper method to set a String value property into the component.
104     * Assumes the that the property value can only be a Value Binding.
105     *
106     * @param component UIComponent
107     * @param name property string name
108     * @param value property string value binding
109     */

110    protected void setStringBindingProperty(UIComponent component, String JavaDoc name, String JavaDoc value)
111    {
112       if (value != null)
113       {
114          if (isValueReference(value))
115          {
116             ValueBinding vb = getFacesContext().getApplication().createValueBinding(value);
117             component.setValueBinding(name, vb);
118          }
119          else
120          {
121             throw new IllegalArgumentException JavaDoc("Property: '" + name + "' must be a value binding expression.");
122          }
123       }
124    }
125    
126    /**
127     * Helper method to set a static String property into the component.
128     * Assumes the that the property value can only be a static string value.
129     *
130     * @param component UIComponent
131     * @param name property string name
132     * @param value property string static value
133     */

134    protected void setStringStaticProperty(UIComponent component, String JavaDoc name, String JavaDoc value)
135    {
136       if (value != null)
137       {
138          component.getAttributes().put(name, value);
139       }
140    }
141    
142    /**
143     * Helper method to set a String property as an Integer value into the component.
144     * Respects the possibility that the property value is a Value Binding.
145     *
146     * @param component UIComponent
147     * @param name property string name
148     * @param value property string value (an Integer will be created)
149     */

150    protected void setIntProperty(UIComponent component, String JavaDoc name, String JavaDoc value)
151    {
152       if (value != null)
153       {
154          if (isValueReference(value))
155          {
156             ValueBinding vb = getFacesContext().getApplication().createValueBinding(value);
157             component.setValueBinding(name, vb);
158          }
159          else
160          {
161             try
162             {
163                component.getAttributes().put(name, Integer.valueOf(value));
164             }
165             catch (NumberFormatException JavaDoc ne)
166             {
167                throw new RuntimeException JavaDoc("Was expecting Int value for property '" + name + "' but passed value: " + value);
168             }
169          }
170       }
171    }
172    
173    protected void setIntStaticProperty(UIComponent component, String JavaDoc name, String JavaDoc value)
174    {
175       if (value != null)
176       {
177          try
178          {
179             component.getAttributes().put(name, Integer.valueOf(value));
180          }
181          catch (NumberFormatException JavaDoc ne)
182          {
183             throw new RuntimeException JavaDoc("Was expecting Int value for property '" + name + "' but passed value: " + value);
184          }
185       }
186    }
187    
188    /**
189     * Helper method to set a String property as an Boolean value into the component.
190     * Respects the possibility that the property value is a Value Binding.
191     *
192     * @param component UIComponent
193     * @param name property string name
194     * @param value property string value (a Boolean will be created)
195     */

196    protected void setBooleanProperty(UIComponent component, String JavaDoc name, String JavaDoc value)
197    {
198       if (value != null)
199       {
200          if (isValueReference(value))
201          {
202             ValueBinding vb = getFacesContext().getApplication().createValueBinding(value);
203             component.setValueBinding(name, vb);
204          }
205          else
206          {
207             component.getAttributes().put(name, Boolean.valueOf(value));
208          }
209       }
210    }
211    
212    protected void setBooleanStaticProperty(UIComponent component, String JavaDoc name, String JavaDoc value)
213    {
214       if (value != null)
215       {
216          component.getAttributes().put(name, Boolean.valueOf(value));
217       }
218    }
219    
220    protected final static Class JavaDoc ACTION_CLASS_ARGS[] = {javax.faces.event.ActionEvent.class};
221 }
222
Popular Tags