KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > valid > FieldLabel


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.valid;
16
17 import org.apache.tapestry.AbstractComponent;
18 import org.apache.tapestry.BindingException;
19 import org.apache.tapestry.IForm;
20 import org.apache.tapestry.IMarkupWriter;
21 import org.apache.tapestry.IRequestCycle;
22 import org.apache.tapestry.Tapestry;
23 import org.apache.tapestry.TapestryUtils;
24 import org.apache.tapestry.form.IFormComponent;
25
26 /**
27  * Used to label an {@link IFormComponent}. Because such fields know their displayName
28  * (user-presentable name), there's no reason to hard code the label in a page's HTML template (this
29  * also helps with localization). [ <a
30  * HREF="../../../../../ComponentReference/FieldLabel.html">Component Reference </a>]
31  *
32  * @author Howard Lewis Lewis Ship
33  */

34
35 public abstract class FieldLabel extends AbstractComponent
36 {
37     /**
38      * Gets the {@link IForm}&nbsp;and {@link IValidationDelegate delegate}, then renders the
39      * label obtained from the field. Does nothing when rewinding.
40      */

41
42     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
43     {
44         if (cycle.isRewinding())
45             return;
46
47         IForm form = TapestryUtils.getForm(cycle, this);
48
49         IFormComponent field = getField();
50
51         if (field != null)
52             form.prerenderField(writer, field, getLocation());
53
54         String JavaDoc displayName = getDisplayName();
55
56         if (displayName == null)
57         {
58             if (field == null)
59                 throw Tapestry.createRequiredParameterException(this, "field");
60
61             displayName = field.getDisplayName();
62
63             if (displayName == null)
64                 throw new BindingException(ValidMessages.noDisplayName(this, field), this, null,
65                         getBinding("field"), null);
66         }
67
68         IValidationDelegate delegate = form.getDelegate();
69
70         String JavaDoc id = field == null ? null : field.getClientId();
71
72         delegate.writeLabelPrefix(field, writer, cycle);
73
74         writer.begin("label");
75
76         if (id != null)
77             writer.attribute("for", id);
78
79         // TODO: Introduce a IValidationDelegate decoration method for inside the
80
// <label> tag.
81

82         renderInformalParameters(writer, cycle);
83
84         writer.print(displayName, getRaw());
85
86         writer.end();
87
88         delegate.writeLabelSuffix(field, writer, cycle);
89     }
90
91     /** displayName parameter */
92     public abstract String JavaDoc getDisplayName();
93
94     /** field parameter */
95     public abstract IFormComponent getField();
96
97     /** raw parameter */
98     public abstract boolean getRaw();
99 }
Popular Tags