01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.corelib.components;
16:
17: import org.apache.tapestry.Binding;
18: import org.apache.tapestry.MarkupWriter;
19: import org.apache.tapestry.annotations.AfterRender;
20: import org.apache.tapestry.annotations.BeginRender;
21: import org.apache.tapestry.annotations.Inject;
22: import org.apache.tapestry.annotations.Parameter;
23: import org.apache.tapestry.corelib.base.AbstractField;
24: import org.apache.tapestry.services.FormSupport;
25: import org.apache.tapestry.services.Request;
26:
27: /** A Checkbox component is simply a <input type="checkbox">. */
28: public class Checkbox extends AbstractField {
29: @Inject
30: private Request _request;
31:
32: /**
33: * The value to be read or updated. If not bound, the Checkbox will attempt to edit a property
34: * of its container whose name matches the component's id.
35: */
36: @Parameter(required=true)
37: private boolean _value;
38:
39: Binding defaultValue() {
40: return createDefaultParameterBinding("value");
41: }
42:
43: @BeginRender
44: void begin(MarkupWriter writer) {
45: writer.element("input", "type", "checkbox", "name",
46: getElementName(), "id", getClientId(), "checked",
47: _value ? "checked" : null);
48:
49: getValidationDecorator().insideField(this );
50: }
51:
52: @AfterRender
53: void after(MarkupWriter writer) {
54: writer.end(); // input
55: }
56:
57: @Override
58: protected void processSubmission(FormSupport formSupport,
59: String elementName) {
60: String postedValue = _request.getParameter(elementName);
61:
62: _value = postedValue != null;
63: }
64:
65: }
|