Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
AbstractFormComponent |
|
| 1.7058823529411764;1.706 |
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 | 104 | 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 | 30 | 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 | 63 | IForm form = TapestryUtils.getForm(cycle, this); |
74 | ||
75 | 63 | setForm(form); |
76 | ||
77 | 63 | if (form.wasPrerendered(writer, this)) |
78 | 5 | return; |
79 | ||
80 | 58 | IValidationDelegate delegate = form.getDelegate(); |
81 | ||
82 | 58 | delegate.setFormComponent(this); |
83 | ||
84 | 58 | setName(form); |
85 | ||
86 | 58 | if (form.isRewinding()) |
87 | { |
|
88 | 21 | if (!isDisabled()) |
89 | { |
|
90 | 17 | 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 | 20 | if (getRenderBodyOnRewind()) |
97 | 1 | renderBody(writer, cycle); |
98 | } |
|
99 | 37 | else if (!cycle.isRewinding()) |
100 | { |
|
101 | 33 | if (!NullWriter.class.isInstance(writer)) |
102 | 32 | form.setFormFieldUpdating(true); |
103 | ||
104 | 32 | renderFormComponent(writer, cycle); |
105 | ||
106 | 31 | if (getCanTakeFocus() && !isDisabled()) |
107 | { |
|
108 | 24 | delegate.registerForFocus(this, |
109 | delegate.isInError() |
|
110 | ? ValidationConstants.ERROR_FIELD |
|
111 | : ValidationConstants.NORMAL_FIELD); |
|
112 | } |
|
113 | ||
114 | } |
|
115 | 56 | } |
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 | 19 | return false; |
130 | } |
|
131 | ||
132 | protected void renderDelegatePrefix(IMarkupWriter writer, IRequestCycle cycle) |
|
133 | { |
|
134 | 21 | getForm().getDelegate().writePrefix(writer, cycle, this, null); |
135 | 21 | } |
136 | ||
137 | protected void renderDelegateAttributes(IMarkupWriter writer, IRequestCycle cycle) |
|
138 | { |
|
139 | 17 | getForm().getDelegate().writeAttributes(writer, cycle, this, null); |
140 | 17 | } |
141 | ||
142 | protected void renderDelegateSuffix(IMarkupWriter writer, IRequestCycle cycle) |
|
143 | { |
|
144 | 21 | getForm().getDelegate().writeSuffix(writer, cycle, this, null); |
145 | 21 | } |
146 | ||
147 | protected void setName(IForm form) |
|
148 | { |
|
149 | 50 | setName(form.getElementId(this)); |
150 | 50 | } |
151 | ||
152 | /** |
|
153 | * {@inheritDoc} |
|
154 | */ |
|
155 | protected void generateClientId() |
|
156 | { |
|
157 | 38 | } |
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 | } |