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 java.util.List; 018 019 import org.apache.tapestry.IAsset; 020 import org.apache.tapestry.IMarkupWriter; 021 import org.apache.tapestry.IPage; 022 import org.apache.tapestry.IRender; 023 import org.apache.tapestry.IRequestCycle; 024 025 /** 026 * Outputs the main js packages and the tapestry js 027 * that are defined by the {@link JavascriptManager} service. 028 */ 029 public class SimpleAjaxShellDelegate implements IRender 030 { 031 private static final String SYSTEM_NEWLINE = "\n"; 032 033 private JavascriptManager _javascriptManager; 034 035 public SimpleAjaxShellDelegate(JavascriptManager javascriptManager) 036 { 037 _javascriptManager = javascriptManager; 038 } 039 040 /** 041 * {@inheritDoc} 042 */ 043 public void render(IMarkupWriter writer, IRequestCycle cycle) 044 { 045 StringBuffer str = new StringBuffer(300); 046 047 processPath(str, cycle, _javascriptManager.getPath()); 048 049 // include all the main js packages 050 appendAssetsAsJavascript(str, cycle, _javascriptManager.getAssets()); 051 052 IPage page = cycle.getPage(); 053 if (page.hasFormComponents()) 054 { 055 appendAssetsAsJavascript(str, cycle, _javascriptManager.getFormAssets()); 056 } 057 if (page.hasWidgets()) 058 { 059 appendAssetsAsJavascript(str, cycle, _javascriptManager.getWidgetAssets()); 060 } 061 062 processTapestryPath(str, cycle, _javascriptManager.getTapestryPath()); 063 064 // include the tapestry js 065 IAsset tapestryAsset = _javascriptManager.getTapestryAsset(); 066 if (tapestryAsset!=null) 067 { 068 appendAssetAsJavascript(str, cycle, tapestryAsset); 069 } 070 071 writer.printRaw(str.toString()); 072 writer.println(); 073 } 074 075 /** 076 * Called before including any javascript. It does nothing by default, but allows 077 * subclasses to change this behavior. 078 * @param cycle 079 * @param str 080 * @param path The base path to the javascript files. May be null. 081 */ 082 protected void processPath(StringBuffer str, IRequestCycle cycle, IAsset path) 083 { 084 } 085 086 /** 087 * Called before including tapestry's base javascript. It does nothing by default, 088 * but allows subclasses to change this behavior. 089 * @param cycle 090 * @param str 091 * @param path The base path to the tapestry javascript file. May be null. 092 */ 093 protected void processTapestryPath(StringBuffer str, IRequestCycle cycle, IAsset path) 094 { 095 } 096 097 /** 098 * Appends a script tag to include the given asset. 099 * @param str 100 * @param cycle 101 * @param asset 102 */ 103 protected void appendAssetAsJavascript(StringBuffer str, IRequestCycle cycle, IAsset asset) 104 { 105 final String url = asset.buildURL(); 106 str.append("<script type=\"text/javascript\" src=\"").append(url) 107 .append("\"></script>").append(SYSTEM_NEWLINE); 108 109 } 110 111 private void appendAssetsAsJavascript(StringBuffer str, IRequestCycle cycle, List jsAssets) 112 { 113 for (int i = 0; i < jsAssets.size(); i++) 114 { 115 appendAssetAsJavascript(str, cycle, (IAsset) jsAssets.get(i)); 116 } 117 } 118 }