KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package org.apache.myfaces.custom.jslistener;
5
6 import org.apache.myfaces.renderkit.RendererUtils;
7 import org.apache.myfaces.renderkit.html.HTML;
8 import org.apache.myfaces.renderkit.html.HtmlRenderer;
9 import org.apache.myfaces.custom.jsvalueset.HtmlJsValueSet;
10 import org.apache.myfaces.component.html.util.AddResource;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13
14 import javax.faces.application.Application;
15 import javax.faces.component.UIComponent;
16 import javax.faces.component.UINamingContainer;
17 import javax.faces.context.FacesContext;
18 import javax.faces.context.ResponseWriter;
19 import java.io.IOException JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * $Log: JsValueChangeListenerRenderer.java,v $
26  * Revision 1.1 2005/01/04 16:09:57 mmarinschek
27  * ChangeListener now uses renderer
28  *
29  * Revision 1.1 2004/12/19 00:50:55 mmarinschek
30  * JsValueSetTag
31  *
32  *
33  * @author Martin Marinschek (latest modification by $Author: mmarinschek $)
34  * @version $Revision: 1.1 $ $Date: 2005/01/04 16:09:57 $
35  */

36 public class JsValueChangeListenerRenderer
37         extends HtmlRenderer
38 {
39     private static Log log = LogFactory.getLog(JsValueChangeListenerRenderer.class);
40
41     public void encodeEnd(FacesContext facesContext, UIComponent component)
42             throws IOException JavaDoc
43     {
44         RendererUtils.checkParamValidity(facesContext, component, JsValueChangeListener.class);
45
46         UIComponent parent = component.getParent();
47
48         JsValueChangeListener jsValueChangeListener = (JsValueChangeListener) component;
49
50
51         String JavaDoc aFor = jsValueChangeListener.getFor();
52         String JavaDoc expressionValue = jsValueChangeListener.getExpressionValue();
53         String JavaDoc property = jsValueChangeListener.getProperty();
54
55         AddResource.addJavaScriptToHeader(
56                 JsValueChangeListenerTag.class, "JSListener.js", false, getFacesContext());
57
58         if(aFor!=null)
59         {
60             UIComponent forComponent = component.findComponent(aFor);
61
62             String JavaDoc forComponentId = null;
63
64             if (forComponent == null)
65             {
66                 if (log.isInfoEnabled())
67                 {
68                     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)");
69                 }
70                 if (aFor.length() > 0 && aFor.charAt(0) == UINamingContainer.SEPARATOR_CHAR)
71                 {
72                     //absolute id path
73
forComponentId = aFor.substring(1);
74                 }
75                 else
76                 {
77                     //relative id path, we assume a component on the same level as the label component
78
String JavaDoc labelClientId = component.getClientId(getFacesContext());
79                     int colon = labelClientId.lastIndexOf(UINamingContainer.SEPARATOR_CHAR);
80                     if (colon == -1)
81                     {
82                         forComponentId = aFor;
83                     }
84                     else
85                     {
86                         forComponentId = labelClientId.substring(0, colon + 1) + aFor;
87                     }
88                 }
89             }
90             else
91             {
92                 forComponentId = forComponent.getClientId(getFacesContext());
93             }
94
95             expressionValue = expressionValue.replaceAll("\\'","\\\\'");
96             expressionValue = expressionValue.replaceAll("\"","\\\"");
97
98
99             String JavaDoc methodCall = "orgApacheMyfacesJsListenerSetExpressionProperty('"+
100                     parent.getClientId(getFacesContext())+"','"+
101                     forComponentId+"',"+
102                     (property==null?"null":"'"+property+"'")+
103                     ",'"+expressionValue+"');";
104
105
106             callMethod(parent, "onchange",methodCall);
107
108         }
109     }
110
111     private void callMethod(UIComponent uiComponent, String JavaDoc propName, String JavaDoc value)
112     {
113         Object JavaDoc oldValue = uiComponent.getAttributes().get(propName);
114
115         if(oldValue != null)
116         {
117             String JavaDoc oldValueStr = oldValue.toString().trim();
118
119             //check if method call has already been added...
120
if(oldValueStr.indexOf(value)!=-1)
121                 return;
122
123             if(oldValueStr.length()>0 && !oldValueStr.endsWith(";"))
124                 oldValueStr +=";";
125
126             value = oldValueStr + value;
127
128         }
129
130         uiComponent.getAttributes().put(propName, value);
131     }
132
133
134     protected Application getApplication()
135     {
136         return getFacesContext().getApplication();
137     }
138
139     protected FacesContext getFacesContext()
140     {
141         return FacesContext.getCurrentInstance();
142     }
143
144 }
145
Popular Tags