001    package net.sf.tacos.javascript.jquery;
002    
003    import java.util.List;
004    
005    import org.apache.tapestry.IAsset;
006    import org.apache.tapestry.IMarkupWriter;
007    import org.apache.tapestry.IRequestCycle;
008    import org.apache.tapestry.javascript.JavascriptManagerImpl;
009    
010    /**
011     * For some jquery plugins we bundle, we want to add their css.
012     * 
013     * @author andyhot
014     */
015    public class JqueryJavascriptManagerImpl extends JavascriptManagerImpl {
016            private List cssAssets;
017            
018        public void setCssFiles(String cssFiles)
019        {
020            this.cssAssets = buildAssetList(cssFiles, "cssFiles");
021        }
022        
023        public List getCssAssets() {
024            return this.cssAssets;
025        }
026    
027            public void renderLibraryResources(IMarkupWriter writer, IRequestCycle cycle, 
028                            boolean hasForm, boolean hasWidget) {
029                    super.renderLibraryResources(writer, cycle, hasForm, hasWidget);
030    
031                    appendAssetsAsCss(writer, cycle, getCssAssets());
032            }
033            
034        
035        protected void appendAssetsAsCss(IMarkupWriter writer, IRequestCycle cycle, List assets) {
036            if (assets==null) {
037                    return;
038            }
039            
040            for (int i = 0; i < assets.size(); i++) {
041                    appendAssetAsCss(writer, cycle, (IAsset) assets.get(i));
042            }       
043        }     
044        
045        /**
046         * Appends a link tag to include the given asset. 
047         * @param str
048         * @param cycle
049         * @param asset
050         */
051        protected void appendAssetAsCss(IMarkupWriter writer, IRequestCycle cycle, IAsset asset)
052        {
053            final String url = asset.buildURL();
054            
055            writer.beginEmpty("link");
056            writer.attribute("media", "screen");
057            writer.attribute("rel", "stylesheet");
058            writer.attribute("type", "text/css");
059            writer.attribute("href", url);
060            writer.println();        
061        }   
062        
063    }