1 5 package org.exoplatform.faces.core.renderer.html; 6 7 import java.io.IOException ; 8 import java.util.List ; 9 import java.util.ResourceBundle ; 10 import javax.faces.component.UIComponent; 11 import javax.faces.context.FacesContext; 12 import javax.faces.context.ResponseWriter; 13 import org.exoplatform.commons.utils.ExpressionUtil; 14 import org.exoplatform.faces.FacesConstants; 15 import org.exoplatform.faces.core.component.UIForm; 16 import org.exoplatform.faces.core.component.model.SelectItem; 17 18 public class FormRenderer extends HtmlBasicRenderer { 19 20 public void encodeChildren( FacesContext context, UIComponent component ) throws IOException { 21 ResponseWriter w = context.getResponseWriter() ; 22 ResourceBundle res = getApplicationResourceBundle(context.getExternalContext()) ; 23 UIForm uiForm = (UIForm) component ; 24 String clazz = uiForm.getClazz() ; 25 if(clazz == null) clazz = "UIForm" ; 26 w.write("<form name='"); w.write(uiForm.getFormName()); w.write("'") ; 28 w.write(" action='"); w.write(uiForm.getBaseURL(context)); w.write("' method='post'") ; 29 w.write(" class='"); w.write(clazz) ; w.write("'>\n"); 30 w.write("<input type='hidden' name='"); w.write(ACTION); w.write("' value=''/>") ; 31 List list = uiForm.getContainers() ; 32 for(int i = 0; i < list.size(); i++) { 33 UIForm.Container container = (UIForm.Container) list.get(i) ; 34 renderContainer(w ,container, res) ; 35 } 36 List actions = uiForm.getActions() ; 37 w.write("<div class='buttons'>") ; 38 for(int i = 0; i < actions.size(); i++) { 39 UIForm.Button button = (UIForm.Button) actions.get(i) ; 40 String label = ExpressionUtil.getExpressionValue(res, button.getLabel()) ; 41 renderFormButton(w,uiForm, label, button.getAction()) ; 42 } 43 w.write("</div>") ; 44 w.write(getNormalScript(uiForm.getFormName())); 45 w.write("</form>") ; 46 } 48 49 private void renderContainer(ResponseWriter w , UIForm.Container container, 50 ResourceBundle res) throws IOException { 51 List fields = container.getFields(); 52 String legend = ExpressionUtil.getExpressionValue(res, container.getLegend()) ; 53 w.write("<fieldset>") ; 54 w. write("<legend>"); w.write(legend); w.write("</legend>") ; 55 w. write("<table>") ; 56 for(int i =0 ; i < fields.size(); i++) { 57 Object obj = fields.get(i) ; 58 w.write("\n<tr>\n") ; 59 if(obj instanceof UIForm.Field) { 60 UIForm.Field field = (UIForm.Field) obj ; 61 String label = ExpressionUtil.getExpressionValue(res, field.getLabel()) ; 62 w.write("<td class='label'>") ;w.write(label) ; w.write("</td>") ; 63 w.write("<td>") ; 64 renderField(w , field, res) ; 65 w.write("</td>") ; 66 } 67 w.write("\n</tr>\n") ; 68 } 69 w. write("</table>") ; 70 w.write("</fieldset>") ; 71 } 72 73 private void renderField(ResponseWriter w , UIForm.Field field, ResourceBundle res) throws IOException { 74 if(field instanceof UIForm.TextAreaField) { 75 UIForm.StringField sfield = (UIForm.StringField) field ; 76 w.write("<textarea name='"); w.write(sfield.getName()) ; w.write("'>") ; 77 w.write(sfield.getValue()) ; 78 w.write("</textarea>"); 79 } else if(field instanceof UIForm.SelectBoxField) { 80 UIForm.SelectBoxField sfield = (UIForm.SelectBoxField) field ; 81 w.write("<select "); w.write("name='"); w.write(sfield.getName()); w.write("'") ; 82 if (!sfield.isEditable()) { 83 w.write(" disabled='true'"); 84 } 85 w.write(">\n") ; 86 List options = sfield.getOptions() ; 87 String selectValue = sfield.getValue() ; 88 for(int i=0; i < options.size(); i++) { 89 SelectItem si = (SelectItem) options.get(i) ; 90 if (si.value_.equals(selectValue)) { 91 w.write("<option selected='selected' value='"); w.write(si.value_); w.write("'>"); 92 w.write(ExpressionUtil.getExpressionValue(res,si.display_)); w.write("</option>\n"); 93 } else { 94 w.write("<option value=\""); w.write(si.value_); w.write("\">"); 95 w.write(ExpressionUtil.getExpressionValue(res,si.display_)); w.write("</option>\n"); 96 } 97 } 98 w.write("</select>\n") ; 99 } else if(field instanceof UIForm.StringPasswordField) { 100 UIForm.StringField sfield = (UIForm.StringField) field ; 101 w.write("<input name='"); w.write(sfield.getName()) ; w.write("' value='") ; 102 w.write(sfield.getValue()) ; w.write("'") ; 103 w.write(" type='password'"); 104 if(!sfield.isEditable()) w.write(" readonly='readonly'"); 105 w.write("/>"); 106 if(sfield.hasError()) w.write("<span style='color:red'>*</span>"); 107 } else if(field instanceof UIForm.StringField) { 108 UIForm.StringField sfield = (UIForm.StringField) field ; 109 w.write("<input name='"); w.write(sfield.getName()) ; w.write("' value='") ; 110 w.write(sfield.getValue()) ; w.write("'") ; 111 if(!sfield.isEditable()) w.write(" readonly='readonly'"); 112 w.write("/>"); 113 if(sfield.hasError()) w.write("<span style='color:red'>*</span>"); 114 } 115 } 116 117 public void renderFormButton(ResponseWriter w, UIForm uiForm, 118 String label, String action) throws IOException { 119 w.write("<a"); 120 w.write(" HREF=\"javascript:submit_"); w.write(uiForm.getFormName()); w.write("('"); 121 w.write(action) ; w.write("');\">"); 122 w.write(label); 123 w.write("</a>"); 124 } 125 126 private String getNormalScript(String formName) { 127 String script = 128 "<script type='text/javascript'>\n" + 129 " //ie bug you cannot have more than one button tag\n" + 130 " function submit_" + formName + "(action) {\n" + 131 " document." + formName + ".elements['" + FacesConstants.ACTION + "'].value = action ;\n" + 132 " document." + formName + ".submit();\n" + 133 " }\n" + 134 "</script>\n"; 135 return script; 136 } 137 } | Popular Tags |