001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.corelib.components;
016:
017: import org.apache.tapestry.Binding;
018: import org.apache.tapestry.ComponentResources;
019: import org.apache.tapestry.Field;
020: import org.apache.tapestry.MarkupWriter;
021: import org.apache.tapestry.PageRenderSupport;
022: import org.apache.tapestry.RadioContainer;
023: import org.apache.tapestry.annotations.Environmental;
024: import org.apache.tapestry.annotations.Inject;
025: import org.apache.tapestry.annotations.Mixin;
026: import org.apache.tapestry.annotations.Parameter;
027: import org.apache.tapestry.corelib.mixins.DiscardBody;
028: import org.apache.tapestry.corelib.mixins.RenderDisabled;
029: import org.apache.tapestry.corelib.mixins.RenderInformals;
030: import org.apache.tapestry.services.ComponentDefaultProvider;
031:
032: /**
033: * A radio button (i.e., <input type="radio">). Radio buttons <strong>must</strong> operate
034: * within a {@link RadioContainer} (normally, the {@link RadioGroup} component).
035: * <p>
036: * If the value parameter is not bound, then the default value is a property of the container
037: * component whose name matches the Radio component's id.
038: */
039: public class Radio implements Field {
040: @Environmental
041: private RadioContainer _container;
042:
043: /**
044: * The user presentable label for the field. If not provided, a reasonable label is generated
045: * from the component's id, first by looking for a message key named "id-label" (substituting
046: * the component's actual id), then by converting the actual id to a presentable string (for
047: * example, "userId" to "User Id").
048: */
049: @Parameter(defaultPrefix="literal")
050: private String _label;
051:
052: /**
053: * The value associated with this radio button. This is used to determine which radio button
054: * will be selected when the page is rendered, and also becomes the value assigned when the form
055: * is submitted.
056: */
057: @Parameter(required=true,principal=true)
058: private Object _value;
059:
060: @Inject
061: private ComponentDefaultProvider _defaultProvider;
062:
063: @Inject
064: private ComponentResources _resources;
065:
066: @SuppressWarnings("unused")
067: @Mixin
068: private RenderInformals _renderInformals;
069:
070: @SuppressWarnings("unused")
071: @Mixin
072: private RenderDisabled _renderDisabled;
073:
074: @SuppressWarnings("unused")
075: @Mixin
076: private DiscardBody _discardBody;
077:
078: @Inject
079: private PageRenderSupport _pageRenderSupport;
080:
081: private String _clientId;
082:
083: /**
084: * If true, then the field will render out with a disabled attribute (to turn off client-side
085: * behavior). Further, a disabled field ignores any value in the request when the form is
086: * submitted.
087: */
088: @Parameter("false")
089: private boolean _disabled;
090:
091: String defaultLabel() {
092: return _defaultProvider.defaultLabel(_resources);
093: }
094:
095: Binding defaultValue() {
096: return _defaultProvider.defaultBinding("value", _resources);
097: }
098:
099: /**
100: * Returns the element name from the {@link RadioContainer#getElementName() container}.
101: */
102: public String getElementName() {
103: return _container.getElementName();
104: }
105:
106: public String getLabel() {
107: return _label;
108: }
109:
110: /**
111: * Returns true if this component has been expressly disabled (via its disabled parameter), or
112: * if the {@link RadioContainer container} has been disabled.
113: */
114: public boolean isDisabled() {
115: return _disabled || _container.isDisabled();
116: }
117:
118: public String getClientId() {
119: return _clientId;
120: }
121:
122: void beginRender(MarkupWriter writer) {
123: String value = _container.toClient(_value);
124:
125: _clientId = _pageRenderSupport.allocateClientId(_resources
126: .getId());
127:
128: writer.element("input", "type", "radio", "id", _clientId,
129: "name", getElementName(), "value", value);
130:
131: if (_container.isSelected(_value))
132: writer.attributes("checked", "checked");
133: }
134:
135: void afterRender(MarkupWriter writer) {
136: writer.end();
137: }
138: }
|