Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
MultiplePropertySelection |
|
| 1.8;1.8 |
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.contrib.form; |
|
16 | ||
17 | import java.util.ArrayList; |
|
18 | import java.util.Collection; |
|
19 | import java.util.List; |
|
20 | ||
21 | import org.apache.tapestry.IMarkupWriter; |
|
22 | import org.apache.tapestry.IRequestCycle; |
|
23 | import org.apache.tapestry.Tapestry; |
|
24 | import org.apache.tapestry.form.AbstractFormComponent; |
|
25 | import org.apache.tapestry.form.IPropertySelectionModel; |
|
26 | import org.apache.tapestry.form.ValidatableField; |
|
27 | import org.apache.tapestry.form.ValidatableFieldSupport; |
|
28 | import org.apache.tapestry.valid.ValidatorException; |
|
29 | ||
30 | /** |
|
31 | * A component which uses <input type=checkbox> to set a property of some |
|
32 | * object. Typically, the values for the object are defined using an |
|
33 | * {@link java.lang.Enum}. A MultiplePropertySelection is |
|
34 | * dependent on an {link IPropertySelectionModel} to provide the list of |
|
35 | * possible values. |
|
36 | * <p> |
|
37 | * Often, this is used to select one or more |
|
38 | * {@link java.lang.Enum} to assign to a property; the |
|
39 | * {@link org.apache.tapestry.form.EnumPropertySelectionModel}class simplifies |
|
40 | * this. |
|
41 | * <p> |
|
42 | * The {@link org.apache.tapestry.contrib.palette.Palette}component is more |
|
43 | * powerful, but requires client-side JavaScript and is not fully cross-browser |
|
44 | * compatible. |
|
45 | * <p> |
|
46 | * <table border=1> |
|
47 | * <tr> |
|
48 | * <td>Parameter</td> |
|
49 | * <td>Type</td> |
|
50 | * <td>Direction</td> |
|
51 | * <td>Required</td> |
|
52 | * <td>Default</td> |
|
53 | * <td>Description</td> |
|
54 | * </tr> |
|
55 | * <tr> |
|
56 | * <td>selectedList</td> |
|
57 | * <td>java.util.List</td> |
|
58 | * <td>in-out</td> |
|
59 | * <td>yes</td> |
|
60 | * <td> </td> |
|
61 | * <td>The property to set. During rendering, this property is read, and sets |
|
62 | * the default value of the options in the select. When the form is submitted, |
|
63 | * list is cleared, then has each selected option added to it.</td> |
|
64 | * </tr> |
|
65 | * <tr> |
|
66 | * <td>renderer</td> |
|
67 | * <td>{@link IMultiplePropertySelectionRenderer}</td> |
|
68 | * <td>in</td> |
|
69 | * <td>no</td> |
|
70 | * <td>shared instance of {@link CheckBoxMultiplePropertySelectionRenderer}</td> |
|
71 | * <td>Defines the object used to render this component. The default renders a |
|
72 | * table of checkboxes. </td> |
|
73 | * </tr> |
|
74 | * <tr> |
|
75 | * <td>model</td> |
|
76 | * <td>{@link IPropertySelectionModel}</td> |
|
77 | * <td>in</td> |
|
78 | * <td>yes</td> |
|
79 | * <td> </td> |
|
80 | * <td>The model provides a list of possible labels, and matches those labels |
|
81 | * against possible values that can be assigned back to the property.</td> |
|
82 | * </tr> |
|
83 | * <tr> |
|
84 | * <td>disabled</td> |
|
85 | * <td>boolean</td> |
|
86 | * <td>in</td> |
|
87 | * <td>no</td> |
|
88 | * <td>false</td> |
|
89 | * <td>Controls whether the <select> is active or not. A disabled |
|
90 | * PropertySelection does not update its value parameter. |
|
91 | * <p> |
|
92 | * Corresponds to the <code>disabled</code> HTML attribute.</td> |
|
93 | * </tr> |
|
94 | * </table> |
|
95 | * <p> |
|
96 | * Informal parameters are not allowed. |
|
97 | * <p> |
|
98 | * As of 4.0, this component can be validated. |
|
99 | * |
|
100 | * @author Sanjay Munjal |
|
101 | */ |
|
102 | ||
103 | 0 | public abstract class MultiplePropertySelection extends AbstractFormComponent |
104 | implements ValidatableField |
|
105 | { |
|
106 | ||
107 | /** |
|
108 | * A shared instance of {@link CheckBoxMultiplePropertySelectionRenderer}. |
|
109 | */ |
|
110 | 0 | public static final IMultiplePropertySelectionRenderer DEFAULT_CHECKBOX_RENDERER = new CheckBoxMultiplePropertySelectionRenderer(); |
111 | ||
112 | public abstract Collection getSelectedList(); |
|
113 | ||
114 | public abstract void setSelectedList(Collection selectedList); |
|
115 | ||
116 | protected void finishLoad() |
|
117 | { |
|
118 | 0 | setRenderer(DEFAULT_CHECKBOX_RENDERER); |
119 | 0 | } |
120 | ||
121 | protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
|
122 | { |
|
123 | 0 | Collection selectedList = getSelectedList(); |
124 | ||
125 | 0 | if (selectedList == null) |
126 | 0 | throw Tapestry.createRequiredParameterException(this, |
127 | "selectedList"); |
|
128 | ||
129 | 0 | IPropertySelectionModel model = getModel(); |
130 | ||
131 | 0 | if (model == null) |
132 | 0 | throw Tapestry.createRequiredParameterException(this, "model"); |
133 | ||
134 | 0 | IMultiplePropertySelectionRenderer renderer = getRenderer(); |
135 | ||
136 | // Start rendering |
|
137 | 0 | renderer.beginRender(this, writer, cycle); |
138 | ||
139 | 0 | int count = model.getOptionCount(); |
140 | ||
141 | 0 | for(int i = 0; i < count; i++) |
142 | { |
|
143 | 0 | Object option = model.getOption(i); |
144 | ||
145 | // Try to find the option in the list and if yes, then it is |
|
146 | // checked. |
|
147 | 0 | boolean optionSelected = selectedList.contains(option); |
148 | ||
149 | 0 | renderer.renderOption(this, writer, cycle, model, option, i, |
150 | optionSelected); |
|
151 | } |
|
152 | ||
153 | // A PropertySelection doesn't allow a body, so no need to worry about |
|
154 | // wrapped components. |
|
155 | 0 | renderer.endRender(this, writer, cycle); |
156 | 0 | } |
157 | ||
158 | /** |
|
159 | * @see org.apache.tapestry.form.AbstractRequirableField#rewindFormComponent(org.apache.tapestry.IMarkupWriter, |
|
160 | * org.apache.tapestry.IRequestCycle) |
|
161 | */ |
|
162 | protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
|
163 | { |
|
164 | // get all the values |
|
165 | 0 | String[] optionValues = cycle.getParameters(getName()); |
166 | ||
167 | 0 | IPropertySelectionModel model = getModel(); |
168 | ||
169 | 0 | List selectedList = new ArrayList(getModel().getOptionCount()); |
170 | ||
171 | // Nothing was selected |
|
172 | 0 | if (optionValues != null) |
173 | { |
|
174 | // Go through the array and translate and put back in the list |
|
175 | 0 | for(int i = 0; i < optionValues.length; i++) |
176 | { |
|
177 | // Translate the new value |
|
178 | 0 | Object selectedValue = model.translateValue(optionValues[i]); |
179 | ||
180 | // Add this element in the list back |
|
181 | 0 | selectedList.add(selectedValue); |
182 | } |
|
183 | } |
|
184 | ||
185 | try |
|
186 | { |
|
187 | 0 | getValidatableFieldSupport().validate(this, writer, cycle, |
188 | selectedList); |
|
189 | ||
190 | 0 | setSelectedList(selectedList); |
191 | } |
|
192 | 0 | catch (ValidatorException e) |
193 | { |
|
194 | 0 | getForm().getDelegate().record(e); |
195 | 0 | } |
196 | 0 | } |
197 | ||
198 | public abstract IPropertySelectionModel getModel(); |
|
199 | ||
200 | public abstract IMultiplePropertySelectionRenderer getRenderer(); |
|
201 | ||
202 | public abstract void setRenderer(IMultiplePropertySelectionRenderer renderer); |
|
203 | ||
204 | public abstract ValidatableFieldSupport getValidatableFieldSupport(); |
|
205 | ||
206 | /** |
|
207 | * @see org.apache.tapestry.form.AbstractFormComponent#isRequired() |
|
208 | */ |
|
209 | public boolean isRequired() |
|
210 | { |
|
211 | 0 | return getValidatableFieldSupport().isRequired(this); |
212 | } |
|
213 | } |