1 5 package org.exoplatform.faces.core.component; 6 7 import java.util.ArrayList ; 8 import java.util.List ; 9 import java.util.Map ; 10 11 import javax.faces.application.FacesMessage; 12 import javax.faces.component.UIComponent; 13 import javax.faces.context.FacesContext; 14 import javax.faces.validator.Validator; 15 import javax.faces.validator.ValidatorException; 16 import org.exoplatform.container.PortalContainer; 17 import org.exoplatform.faces.FacesConstants; 18 import org.exoplatform.faces.ValidatorManager; 19 import org.exoplatform.faces.core.Util; 20 21 27 public class UISimpleForm extends UIGrid { 28 public static final String COMPONENT_FAMILY = "org.exoplatform.faces.core.component.UISimpleForm" ; 29 final public static String SIMPLE_FORM_RENDERER = "SimpleFormRenderer"; 30 final static public short ALL_MODE = 0 ; 31 final static public short VIEW_MODE = 1 ; 32 final static public short EDIT_MODE = 2 ; 33 34 private List hiddenInputs_; 35 private boolean error_; 36 private String formName_; 37 private String method_; 38 private String formId_; 39 private String userAction_; 40 protected List validators_ ; 41 private String script_; 42 private short mode_ ; 43 44 public UISimpleForm(String name, String method, String formId) { 45 hiddenInputs_ = new ArrayList (3); 46 formName_ = name; 47 method_ = method; 48 formId_ = formId; 49 if (formId_ == null) formId_ = name ; 50 setRendererType(SIMPLE_FORM_RENDERER); 51 setId(name) ; 52 mode_ = EDIT_MODE ; 53 } 54 55 public boolean hasError() { return error_; } 56 public void setError(boolean b) { error_ = b; } 57 58 public String getFormName() { return formName_; } 59 60 public String getMethod() { return method_; } 61 62 public String getFormId() { return formId_; } 63 64 public String getUserAction() { return userAction_; } 65 66 public void setUserAction(String userAction) { userAction_ = userAction; } 67 68 public List getHiddenInputs() { return hiddenInputs_;} 69 70 public String getScript() { 71 if (script_ == null) script_ = getNormalScript(); 72 return script_; 73 } 74 75 public UISimpleForm add(UIHiddenInput input) { 76 getChildren().add(input); 77 hiddenInputs_.add(input); 78 return this; 79 } 80 81 public void addValidator(Validator validator) { 82 if(validators_ == null ) validators_ = new ArrayList (3) ; 83 validators_.add(validator) ; 84 } 85 86 public void addValidator(Class clazz) { 87 if(validators_ == null ) validators_ = new ArrayList (3) ; 88 ComponentUtil.addValidator(validators_, clazz) ; 89 } 90 91 public short getMode() { return mode_ ; } 92 public void setMode(short mode) { 93 List children = getChildren() ; 94 for(int i = 0; i < children.size(); i++) { 95 Object child = children.get(i) ; 96 if(child instanceof UIInput) { 97 UIInput input = (UIInput) child ; 98 if(VIEW_MODE == mode) { 99 input.setReadonly(true) ; 100 mode_ = VIEW_MODE ; 101 } else { 102 input.setReadonly(false) ; 103 mode_ = EDIT_MODE ; 104 } 105 } 106 } 107 } 108 109 public void decode(FacesContext context) { 110 getRenderer(context).decode(context, this) ; 111 } 112 113 public void processDecodes(FacesContext context) { 114 error_ = false; 115 Map paramMap = context.getExternalContext().getRequestParameterMap(); 116 String uicomponent = (String ) paramMap.get(UICOMPONENT); 117 if (!getId().equals(uicomponent)) return; 118 List children = getChildren(); 119 for (int i = 0; i < children.size(); i++) { 120 UIComponent child = (UIComponent) children.get(i); 121 child.processDecodes(context); 122 } 123 decode(context); 124 } 125 126 final public void processValidators(FacesContext context) { 127 if(!isRendered()) return ; 128 List children = getChildren() ; 129 for(int i = 0; i < children.size(); i++) { 130 UIComponent uiChild = (UIComponent) children.get(i) ; 131 if(uiChild.isRendered()) { 132 uiChild.processValidators(context) ; 133 } 134 } 135 if(context.getRenderResponse()) return ; 136 error_ = false ; 137 if(validators_ != null) { 138 Validator validator = null ; 139 try { 140 for(int i = 0; i < validators_.size(); i++) { 141 validator = (Validator) validators_.get(i) ; 142 validator.validate(context, this, null) ; 143 } 144 } catch(ValidatorException ex) { 145 error_ = true ; 146 FacesMessage message = ex.getFacesMessage() ; 147 InformationProvider iprovider = Util.findInformationProvider(this) ; 148 iprovider.addMessage(message) ; 149 context.renderResponse() ; 150 } 151 } 152 } 153 154 private String getNormalScript() { 155 String script = 156 "<script type='text/javascript'>\n" + 157 " //ie bug you cannot have more than one button tag\n" + 158 " function submit_" + formName_ + "(action) {\n" + 159 " document." + formName_ + ".elements['" + FacesConstants.ACTION + "'].value = action ;\n" + 160 " document." + formName_ + ".submit();\n" + 161 " }\n" + 162 "</script>\n"; 163 return script; 164 } 165 166 public void setNoScript() { 167 script_ = ""; 168 } 169 170 public void setEnhancedScript(String taId, String width, String height) { 171 String script = 172 "<script type='text/javascript'>\n" + 173 " _editor_url = '/htmlarea';\n" + 174 " _editor_lang = 'en'; \n" + 175 "</script>" + 176 "<script type='text/javascript' SRC='/htmlarea/htmlarea.js'></script>\n" + 177 "<script type='text/javascript'>\n" + 178 " var editor = null;\n" + 179 " function initEditor() {\n" + 180 " editor = new HTMLArea('" + taId + "');\n" + 181 " var cfg = editor.config; // this is the default configuration\n" + 182 " editor.generate();\n" + 185 " editor._iframe.style.width = \"" + width + "\";" + 186 " editor._textArea.style.width = \"" + width + "\";" + 187 " editor._iframe.style.height = \"" + height + "\";" + 188 " editor._textArea.style.height = \"" + height + "\";" + 189 " }\n" + 190 191 " //ie bug you cannot have more than one button tag\n" + 192 " function submit_" + formName_ + "(action) {\n" + 193 " document." + formName_ + ".elements['" + ACTION + "'].value = action ;\n" + 194 " document." + formName_ + ".onsubmit();\n" + 195 " document." + formName_ + ".submit();\n" + 196 " }\n" + 197 " initEditor() ;\n" + 198 "</script>\n"; 199 script_ = script; 200 } 201 202 public String getFamily() { return COMPONENT_FAMILY ; } 203 204 } | Popular Tags |