1 5 package org.exoplatform.faces.core.renderer.html; 6 7 import java.io.IOException ; 8 import java.util.ResourceBundle ; 9 import java.util.List ; 10 import java.util.Map ; 11 12 import javax.faces.component.UIComponent; 13 import javax.faces.event.PhaseId; 14 import javax.faces.context.FacesContext; 15 import javax.faces.context.ResponseWriter; 16 import org.exoplatform.faces.FacesConstants; 17 import org.exoplatform.faces.core.Util; 18 import org.exoplatform.faces.core.component.UIGrid; 19 import org.exoplatform.faces.core.component.UIHiddenInput; 20 import org.exoplatform.faces.core.component.UISimpleForm; 21 import org.exoplatform.faces.core.component.model.FormButton; 22 import org.exoplatform.faces.core.component.model.Row; 23 import org.exoplatform.faces.core.event.ExoActionEvent; 24 import org.exoplatform.commons.utils.ExpressionUtil ; 25 26 public class SimpleFormRenderer extends HtmlBasicRenderer { 27 28 public void decode(FacesContext context, UIComponent component ) { 29 UISimpleForm uiForm = (UISimpleForm) component ; 30 Map paramMap = context.getExternalContext().getRequestParameterMap() ; 31 uiForm.setUserAction((String ) paramMap.get(FacesConstants.ACTION)); 32 String action = uiForm.getUserAction() ; 33 ExoActionEvent event =new ExoActionEvent(uiForm, action, paramMap); 34 if(Util.getActionPhaseId(action) == PhaseId.APPLY_REQUEST_VALUES) { 35 uiForm.broadcast(event) ; 36 } else { 37 uiForm.queueEvent(event) ; 38 } 39 } 40 41 public void encodeChildren( FacesContext context, UIComponent component ) throws IOException { 42 ResponseWriter w = context.getResponseWriter() ; 43 ResourceBundle res = getApplicationResourceBundle(context.getExternalContext()) ; 44 UISimpleForm uiForm = (UISimpleForm) component ; 45 String actionURL = uiForm.getBaseURL(context) ; 46 String formName = uiForm.getFormName() ; 47 String method = uiForm.getMethod() ; 49 List hiddenInputs = uiForm.getHiddenInputs() ; 50 String clazz = uiForm.getClazz() ; 51 if(clazz == null) clazz = "" ; 52 w.write("<form name='"); w.write(formName); w.write("'") ; 53 w.write(" action='"); w.write(actionURL); w.write("'") ; 54 if(method != null){ 55 w.write(" method='"); w.write(method); w.write("'") ; 56 } 57 w.write(" class='UISimpleForm"); 58 if(clazz != null) { 59 w.write(' ') ; w.write(clazz) ; 60 } 61 w.write("'>\n"); 62 w.write("<input type='hidden' name='"); w.write(ACTION); w.write("'") ; 63 w.write(" value=''/>\n") ; 64 for (int i = 0; i < hiddenInputs.size(); i++) { 65 UIHiddenInput uiInput = (UIHiddenInput) hiddenInputs.get(i) ; 66 uiInput.encodeBegin(context) ; 67 } 68 w. write("<table>\n") ; 69 List rows = uiForm.getRows() ; 70 for (int i = 0 ; i < rows.size() ; i++) { 71 Row row = (Row) rows.get(i) ; 72 row.render(w, res, uiForm) ; 73 } 74 w. write("</table>") ; 75 w.write(getScript(uiForm)); 76 w.write("</form>") ; 77 } 78 79 public void renderFormButton(FormButton button, ResourceBundle res, 80 ResponseWriter w, UIGrid uiParent) throws IOException { 81 UISimpleForm uiForm = (UISimpleForm) uiParent ; 82 String label = ExpressionUtil.getExpressionValue(res, button.getLabel()) ; 83 w.write("<a"); 84 if (button.getClazz() != null) { 85 w.write(" class='"); w.write(button.getClazz()); w.write("'"); 86 } else { 87 w.write(" class='"); w.write(getDefaultClass()); w.write("'"); 88 } 89 w.write(" HREF=\"javascript:submit_"); w.write(uiForm.getFormName()); w.write("('"); 90 w.write(button.getAction()) ; w.write("');\">"); 91 w.write(formatText(label)); 92 w.write("</a>"); 93 } 94 95 protected String formatText(String text) { 96 return text ; 97 } 98 99 protected String getDefaultClass() { return "form-link" ; } 100 101 protected String getScript(UISimpleForm uiForm) { 102 return uiForm.getScript() ; 103 } 104 } | Popular Tags |