001    // Copyright 2007 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.javascript;
016    
017    import org.apache.tapestry.IAsset;
018    import org.apache.tapestry.IMarkupWriter;
019    import org.apache.tapestry.IPage;
020    import org.apache.tapestry.IRender;
021    import org.apache.tapestry.IRequestCycle;
022    
023    /**
024     * Outputs the main js packages and the tapestry js 
025     * that are defined by the {@link JavascriptManager} service.
026     */
027    public class SimpleAjaxShellDelegate implements IRender
028    {
029        private JavascriptManager _javascriptManager;
030    
031        public SimpleAjaxShellDelegate(JavascriptManager javascriptManager)
032        {
033            _javascriptManager = javascriptManager;
034        }
035        
036        protected JavascriptManager getJavascriptManager() {
037            return _javascriptManager;
038        }
039    
040        /**
041         * {@inheritDoc}
042         */
043        public void render(IMarkupWriter writer, IRequestCycle cycle)
044        {   
045            IPage page = cycle.getPage();
046            
047            processPath(writer, cycle, _javascriptManager.getPath());
048            
049            _javascriptManager.renderLibraryResources(writer, cycle, page.hasFormComponents(), page.hasWidgets());
050            
051            processTapestryPath(writer, cycle, _javascriptManager.getTapestryPath());
052            
053            _javascriptManager.renderLibraryAdaptor(writer, cycle);
054        }
055        
056        /**
057         * Called before including any javascript. It does nothing by default, but allows
058         * subclasses to change this behavior.
059         * @param writer   
060         * @param cycle
061         * @param path The base path to the javascript files. May be null.
062         */
063        protected void processPath(IMarkupWriter writer, IRequestCycle cycle, IAsset path) 
064        {
065        }  
066        
067        /**
068         * Called before including tapestry's base javascript. It does nothing by default, 
069         * but allows subclasses to change this behavior.
070         * @param writer   
071         * @param cycle
072         * @param path The base path to the tapestry javascript file. May be null.
073         */    
074        protected void processTapestryPath(IMarkupWriter writer, IRequestCycle cycle, IAsset path) 
075        {
076        }
077    }