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