KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > form > MultiplePropertySelection


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 JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.apache.tapestry.IMarkupWriter;
21 import org.apache.tapestry.IRequestCycle;
22 import org.apache.tapestry.Tapestry;
23 import org.apache.tapestry.form.AbstractRequirableField;
24 import org.apache.tapestry.form.IPropertySelectionModel;
25 import org.apache.tapestry.valid.ValidationStrings;
26 import org.apache.tapestry.valid.ValidatorException;
27
28 /**
29  * A component which uses <input type=checkbox> to set a property of some object. Typically,
30  * the values for the object are defined using an {@link org.apache.commons.lang.enum.Enum}. A
31  * MultiplePropertySelection is dependent on an {link IPropertySelectionModel} to provide the list
32  * of possible values.
33  * <p>
34  * Often, this is used to select one or more {@link org.apache.commons.lang.enum.Enum}to assign to
35  * a property; the {@link org.apache.tapestry.form.EnumPropertySelectionModel}class simplifies
36  * this.
37  * <p>
38  * The {@link org.apache.tapestry.contrib.palette.Palette}component is more powerful, but requires
39  * client-side JavaScript and is not fully cross-browser compatible.
40  * <p>
41  * <table border=1>
42  * <tr>
43  * <td>Parameter</td>
44  * <td>Type</td>
45  * <td>Direction</td>
46  * <td>Required</td>
47  * <td>Default</td>
48  * <td>Description</td>
49  * </tr>
50  * <tr>
51  * <td>selectedList</td>
52  * <td>java.util.List</td>
53  * <td>in-out</td>
54  * <td>yes</td>
55  * <td>&nbsp;</td>
56  * <td>The property to set. During rendering, this property is read, and sets the default value of
57  * the options in the select. When the form is submitted, list is cleared, then has each selected
58  * option added to it.</td>
59  * </tr>
60  * <tr>
61  * <td>renderer</td>
62  * <td>{@link IMultiplePropertySelectionRenderer}</td>
63  * <td>in</td>
64  * <td>no</td>
65  * <td>shared instance of {@link CheckBoxMultiplePropertySelectionRenderer}</td>
66  * <td>Defines the object used to render this component. The default renders a table of checkboxes.
67  * </td>
68  * </tr>
69  * <tr>
70  * <td>model</td>
71  * <td>{@link IPropertySelectionModel}</td>
72  * <td>in</td>
73  * <td>yes</td>
74  * <td>&nbsp;</td>
75  * <td>The model provides a list of possible labels, and matches those labels against possible
76  * values that can be assigned back to the property.</td>
77  * </tr>
78  * <tr>
79  * <td>disabled</td>
80  * <td>boolean</td>
81  * <td>in</td>
82  * <td>no</td>
83  * <td>false</td>
84  * <td>Controls whether the &lt;select&gt; is active or not. A disabled PropertySelection does not
85  * update its value parameter.
86  * <p>
87  * Corresponds to the <code>disabled</code> HTML attribute.</td>
88  * </tr>
89  * </table>
90  * <p>
91  * Informal parameters are not allowed.
92  *
93  * As of 4.0, MultiplePropertySelection can indicate that it is required.
94  *
95  * @author Sanjay Munjal
96  */

97
98 public abstract class MultiplePropertySelection extends AbstractRequirableField
99 {
100     /**
101      * A shared instance of {@link CheckBoxMultiplePropertySelectionRenderer}.
102      */

103     public static final IMultiplePropertySelectionRenderer DEFAULT_CHECKBOX_RENDERER = new CheckBoxMultiplePropertySelectionRenderer();
104
105     public abstract List JavaDoc getSelectedList();
106     
107     public abstract void setSelectedList(List JavaDoc selectedList);
108     
109     protected void finishLoad()
110     {
111         setRenderer(DEFAULT_CHECKBOX_RENDERER);
112         setRequiredMessage(ValidationStrings.getMessagePattern(ValidationStrings.REQUIRED_SELECT_FIELD, getPage().getLocale()));
113     }
114
115     /**
116      * @see org.apache.tapestry.form.AbstractRequirableField#getSubmittedValue(org.apache.tapestry.IRequestCycle)
117      */

118     public String JavaDoc getSubmittedValue(IRequestCycle cycle)
119     {
120         return super.getSubmittedValue(cycle);
121     }
122
123     /**
124      * @see org.apache.tapestry.form.AbstractRequirableField#renderFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
125      */

126     protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
127     {
128         super.renderFormComponent(writer, cycle);
129     }
130
131     /**
132      * @see org.apache.tapestry.form.AbstractRequirableField#rewindFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
133      */

134     protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
135     {
136         super.rewindFormComponent(writer, cycle);
137     }
138
139     /**
140      * @see org.apache.tapestry.form.RequirableField#bind(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
141      */

142     public void bind(IMarkupWriter writer, IRequestCycle cycle) throws ValidatorException
143     {
144         // get all the values
145
String JavaDoc[] optionValues = cycle.getParameters(getName());
146
147         IPropertySelectionModel model = getModel();
148         
149         List JavaDoc selectedList = new ArrayList JavaDoc(getModel().getOptionCount());
150
151         // Nothing was selected
152
if (optionValues != null)
153         {
154             // Go through the array and translate and put back in the list
155
for (int i = 0; i < optionValues.length; i++)
156             {
157                 // Translate the new value
158
Object JavaDoc selectedValue = model.translateValue(optionValues[i]);
159
160                 // Add this element in the list back
161
selectedList.add(selectedValue);
162             }
163         }
164         
165         this.setSelectedList(selectedList);
166     }
167
168     /**
169      * Renders the component, much of which is the responsiblity of the
170      * {@link IMultiplePropertySelectionRenderer renderer}. The possible options, their labels, and
171      * the values to be encoded in the form are provided by the
172      * {@link IPropertySelectionModel model}.
173      */

174     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
175     {
176         List JavaDoc selectedList = getSelectedList();
177
178         if (selectedList == null)
179             throw Tapestry.createRequiredParameterException(this, "selectedList");
180
181         IPropertySelectionModel model = getModel();
182
183         if (model == null)
184             throw Tapestry.createRequiredParameterException(this, "model");
185
186         IMultiplePropertySelectionRenderer renderer = getRenderer();
187
188         // Start rendering
189
renderer.beginRender(this, writer, cycle);
190
191         int count = model.getOptionCount();
192
193         for (int i = 0; i < count; i++)
194         {
195             Object JavaDoc option = model.getOption(i);
196
197             // Try to find the option in the list and if yes, then it is checked.
198
boolean optionSelected = selectedList.contains(option);
199
200             renderer.renderOption(this, writer, cycle, model, option, i, optionSelected);
201         }
202
203         // A PropertySelection doesn't allow a body, so no need to worry about
204
// wrapped components.
205
renderer.endRender(this, writer, cycle);
206     }
207
208     public abstract IPropertySelectionModel getModel();
209
210     public abstract IMultiplePropertySelectionRenderer getRenderer();
211
212     public abstract void setRenderer(IMultiplePropertySelectionRenderer renderer);
213 }
Popular Tags