    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     * An alternate {@link org.apache.tapestry5.Block} to render if {@link #test()} is false. The default, null, means
    048     * render nothing in that situation.
    049     */
    050    @Parameter(name = "else", defaultPrefix = BindingConstants.LITERAL)
    051    private Block elseBlock;
    052
    053    private boolean renderTag;
    054
    055    /**
    056     * Returns null if the {@link #test()} is true, which allows normal rendering (of the body). If the test parameter
    057     * is false, returns the else parameter (this may also be null).
    058     */
    059    Object beginRender(MarkupWriter writer)
    060    {
    061        boolean enabled = test();
    062
    063        Block toRender = enabled ? resources.getBody() : elseBlock;
    064
    065        String elementName = resources.getElementName();
    066
    067        if (enabled && elementName != null)
    068        {
    069            renderTag = true;
    070            writer.element(elementName);
    071            resources.renderInformalParameters(writer);
    072        }
    073
    074        return toRender;
    075    }
    076
    077    /**
    078     * If {@link #test()} is true, then the body is rendered, otherwise not. The component does not have a template or
    079     * do any other rendering besides its body (and possibly an element around its body).
    080     */
    081    boolean beforeRenderBody()
    082    {
    083        return false;
    084    }
    085
    086    void afterRender(MarkupWriter writer)
    087    {
    088        if (renderTag)
    089        {
    090            writer.end();
    091            renderTag = false;
    092        }
    093    }
    094
    095
    096}