KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > jslistener > JsValueChangeListenerTag


1 /*
2  * Copyright (c) 2004 Your Corporation. All Rights Reserved.
3  */

4 package org.apache.myfaces.custom.jslistener;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.apache.myfaces.component.html.util.AddResource;
9 import org.apache.myfaces.custom.jsvalueset.HtmlJsValueSet;
10 import org.apache.myfaces.taglib.UIComponentTagBase;
11
12 import javax.faces.application.Application;
13 import javax.faces.component.UIComponent;
14 import javax.faces.component.UINamingContainer;
15 import javax.faces.context.FacesContext;
16 import javax.faces.el.ValueBinding;
17 import javax.faces.webapp.UIComponentTag;
18 import javax.servlet.jsp.JspException JavaDoc;
19 import javax.servlet.jsp.tagext.Tag JavaDoc;
20 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
21
22 /**
23  * @author Martin Marinschek (latest modification by $Author: matzew $)
24  * @version $Revision: 1.5 $ $Date: 2005/02/18 17:19:30 $
25  * $Log: JsValueChangeListenerTag.java,v $
26  * Revision 1.5 2005/02/18 17:19:30 matzew
27  * added release() to tag clazzes.
28  *
29  * Revision 1.4 2005/01/04 16:09:57 mmarinschek
30  * ChangeListener now uses renderer
31  *
32  * Revision 1.3 2004/12/20 06:33:51 mmarinschek
33  * bugs killed
34  *
35  * Revision 1.2 2004/12/19 00:50:55 mmarinschek
36  * JsValueSetTag
37  *
38  * Revision 1.1 2004/12/17 13:19:10 mmarinschek
39  * new component jsValueChangeListener
40  *
41  *
42  */

43 public class JsValueChangeListenerTag
44         extends UIComponentTagBase
45 {
46
47     public String JavaDoc getComponentType()
48     {
49         return JsValueChangeListener.COMPONENT_TYPE;
50     }
51
52     public String JavaDoc getRendererType()
53     {
54         return "org.apache.myfaces.JsValueChangeListener";
55     }
56
57     private String JavaDoc _for;
58     private String JavaDoc _property;
59     private String JavaDoc _expressionValue;
60     
61     public void release() {
62         super.release();
63         _for=null;
64         _property=null;
65         _expressionValue=null;
66     }
67
68     protected void setProperties(UIComponent component)
69     {
70         super.setProperties(component);
71
72         setStringProperty(component, "for", _for);
73         setStringProperty(component, "property", _property);
74         setStringProperty(component, "expressionValue", _expressionValue);
75     }
76
77     public void setExpressionValue(String JavaDoc expressionValue)
78     {
79         _expressionValue = expressionValue;
80     }
81
82     public void setFor(String JavaDoc aFor)
83     {
84         _for = aFor;
85     }
86
87     public void setProperty(String JavaDoc property)
88     {
89         _property = property;
90     }
91
92     /*
93
94     //private static final Log log = LogFactory.getLog(UpdateActionListenerTag.class);
95
96     private String _for;
97     private String _property;
98     private String _expressionValue;
99     private static Log log = LogFactory.getLog(JsValueChangeListenerTag.class);
100
101     public JsValueChangeListenerTag()
102     {
103     }
104
105     public void setFor(String aFor)
106     {
107         _for = aFor;
108     }
109
110     public void setExpressionValue(String expressionValue)
111     {
112         _expressionValue = expressionValue;
113     }
114
115     public void setProperty(String property)
116     {
117         _property = property;
118     }
119
120
121     public int doStartTag() throws JspException
122     {
123         try
124         {
125             if (_for == null) throw new JspException("for attribute not set");
126             if (_expressionValue == null) throw new JspException("expressionValue attribute not set");
127
128             //Find parent UIComponentTag
129             UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
130             if (componentTag == null)
131             {
132                 throw new JspException("ValueChangeListenerTag has no UIComponentTag ancestor");
133             }
134
135             if (componentTag.getCreated())
136             {
137                 String aFor = getValueOrBinding(_for);
138                 String expressionValue = getValueOrBinding(_expressionValue);
139                 String property = getValueOrBinding(_property);
140
141                 AddResource.addJavaScriptToHeader(
142                         JsValueChangeListenerTag.class, "JSListener.js", false, getFacesContext());
143
144                 //Component was just created, so we add the Listener
145                 UIComponent component = componentTag.getComponentInstance();
146
147                 if(aFor!=null)
148                 {
149                     UIComponent forComponent = component.findComponent(aFor);
150
151                     String forComponentId = null;
152
153                     if (forComponent == null)
154                     {
155                         if (log.isInfoEnabled())
156                         {
157                             log.info("Unable to find component '" + aFor + "' (calling findComponent on component '" + component.getClientId(getFacesContext()) + "') - will try to render component id based on the parent-id (on same level)");
158                         }
159                         if (aFor.length() > 0 && aFor.charAt(0) == UINamingContainer.SEPARATOR_CHAR)
160                         {
161                             //absolute id path
162                             forComponentId = aFor.substring(1);
163                         }
164                         else
165                         {
166                             //relative id path, we assume a component on the same level as the label component
167                             String labelClientId = component.getClientId(getFacesContext());
168                             int colon = labelClientId.lastIndexOf(UINamingContainer.SEPARATOR_CHAR);
169                             if (colon == -1)
170                             {
171                                 forComponentId = aFor;
172                             }
173                             else
174                             {
175                                 forComponentId = labelClientId.substring(0, colon + 1) + aFor;
176                             }
177                         }
178                     }
179                     else
180                     {
181                         forComponentId = forComponent.getClientId(getFacesContext());
182                     }
183
184                     expressionValue = expressionValue.replaceAll("\\'","\\\\'");
185                     expressionValue = expressionValue.replaceAll("\"","\\\"");
186
187
188                     String methodCall = "orgApacheMyfacesJsListenerSetExpressionProperty('"+
189                             component.getClientId(getFacesContext())+"','"+
190                             forComponentId+"',"+
191                             (property==null?"null":"'"+property+"'")+
192                             ",'"+expressionValue+"');";
193
194
195                     callMethod(component, "onchange",methodCall);
196
197                 }
198             }
199
200             return Tag.SKIP_BODY;
201         }
202         catch(JspException ex)
203         {
204             log.error("Exception : ",ex);
205             throw ex;
206         }
207     }
208
209     private String getValueOrBinding(String valueOrBinding)
210     {
211         if(valueOrBinding == null)
212             return null;
213
214         String value = valueOrBinding;
215
216         if (UIComponentTag.isValueReference(valueOrBinding))
217         {
218             ValueBinding binding = getApplication().createValueBinding(valueOrBinding);
219             Object val = binding.getValue(getFacesContext());
220             value = (val==null?"":val.toString());
221         }
222
223         return value;
224     }
225
226     private void callMethod(UIComponent uiComponent, String propName, String value)
227     {
228         Object oldValue = uiComponent.getAttributes().get(propName);
229
230         if(oldValue != null)
231         {
232             String oldValueStr = oldValue.toString().trim();
233
234             //check if method call has already been added...
235             if(oldValueStr.indexOf(value)!=-1)
236                 return;
237
238             if(oldValueStr.length()>0 && !oldValueStr.endsWith(";"))
239                 oldValueStr +=";";
240
241             value = oldValueStr + value;
242
243         }
244
245         uiComponent.getAttributes().put(propName, value);
246     }
247
248     protected Application getApplication()
249     {
250         return getFacesContext().getApplication();
251     }
252
253     protected FacesContext getFacesContext()
254     {
255         return FacesContext.getCurrentInstance();
256     } */

257 }
258
259
260
Popular Tags