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.components;
016
017import org.apache.hivemind.ApplicationRuntimeException;
018import org.apache.tapestry.IMarkupWriter;
019import org.apache.tapestry.IRequestCycle;
020import org.apache.tapestry.Tapestry;
021import org.apache.tapestry.components.Conditional;
022
023/**
024 *  Represents an alternative whithin a {@link Choose} component. 
025 *  The default alternative is described by the Otherwise component.
026 *
027 *  [<a href="../../../../../../ComponentReference/contrib.When.html">Component Reference</a>]
028 *
029 *  @author David Solis
030 * 
031 **/
032public abstract class When extends Conditional
033{
034    /** Parent of this component. */
035
036    private Choose _choose;
037
038    /**
039     *  Renders its wrapped components only if the condition is true and its parent {@link Choose}
040     *  allows it. In addition, if element is specified, can emulate that HTML element.
041     *
042     **/
043
044    protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
045    {
046        Choose choose = getChoose();
047
048        if (choose == null)
049            throw new ApplicationRuntimeException(
050                Tapestry.getMessage("When.must-be-contained-by-choose"),
051                this,
052                null,
053                null);
054
055        if (!choose.isConditionMet() && getCondition())
056        {
057            choose.setConditionMet(true);
058            super.renderComponent(writer, cycle);
059        }
060    }
061
062    protected boolean evaluateCondition()
063    {
064        return true;
065    }
066
067    public boolean getInvert()
068    {
069        // This component doesn't require invert parameter.
070        return false;
071    }
072
073    /**
074     *  @return Choose
075     */
076    public Choose getChoose()
077    {
078        return _choose;
079    }
080
081    /**
082     *  Sets the choose.
083     *  @param value The choose to set
084     */
085    public void setChoose(Choose value)
086    {
087        _choose = value;
088    }
089
090}