KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > Radio


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// 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.form;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.tapestry.AbstractComponent;
19 import org.apache.tapestry.IMarkupWriter;
20 import org.apache.tapestry.IRequestCycle;
21 import org.apache.tapestry.Tapestry;
22
23 /**
24  * Implements a component that manages an HTML <input type=radio> form element.
25  * Such a component must be wrapped (possibly indirectly)
26  * inside a {@link RadioGroup} component.
27  *
28  * [<a HREF="../../../../../ComponentReference/Radio.html">Component Reference</a>]
29  *
30  *
31  * <p>{@link Radio} and {@link RadioGroup} are generally not used (except
32  * for very special cases). Instead, a {@link PropertySelection} component is used.
33  *
34  *
35  * @author Howard Lewis Ship
36  *
37  **/

38
39 public abstract class Radio extends AbstractComponent
40 {
41     /**
42      * Renders the form element, or responds when the form containing the element
43      * is submitted (by checking {@link Form#isRewinding()}.
44      *
45      *
46      **/

47
48     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
49     {
50
51         RadioGroup group = RadioGroup.get(cycle);
52         if (group == null)
53             throw new ApplicationRuntimeException(
54                 Tapestry.getMessage("Radio.must-be-contained-by-group"),
55                 this,
56                 null,
57                 null);
58
59         // The group determines rewinding from the form.
60

61         boolean rewinding = group.isRewinding();
62
63         int option = group.getNextOptionId();
64
65         if (rewinding)
66         {
67             // If not disabled and this is the selected button within the radio group,
68
// then update set the selection from the group to the value for this
69
// radio button. This will update the selected parameter of the RadioGroup.
70

71             if (!isDisabled() && !group.isDisabled() && group.isSelected(option))
72                 group.updateSelection(getValue());
73             return;
74         }
75
76         writer.beginEmpty("input");
77
78         writer.attribute("type", "radio");
79
80         writer.attribute("name", group.getName());
81
82         // As the group if the value for this Radio matches the selection
83
// for the group as a whole; if so this is the default radio and is checked.
84

85         if (group.isSelection(getValue()))
86             writer.attribute("checked", "checked");
87
88         if (isDisabled() || group.isDisabled())
89             writer.attribute("disabled", "disabled");
90
91         // The value for the Radio matches the option number (provided by the RadioGroup).
92
// When the form is submitted, the RadioGroup will know which option was,
93
// in fact, selected by the user.
94

95         writer.attribute("value", option);
96
97         renderInformalParameters(writer, cycle);
98
99     }
100
101     public abstract boolean isDisabled();
102
103     public abstract Object JavaDoc getValue();
104 }
Popular Tags