1 17 package org.alfresco.web.ui.common.component.description; 18 19 import java.io.IOException ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 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 40 public class UIDynamicDescription extends SelfRenderingComponent 41 { 42 private static Log logger = LogFactory.getLog(UIDynamicDescription.class); 43 private String selected; 44 private String functionName; 45 46 49 public String getSelected() 50 { 51 if (this.selected == null) 52 { 53 ValueBinding vb = getValueBinding("selected"); 54 if (vb != null) 55 { 56 this.selected = (String )vb.getValue(getFacesContext()); 57 } 58 } 59 60 return this.selected; 61 } 62 63 66 public void setSelected(String selected) 67 { 68 this.selected = selected; 69 } 70 71 74 public String getFunctionName() 75 { 76 if (this.functionName == null) 77 { 78 this.functionName = "itemSelected"; 79 } 80 81 return this.functionName; 82 } 83 84 87 public void setFunctionName(String functionName) 88 { 89 this.functionName = functionName; 90 } 91 92 95 public String getFamily() 96 { 97 return "org.alfresco.faces.DynamicDescription"; 98 } 99 100 103 public void encodeBegin(FacesContext context) throws IOException 104 { 105 if (this.isRendered() == false) 106 { 107 return; 108 } 109 110 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 144 public void encodeChildren(FacesContext context) throws IOException 145 { 146 if (this.isRendered() == false) 147 { 148 return; 149 } 150 151 List <UIComponent> kids = getChildren(); 152 for (UIComponent child : kids) 153 { 154 if (child instanceof UIDescription) 155 { 156 renderDescription(context, ((UIDescription)child).getControlValue(), 158 ((UIDescription)child).getText()); 159 } 160 else if (child instanceof UIDescriptions) 161 { 162 renderDescriptions(context, (UIDescriptions)child); 165 } 166 } 167 } 168 169 172 public void encodeEnd(FacesContext context) throws IOException 173 { 174 } 176 177 180 public boolean getRendersChildren() 181 { 182 return true; 183 } 184 185 188 public void restoreState(FacesContext context, Object state) 189 { 190 Object values[] = (Object [])state; 191 super.restoreState(context, values[0]); 193 this.selected = (String )values[1]; 194 } 195 196 199 public Object saveState(FacesContext context) 200 { 201 Object values[] = new Object [2]; 202 values[0] = super.saveState(context); 204 values[1] = this.selected; 205 return (values); 206 } 207 208 215 private void renderDescription(FacesContext context, String controlId, String text) 216 throws IOException 217 { 218 ResponseWriter out = context.getResponseWriter(); 219 out.write("<span"); 220 String 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 243 private void renderDescriptions(FacesContext context, UIDescriptions descriptions) 244 throws IOException 245 { 246 Object obj = descriptions.getValue(); 249 250 if (obj instanceof Map ) 251 { 252 Map <String , String > items = (Map )obj; 253 for (String id : items.keySet()) 254 { 255 renderDescription(context, id, items.get(id)); 256 } 257 } 258 else if (obj instanceof List ) 259 { 260 Iterator iter = ((List )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 |