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