001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.components;
016
017 import org.apache.hivemind.HiveMind;
018 import org.apache.tapestry.AbstractComponent;
019 import org.apache.tapestry.IMarkupWriter;
020 import org.apache.tapestry.IRequestCycle;
021
022 /**
023 * A conditional element on a page which will render its wrapped elements zero or one times.
024 *
025 * @author Howard Lewis Ship, David Solis
026 */
027
028 public abstract class Conditional extends AbstractComponent
029 {
030 /**
031 * Renders its wrapped components only if the condition is true (technically, if condition
032 * matches invert). Additionally, if element is specified, can emulate that HTML element if
033 * condition is met
034 */
035
036 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
037 {
038 if (evaluateCondition())
039 {
040 String element = getElement();
041
042 boolean render = !cycle.isRewinding() && HiveMind.isNonBlank(element);
043
044 if (render)
045 {
046 writer.begin(element);
047 renderInformalParameters(writer, cycle);
048 }
049
050 renderBody(writer, cycle);
051
052 if (render)
053 writer.end(element);
054 }
055 }
056
057 protected boolean evaluateCondition()
058 {
059 return getCondition() != getInvert();
060 }
061
062 public abstract boolean getCondition();
063
064 public abstract boolean getInvert();
065
066 public abstract String getElement();
067 }