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.*;
018 import org.apache.tapestry.form.IFormComponent;
019
020 /**
021 * Used to label an {@link IFormComponent}. Because such fields know their
022 * displayName (user-presentable name), there's no reason to hard code the label
023 * in a page's HTML template (this also helps with localization). [ <a
024 * href="../../../../../ComponentReference/FieldLabel.html">Component Reference
025 * </a>]
026 *
027 * @author Howard Lewis Lewis Ship
028 */
029
030 public abstract class FieldLabel extends AbstractComponent
031 {
032
033 // Parameter
034 public abstract boolean isPrerender();
035
036 /**
037 * Gets the {@link IForm} and {@link IValidationDelegate delegate},
038 * then renders the label obtained from the field. Does nothing when
039 * 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 && isPrerender())
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, getBinding("field"), null);
065 }
066
067 IValidationDelegate delegate = form.getDelegate();
068
069 String id = (field == null) ? null : field.getClientId();
070
071 if (field != null)
072 delegate.writeLabelPrefix(field, writer, cycle);
073
074 writer.begin("label");
075
076 renderInformalParameters(writer, cycle);
077
078 if (id != null)
079 writer.attribute("for", id);
080
081 delegate.writeLabelAttributes(writer, cycle, field);
082
083 delegate.beforeLabelText(writer, cycle, field);
084
085 writer.print(displayName, getRaw());
086
087 delegate.afterLabelText(writer, cycle, field);
088
089 writer.end();
090
091 if (field != null)
092 delegate.writeLabelSuffix(field, writer, cycle);
093 }
094
095 /** displayName parameter. */
096 public abstract String getDisplayName();
097
098 /** field parameter. */
099 public abstract IFormComponent getField();
100
101 /** raw parameter. */
102 public abstract boolean getRaw();
103 }