KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > faces > core > renderer > html > SimpleFormRenderer


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.faces.core.renderer.html;
6
7 import java.io.IOException JavaDoc;
8 import java.util.ResourceBundle JavaDoc ;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
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 JavaDoc paramMap = context.getExternalContext().getRequestParameterMap() ;
31         uiForm.setUserAction((String JavaDoc) paramMap.get(FacesConstants.ACTION));
32         String JavaDoc 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 JavaDoc {
42     ResponseWriter w = context.getResponseWriter() ;
43     ResourceBundle JavaDoc res = getApplicationResourceBundle(context.getExternalContext()) ;
44     UISimpleForm uiForm = (UISimpleForm) component ;
45     String JavaDoc actionURL = uiForm.getBaseURL(context) ;
46     String JavaDoc formName = uiForm.getFormName() ;
47     //String formId = uiForm.getFormId() ;
48
String JavaDoc method = uiForm.getMethod() ;
49     List JavaDoc hiddenInputs = uiForm.getHiddenInputs() ;
50     String JavaDoc 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 JavaDoc 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 JavaDoc res,
80                                ResponseWriter w, UIGrid uiParent) throws IOException JavaDoc {
81       UISimpleForm uiForm = (UISimpleForm) uiParent ;
82       String JavaDoc 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 JavaDoc formatText(String JavaDoc text) {
96     return text ;
97   }
98   
99   protected String JavaDoc getDefaultClass() { return "form-link" ; }
100   
101   protected String JavaDoc getScript(UISimpleForm uiForm) {
102     return uiForm.getScript() ;
103   }
104 }
Popular Tags