KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > formmodel > BooleanField


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.woody.formmodel;
17
18 import org.apache.cocoon.woody.Constants;
19 import org.apache.cocoon.woody.FormContext;
20 import org.apache.cocoon.woody.event.WidgetEvent;
21 import org.apache.cocoon.woody.event.ValueChangedEvent;
22 import org.apache.cocoon.xml.AttributesImpl;
23 import org.xml.sax.ContentHandler JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25
26 import java.util.Locale JavaDoc;
27
28 /**
29  * A widget to select a boolean value. Usually rendered as a checkbox.
30  *
31  * <p>You may wonder why we don't use a {@link Field} widget with an associated
32  * Boolean Datatype instead. The reason is that many of the features of the Field
33  * widget are overkill for a Boolean: validation is unnecessary (if the field is
34  * not true it is false), the selectionlist associated with a Datatype also
35  * has no purpose here (there would always be only 2 choices: true or false),
36  * and the manner in which the request parameter of this widget is interpreted
37  * is different (missing or empty request parameter means 'false', rather than null value).
38  *
39  * @version $Id: BooleanField.java 30932 2004-07-29 17:35:38Z vgritsenko $
40  */

41 public class BooleanField extends AbstractWidget {
42     // FIXME(SW) : should the initial value be false or null ? This would allow
43
// event listeners to be triggered at bind time.
44
private Boolean JavaDoc value = Boolean.FALSE;
45     private BooleanFieldDefinition definition;
46
47     public BooleanField(BooleanFieldDefinition definition) {
48         this.definition = definition;
49         setLocation(definition.getLocation());
50     }
51
52     public String JavaDoc getId() {
53         return definition.getId();
54     }
55
56     public void readFromRequest(FormContext formContext) {
57         Object JavaDoc oldValue = value;
58         String JavaDoc param = formContext.getRequest().getParameter(getFullyQualifiedId());
59         if (param != null && param.equalsIgnoreCase("true"))
60             value = Boolean.TRUE;
61         else
62             value = Boolean.FALSE;
63         
64         if (value != oldValue) {
65             getForm().addWidgetEvent(new ValueChangedEvent(this, oldValue, value));
66         }
67     }
68
69     /**
70      * Always return <code>true</code> (an action has no validation)
71      *
72      * @todo is there a use case for boolean fields having validators?
73      */

74     public boolean validate(FormContext formContext) {
75         // a boolean field is always valid
76
return true;
77     }
78
79     private static final String JavaDoc BOOLEAN_FIELD_EL = "booleanfield";
80     private static final String JavaDoc VALUE_EL = "value";
81
82     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
83         AttributesImpl fieldAttrs = new AttributesImpl();
84         fieldAttrs.addCDATAAttribute("id", getFullyQualifiedId());
85         contentHandler.startElement(Constants.WI_NS, BOOLEAN_FIELD_EL, Constants.WI_PREFIX_COLON + BOOLEAN_FIELD_EL, fieldAttrs);
86
87         // value element
88
contentHandler.startElement(Constants.WI_NS, VALUE_EL, Constants.WI_PREFIX_COLON + VALUE_EL, Constants.EMPTY_ATTRS);
89         String JavaDoc stringValue = String.valueOf(value != null && value.booleanValue() == true? "true": "false");
90         contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length());
91         contentHandler.endElement(Constants.WI_NS, VALUE_EL, Constants.WI_PREFIX_COLON + VALUE_EL);
92
93         // generate label, help, hint, etc.
94
definition.generateDisplayData(contentHandler);
95
96         contentHandler.endElement(Constants.WI_NS, BOOLEAN_FIELD_EL, Constants.WI_PREFIX_COLON + BOOLEAN_FIELD_EL);
97     }
98
99     public void generateLabel(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
100         definition.generateLabel(contentHandler);
101     }
102
103     public Object JavaDoc getValue() {
104         return value;
105     }
106
107     /**
108      * Sets value of the field. If value is null, it is considered to be false
109      * (see class comment).
110      */

111     public void setValue(Object JavaDoc object) {
112         if (object == null) {
113             object = Boolean.FALSE;
114         }
115         
116         if (!(object instanceof Boolean JavaDoc)) {
117             throw new RuntimeException JavaDoc("Cannot set value of boolean field \"" + getFullyQualifiedId() + "\" to a non-Boolean value.");
118         }
119         
120         Object JavaDoc oldValue = value;
121         value = (Boolean JavaDoc)object;
122         if (value != oldValue) {
123             getForm().addWidgetEvent(new ValueChangedEvent(this, oldValue, value));
124         }
125     }
126     
127     public void broadcastEvent(WidgetEvent event) {
128         this.definition.fireValueChangedEvent((ValueChangedEvent)event);
129     }
130 }
131
Popular Tags