KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > AbstractFormComponent


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.form;
16
17 import org.apache.tapestry.AbstractComponent;
18 import org.apache.tapestry.IForm;
19 import org.apache.tapestry.IMarkupWriter;
20 import org.apache.tapestry.IRequestCycle;
21 import org.apache.tapestry.PageRenderSupport;
22 import org.apache.tapestry.TapestryUtils;
23 import org.apache.tapestry.valid.IValidationDelegate;
24
25 /**
26  * A base class for building components that correspond to HTML form elements. All such components
27  * must be wrapped (directly or indirectly) by a {@link Form} component.
28  *
29  * @author Howard Lewis Ship
30  * @author Paul Ferraro
31  * @since 1.0.3
32  */

33 public abstract class AbstractFormComponent extends AbstractComponent implements IFormComponent
34 {
35     private static final String JavaDoc SELECTED_ATTRIBUTE_NAME = "org.apache.tapestry.form.SelectedField";
36
37     public abstract IForm getForm();
38
39     public abstract void setForm(IForm form);
40
41     public abstract String JavaDoc getName();
42
43     public abstract void setName(String JavaDoc name);
44
45     /**
46      * Should be connected to a parameter named "id" (annotations would be helpful here!). For
47      * components w/o such a parameter, this will simply return null.
48      */

49
50     public abstract String JavaDoc getIdParameter();
51
52     /**
53      * Stores the actual id allocated (or null if the component doesn't support this).
54      */

55
56     public abstract void setClientId(String JavaDoc id);
57
58     /**
59      * Invoked from {@link #renderFormComponent(IMarkupWriter, IRequestCycle)} (that is, an
60      * implementation in a subclass), to obtain an id and render an id attribute. Reads
61      * {@link #getIdParameter()}.
62      */

63
64     protected void renderIdAttribute(IMarkupWriter writer, IRequestCycle cycle)
65     {
66         String JavaDoc rawId = getIdParameter();
67
68         if (rawId == null)
69             return;
70
71         String JavaDoc id = cycle.getUniqueId(rawId);
72
73         // Store for later access by the FieldLabel (or JavaScript).
74

75         setClientId(id);
76
77         writer.attribute("id", id);
78     }
79
80     /**
81      * @see org.apache.tapestry.AbstractComponent#renderComponent(org.apache.tapestry.IMarkupWriter,
82      * org.apache.tapestry.IRequestCycle)
83      */

84     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
85     {
86         IForm form = TapestryUtils.getForm(cycle, this);
87
88         setForm(form);
89
90         if (form.wasPrerendered(writer, this))
91             return;
92
93         IValidationDelegate delegate = form.getDelegate();
94
95         delegate.setFormComponent(this);
96
97         setName(form);
98
99         if (form.isRewinding())
100         {
101             if (!isDisabled())
102             {
103                 rewindFormComponent(writer, cycle);
104             }
105         }
106         else if (!cycle.isRewinding())
107         {
108             renderFormComponent(writer, cycle);
109
110             if (delegate.isInError())
111             {
112                 select(cycle);
113             }
114         }
115     }
116
117     protected void select(IRequestCycle cycle)
118     {
119         if (cycle.getAttribute(SELECTED_ATTRIBUTE_NAME) == null)
120         {
121             PageRenderSupport pageRenderSupport = TapestryUtils.getOptionalPageRenderSupport(cycle);
122
123             if (pageRenderSupport != null)
124             {
125                 String JavaDoc formName = getForm().getName();
126                 String JavaDoc fieldName = getName();
127
128                 String JavaDoc script = "focus(document." + formName + "." + fieldName + ")";
129
130                 pageRenderSupport.addInitializationScript(script);
131
132                 // Put a marker in, indicating that the selected field is known.
133

134                 cycle.setAttribute(SELECTED_ATTRIBUTE_NAME, Boolean.TRUE);
135             }
136         }
137     }
138
139     protected void renderDelegatePrefix(IMarkupWriter writer, IRequestCycle cycle)
140     {
141         getForm().getDelegate().writePrefix(writer, cycle, this, null);
142     }
143
144     protected void renderDelegateAttributes(IMarkupWriter writer, IRequestCycle cycle)
145     {
146         getForm().getDelegate().writeAttributes(writer, cycle, this, null);
147     }
148
149     protected void renderDelegateSuffix(IMarkupWriter writer, IRequestCycle cycle)
150     {
151         getForm().getDelegate().writeSuffix(writer, cycle, this, null);
152     }
153
154     protected void setName(IForm form)
155     {
156         form.getElementId(this);
157     }
158
159     protected abstract void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle);
160
161     protected abstract void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle);
162 }
Popular Tags