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