1 5 package org.exoplatform.faces.core.component; 6 7 import java.util.*; 8 import javax.faces.application.FacesMessage; 9 import javax.faces.context.FacesContext; 10 import javax.faces.event.PhaseId; 11 import javax.faces.validator.Validator; 12 import javax.faces.validator.ValidatorException; 13 import org.exoplatform.faces.application.ExoFacesMessage; 14 import org.exoplatform.faces.core.Util; 15 import org.exoplatform.faces.core.event.ExoActionEvent; 16 22 public class UIForm extends UIExoCommand { 23 public static final String COMPONENT_FAMILY = "org.exoplatform.faces.core.component.UIForm" ; 24 25 private Map mapFields_ ; 26 private List containers_ ; 27 private List buttons_ ; 28 private String formName_ ; 29 protected List fieldValidators_ ; 30 protected List formValidators_ ; 31 32 public UIForm(String name) { 33 setRendererType("FormRenderer") ; 34 mapFields_ = new HashMap(10) ; 35 containers_ = new ArrayList(3) ; 36 buttons_ = new ArrayList(5) ; 37 formName_ = name ; 38 } 39 40 public String getFormName() { return formName_ ; } 41 42 public Container addContainer(String label) { 43 Container container = new Container(label) ; 44 containers_.add(container) ; 45 return container; 46 } 47 48 public Container addContainer(Container block) { 49 containers_.add(block) ; 50 return block ; 51 } 52 53 public void addButton(Button button) { buttons_.add(button) ; } 54 55 public List getContainers() { return containers_ ; } 56 57 public List getActions() { return buttons_ ; } 58 59 public Map getMapFields() { return mapFields_ ; } 60 61 public Field getField(String fieldName) { 62 return ((Field)mapFields_.get(fieldName)) ; 63 } 64 65 public StringField getStringField(String fieldName) { 66 return ((StringField)mapFields_.get(fieldName)) ; 67 } 68 69 public String getStringFieldValue(String fieldName) { 70 return ((StringField)mapFields_.get(fieldName)).getValue() ; 71 } 72 73 public void setStringFieldValue(String fieldName, String value) { 74 ((StringField)mapFields_.get(fieldName)).setValue(value) ; 75 } 76 77 public IntegerField getIntegerField(String fieldName) { 78 return ((IntegerField)mapFields_.get(fieldName)) ; 79 } 80 81 public int getIntegerFieldValue(String fieldName) { 82 return ((IntegerField)mapFields_.get(fieldName)).getIntValue() ; 83 } 84 85 public void setIntegerFieldValue(String fieldName, int value) { 86 ((IntegerField)mapFields_.get(fieldName)).setIntValue(value) ; 87 } 88 89 public void resetFields() { 90 Iterator i = mapFields_.values().iterator() ; 91 while(i.hasNext()) { 92 Object object = i.next() ; 93 if(object instanceof StringField) { 94 StringField field = (StringField) object ; 95 field.setValue("") ; 96 } 97 } 98 } 99 100 public void decode(FacesContext context) { 101 Map paramMap = context.getExternalContext().getRequestParameterMap(); 102 String uicomponent = (String ) paramMap.get(UICOMPONENT) ; 103 if (uicomponent != null && uicomponent.equals(getId())) { 104 String action = (String ) paramMap.get(ACTION) ; 105 if(action != null) { 106 ExoActionEvent event =new ExoActionEvent(this, action); 107 if(Util.getActionPhaseId(action) == PhaseId.APPLY_REQUEST_VALUES) { 108 broadcast(event) ; 109 return ; 110 } 111 InformationProvider iprovider = Util.findInformationProvider(this) ; 112 Iterator i = mapFields_.values().iterator() ; 113 while(i.hasNext()) { 114 Object object = i.next() ; 115 if(object instanceof IntegerField) { 116 IntegerField field = (IntegerField) object ; 117 field.setError(false) ; 118 String value = (String )paramMap.get(field.getName()) ; 119 if(value != null) { 120 try { 121 int intValue = Integer.parseInt(value) ; 122 field.setIntValue(intValue); 123 } catch (NumberFormatException ex) { 124 field.setError(true) ; 125 Object [] args = {value} ; 126 iprovider.addMessage(new ExoFacesMessage("#{UIForm.msg.invalid-integer-format}", args)) ; 127 } 128 field.setValue(value) ; 129 } 130 } else if(object instanceof StringField) { 131 StringField field = (StringField) object ; 132 field.setError(false) ; 133 String value = (String )paramMap.get(field.getName()) ; 134 if(value != null) field.setValue(value) ; 135 } 136 } 137 queueEvent(event) ; 138 } 139 } 140 } 141 142 public void addFieldValidator(Class clazz) { 143 if(fieldValidators_ == null ) fieldValidators_ = new ArrayList(3) ; 144 ComponentUtil.addValidator(fieldValidators_, clazz) ; 145 } 146 147 public void addFormValidator(Class clazz) { 148 if(formValidators_ == null ) formValidators_ = new ArrayList(3) ; 149 ComponentUtil.addValidator(formValidators_, clazz) ; 150 } 151 152 final public void processValidators(FacesContext context) { 153 if(!isRendered()) return ; 154 InformationProvider iprovider = Util.findInformationProvider(this) ; 155 Iterator iterator = mapFields_.values().iterator() ; 157 while(iterator.hasNext()) { 158 Field field = (Field)iterator.next() ; 159 Validator v = field.getValidator() ; 160 try { 161 if(v != null) v.validate(context, this, field) ; 162 } catch(ValidatorException ex) { 163 FacesMessage message = ex.getFacesMessage() ; 164 iprovider.addMessage(message) ; 165 context.renderResponse() ; 166 } 167 if(fieldValidators_ != null) { 168 try { 169 for(int i = 0; i < fieldValidators_.size(); i++) { 170 v = (Validator) fieldValidators_.get(i) ; 171 v.validate(context, this, field) ; 172 } 173 } catch(ValidatorException ex) { 174 FacesMessage message = ex.getFacesMessage() ; 175 iprovider.addMessage(message) ; 176 context.renderResponse() ; 177 } 178 } 179 } 180 181 if(context.getRenderResponse()) return ; 182 if(formValidators_ != null) { 184 Validator validator = null ; 185 try { 186 for(int i = 0; i < formValidators_.size(); i++) { 187 validator = (Validator) formValidators_.get(i) ; 188 validator.validate(context, this, null) ; 189 } 190 } catch(ValidatorException ex) { 191 FacesMessage message = ex.getFacesMessage() ; 192 iprovider.addMessage(message) ; 193 context.renderResponse() ; 194 } 195 } 196 } 197 198 public String getFamily() { return COMPONENT_FAMILY ; } 199 200 static public class Element { 201 protected String label_ ; 202 public Element(String label) { 203 label_ = label ; 204 } 205 206 public String getLabel() { return label_ ; } 207 } 208 public class Container extends Element { 209 private List fields_ = new ArrayList(8); 210 211 public Container(String legend) { super(legend) ; } 212 public String getLegend() { return label_ ; } 213 214 public List getFields() { return fields_ ; } 215 216 public Container add(Field field) { 217 mapFields_.put(field.getName(), field) ; 218 fields_.add(field) ; 219 return this ; 220 } 221 } 222 static public class Field extends Element { 223 protected String name_ ; 224 protected String css_ ; 225 private Validator validator_; 226 private boolean error_ ; 227 private boolean editable_ = true; 228 229 public Field(String name, String label) { 230 super(label) ; 231 name_ = name; 232 } 233 234 public String getName() { return name_ ; } 235 236 public boolean isEditable() { return editable_ ;} 237 public void setEditable(boolean b) { editable_ = b ; } 238 239 public boolean hasError() { return error_ ;} 240 public void setError(boolean b) { error_ = b ; } 241 242 public Validator getValidator() { return validator_ ; } 243 public Field setValidator(Validator v) { 244 validator_ = v ; 245 return this ; 246 } 247 248 public Field setValidator(Class clazz) { 249 validator_ = ComponentUtil.getValidator(clazz) ; 250 return this ; 251 } 252 } 253 static public class StringField extends Field { 254 private String value_ ; 255 256 public StringField(String name, String label, String value) { 257 super(name, label) ; 258 value_ = value ; 259 if(value_ == null) value_ = "" ; 260 } 261 262 public String getValue() { return value_ ; } 263 public void setValue(String value) { value_ = value ; } 264 } 265 static public class StringPasswordField extends StringField { 266 public StringPasswordField(String name, String label, String value) { 267 super(name,label, value) ; 268 } 269 } 270 static public class TextAreaField extends StringField { 271 public TextAreaField(String name, String label, String value) { 272 super(name,label, value) ; 273 } 274 } 275 static public class SelectBoxField extends StringField { 276 private List options_ ; 277 278 public SelectBoxField(String name, String label, String value, List options) { 279 super(name,label, value) ; 280 options_ = options ; 281 } 282 283 public List getOptions() { return options_ ; } 284 public void setOptions(List options) { options_ = options ; } 285 } 286 static public class IntegerField extends StringField { 287 private int intValue_ ; 288 289 public IntegerField(String name, String label, int value) { 290 super(name,label, Integer.toString(value)) ; 291 intValue_ = value ; 292 } 293 294 public int getIntValue() { return intValue_ ;} 295 public void setIntValue(int value) { 296 setValue(Integer.toString(value)) ; 297 intValue_ = value ; 298 } 299 } 300 301 static public class Button extends Element { 302 protected String action_ ; 303 304 public Button(String label, String action) { 305 super(label) ; 306 action_ = action; 307 } 308 309 public String getAction() { return action_ ; } 310 } 311 } | Popular Tags |