KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > common > component > description > UIDynamicDescription


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.component.description;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import javax.faces.context.ResponseWriter;
27 import javax.faces.el.ValueBinding;
28
29 import org.alfresco.web.ui.common.Utils;
30 import org.alfresco.web.ui.common.component.SelfRenderingComponent;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 /**
35  * Dynamic description component that switches text based on the events
36  * of another input control
37  *
38  * @author gavinc
39  */

40 public class UIDynamicDescription extends SelfRenderingComponent
41 {
42    private static Log logger = LogFactory.getLog(UIDynamicDescription.class);
43    private String JavaDoc selected;
44    private String JavaDoc functionName;
45    
46    /**
47     * @return The id of the selected description
48     */

49    public String JavaDoc getSelected()
50    {
51       if (this.selected == null)
52       {
53          ValueBinding vb = getValueBinding("selected");
54          if (vb != null)
55          {
56             this.selected = (String JavaDoc)vb.getValue(getFacesContext());
57          }
58       }
59       
60       return this.selected;
61    }
62
63    /**
64     * @param selected The id of the selected
65     */

66    public void setSelected(String JavaDoc selected)
67    {
68       this.selected = selected;
69    }
70    
71    /**
72     * @return Returns the JavaScript function name to use
73     */

74    public String JavaDoc getFunctionName()
75    {
76       if (this.functionName == null)
77       {
78          this.functionName = "itemSelected";
79       }
80       
81       return this.functionName;
82    }
83
84    /**
85     * @param functionName Sets the name of the JavaScript function to use
86     */

87    public void setFunctionName(String JavaDoc functionName)
88    {
89       this.functionName = functionName;
90    }
91
92    /**
93     * @see javax.faces.component.UIComponent#getFamily()
94     */

95    public String JavaDoc getFamily()
96    {
97       return "org.alfresco.faces.DynamicDescription";
98    }
99
100    /**
101     * @see javax.faces.component.UIComponent#encodeBegin(javax.faces.context.FacesContext)
102     */

103    public void encodeBegin(FacesContext context) throws IOException JavaDoc
104    {
105       if (this.isRendered() == false)
106       {
107          return;
108       }
109       
110       // output the required JavaScript
111
ResponseWriter out = context.getResponseWriter();
112       
113       out.write("<script language='JavaScript'>\n");
114       out.write("var m_");
115       out.write(getFunctionName());
116       out.write(" = '");
117       if (getSelected() != null)
118       {
119          out.write("desc-");
120          out.write(getSelected());
121       }
122       out.write("';\n");
123       out.write("function ");
124       out.write(getFunctionName());
125       out.write("(inputControl) {\n");
126       out.write("if (m_");
127       out.write(getFunctionName());
128       out.write(" != '') {\n");
129       out.write(" document.getElementById(m_");
130       out.write(getFunctionName());
131       out.write(").style.display = 'none';\n");
132       out.write("}\nm_");
133       out.write(getFunctionName());
134       out.write(" = 'desc-' + inputControl.value;\n");
135       out.write("document.getElementById(m_");
136       out.write(getFunctionName());
137       out.write(").style.display = 'inline';\n");
138       out.write("} </script>\n");
139    }
140
141    /**
142     * @see javax.faces.component.UIComponent#encodeChildren(javax.faces.context.FacesContext)
143     */

144    public void encodeChildren(FacesContext context) throws IOException JavaDoc
145    {
146       if (this.isRendered() == false)
147       {
148          return;
149       }
150       
151       List JavaDoc<UIComponent> kids = getChildren();
152       for (UIComponent child : kids)
153       {
154          if (child instanceof UIDescription)
155          {
156             // render the single description
157
renderDescription(context, ((UIDescription)child).getControlValue(),
158                   ((UIDescription)child).getText());
159          }
160          else if (child instanceof UIDescriptions)
161          {
162             // retrieve the object being pointed to and get
163
// the descriptions from that
164
renderDescriptions(context, (UIDescriptions)child);
165          }
166       }
167    }
168
169    /**
170     * @see javax.faces.component.UIComponent#encodeEnd(javax.faces.context.FacesContext)
171     */

172    public void encodeEnd(FacesContext context) throws IOException JavaDoc
173    {
174       // don't need to do anything
175
}
176
177    /**
178     * @see javax.faces.component.UIComponent#getRendersChildren()
179     */

180    public boolean getRendersChildren()
181    {
182       return true;
183    }
184    
185    /**
186     * @see javax.faces.component.StateHolder#restoreState(javax.faces.context.FacesContext, java.lang.Object)
187     */

188    public void restoreState(FacesContext context, Object JavaDoc state)
189    {
190       Object JavaDoc values[] = (Object JavaDoc[])state;
191       // standard component attributes are restored by the super class
192
super.restoreState(context, values[0]);
193       this.selected = (String JavaDoc)values[1];
194    }
195    
196    /**
197     * @see javax.faces.component.StateHolder#saveState(javax.faces.context.FacesContext)
198     */

199    public Object JavaDoc saveState(FacesContext context)
200    {
201       Object JavaDoc values[] = new Object JavaDoc[2];
202       // standard component attributes are saved by the super class
203
values[0] = super.saveState(context);
204       values[1] = this.selected;
205       return (values);
206    }
207    
208    /**
209     * Renders a description item
210     *
211     * @param context The faces context
212     * @param controlId The id of the control the description is for
213     * @param test The description text
214     */

215    private void renderDescription(FacesContext context, String JavaDoc controlId, String JavaDoc text)
216       throws IOException JavaDoc
217    {
218       ResponseWriter out = context.getResponseWriter();
219       out.write("<span");
220       String JavaDoc spanId = "desc-" + controlId;
221       outputAttribute(out, spanId, "id");
222       
223       if (controlId.equals(this.selected))
224       {
225          outputAttribute(out, "display: inline", "style");
226       }
227       else
228       {
229          outputAttribute(out, "display: none", "style");
230       }
231       
232       out.write(">");
233       out.write(Utils.encode(text));
234       out.write("</span>\n");
235    }
236    
237    /**
238     * Renders the given descriptions component
239     *
240     * @param context The faces context
241     * @param descriptions The descriptions to render
242     */

243    private void renderDescriptions(FacesContext context, UIDescriptions descriptions)
244       throws IOException JavaDoc
245    {
246       // get hold of the object holding the descriptions and make sure
247
// it is of the correct type
248
Object JavaDoc obj = descriptions.getValue();
249       
250       if (obj instanceof Map JavaDoc)
251       {
252          Map JavaDoc<String JavaDoc, String JavaDoc> items = (Map JavaDoc)obj;
253          for (String JavaDoc id : items.keySet())
254          {
255             renderDescription(context, id, items.get(id));
256          }
257       }
258       else if (obj instanceof List JavaDoc)
259       {
260          Iterator JavaDoc iter = ((List JavaDoc)obj).iterator();
261          while (iter.hasNext())
262          {
263             UIDescription desc = (UIDescription)iter.next();
264             renderDescription(context, desc.getControlValue(), desc.getText());
265          }
266       }
267    }
268 }
269
Popular Tags