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 java.util.Collection;
018    
019    import org.apache.tapestry.BaseComponent;
020    import org.apache.tapestry.IMarkupWriter;
021    import org.apache.tapestry.IRequestCycle;
022    import org.apache.tapestry.PageRenderSupport;
023    import org.apache.tapestry.TapestryUtils;
024    
025    /**
026     * @author mb
027     * @since 4.0
028     */
029    public abstract class CheckboxGroup extends BaseComponent
030    {
031        public static final String CHECKBOX_GROUP_ATTRIBUTE = "org.apache.tapestry.contrib.form.CheckboxGroup";
032        
033        public abstract Collection getCheckboxNames();
034        public abstract String getFunctionName();
035        public abstract void setFunctionName(String functionName);    
036    
037        public void registerControlledCheckbox(ControlledCheckbox checkbox)
038        {
039            String name = checkbox.getCheckboxName();
040            String form = checkbox.getForm().getName();
041            getCheckboxNames().add(form + "." + name);
042        }
043        
044        /**
045         * @see org.apache.tapestry.BaseComponent#renderComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
046         */
047        protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
048        {
049            Object objOtherGroup = cycle.getAttribute(CHECKBOX_GROUP_ATTRIBUTE);
050            cycle.setAttribute(CHECKBOX_GROUP_ATTRIBUTE, this);
051            
052            initialize(cycle);
053            
054            super.renderComponent(writer, cycle);
055    
056            // clear the registered checkbox names after rendering
057            // allows the component to be used in cycles
058            getCheckboxNames().clear();
059    
060            cycle.setAttribute(CHECKBOX_GROUP_ATTRIBUTE, objOtherGroup);
061        }
062    
063        private void initialize(IRequestCycle cycle)
064        {
065            String functionName = "setCheckboxGroup";
066    
067            if (!cycle.isRewinding()) {
068                PageRenderSupport body = TapestryUtils.getPageRenderSupport(cycle, this);
069                if (body != null)
070                    functionName = body.getUniqueString("setCheckboxGroup");
071            }
072            
073            setFunctionName(functionName);
074        }
075        
076    }