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.components;
016
017import org.apache.hivemind.util.Defense;
018import org.apache.tapestry.AbstractComponent;
019import org.apache.tapestry.IComponent;
020import org.apache.tapestry.IMarkupWriter;
021import org.apache.tapestry.IRequestCycle;
022
023/**
024 * Prevents its contents from being rendered until triggered by an {@link RenderBlock} component. [<a
025 * href="../../../../../ComponentReference/Block.html">Component Reference</a>]
026 * <p>
027 * Block and {@link RenderBlock} are used to build a certain class of complicated component that
028 * can't be assembled using the normal wrapping containment. Such a super component would have two
029 * or more sections that need to be supplied by the containing page (or component).
030 * <p>
031 * Using Blocks, the blocks can be provided as parameters to the super component.
032 * <p>
033 * The invoker property gives the components inside the block access to the component (typically an
034 * {@link RenderBlock}) that rendered the block. More often, the {@link #getParameter(String)}
035 * method is used to get parameters <em>of the invoking component</em>.
036 * 
037 * @author Howard Lewis Ship
038 * @since 0.2.9
039 */
040
041public abstract class Block extends AbstractComponent
042{
043    private IComponent _invoker;
044
045    /**
046     * Provides access to the invoking component's parameters.
047     * 
048     * @since 4.0
049     */
050
051    public Object getParameter(String name)
052    {
053        return _invoker.getBinding(name).getObject();
054    }
055
056    public void renderForComponent(IMarkupWriter writer, IRequestCycle cycle, IComponent invoker)
057    {
058        Defense.notNull(invoker, "invoker");
059
060        IComponent oldInvoker = _invoker;
061
062        try
063        {
064            _invoker = invoker;
065            renderBody(writer, cycle);
066        }
067        finally
068        {
069            _invoker = oldInvoker;
070
071        }
072    }
073
074    /**
075     * Does nothing; the idea of a Block is to defer the rendering of the body of the block until an
076     * {@link RenderBlock} forces it out.
077     */
078
079    protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
080    {
081        // Nothing!
082    }
083
084    /**
085     * @deprecated Use {@link #getInvoker()} instead.
086     */
087    public IComponent getInserter()
088    {
089        return _invoker;
090    }
091
092    /**
093     * Returns the object which invoked this Block's
094     * {@link #renderForComponent(IMarkupWriter, IRequestCycle, IComponent)} method. This is often
095     * used to access the informal parameters of a {@link RenderBlock} component.
096     * 
097     * @since 4.0
098     */
099    public IComponent getInvoker()
100    {
101        return _invoker;
102    }
103
104    /**
105     * Used for testing only.
106     * 
107     * @since 4.0
108     */
109
110    void setInvoker(IComponent invoker)
111    {
112        _invoker = invoker;
113    }
114}