KickJava   Java API By Example, From Geeks To Geeks.

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


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.IMarkupWriter;
19 import org.apache.tapestry.IRequestCycle;
20 import org.apache.tapestry.Tapestry;
21 import org.apache.tapestry.valid.ValidationStrings;
22 import org.apache.tapestry.valid.ValidatorException;
23
24 /**
25  * A special type of form component that is used to contain {@link Radio}components. The Radio and
26  * {@link Radio}group components work together to update a property of some other object, much like
27  * a more flexible version of a {@link PropertySelection}. [ <a
28  * HREF="../../../../../ComponentReference/RadioGroup.html">Component Reference </a>]
29  *
30  * As of 4.0, RadioGroup can indicate that it is required.
31  *
32  * @author Howard Lewis Ship
33  * @author Paul Ferraro
34  */

35 public abstract class RadioGroup extends AbstractRequirableField
36 {
37     // Cached copy of the value from the selectedBinding
38
private Object JavaDoc _selection;
39
40     // The value from the HTTP request indicating which
41
// Radio was selected by the user.
42
private int _selectedOption;
43
44     private boolean _rewinding;
45
46     private boolean _rendering;
47
48     private int _nextOptionId;
49
50     /**
51      * A <code>RadioGroup</code> places itself into the {@link IRequestCycle}as an attribute, so
52      * that its wrapped {@link Radio}components can identify thier state.
53      */

54
55     private static final String JavaDoc ATTRIBUTE_NAME = "org.apache.tapestry.active.RadioGroup";
56
57     public static RadioGroup get(IRequestCycle cycle)
58     {
59         return (RadioGroup) cycle.getAttribute(ATTRIBUTE_NAME);
60     }
61
62     public int getNextOptionId()
63     {
64         if (!_rendering)
65             throw Tapestry.createRenderOnlyPropertyException(this, "nextOptionId");
66
67         return _nextOptionId++;
68     }
69
70     public boolean isRewinding()
71     {
72         if (!_rendering)
73             throw Tapestry.createRenderOnlyPropertyException(this, "rewinding");
74
75         return _rewinding;
76     }
77
78     /**
79      * Returns true if the value is equal to the current selection for the group. This is invoked by
80      * a {@link Radio}during rendering to determine if it should be marked 'checked'.
81      */

82
83     public boolean isSelection(Object JavaDoc value)
84     {
85         if (!_rendering)
86             throw Tapestry.createRenderOnlyPropertyException(this, "selection");
87
88         if (_selection == value)
89             return true;
90
91         if (_selection == null || value == null)
92             return false;
93
94         return _selection.equals(value);
95     }
96
97     /**
98      * Invoked by the {@link Radio}which is selected to update the property bound to the selected
99      * parameter.
100      */

101
102     public void updateSelection(Object JavaDoc value)
103     {
104         getBinding("selected").setObject(value);
105     }
106
107     /**
108      * Used by {@link Radio}components when rewinding to see if their value was submitted.
109      */

110
111     public boolean isSelected(int option)
112     {
113         return _selectedOption == option;
114     }
115
116     /**
117      * @see org.apache.tapestry.AbstractComponent#prepareForRender(org.apache.tapestry.IRequestCycle)
118      */

119     protected void prepareForRender(IRequestCycle cycle)
120     {
121         if (cycle.getAttribute(ATTRIBUTE_NAME) != null)
122             throw new ApplicationRuntimeException(Tapestry.getMessage("RadioGroup.may-not-nest"),
123                     this, null, null);
124         
125         cycle.setAttribute(ATTRIBUTE_NAME, this);
126
127         _rendering = true;
128         _nextOptionId = 0;
129     }
130
131     /**
132      * @see org.apache.tapestry.AbstractComponent#cleanupAfterRender(org.apache.tapestry.IRequestCycle)
133      */

134     protected void cleanupAfterRender(IRequestCycle cycle)
135     {
136         _rendering = false;
137         _selection = null;
138         
139         cycle.removeAttribute(ATTRIBUTE_NAME);
140     }
141
142     /**
143      * @see org.apache.tapestry.AbstractComponent#finishLoad()
144      */

145     protected void finishLoad()
146     {
147         setRequiredMessage(ValidationStrings.getMessagePattern(ValidationStrings.REQUIRED_SELECT_FIELD, getPage().getLocale()));
148     }
149
150     /**
151      * @see org.apache.tapestry.form.AbstractRequirableField#bind(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
152      */

153     public void bind(IMarkupWriter writer, IRequestCycle cycle) throws ValidatorException
154     {
155         String JavaDoc value = getSubmittedValue(cycle);
156         
157         if (value == null)
158             _selectedOption = -1;
159         else
160             _selectedOption = Integer.parseInt(value);
161         
162         _rewinding = true;
163         
164         renderBody(writer, cycle);
165     }
166
167     /**
168      * @see org.apache.tapestry.form.AbstractRequirableField#renderFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
169      */

170     protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
171     {
172         super.renderFormComponent(writer, cycle);
173
174         _rewinding = false;
175         
176         // For rendering, the Radio components need to know what the current
177
// selection is, so that the correct one can mark itself 'checked'.
178
_selection = getBinding("selected").getObject();
179         
180         renderBody(writer, cycle);
181     }
182 }
Popular Tags