    001// Copyright 2009-2013 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.tapestry5.corelib.base;
    016
    017import org.apache.tapestry5.BindingConstants;
    018import org.apache.tapestry5.Block;
    019import org.apache.tapestry5.ComponentResources;
    020import org.apache.tapestry5.MarkupWriter;
    021import org.apache.tapestry5.annotations.Parameter;
    022import org.apache.tapestry5.annotations.SupportsInformalParameters;
    023import org.apache.tapestry5.ioc.annotations.Inject;
    024
    025/**
    026 * Base class for {@link org.apache.tapestry5.corelib.components.If} and {@link org.apache.tapestry5.corelib.components.Unless}.
    027 * Will render its body or the block from its else parameter.  If it renders anything and it has an element name, then
    028 * it renders the element and its informal parameters.
    029 *
    030 * @tapestrydoc
    031 */
    032@SupportsInformalParameters
    033public abstract class AbstractConditional
    034{
    035    @Inject
    036    private ComponentResources resources;
    037
    038    /**
    039     * Performs the test via the parameters; return true to render the body of the component, false to render the else
    040     * block (or nothing).
    041     *
    042     * @return true to render body
    043     */
    044    protected abstract boolean test();
    045
    046    /**
    047     * The {@link org.apache.tapestry5.Block} to render if {@link #test()} is true. The default, null, means
    048     * render the component body in that situation..
    049     */
    050    @Parameter(name = "then", defaultPrefix = BindingConstants.LITERAL)
    051    private Block thenBlock;
    052
    053    /**
    054     * An alternate {@link org.apache.tapestry5.Block} to render if {@link #test()} is false. The default, null, means
    055     * render nothing in that situation.
    056     */
    057    @Parameter(name = "else", defaultPrefix = BindingConstants.LITERAL)
    058    private Block elseBlock;
    059
    060    private boolean renderTag;
    061
    062    /**
    063     * Returns null if the {@link #test()} is true, which allows normal rendering (of the body). If the test parameter
    064     * is false, returns the else parameter (this may also be null).
    065     */
    066    Object beginRender(MarkupWriter writer)
    067    {
    068        boolean enabled = test();
    069
    070        Block toRender = enabled ? (thenBlock == null ? resources.getBody() : thenBlock) : elseBlock;
    071
    072        String elementName = resources.getElementName();
    073
    074        if (enabled && elementName != null)
    075        {
    076            renderTag = true;
    077            writer.element(elementName);
    078            resources.renderInformalParameters(writer);
    079        }
    080
    081        return toRender;
    082    }
    083
    084    /**
    085     * If {@link #test()} is true, then the body is rendered, otherwise not. The component does not have a template or
    086     * do any other rendering besides its body (and possibly an element around its body).
    087     */
    088    boolean beforeRenderBody()
    089    {
    090        return false;
    091    }
    092
    093    void afterRender(MarkupWriter writer)
    094    {
    095        if (renderTag)
    096        {
    097            writer.end();
    098            renderTag = false;
    099        }
    100    }
    101
    102
    103}