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()) return;
058
059 String displayName = getDisplayName();
060
061 if (displayName == null)
062 {
063 if (field == null)
064 throw Tapestry.createRequiredParameterException(this, "field");
065
066 displayName = field.getDisplayName();
067
068 if (displayName == null)
069 throw new BindingException(ValidMessages.noDisplayName(this,
070 field), this, null, getBinding("field"), null);
071 }
072
073 IValidationDelegate delegate = form.getDelegate();
074
075 String id = field == null ? null : field.getClientId();
076
077 delegate.writeLabelPrefix(field, writer, cycle);
078
079 writer.begin("label");
080
081 if (id != null) writer.attribute("for", id);
082
083 delegate.writeLabelAttributes(writer, cycle, field);
084 renderInformalParameters(writer, cycle);
085
086 writer.print(displayName, getRaw());
087
088 writer.end();
089
090 delegate.writeLabelSuffix(field, writer, cycle);
091 }
092
093 /** displayName parameter. */
094 public abstract String getDisplayName();
095
096 /** field parameter. */
097 public abstract IFormComponent getField();
098
099 /** raw parameter. */
100 public abstract boolean getRaw();
101 }