001 // Copyright 2004, 2005 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.form; 016 017 import org.apache.hivemind.ApplicationRuntimeException; 018 import org.apache.tapestry.IForm; 019 import org.apache.tapestry.IMarkupWriter; 020 import org.apache.tapestry.IRequestCycle; 021 import org.apache.tapestry.Tapestry; 022 023 /** 024 * Implements a component that manages an HTML <input type=radio> form element. 025 * Such a component must be wrapped (possibly indirectly) 026 * inside a {@link RadioGroup} component. 027 * 028 * [<a href="../../../../../ComponentReference/Radio.html">Component Reference</a>] 029 * 030 * 031 * <p>{@link Radio} and {@link RadioGroup} are generally not used (except 032 * for very special cases). Instead, a {@link PropertySelection} component is used. 033 * 034 * 035 * @author Howard Lewis Ship 036 * 037 **/ 038 039 public abstract class Radio extends AbstractFormComponent 040 { 041 /** 042 * Renders the form element, or responds when the form containing the element 043 * is submitted (by checking {@link Form#isRewinding()}. 044 * 045 * 046 **/ 047 048 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) 049 { 050 RadioGroup group = RadioGroup.get(cycle); 051 052 if (group == null) 053 throw new ApplicationRuntimeException(Tapestry.getMessage("Radio.must-be-contained-by-group"), 054 this, 055 null, 056 null); 057 058 int option = group.getNextOptionId(); 059 060 setClientId(group.getName()+option); 061 setName(group.getName()); 062 063 writer.beginEmpty("input"); 064 065 writer.attribute("type", "radio"); 066 067 writer.attribute("name", getName()); 068 069 renderIdAttribute(writer, cycle); 070 071 // As the group if the value for this Radio matches the selection 072 // for the group as a whole; if so this is the default radio and is checked. 073 074 if (group.isSelection(getValue())) 075 writer.attribute("checked", "checked"); 076 077 if (isDisabled() || group.isDisabled()) 078 writer.attribute("disabled", "disabled"); 079 080 // The value for the Radio matches the option number (provided by the RadioGroup). 081 // When the form is submitted, the RadioGroup will know which option was, 082 // in fact, selected by the user. 083 084 writer.attribute("value", option); 085 086 renderInformalParameters(writer, cycle); 087 088 writer.closeTag(); 089 } 090 091 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) 092 { 093 RadioGroup group = RadioGroup.get(cycle); 094 095 if (group == null) 096 throw new ApplicationRuntimeException( 097 Tapestry.getMessage("Radio.must-be-contained-by-group"), 098 this, 099 null, 100 null); 101 102 int option = group.getNextOptionId(); 103 104 setClientId(group.getName()+option); 105 setName(group.getName()); 106 107 // If not disabled and this is the selected button within the radio group, 108 // then update set the selection from the group to the value for this 109 // radio button. This will update the selected parameter of the RadioGroup. 110 111 if (!isDisabled() && !group.isDisabled() && group.isSelected(option)) 112 group.updateSelection(getValue()); 113 } 114 115 /** 116 * Overridden to do nothing so that special {@link RadioGroup} semantics are handled properly. 117 * @param form The form to set the name on. 118 */ 119 protected void setName(IForm form) 120 { 121 } 122 123 public abstract boolean isDisabled(); 124 125 public abstract Object getValue(); 126 }