| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AbstractConditional |
|
| 0.0;0 |
| 1 | // Copyright 2009 The Apache Software Foundation | |
| 2 | // | |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 | // you may not use this file except in compliance with the License. | |
| 5 | // You may obtain a copy of the License at | |
| 6 | // | |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | // | |
| 9 | // Unless required by applicable law or agreed to in writing, software | |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 | // See the License for the specific language governing permissions and | |
| 13 | // limitations under the License. | |
| 14 | ||
| 15 | package org.apache.tapestry5.corelib.base; | |
| 16 | ||
| 17 | import org.apache.tapestry5.BindingConstants; | |
| 18 | import org.apache.tapestry5.Block; | |
| 19 | import org.apache.tapestry5.ComponentResources; | |
| 20 | import org.apache.tapestry5.MarkupWriter; | |
| 21 | import org.apache.tapestry5.annotations.Parameter; | |
| 22 | import org.apache.tapestry5.annotations.SupportsInformalParameters; | |
| 23 | import org.apache.tapestry5.ioc.annotations.Inject; | |
| 24 | ||
| 25 | /** | |
| 26 | * Base class for {@link org.apache.tapestry5.corelib.components.If} and {@link org.apache.tapestry5.corelib.components.Unless}. | |
| 27 | * Will render its body or the block from its else parameter. If it renders anything and it has an element name, then | |
| 28 | * it renders the element and its informal parameters. | |
| 29 | */ | |
| 30 | @SupportsInformalParameters | |
| 31 | 146 | public abstract class AbstractConditional |
| 32 | { | |
| 33 | @Inject | |
| 34 | private ComponentResources resources; | |
| 35 | ||
| 36 | /** | |
| 37 | * Performs the test via the parameters; return true to render the body of the component, false to render the else | |
| 38 | * block (or nothing). | |
| 39 | * | |
| 40 | * @return true to render body | |
| 41 | */ | |
| 42 | protected abstract boolean test(); | |
| 43 | ||
| 44 | /** | |
| 45 | * An alternate {@link org.apache.tapestry5.Block} to render if {@link #test()} is false. The default, null, means | |
| 46 | * render nothing in that situation. | |
| 47 | */ | |
| 48 | @Parameter(name = "else", defaultPrefix = BindingConstants.LITERAL) | |
| 49 | private Block elseBlock; | |
| 50 | ||
| 51 | private boolean renderTag; | |
| 52 | ||
| 53 | /** | |
| 54 | * Returns null if the {@link #test()} is true, which allows normal rendering (of the body). If the test parameter | |
| 55 | * is false, returns the else parameter (this may also be null). | |
| 56 | */ | |
| 57 | Object beginRender(MarkupWriter writer) | |
| 58 | { | |
| 59 | 5390 | Block toRender = test() ? resources.getBody() : elseBlock; |
| 60 | ||
| 61 | 5390 | String elementName = resources.getElementName(); |
| 62 | ||
| 63 | 5390 | renderTag = toRender != null && elementName != null; |
| 64 | ||
| 65 | 5390 | if (renderTag) |
| 66 | { | |
| 67 | 280 | writer.element(elementName); |
| 68 | 280 | resources.renderInformalParameters(writer); |
| 69 | } | |
| 70 | ||
| 71 | 5390 | return toRender; |
| 72 | } | |
| 73 | ||
| 74 | /** | |
| 75 | * If {@link #test()} is true, then the body is rendered, otherwise not. The component does not have a template or | |
| 76 | * do any other rendering besides its body. | |
| 77 | */ | |
| 78 | boolean beforeRenderBody() | |
| 79 | { | |
| 80 | 5390 | return false; |
| 81 | } | |
| 82 | ||
| 83 | void afterRenderBody(MarkupWriter writer) | |
| 84 | { | |
| 85 | 5390 | if (renderTag) |
| 86 | 280 | writer.end(); |
| 87 | 5390 | } |
| 88 | ||
| 89 | ||
| 90 | } |