001 /*
002 * Apache License
003 * Version 2.0, January 2004
004 * http://www.apache.org/licenses/
005 *
006 * Copyright 2008-2010 by chenillekit.org
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 */
014
015 package org.chenillekit.tapestry.core.components;
016
017 import org.apache.tapestry5.Block;
018 import org.apache.tapestry5.annotations.Parameter;
019
020 /**
021 * Conditionally renders its body.
022 *
023 * @version $Id: Equals.java 645 2010-03-11 13:05:54Z homburgs $
024 */
025 public class Equals
026 {
027 /**
028 * If true, then the body of the If component is rendered.
029 * If false, the body is omitted.
030 */
031 @Parameter(required = true)
032 private Object left;
033
034 @Parameter(required = true)
035 private Object right;
036
037 /**
038 * Optional parameter to invert the test.
039 * If true, then the body is rendered when the test parameter is false (not true).
040 */
041 @Parameter
042 private boolean negate;
043
044 /**
045 * An alternate {@link org.apache.tapestry5.Block} to render if the test parameter is false.
046 * The default, null, means render nothing in that situation.
047 */
048 @Parameter(name = "else")
049 private Block elseBlock;
050
051 /**
052 * Returns null if the test parameter is true, which allows normal
053 * rendering (of the body). If
054 * the test parameter is false, returns the else parameter (this
055 * may also be null).
056 */
057 Object beginRender()
058 {
059 if (left == null)
060 {
061 if (right == null)
062 return null;
063 else
064 return elseBlock;
065 }
066
067 return left.equals(right) != negate ? null : elseBlock;
068 }
069
070 /**
071 * If the test parameter is true, then the body is rendered,
072 * otherwise not. The component does
073 * not have a template or do any other rendering besides its body.
074 */
075 boolean beforeRenderBody()
076 {
077 if (left == null)
078 return right == null;
079
080 return left.equals(right) != negate;
081 }
082
083 /**
084 * only for testing.
085 *
086 * @param left
087 * @param right
088 * @param negate
089 * @param elseBlock
090 */
091 void setup(Object left, Object right, boolean negate, Block elseBlock)
092 {
093 this.left = left;
094 this.right = right;
095 this.negate = negate;
096 this.elseBlock = elseBlock;
097 }
098 }