Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractFormComponent |
|
| 1.8235294117647058;1.824 |
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.*; | |
18 | import org.apache.tapestry.engine.NullWriter; | |
19 | import org.apache.tapestry.valid.IValidationDelegate; | |
20 | import org.apache.tapestry.valid.ValidationConstants; | |
21 | ||
22 | /** | |
23 | * A base class for building components that correspond to HTML form elements. All such components | |
24 | * must be wrapped (directly or indirectly) by a {@link Form} component. | |
25 | * | |
26 | * @author Howard Lewis Ship | |
27 | * @author Paul Ferraro | |
28 | * @since 1.0.3 | |
29 | */ | |
30 | 0 | public abstract class AbstractFormComponent extends AbstractComponent implements IFormComponent |
31 | { | |
32 | ||
33 | public abstract IForm getForm(); | |
34 | ||
35 | public abstract void setForm(IForm form); | |
36 | ||
37 | public abstract String getName(); | |
38 | ||
39 | public abstract void setName(String name); | |
40 | ||
41 | /** | |
42 | * Returns true if the corresponding field, on the client side, can accept user focus (i.e., | |
43 | * implements the focus() method). Most components can take focus (if not disabled), but a few ({@link Hidden}) | |
44 | * override this method to always return false. | |
45 | */ | |
46 | ||
47 | protected boolean getCanTakeFocus() | |
48 | { | |
49 | 0 | return !isDisabled(); |
50 | } | |
51 | ||
52 | /** | |
53 | * Should be connected to a parameter named "id" (annotations would be helpful here!). For | |
54 | * components w/o such a parameter, this will simply return null. | |
55 | */ | |
56 | ||
57 | public abstract String getIdParameter(); | |
58 | ||
59 | /** | |
60 | * Invoked by {@link AbstractComponent#render(IMarkupWriter, IRequestCycle)} to actually | |
61 | * render the component (with any parameter values already set). | |
62 | * This implementation checks the rewinding state of the {@link IForm} that contains the | |
63 | * component and forwards processing to either | |
64 | * {@link #renderFormComponent(IMarkupWriter, IRequestCycle)} or | |
65 | * {@link #rewindFormComponent(IMarkupWriter, IRequestCycle)}. | |
66 | * Those two are the methods that subclasses should implement. | |
67 | * | |
68 | * @see org.apache.tapestry.AbstractComponent#renderComponent(org.apache.tapestry.IMarkupWriter, | |
69 | * org.apache.tapestry.IRequestCycle) | |
70 | */ | |
71 | protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) | |
72 | { | |
73 | 0 | IForm form = TapestryUtils.getForm(cycle, this); |
74 | ||
75 | 0 | setForm(form); |
76 | ||
77 | 0 | if (form.wasPrerendered(writer, this)) |
78 | 0 | return; |
79 | ||
80 | 0 | IValidationDelegate delegate = form.getDelegate(); |
81 | ||
82 | 0 | delegate.setFormComponent(this); |
83 | ||
84 | 0 | setName(form); |
85 | ||
86 | 0 | if (form.isRewinding()) |
87 | { | |
88 | 0 | if (!isDisabled()) |
89 | { | |
90 | 0 | rewindFormComponent(writer, cycle); |
91 | } | |
92 | ||
93 | // This is for the benefit of the couple of components (LinkSubmit) that allow a body. | |
94 | // The body should render when the component rewinds. | |
95 | ||
96 | 0 | if (getRenderBodyOnRewind()) |
97 | 0 | renderBody(writer, cycle); |
98 | } | |
99 | 0 | else if (!cycle.isRewinding()) |
100 | { | |
101 | 0 | if (!NullWriter.class.isInstance(writer)) |
102 | 0 | form.setFormFieldUpdating(true); |
103 | ||
104 | 0 | renderFormComponent(writer, cycle); |
105 | ||
106 | 0 | if (getCanTakeFocus() && !isDisabled()) |
107 | { | |
108 | 0 | delegate.registerForFocus(this, |
109 | delegate.isInError() | |
110 | ? ValidationConstants.ERROR_FIELD | |
111 | : ValidationConstants.NORMAL_FIELD); | |
112 | } | |
113 | ||
114 | } | |
115 | 0 | } |
116 | ||
117 | /** | |
118 | * A small number of components should always render their body on rewind (even if the component | |
119 | * is itself disabled) and should override this method to return true. Components that | |
120 | * explicitly render their body inside | |
121 | * {@link #rewindFormComponent(IMarkupWriter, IRequestCycle)} should leave this method returning | |
122 | * false. Remember that if the component is {@link IFormComponent#isDisabled() disabled} then | |
123 | * {@link #rewindFormComponent(IMarkupWriter, IRequestCycle)} won't be invoked. | |
124 | * | |
125 | * @return false; override this method to change. | |
126 | */ | |
127 | protected boolean getRenderBodyOnRewind() | |
128 | { | |
129 | 0 | return false; |
130 | } | |
131 | ||
132 | protected void renderDelegatePrefix(IMarkupWriter writer, IRequestCycle cycle) | |
133 | { | |
134 | 0 | getForm().getDelegate().writePrefix(writer, cycle, this, null); |
135 | 0 | } |
136 | ||
137 | protected void renderDelegateAttributes(IMarkupWriter writer, IRequestCycle cycle) | |
138 | { | |
139 | 0 | getForm().getDelegate().writeAttributes(writer, cycle, this, null); |
140 | 0 | } |
141 | ||
142 | protected void renderDelegateSuffix(IMarkupWriter writer, IRequestCycle cycle) | |
143 | { | |
144 | 0 | getForm().getDelegate().writeSuffix(writer, cycle, this, null); |
145 | 0 | } |
146 | ||
147 | protected void setName(IForm form) | |
148 | { | |
149 | 0 | setName(form.getElementId(this)); |
150 | 0 | } |
151 | ||
152 | /** | |
153 | * {@inheritDoc} | |
154 | */ | |
155 | protected void generateClientId() | |
156 | { | |
157 | 0 | } |
158 | ||
159 | /** | |
160 | * {@inheritDoc} | |
161 | */ | |
162 | public String peekClientId() | |
163 | { | |
164 | 0 | if (getPage() == null) |
165 | 0 | return null; |
166 | ||
167 | 0 | IForm form = (IForm) getPage().getRequestCycle().getAttribute(TapestryUtils.FORM_ATTRIBUTE); |
168 | 0 | if (form == null) |
169 | 0 | return null; |
170 | ||
171 | 0 | return form.peekClientId(this); |
172 | } | |
173 | ||
174 | /** | |
175 | * Returns false. Subclasses that might be required must override this method. Typically, this | |
176 | * involves checking against the component's validators. | |
177 | * | |
178 | * @since 4.0 | |
179 | */ | |
180 | public boolean isRequired() | |
181 | { | |
182 | 0 | return false; |
183 | } | |
184 | ||
185 | /** | |
186 | * Invoked from {@link #renderComponent(IMarkupWriter, IRequestCycle)} | |
187 | * to render the component. | |
188 | * | |
189 | * @param writer | |
190 | * @param cycle | |
191 | */ | |
192 | protected abstract void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle); | |
193 | ||
194 | /** | |
195 | * Invoked from {@link #renderComponent(IMarkupWriter, IRequestCycle)} to rewind the | |
196 | * component. If the component is {@link IFormComponent#isDisabled() disabled} | |
197 | * this will not be invoked. | |
198 | * | |
199 | * @param writer | |
200 | * @param cycle | |
201 | */ | |
202 | protected abstract void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle); | |
203 | } |