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.valid; 016 017 import org.apache.tapestry.AbstractComponent; 018 import org.apache.tapestry.BindingException; 019 import org.apache.tapestry.IForm; 020 import org.apache.tapestry.IMarkupWriter; 021 import org.apache.tapestry.IRequestCycle; 022 import org.apache.tapestry.Tapestry; 023 import org.apache.tapestry.TapestryUtils; 024 import org.apache.tapestry.form.IFormComponent; 025 026 /** 027 * Used to label an {@link IFormComponent}. Because such fields know their 028 * displayName (user-presentable name), there's no reason to hard code the label 029 * in a page's HTML template (this also helps with localization). [ <a 030 * href="../../../../../ComponentReference/FieldLabel.html">Component Reference 031 * </a>] 032 * 033 * @author Howard Lewis Lewis Ship 034 */ 035 036 public abstract class FieldLabel extends AbstractComponent 037 { 038 039 // Parameter 040 public abstract boolean isPrerender(); 041 042 /** 043 * Gets the {@link IForm} and {@link IValidationDelegate delegate}, 044 * then renders the label obtained from the field. Does nothing when 045 * rewinding. 046 */ 047 048 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) 049 { 050 IForm form = TapestryUtils.getForm(cycle, this); 051 052 IFormComponent field = getField(); 053 054 if (field != null && isPrerender()) 055 form.prerenderField(writer, field, getLocation()); 056 057 if (cycle.isRewinding()) 058 return; 059 060 String displayName = getDisplayName(); 061 062 if (displayName == null) 063 { 064 if (field == null) 065 throw Tapestry.createRequiredParameterException(this, "field"); 066 067 displayName = field.getDisplayName(); 068 069 if (displayName == null) 070 throw new BindingException(ValidMessages.noDisplayName(this, 071 field), this, null, getBinding("field"), null); 072 } 073 074 IValidationDelegate delegate = form.getDelegate(); 075 076 String id = (field == null) ? null : field.getClientId(); 077 078 if (field != null) 079 delegate.writeLabelPrefix(field, writer, cycle); 080 081 writer.begin("label"); 082 083 if (id != null) 084 writer.attribute("for", id); 085 086 delegate.writeLabelAttributes(writer, cycle, field); 087 renderInformalParameters(writer, cycle); 088 089 delegate.beforeLabelText(writer, cycle, field); 090 091 writer.print(displayName, getRaw()); 092 093 delegate.afterLabelText(writer, cycle, field); 094 095 writer.end(); 096 097 if (field != null) 098 delegate.writeLabelSuffix(field, writer, cycle); 099 } 100 101 /** displayName parameter. */ 102 public abstract String getDisplayName(); 103 104 /** field parameter. */ 105 public abstract IFormComponent getField(); 106 107 /** raw parameter. */ 108 public abstract boolean getRaw(); 109 }