Author: dolander Date: Fri Jan 14 17:36:28 2005 New Revision: 125242 URL: http://svn.apache.org/viewcvs?view=rev&rev=125242 Log: Implement tagId support in the Tree.
Modified: incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/AbstractSimpleTag.java incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/tree/Tree.java Modified: incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/AbstractSimpleTag.java Url: http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/AbstractSimpleTag.java?view=diff&rev=125242&p1=incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/AbstractSimpleTag.java&r1=125241&p2=incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/AbstractSimpleTag.java&r2=125242 ============================================================================== --- incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/AbstractSimpleTag.java (original) +++ incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/AbstractSimpleTag.java Fri Jan 14 17:36:28 2005 @@ -3,6 +3,7 @@ import org.apache.beehive.netui.pageflow.util.URLRewriterService; import org.apache.beehive.netui.util.Bundle; import org.apache.beehive.netui.util.logging.Logger; +import org.apache.beehive.netui.tags.html.ScriptContainer; import org.apache.struts.Globals; import org.apache.struts.util.RequestUtils; @@ -10,8 +11,7 @@ import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.PageContext; -import javax.servlet.jsp.tagext.JspFragment; -import javax.servlet.jsp.tagext.SimpleTagSupport; +import javax.servlet.jsp.tagext.*; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; @@ -218,28 +218,6 @@ } /** - * This method will add a tagId and value to the ScriptRepoter TagId map. - * The a ScriptContainer tag will create a JavaScript table that allows - * the container, such as a portal, to rewrite the id so it's unique. - * The real name may be looked up based upon the tagId. - * - * If the no ScriptReporter is found, a script string will be returned - * to the caller so they can output the script block. - * @param tagId - * @param value - * @return String - */ - protected String mapLegacyTagId(String tagId, String value) - { - IScriptReporter scriptReporter = getScriptReporter(); - if (scriptReporter != null) { - scriptReporter.addLegacyTagIdMappings(tagId, value); - return null; - } - return tagId; - } - - /** * This method will rewrite the name (id) by passing it to the * URL Rewritter and getting back a value. * @param name the name that will be rewritten @@ -249,5 +227,30 @@ { PageContext pageContext = getPageContext(); return URLRewriterService.rewriteName(pageContext.getServletContext(), pageContext.getRequest(), name); + } + + /** + * This method will generate a real id based upon the passed in tagId. The generated + * id will be constucted by searching upward for all the script containers that have a + * scope id set. These will form a fully qualified id. + * @param tagId The base tagId set on a tag + * @return an id value formed by considering all of the scope id's found in the tag hierarchy. + */ + final protected String getIdForTagId(String tagId) + { + JspTag tag = this; + while (tag != null) { + if (tag instanceof ScriptContainer) { + String sid = ((ScriptContainer) tag).getScopeId(); + if (sid != null) { + tagId = sid + "." + tagId; + } + } + if (tag instanceof Tag) + tag = ((Tag) tag).getParent(); + else + tag = ((SimpleTag) tag).getParent(); + } + return tagId; } } Modified: incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/tree/Tree.java Url: http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/tree/Tree.java?view=diff&rev=125242&p1=incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/tree/Tree.java&r1=125241&p2=incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/tree/Tree.java&r2=125242 ============================================================================== --- incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/tree/Tree.java (original) +++ incubator/beehive/trunk/netui/src/tags-html/org/apache/beehive/netui/tags/tree/Tree.java Fri Jan 14 17:36:28 2005 @@ -27,11 +27,13 @@ import org.apache.beehive.netui.tags.*; import org.apache.beehive.netui.tags.html.HtmlConstants; import org.apache.beehive.netui.tags.html.JavaScriptUtils; +import org.apache.beehive.netui.tags.html.HtmlBaseTag; import org.apache.beehive.netui.tags.internal.PageFlowTagUtils; import org.apache.beehive.netui.tags.rendering.*; import org.apache.beehive.netui.util.Bundle; import javax.servlet.ServletContext; +import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.jsp.JspException; @@ -112,6 +114,8 @@ private String _tagId; // tag that should uniquely indentify a tree. Required for multiple tree using auto expand private String[] _expanded = null; private boolean _runAtClient = false; // run at client + private boolean _escapeLabels = false; // esapce the content of labels + private boolean _outputJavaScript = false; // cause the base javascript support to be output private InheritableState _iState = new InheritableState(); @@ -196,7 +200,6 @@ public void setTagId(String tagId) throws JspException { - // @todo: Need to fully support the tagid including rewrite and script _tagId = setRequiredValueAttribute(tagId, "tagId"); } @@ -380,6 +383,31 @@ } /** + * This attribue will cause the content of labels to be escaped when the value if <i>true</i>. + * The default value is <i>false</i>. + * @param escapeLabels + * @netui:attribute required="false" + * description="When true the content of labels will be escaped for HTML." + */ + public void setEscapeLabels(boolean escapeLabels) + { + _escapeLabels = escapeLabels; + } + + /** + * This will cause the standard tagId to Id JavaScript to be output. For most + * of the HTML tags this is automatically output. For the tree, because the tagId + * is a required attribute, we make javascript support optional. + * @param outputJavaScript + * @netui:attribute required="false" + * description="Output the standard tagId to Id JavaScript." + */ + public void setRenderJavaScript(boolean outputJavaScript) + { + _outputJavaScript = outputJavaScript; + } + + /** * Sets the root <code>TreeElement</code> of this tree. * @param rootNode - the root treeNode */ @@ -624,6 +652,11 @@ _divState.clear(); _divState.styleClass = _treeClass; _divState.style = _treeStyle; + String divId = null; + if (_outputJavaScript) { + _divState.id = getIdForTagId(_tagId); + divId = _divState.id; + } StringBuilderRenderAppender writer = new StringBuilderRenderAppender(sb); TagRenderingBase divRenderer = TagRenderingBase.Factory.getRendering(TagRenderingBase.DIV_TAG, request); divRenderer.doStartTag(writer, _divState); @@ -641,6 +674,13 @@ divRenderer.doEndTag(writer); sb.append("\n"); write(sb.toString()); + + // check to see if we are writing out the java. + if (_outputJavaScript) { + String jsOut = renderDefaultJavaScript(divId); + if (jsOut != null) + write(jsOut); + } } /** @@ -721,8 +761,9 @@ // Render the beginning of this node _divState.clear(); String tagId = node.getTagId(); + String script = null; if (tagId != null) { - renderTagId(tagId, _divState); + script = renderTagId(tagId, _divState); } attrs.renderDiv(_divState, node); if (_runAtClient) { @@ -732,6 +773,8 @@ StringBuilderRenderAppender writer = new StringBuilderRenderAppender(sb); divRenderer.doStartTag(writer, _divState); sb.append("\n"); + if (script != null) + sb.append(script); // In devMode we will verify the structure of the tree. This will not run in // production mode. @@ -1129,17 +1172,10 @@ */ protected final String renderTagId(String tagId, AbstractHtmlState state) { - assert(_tagId != null); - - String realName = rewriteName(tagId); - String idScript = mapLegacyTagId(tagId, realName); - - // some tags will output the id attribute themselves so they don't write this out - if (realName != null) { - state.id = realName; - } - return idScript; + state.id = getIdForTagId(tagId); + String script = renderDefaultJavaScript(state.id); + return script; } /** @@ -1152,5 +1188,56 @@ _errorText = new StringBuilder(32); } _errorText.append(error); + } + + //**************************** + //@TODO: move this into a tag helper class of some type and remove the duplication from HtmlBaseTag + /** Much of the code below is taken from the HtmlBaseTag. We need to eliminate this duplication + * through some type of helper methods. + */ + private String renderDefaultJavaScript(String realId) + { + String idScript = null; + + // map the tagId to the real id + if (TagConfig.isDefaultJavaScript()) { + idScript = mapTagId(_tagId, realId); + } + return idScript; + } + + private String mapTagId(String tagId, String realId) + { + IScriptReporter scriptReporter = getScriptReporter(); + if (scriptReporter != null) { + scriptReporter.addTagIdMappings(tagId, realId, null); + return null; + } + + // without a scripRepoter we need to create the actual JavaScript that will be written out + JavaScriptUtils jsu = getJavaScriptUtils((HttpServletRequest) getPageContext().getRequest()); + StringBuilder sb = new StringBuilder(128); + StringBuilderRenderAppender writer = new StringBuilderRenderAppender(sb); + jsu.getTagIdMapping(tagId, realId, null, writer); + return sb.toString(); + } + + /** + * This method will return the <code>javaScriptUtils</code> that is assocated + * with this request. If this doesn't exist, it will be created before it is + * returned. + * @param request the HttpServletRequest associated with this request + * @return a <code>JavaScriptUtils</code> assocated with the request + */ + static JavaScriptUtils getJavaScriptUtils(ServletRequest request) + { + assert (request != null); + JavaScriptUtils jsu = (JavaScriptUtils) request.getAttribute(HtmlBaseTag.JAVASCRIPT_STATUS); + if (jsu == null) { + jsu = new JavaScriptUtils(); + request.setAttribute(HtmlBaseTag.JAVASCRIPT_STATUS, jsu); + } + assert (jsu != null); + return jsu; } }
