Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractFormComponentContributor |
|
| 1.25;1.25 |
1 | // Copyright 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.hivemind.util.PropertyUtils; | |
18 | import org.apache.tapestry.IMarkupWriter; | |
19 | import org.apache.tapestry.IRequestCycle; | |
20 | import org.apache.tapestry.json.JSONObject; | |
21 | ||
22 | /** | |
23 | * Abstract {@link FormComponentContributor} implementation that adds an optional static javscript | |
24 | * method reference to the page. | |
25 | * | |
26 | * @author Paul Ferraro | |
27 | * @since 4.0 | |
28 | */ | |
29 | public abstract class AbstractFormComponentContributor implements FormComponentContributor | |
30 | { | |
31 | 0 | private String _script = defaultScript(); |
32 | ||
33 | public AbstractFormComponentContributor() | |
34 | 0 | { |
35 | 0 | } |
36 | ||
37 | // Needed until HIVEMIND-134 fix is available | |
38 | public AbstractFormComponentContributor(String initializer) | |
39 | 0 | { |
40 | 0 | PropertyUtils.configureProperties(this, initializer); |
41 | 0 | } |
42 | ||
43 | /** | |
44 | * Defines the default JavaScript file used by this contributor. Overriden by most subclasses | |
45 | * that use JavaScript. | |
46 | */ | |
47 | public String defaultScript() | |
48 | { | |
49 | 0 | return null; |
50 | } | |
51 | ||
52 | public String getScript() | |
53 | { | |
54 | 0 | return _script; |
55 | } | |
56 | ||
57 | public void setScript(String script) | |
58 | { | |
59 | 0 | _script = script; |
60 | 0 | } |
61 | ||
62 | /** | |
63 | * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IMarkupWriter, | |
64 | * org.apache.tapestry.IRequestCycle, FormComponentContributorContext, | |
65 | * org.apache.tapestry.form.IFormComponent) | |
66 | */ | |
67 | public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, | |
68 | FormComponentContributorContext context, IFormComponent field) | |
69 | { | |
70 | 0 | if (_script != null) |
71 | 0 | context.includeClasspathScript(_script); |
72 | 0 | } |
73 | ||
74 | /** | |
75 | * Utility used to append onto an existing property represented as an | |
76 | * object array. | |
77 | * @param profile | |
78 | * @param key | |
79 | * @param value | |
80 | */ | |
81 | public void accumulateProperty(JSONObject profile, String key, Object value) | |
82 | { | |
83 | 0 | profile.accumulate(key, value); |
84 | 0 | } |
85 | ||
86 | /** | |
87 | * Utility method to store a field specific profile property which can later | |
88 | * be used by client side validation. | |
89 | * | |
90 | * @param field | |
91 | * The field to store the property for, will key off of {@link IFormComponent#getClientId()}. | |
92 | * @param profile | |
93 | * The profile for the form. | |
94 | * @param key | |
95 | * The property key to store. | |
96 | * @param property | |
97 | * The property to store. | |
98 | */ | |
99 | public void accumulateProfileProperty(IFormComponent field, JSONObject profile, | |
100 | String key, Object property) | |
101 | { | |
102 | 0 | if (!profile.has(field.getClientId())) |
103 | 0 | profile.put(field.getClientId(), new JSONObject()); |
104 | ||
105 | 0 | JSONObject fieldProps = profile.getJSONObject(field.getClientId()); |
106 | ||
107 | 0 | accumulateProperty(fieldProps, key, property); |
108 | 0 | } |
109 | } |