001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.forms.element;
025:
026: import java.io.PrintWriter;
027:
028: import org.riotfamily.common.markup.Html;
029: import org.riotfamily.common.markup.TagWriter;
030: import org.riotfamily.forms.AbstractEditorBase;
031: import org.riotfamily.forms.Editor;
032: import org.riotfamily.forms.ErrorUtils;
033: import org.riotfamily.forms.request.FormRequest;
034:
035: /**
036: * A Checkbox widget.
037: */
038: public class Checkbox extends AbstractEditorBase implements Editor {
039:
040: private static final String TYPE_CHECKBOX = "checkbox";
041:
042: private boolean checked;
043:
044: private Object checkedValue = Boolean.TRUE;
045:
046: private Object uncheckedValue = Boolean.FALSE;
047:
048: private boolean checkedByDefault = false;
049:
050: public Checkbox() {
051: setStyleClass(TYPE_CHECKBOX);
052: }
053:
054: /**
055: * Sets the value representing the element's checked state. Defaults to
056: * <code>Boolean.TRUE</code>
057: */
058: public void setCheckedValue(Object checkedValue) {
059: this .checkedValue = checkedValue;
060: }
061:
062: /**
063: * Sets the value representing the element's unchecked state. Defaults to
064: * <code>Boolean.FALSE</code>
065: */
066: public void setUncheckedValue(Object uncheckedValue) {
067: this .uncheckedValue = uncheckedValue;
068: }
069:
070: public void setCheckedByDefault(boolean checkedByDefault) {
071: this .checkedByDefault = checkedByDefault;
072: }
073:
074: public boolean isCheckedByDefault() {
075: return this .checkedByDefault;
076: }
077:
078: public void setChecked(boolean checked) {
079: this .checked = checked;
080: }
081:
082: public boolean isChecked() {
083: return checked;
084: }
085:
086: public void renderInternal(PrintWriter writer) {
087: TagWriter inputTag = new TagWriter(writer);
088: inputTag.startEmpty(Html.INPUT).attribute(Html.INPUT_TYPE,
089: TYPE_CHECKBOX).attribute(Html.INPUT_NAME,
090: getParamName()).attribute(Html.COMMON_ID, getId())
091: .attribute(Html.COMMON_CLASS, getStyleClass())
092: .attribute(Html.INPUT_CHECKED, checked).end();
093: }
094:
095: /**
096: * @see Editor#setValue(Object)
097: */
098: public void setValue(Object value) {
099: if (value != null) {
100: this .checked = checkedValue.equals(value);
101: } else {
102: this .checked = checkedByDefault;
103: }
104: }
105:
106: /**
107: * Returns the checked or unchecked value depending on the element's state.
108: *
109: * @see org.riotfamily.forms.Editor#getValue()
110: * @see #setCheckedValue(Object)
111: * @see #setUncheckedValue(Object)
112: * @see #isChecked()
113: */
114: public Object getValue() {
115: return checked ? checkedValue : uncheckedValue;
116: }
117:
118: public void processRequest(FormRequest request) {
119: Object newValue = request.getParameter(getParamName());
120: checked = newValue != null;
121: validate();
122: }
123:
124: protected void validate() {
125: if (isRequired() && !isChecked()) {
126: ErrorUtils.reject(this , "required");
127: }
128: }
129:
130: }
|