1 5 package org.exoplatform.faces.core.component; 6 7 import java.util.* ; 8 import java.io.IOException ; 9 import javax.faces.context.FacesContext; 10 import javax.faces.context.ResponseWriter; 11 import javax.faces.validator.Validator; 12 import org.exoplatform.faces.core.component.model.*; 13 14 20 public class UIRadioBox extends UIInput { 21 private static int VERTICAL_ALIGN = 1 ; 22 private static int HORIZONTAL_ALIGN = 2 ; 23 24 protected String value_ ; 25 private List options_ ; 26 private int align_ ; 27 28 public UIRadioBox(String name, String value) { 29 name_ = name ; 30 value_ = value ; 31 align_ = HORIZONTAL_ALIGN ; 32 } 33 34 public UIRadioBox(String name, String value, List options) { 35 name_ = name ; 36 value_ = value ; 37 options_ = options ; 38 align_ = HORIZONTAL_ALIGN ; 39 } 40 41 final public String getValue() { return value_ ; } 42 43 final public UIRadioBox setValue(String s) { 44 value_ = s ; 45 return this ; 46 } 47 48 final public List getOptions() { return options_ ; } 49 final public UIRadioBox setOptions(List options) { 50 options_ = options ; 51 return this ; 52 } 53 54 final public UIRadioBox setAlign(int val) { 55 align_ = val ; 56 return this ; 57 } 58 59 public UIRadioBox addValidator(Validator validator) { 60 addComponentValidator(validator) ; 61 return this ; 62 } 63 64 public UIRadioBox addValidator(Class clazz) { 65 addComponentValidator(clazz) ; 66 return this ; 67 } 68 69 public void decode(FacesContext context) { 70 if (!editable_ || readonly_) return ; 71 Map paramMap = context.getExternalContext().getRequestParameterMap() ; 72 String value = (String ) paramMap.get(name_) ; 73 if (value != null) { 74 value_ = value ; 75 } 76 } 77 78 final public void processValidators(FacesContext context) { 79 processValidators(context, value_) ; 80 } 81 82 public void encodeBegin(FacesContext context) throws IOException { 83 ResponseWriter w = context.getResponseWriter() ; 84 if(value_ == null) { 85 SelectItem si = (SelectItem) options_.get(0) ; 86 value_ = si.value_ ; 87 } 88 for(int i=0; i < options_.size(); i++) { 89 SelectItem si = (SelectItem) options_.get(i) ; 90 String checked = "" ; 91 if (si.value_.equals(value_)) checked = " checked" ; 92 w.write("<input class='radio' type='radio'"); 93 if (!editable_ || readonly_) { 94 w.write(" readonly='readonly'"); 95 } 96 w.write(checked); w.write(" name='"); w.write(name_); w.write("'") ; 97 w.write(" value='"); w.write(si.value_); 98 w.write("'/>"); 99 w.write(si.display_); 100 if (align_ == VERTICAL_ALIGN) w.write("<br/>"); 101 } 102 } 103 } 104 | Popular Tags |