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.html;
016    
017    import org.apache.tapestry.AbstractComponent;
018    import org.apache.tapestry.IMarkupWriter;
019    import org.apache.tapestry.IRequestCycle;
020    import org.apache.tapestry.services.ResponseBuilder;
021    
022    /**
023     * The body of a Tapestry page. This is used since it allows components on the page access to an
024     * initialization script (that is written the start, just inside the <body> tag). This is
025     * currently used by {@link Rollover}and {@link Script}components. [ <a
026     * href="../../../../../ComponentReference/Body.html">Component Reference </a>]
027     * 
028     * @author Howard Lewis Ship
029     */
030    
031    public abstract class Body extends AbstractComponent
032    {
033        
034        protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
035        {
036            IMarkupWriter nested = writer.getNestedWriter();
037    
038            renderBody(nested, cycle);
039            
040            getRenderWorker().renderBody(cycle, this);
041            
042            // This is a little tricky, if we didn't call this manually
043            // now the page would never be able to have any javascript related
044            // render workers actually work because the javascript output would
045            // essentially be non writable
046            
047            getRenderWorker().renderComponent(cycle, getPage());
048            
049            // Start the body tag.
050            writer.println();
051            writer.begin(getElement());
052            
053            renderInformalParameters(writer, cycle);
054            
055            renderIdAttribute(writer, cycle);
056            
057            writer.println();
058            
059            // Write the page's scripting. This is included scripts
060            // and dynamic JavaScript.
061            
062            getBuilder().writeBodyScript(writer, cycle);
063            
064            // Close the nested writer, which dumps its buffered content
065            // into its parent.
066            
067            nested.close();
068            
069            // Any initialization should go at the very end of the document
070            // just before the close body tag. Older version of Tapestry
071            // would create a window.onload event handler, but this is better
072            // (it doesn't have to wait for external images to load).
073            
074            getBuilder().writeInitializationScript(writer);
075            
076            writer.end(); // <body>
077        }
078    
079        /**
080         * Parameter.
081         */
082        public abstract String getElement();
083        
084        public abstract ResponseBuilder getBuilder();
085    }