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    package org.apache.tapestry.dojo;
015    
016    import org.apache.hivemind.util.Defense;
017    import org.apache.tapestry.IAsset;
018    import org.apache.tapestry.IMarkupWriter;
019    import org.apache.tapestry.IRender;
020    import org.apache.tapestry.IRequestCycle;
021    import org.apache.tapestry.engine.IEngineService;
022    
023    /**
024     * The default rendering delegate responseible for include the 
025     * dojo sources into the {@link Shell} component.
026     *
027     * @author jkuhnert
028     */
029    public class AjaxShellDelegate implements IRender
030    {
031        /** Client side debug log level. */
032        public static final String BROWSER_LOG_DEBUG="DEBUG";
033        /** Client side info log level. */
034        public static final String BROWSER_LOG_INFO="INFO";
035        /** Client side warning log level. */
036        public static final String BROWSER_LOG_WARNING="WARNING";
037        /** Client side error log level. */
038        public static final String BROWSER_LOG_ERROR="ERROR";
039        /** Client side critical log level. */
040        public static final String BROWSER_LOG_CRITICAL="CRITICAL";
041        
042        private IAsset _dojoSource;
043        
044        private IAsset _dojoPath;
045        
046        private IAsset _tapestrySource;
047        
048        private IEngineService _assetService;
049        
050        private String _browserLogLevel = BROWSER_LOG_WARNING;
051        
052        private boolean _debug;
053        
054        private boolean _parseWidgets;
055        
056        private String _debugContainerId;
057        
058        /**
059         * {@inheritDoc}
060         */
061        public void render(IMarkupWriter writer, IRequestCycle cycle)
062        {
063            // TODO: Add ability to make all of these parameters more configurable
064            
065            // first configure dojo, has to happen before package include
066            StringBuffer str = new StringBuffer("<script type=\"text/javascript\">");
067            str.append("djConfig = { isDebug: ").append(_debug).append(",")
068            .append(" debugContainerId:'").append(_debugContainerId).append("',")
069            .append(" baseRelativePath:\"")
070            .append(_assetService.getLink(true,
071                    _dojoPath.getResourceLocation().getPath()).getAbsoluteURL())
072            .append("\", preventBackButtonFix: false, parseWidgets:").append(_parseWidgets).append("}")
073            .append(" </script>\n\n ");
074            
075            // include the core dojo.js package
076            str.append("<script type=\"text/javascript\" src=\"")
077            .append(_assetService.getLink(true,
078                    _dojoSource.getResourceLocation()
079                    .getPath()).getAbsoluteURL()).append("\"></script>");
080            
081            // include core tapestry.js package
082            str.append("<script type=\"text/javascript\" src=\"")
083            .append(_assetService.getLink(true,
084                    _tapestrySource.getResourceLocation()
085                    .getPath()).getAbsoluteURL()).append("\"></script>");
086            
087            // logging configuration
088            str.append("\n<script type=\"text/javascript\">")
089            .append("dojo.require(\"dojo.logging.Logger\");\n")
090            .append("dojo.log.setLevel(dojo.log.getLevel(\"").append(_browserLogLevel)
091            .append("\"));\n")
092            .append("dojo.require(\"tapestry.namespace\")")
093            .append("</script>");
094            
095            writer.printRaw(str.toString());
096            writer.println();
097        }
098        
099        /**
100         * Sets the dojo logging level. Similar to log4j style
101         * log levels. 
102         * @param level The string constant for the level, valid values
103         *              are:
104         *              <p>
105         *              <ul>
106         *              <li>{@link #BROWSER_LOG_DEBUG}</li>
107         *              <li>{@link #BROWSER_LOG_INFO}</li>
108         *              <li>{@link #BROWSER_LOG_WARNING}</li>
109         *              <li>{@link #BROWSER_LOG_ERROR}</li>
110         *              <li>{@link #BROWSER_LOG_CRITICAL}</li>
111         *              </ul>
112         *              </p>
113         */
114        public void setLogLevel(String level)
115        {
116            Defense.notNull("level", level);
117            
118            _browserLogLevel = level;
119        }
120        
121        /**
122         * Allows for turning browser debugging on/off.
123         * 
124         * @param debug If false, no logging output will be written.
125         */
126        public void setDebug(boolean debug)
127        {
128            _debug = debug;
129        }
130        
131        /**
132         * Sets the html element node id of the element you would like all browser
133         * debug content to go to.
134         * 
135         * @param debugContainerId the debugContainerId to set
136         */
137        public void setDebugContainerId(String debugContainerId)
138        {
139            _debugContainerId = debugContainerId;
140        }
141        
142        /**
143         * Tells dojo whether or not to parse widgets by traversing the entire 
144         * dom node of your document. It is highly reccomended that you keep this
145         * at its default value of false.
146         * 
147         * @param parseWidgets the parseWidgets to set
148         */
149        public void setParseWidgets(boolean parseWidgets)
150        {
151            _parseWidgets = parseWidgets;
152        }
153    
154        /**
155         * Sets a valid path to the base dojo javascript installation
156         * directory.
157         * @param dojoSource
158         */
159        public void setDojoSource(IAsset dojoSource)
160        {
161            _dojoSource = dojoSource;
162        }
163        
164        /**
165         * Sets the dojo baseRelativePath value.
166         * @param dojoPath
167         */
168        public void setDojoPath(IAsset dojoPath)
169        {
170            _dojoPath = dojoPath;
171        }
172        
173        /**
174         * Sets a valid base path to resolve tapestry.js.
175         * @param tapestrySource
176         */
177        public void setTapestrySource(IAsset tapestrySource)
178        {
179            _tapestrySource = tapestrySource;
180        }
181        
182        /**
183         * Injected asset service.
184         * @param service
185         */
186        public void setAssetService(IEngineService service)
187        {
188            _assetService = service;
189        }
190    }