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    import java.util.List;
021    
022    /**
023     * Conditionally renders its body.
024     * <p/>
025     * If the value object is in the list, then the body of the Contains
026     * component is rendered. If not, the body is omitted and the else block
027     * is rendered.
028     *
029     * @version $Id: Contains.java 645 2010-03-11 13:05:54Z homburgs $
030     */
031    public class Contains
032    {
033        /**
034         * Value which might be contained in the list.
035         */
036        @Parameter(required = true)
037        private Object value;
038    
039        /**
040         * List, which might contain the object.
041         */
042        @Parameter(required = true)
043        private List<Object> list;
044    
045        /**
046         * Optional parameter to invert the test. If true, then the body
047         * is rendered when the test
048         * parameter is false (not true).
049         */
050        @Parameter
051        private boolean negate;
052    
053        /**
054         * An alternate {@link org.apache.tapestry5.Block} to render if the test parameter is
055         * false. The default, null, means
056         * render nothing in that situation.
057         */
058        @Parameter(name = "else")
059        private Block elseBlock;
060    
061        /**
062         * Returns null if the list contains the object, which allows
063         * normal rendering (of the body). If
064         * the test parameter is false, returns the else parameter (this
065         * may also be null).
066         */
067        Object beginRender()
068        {
069            return (list != null && list.contains(value)) != negate ? null : elseBlock;
070        }
071    
072        /**
073         * If the the list contains the object, then the body is rendered,
074         * otherwise not. The component does
075         * not have a template or do any other rendering besides its body.
076         */
077        boolean beforeRenderBody()
078        {
079            return (list != null && list.contains(value)) != negate;
080        }
081    
082        void setup(Object value, List<Object> list, boolean negate, Block elseBlock)
083        {
084            this.value = value;
085            this.list = list;
086            this.negate = negate;
087            this.elseBlock = elseBlock;
088        }
089    }