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 delegate.writeLabelPrefix(field, writer, cycle);
079
080 writer.begin("label");
081
082 if (id != null) writer.attribute("for", id);
083
084 delegate.writeLabelAttributes(writer, cycle, field);
085 renderInformalParameters(writer, cycle);
086
087 writer.print(displayName, getRaw());
088
089 writer.end();
090
091 delegate.writeLabelSuffix(field, writer, cycle);
092 }
093
094 /** displayName parameter. */
095 public abstract String getDisplayName();
096
097 /** field parameter. */
098 public abstract IFormComponent getField();
099
100 /** raw parameter. */
101 public abstract boolean getRaw();
102 }