01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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.tapestry.corelib.components;
16:
17: import org.apache.tapestry.Block;
18: import org.apache.tapestry.annotations.Parameter;
19:
20: /**
21: * Conditionally renders its body.
22: */
23: public class If {
24: /** If true, then the body of the If component is rendered. If false, the body is omitted. */
25: @Parameter(required=true)
26: private boolean _test;
27:
28: /**
29: * Optional parameter to invert the test. If true, then the body is rendered when the test
30: * parameter is false (not true).
31: */
32: @Parameter
33: private boolean _negate;
34:
35: /**
36: * An alternate {@link Block} to render if the test parameter is false. The default, null, means
37: * render nothing in that situation.
38: */
39: @Parameter
40: private Block _else;
41:
42: /**
43: * Returns null if the test parameter is true, which allows normal rendering (of the body). If
44: * the test parameter is false, returns the else parameter (this may also be null).
45: */
46: Object beginRender() {
47: return _test != _negate ? null : _else;
48: }
49:
50: /**
51: * If the test parameter is true, then the body is rendered, otherwise not. The component does
52: * not have a template or do any other rendering besides its body.
53: */
54: boolean beforeRenderBody() {
55: return _test != _negate;
56: }
57:
58: void setup(boolean test, boolean negate, Block elseBlock) {
59: _test = test;
60: _negate = negate;
61: _else = elseBlock;
62: }
63: }
|