KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > faces > core > component > UIRadioBox


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.component;
6
7 import java.util.* ;
8 import java.io.IOException JavaDoc ;
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 /**
15  * Wed, Dec 22, 2003 @ 23:14
16  * @author: Tuan Nguyen
17  * @email: tuan08@users.sourceforge.net
18  * @version: $Id: UIRadioBox.java,v 1.5 2004/08/23 15:05:41 benjmestrallet Exp $
19  */

20 public class UIRadioBox extends UIInput {
21   private static int VERTICAL_ALIGN = 1 ;
22   private static int HORIZONTAL_ALIGN = 2 ;
23
24   protected String JavaDoc value_ ;
25   private List options_ ;
26   private int align_ ;
27
28   public UIRadioBox(String JavaDoc name, String JavaDoc value) {
29     name_ = name ;
30     value_ = value ;
31     align_ = HORIZONTAL_ALIGN ;
32   }
33
34   public UIRadioBox(String JavaDoc name, String JavaDoc value, List options) {
35     name_ = name ;
36     value_ = value ;
37     options_ = options ;
38     align_ = HORIZONTAL_ALIGN ;
39   }
40
41   final public String JavaDoc getValue() { return value_ ; }
42
43   final public UIRadioBox setValue(String JavaDoc 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 JavaDoc 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 JavaDoc value = (String JavaDoc) 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 JavaDoc {
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 JavaDoc 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