Author: hlship Date: Tue Feb 10 19:43:28 2009 New Revision: 743077 URL: http://svn.apache.org/viewvc?rev=743077&view=rev Log: TAP5-496: Change If and Unless to render thier template element if provided (i.e., when using t:type) as well as informal parameters
Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/base/AbstractConditional.java Removed: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/IfTest.java tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/UnlessTest.java Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/If.java tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Unless.java tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/ExceptionDisplay.tml Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/base/AbstractConditional.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/base/AbstractConditional.java?rev=743077&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/base/AbstractConditional.java (added) +++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/base/AbstractConditional.java Tue Feb 10 19:43:28 2009 @@ -0,0 +1,90 @@ +// Copyright 2009 The Apache Software Foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.apache.tapestry5.corelib.base; + +import org.apache.tapestry5.BindingConstants; +import org.apache.tapestry5.Block; +import org.apache.tapestry5.ComponentResources; +import org.apache.tapestry5.MarkupWriter; +import org.apache.tapestry5.annotations.Parameter; +import org.apache.tapestry5.annotations.SupportsInformalParameters; +import org.apache.tapestry5.ioc.annotations.Inject; + +/** + * Base class for {...@link org.apache.tapestry5.corelib.components.If} and {...@link org.apache.tapestry5.corelib.components.Unless}. + * Will render its body or the block from its else parameter. If it renders anything and it has an element name, then + * it renders the element and its informal parameters. + */ +...@supportsinformalparameters +public abstract class AbstractConditional +{ + @Inject + private ComponentResources resources; + + /** + * Performs the test via the parameters; return true to render the body of the component, false to render the else + * block (or nothing). + * + * @return true to render body + */ + protected abstract boolean test(); + + /** + * An alternate {...@link org.apache.tapestry5.Block} to render if {...@link #test()} is false. The default, null, means + * render nothing in that situation. + */ + @Parameter(name = "else", defaultPrefix = BindingConstants.LITERAL) + private Block elseBlock; + + private boolean renderTag; + + /** + * Returns null if the {...@link #test()} is true, which allows normal rendering (of the body). If the test parameter + * is false, returns the else parameter (this may also be null). + */ + Object beginRender(MarkupWriter writer) + { + Block toRender = test() ? resources.getBody() : elseBlock; + + String elementName = resources.getElementName(); + + renderTag = toRender != null && elementName != null; + + if (renderTag) + { + writer.element(elementName); + resources.renderInformalParameters(writer); + } + + return toRender; + } + + /** + * If {...@link #test()} is true, then the body is rendered, otherwise not. The component does not have a template or + * do any other rendering besides its body. + */ + boolean beforeRenderBody() + { + return false; + } + + void afterRenderBody(MarkupWriter writer) + { + if (renderTag) + writer.end(); + } + + +} Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/If.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/If.java?rev=743077&r1=743076&r2=743077&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/If.java (original) +++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/If.java Tue Feb 10 19:43:28 2009 @@ -14,14 +14,13 @@ package org.apache.tapestry5.corelib.components; -import org.apache.tapestry5.BindingConstants; -import org.apache.tapestry5.Block; import org.apache.tapestry5.annotations.Parameter; +import org.apache.tapestry5.corelib.base.AbstractConditional; /** - * Conditionally renders its body. + * Conditionally renders its body. May render its tag and any informal parameters. */ -public class If +public class If extends AbstractConditional { /** * If true, then the body of the If component is rendered. If false, the body is omitted. @@ -39,34 +38,12 @@ private boolean negate; /** - * An alternate {...@link org.apache.tapestry5.Block} to render if the test parameter is false. The default, null, - * means render nothing in that situation. + * @return test parameter (if negate is false), or test parameter inverted (if negate is true) */ - @Parameter(name = "else", defaultPrefix = BindingConstants.LITERAL) - private Block elseBlock; - - /** - * Returns null if the test parameter is true, which allows normal rendering (of the body). If the test parameter is - * false, returns the else parameter (this may also be null). - */ - Object beginRender() - { - return test != negate ? null : elseBlock; - } - - /** - * If the test parameter is true, then the body is rendered, otherwise not. The component does not have a template - * or do any other rendering besides its body. - */ - boolean beforeRenderBody() + protected boolean test() { return test != negate; } - void setup(boolean test, boolean negate, Block elseBlock) - { - this.test = test; - this.negate = negate; - this.elseBlock = elseBlock; - } + } Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Unless.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Unless.java?rev=743077&r1=743076&r2=743077&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Unless.java (original) +++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Unless.java Tue Feb 10 19:43:28 2009 @@ -14,15 +14,14 @@ package org.apache.tapestry5.corelib.components; -import org.apache.tapestry5.BindingConstants; -import org.apache.tapestry5.Block; import org.apache.tapestry5.annotations.Parameter; +import org.apache.tapestry5.corelib.base.AbstractConditional; /** * A close relative of the {...@link org.apache.tapestry5.corelib.components.If} component that inverts the meaning of its * test. This is easier than an If component with the negate parameter set to true. */ -public class Unless +public class Unless extends AbstractConditional { /** * If true, then the body of the If component is rendered. If false, the body is omitted. @@ -31,33 +30,10 @@ private boolean test; /** - * An alternate {...@link org.apache.tapestry5.Block} to render if the test parameter is false. The default, null, - * means render nothing in that situation. + * @return test parameter inverted */ - @Parameter(name = "else", defaultPrefix = BindingConstants.LITERAL) - private Block elseBlock; - - /** - * Returns null if the test parameter is true, which allows normal rendering (of the body). If the test parameter is - * false, returns the else parameter (this may also be null). - */ - Object beginRender() - { - return !test ? null : elseBlock; - } - - /** - * If the test parameter is true, then the body is rendered, otherwise not. The component does not have a template - * or do any other rendering besides its body. - */ - boolean beforeRenderBody() + protected boolean test() { return !test; } - - void setup(boolean test, Block elseBlock) - { - this.test = test; - this.elseBlock = elseBlock; - } } Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/ExceptionDisplay.tml URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/ExceptionDisplay.tml?rev=743077&r1=743076&r2=743077&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/ExceptionDisplay.tml (original) +++ tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/ExceptionDisplay.tml Tue Feb 10 19:43:28 2009 @@ -4,36 +4,32 @@ <li> <span class="t-exception-class-name">${info.className}</span> - <t:if test="info.message"> - <div class="t-exception-message">${info.message}</div> - </t:if> + <div t:type="if" test="info.message" class="t-exception-message">${info.message}</div> - <t:if test="showPropertyList"> - <dl> - <t:loop source="info.propertyNames" value="propertyName"> - <dt>${propertyName}</dt> - <dd> - <t:renderobject object="propertyValue"/> - </dd> - </t:loop> - <t:if test="info.stackTrace"> - <dt> - <span class="t-exception-stack-controls"> - <input type="checkbox" id="${toggleId}" checked="checked"/> - <label for="${toggleId}">Hide uninteresting stack frames</label> - </span> - Stack trace - </dt> - <dd> - <ul class="t-stack-trace"> - <t:loop source="info.stackTrace" value="frame"> - <li class="${frameClass}">${frame}</li> - </t:loop> - </ul> - </dd> - </t:if> - </dl> - </t:if> + <dl t:type="if" test="showPropertyList"> + <t:loop source="info.propertyNames" value="propertyName"> + <dt>${propertyName}</dt> + <dd> + <t:renderobject object="propertyValue"/> + </dd> + </t:loop> + <t:if test="info.stackTrace"> + <dt> + <span class="t-exception-stack-controls"> + <input type="checkbox" id="${toggleId}" checked="checked"/> + <label for="${toggleId}">Hide uninteresting stack frames</label> + </span> + Stack trace + </dt> + <dd> + <ul class="t-stack-trace"> + <li t:type="loop" source="info.stackTrace" value="frame" class="${frameClass}"> + ${frame} + </li> + </ul> + </dd> + </t:if> + </dl> </li> </t:loop> </ul>