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.contrib.form.checkboxes;
016
017 import org.apache.hivemind.ApplicationRuntimeException;
018 import org.apache.tapestry.BaseComponent;
019 import org.apache.tapestry.IForm;
020 import org.apache.tapestry.IMarkupWriter;
021 import org.apache.tapestry.IRequestCycle;
022 import org.apache.tapestry.form.Checkbox;
023
024 /**
025 * @author mb
026 * @since 4.0
027 */
028 public abstract class ControlledCheckbox extends BaseComponent
029 {
030
031 public abstract CheckboxGroup getGroup();
032
033 public String getCheckboxName()
034 {
035 Checkbox checkbox = (Checkbox) getComponent("checkbox");
036 String name = checkbox.getName();
037 return name;
038 }
039
040 public IForm getForm()
041 {
042 Checkbox checkbox = (Checkbox) getComponent("checkbox");
043 IForm form = checkbox.getForm();
044 return form;
045 }
046
047 /**
048 * @see org.apache.tapestry.BaseComponent#renderComponent(org.apache.tapestry.IMarkupWriter,
049 * org.apache.tapestry.IRequestCycle)
050 */
051 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
052 {
053 super.renderComponent(writer, cycle);
054
055 registerCheckbox();
056 }
057
058 protected void registerCheckbox()
059 {
060 getCheckboxGroup().registerControlledCheckbox(this);
061 }
062
063 public CheckboxGroup getCheckboxGroup()
064 {
065 CheckboxGroup group = getGroup();
066 if (group == null)
067 {
068 IRequestCycle cycle = getPage().getRequestCycle();
069 group = (CheckboxGroup) cycle
070 .getAttribute(CheckboxGroup.CHECKBOX_GROUP_ATTRIBUTE);
071 }
072 if (group == null)
073 throw new ApplicationRuntimeException(
074 "The component "
075 + getExtendedId()
076 + " must be wrapped by a CheckboxGroup or the 'group' parameter must be set.");
077
078 return group;
079 }
080
081 }