KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > element > Checkbox


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms.element;
25
26 import java.io.PrintWriter JavaDoc;
27
28 import org.riotfamily.common.markup.Html;
29 import org.riotfamily.common.markup.TagWriter;
30 import org.riotfamily.forms.AbstractEditorBase;
31 import org.riotfamily.forms.Editor;
32 import org.riotfamily.forms.ErrorUtils;
33 import org.riotfamily.forms.request.FormRequest;
34
35
36 /**
37  * A Checkbox widget.
38  */

39 public class Checkbox extends AbstractEditorBase implements Editor {
40
41     private static final String JavaDoc TYPE_CHECKBOX = "checkbox";
42
43     private boolean checked;
44
45     private Object JavaDoc checkedValue = Boolean.TRUE;
46     
47     private Object JavaDoc uncheckedValue = Boolean.FALSE;
48
49     private boolean checkedByDefault = false;
50     
51     public Checkbox() {
52         setStyleClass(TYPE_CHECKBOX);
53     }
54
55     /**
56      * Sets the value representing the element's checked state. Defaults to
57      * <code>Boolean.TRUE</code>
58      */

59     public void setCheckedValue(Object JavaDoc checkedValue) {
60         this.checkedValue = checkedValue;
61     }
62     
63     /**
64      * Sets the value representing the element's unchecked state. Defaults to
65      * <code>Boolean.FALSE</code>
66      */

67     public void setUncheckedValue(Object JavaDoc uncheckedValue) {
68         this.uncheckedValue = uncheckedValue;
69     }
70     
71     public void setCheckedByDefault(boolean checkedByDefault) {
72         this.checkedByDefault = checkedByDefault;
73     }
74     
75     public boolean isCheckedByDefault() {
76         return this.checkedByDefault;
77     }
78
79     public void setChecked(boolean checked) {
80         this.checked = checked;
81     }
82
83     public boolean isChecked() {
84         return checked;
85     }
86
87     public void renderInternal(PrintWriter JavaDoc writer) {
88         TagWriter inputTag = new TagWriter(writer);
89         inputTag.startEmpty(Html.INPUT)
90                 .attribute(Html.INPUT_TYPE, TYPE_CHECKBOX)
91                 .attribute(Html.INPUT_NAME, getParamName())
92                 .attribute(Html.COMMON_ID, getId())
93                 .attribute(Html.COMMON_CLASS, getStyleClass())
94                 .attribute(Html.INPUT_CHECKED, checked)
95                 .end();
96     }
97
98     /**
99      * @see Editor#setValue(Object)
100      */

101     public void setValue(Object JavaDoc value) {
102         if (value != null) {
103             this.checked = checkedValue.equals(value);
104         }
105         else {
106             this.checked = checkedByDefault;
107         }
108     }
109
110     /**
111      * Returns the checked or unchecked value depending on the element's state.
112      *
113      * @see org.riotfamily.forms.Editor#getValue()
114      * @see #setCheckedValue(Object)
115      * @see #setUncheckedValue(Object)
116      * @see #isChecked()
117      */

118     public Object JavaDoc getValue() {
119         return checked ? checkedValue : uncheckedValue;
120     }
121     
122     public void processRequest(FormRequest request) {
123         Object JavaDoc newValue = request.getParameter(getParamName());
124         checked = newValue != null;
125         validate();
126     }
127     
128     protected void validate() {
129         if (isRequired() && !isChecked()) {
130             ErrorUtils.reject(this, "required");
131         }
132     }
133
134 }
Popular Tags