Coverage Report - org.apache.tapestry.javascript.SimpleAjaxShellDelegate
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleAjaxShellDelegate
0% 
0% 
1.667
 
 1  
 // Copyright 2007 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.javascript;
 16  
 
 17  
 import java.util.List;
 18  
 
 19  
 import org.apache.tapestry.IAsset;
 20  
 import org.apache.tapestry.IMarkupWriter;
 21  
 import org.apache.tapestry.IPage;
 22  
 import org.apache.tapestry.IRender;
 23  
 import org.apache.tapestry.IRequestCycle;
 24  
 
 25  
 /**
 26  
  * Outputs the main js packages and the tapestry js 
 27  
  * that are defined by the {@link JavascriptManager} service.
 28  
  */
 29  
 public class SimpleAjaxShellDelegate implements IRender
 30  
 {
 31  
     private static final String SYSTEM_NEWLINE = "\n";
 32  
 
 33  
     private JavascriptManager _javascriptManager;
 34  
 
 35  
     public SimpleAjaxShellDelegate(JavascriptManager javascriptManager)
 36  0
     {
 37  0
         _javascriptManager = javascriptManager;
 38  0
     }
 39  
 
 40  
     /**
 41  
      * {@inheritDoc}
 42  
      */
 43  
     public void render(IMarkupWriter writer, IRequestCycle cycle)
 44  
     {        
 45  0
         StringBuffer str = new StringBuffer(300);
 46  
         
 47  0
         processPath(str, cycle, _javascriptManager.getPath());
 48  
 
 49  
         // include all the main js packages
 50  0
         appendAssetsAsJavascript(str, cycle, _javascriptManager.getAssets());
 51  
         
 52  0
         IPage page = cycle.getPage();
 53  0
         if (page.hasFormComponents())
 54  
         {
 55  0
             appendAssetsAsJavascript(str, cycle, _javascriptManager.getFormAssets());
 56  
         }
 57  0
         if (page.hasWidgets())
 58  
         {
 59  0
             appendAssetsAsJavascript(str, cycle, _javascriptManager.getWidgetAssets());
 60  
         }
 61  
         
 62  0
         processTapestryPath(str, cycle, _javascriptManager.getTapestryPath());
 63  
         
 64  
         // include the tapestry js
 65  0
         IAsset tapestryAsset = _javascriptManager.getTapestryAsset();
 66  0
         if (tapestryAsset!=null)
 67  
         {
 68  0
             appendAssetAsJavascript(str, cycle, tapestryAsset);
 69  
         }
 70  
 
 71  0
         writer.printRaw(str.toString());
 72  0
         writer.println();
 73  0
     }
 74  
     
 75  
     /**
 76  
      * Called before including any javascript. It does nothing by default, but allows
 77  
      * subclasses to change this behavior.  
 78  
      * @param cycle
 79  
      * @param str 
 80  
      * @param path The base path to the javascript files. May be null.
 81  
      */
 82  
     protected void processPath(StringBuffer str, IRequestCycle cycle, IAsset path) 
 83  
     {
 84  0
     }  
 85  
     
 86  
     /**
 87  
      * Called before including tapestry's base javascript. It does nothing by default, 
 88  
      * but allows subclasses to change this behavior.  
 89  
      * @param cycle
 90  
      * @param str 
 91  
      * @param path The base path to the tapestry javascript file. May be null.
 92  
      */    
 93  
     protected void processTapestryPath(StringBuffer str, IRequestCycle cycle, IAsset path) 
 94  
     {
 95  0
     }      
 96  
     
 97  
     /**
 98  
      * Appends a script tag to include the given asset. 
 99  
      * @param str
 100  
      * @param cycle
 101  
      * @param asset
 102  
      */
 103  
     protected void appendAssetAsJavascript(StringBuffer str, IRequestCycle cycle, IAsset asset)
 104  
     {
 105  0
         final String url = asset.buildURL();
 106  0
         str.append("<script type=\"text/javascript\" src=\"").append(url)
 107  
                 .append("\"></script>").append(SYSTEM_NEWLINE);
 108  
         
 109  0
     }        
 110  
 
 111  
     private void appendAssetsAsJavascript(StringBuffer str, IRequestCycle cycle, List jsAssets)
 112  
     {
 113  0
         for (int i = 0; i < jsAssets.size(); i++)
 114  
         {
 115  0
             appendAssetAsJavascript(str, cycle, (IAsset) jsAssets.get(i));
 116  
         }
 117  0
     }
 118  
 }