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. [ <a 024 * href="../../../../../ComponentReference/Conditional.html">Component Reference </a>] 025 * 026 * @author Howard Lewis Ship, David Solis 027 */ 028 029 public abstract class Conditional extends AbstractComponent 030 { 031 /** 032 * Renders its wrapped components only if the condition is true (technically, if condition 033 * matches invert). Additionally, if element is specified, can emulate that HTML element if 034 * condition is met 035 */ 036 037 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) 038 { 039 if (evaluateCondition()) 040 { 041 String element = getElement(); 042 043 boolean render = !cycle.isRewinding() && HiveMind.isNonBlank(element); 044 045 if (render) 046 { 047 writer.begin(element); 048 renderInformalParameters(writer, cycle); 049 } 050 051 renderBody(writer, cycle); 052 053 if (render) 054 writer.end(element); 055 } 056 } 057 058 protected boolean evaluateCondition() 059 { 060 return getCondition() != getInvert(); 061 } 062 063 public abstract boolean getCondition(); 064 065 public abstract boolean getInvert(); 066 067 public abstract String getElement(); 068 }