|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
PropertySelection.java | 58.8% | 65.2% | 66.7% | 63.1% |
|
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.IForm;
|
|
18 |
import org.apache.tapestry.IMarkupWriter;
|
|
19 |
import org.apache.tapestry.IRequestCycle;
|
|
20 |
import org.apache.tapestry.Tapestry;
|
|
21 |
|
|
22 |
/**
|
|
23 |
* A component used to render a drop-down list of options that the user may select. [ <a
|
|
24 |
* href="../../../../../ComponentReference/PropertySelection.html">Component Reference </a>]
|
|
25 |
* <p>
|
|
26 |
* Earlier versions of PropertySelection (through release 2.2) were more flexible, they included a
|
|
27 |
* <b>renderer </b> property that controlled how the selection was rendered. Ultimately, this proved
|
|
28 |
* of little value and this portion of functionality was deprecated in 2.3 and will be removed in
|
|
29 |
* 2.3.
|
|
30 |
* <p>
|
|
31 |
* Typically, the values available to be selected are defined using an
|
|
32 |
* {@link org.apache.commons.lang.enum.Enum}. A PropertySelection is dependent on an
|
|
33 |
* {@link IPropertySelectionModel}to provide the list of possible values.
|
|
34 |
* <p>
|
|
35 |
* Often, this is used to select a particular {@link org.apache.commons.lang.enum.Enum}to assign to
|
|
36 |
* a property; the {@link EnumPropertySelectionModel}class simplifies this.
|
|
37 |
*
|
|
38 |
* @author Howard Lewis Ship
|
|
39 |
*/
|
|
40 |
|
|
41 |
public abstract class PropertySelection extends AbstractFormComponent |
|
42 |
{ |
|
43 |
/**
|
|
44 |
* A shared instance of {@link SelectPropertySelectionRenderer}.
|
|
45 |
*/
|
|
46 |
|
|
47 |
public static final IPropertySelectionRenderer DEFAULT_SELECT_RENDERER = new SelectPropertySelectionRenderer(); |
|
48 |
|
|
49 |
/**
|
|
50 |
* A shared instance of {@link RadioPropertySelectionRenderer}.
|
|
51 |
*/
|
|
52 |
|
|
53 |
public static final IPropertySelectionRenderer DEFAULT_RADIO_RENDERER = new RadioPropertySelectionRenderer(); |
|
54 |
|
|
55 |
/**
|
|
56 |
* Renders the component, much of which is the responsiblity of the
|
|
57 |
* {@link IPropertySelectionRenderer renderer}. The possible options, thier labels, and the
|
|
58 |
* values to be encoded in the form are provided by the {@link IPropertySelectionModel model}.
|
|
59 |
*/
|
|
60 |
|
|
61 | 19 |
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
62 |
{ |
|
63 | 19 |
IForm form = getForm(cycle); |
64 |
|
|
65 | 19 |
if (form.wasPrerendered(writer, this)) |
66 | 0 |
return;
|
67 |
|
|
68 | 19 |
boolean rewinding = form.isRewinding();
|
69 |
|
|
70 | 19 |
String name = form.getElementId(this);
|
71 |
|
|
72 | 19 |
if (rewinding)
|
73 |
{ |
|
74 |
// If disabled, ignore anything that comes up from the client.
|
|
75 |
|
|
76 | 10 |
if (isDisabled())
|
77 | 0 |
return;
|
78 |
|
|
79 | 10 |
String optionValue = cycle.getParameter(name); |
80 |
|
|
81 | 10 |
Object value = (optionValue == null) ? null : getModel().translateValue(optionValue); |
82 |
|
|
83 | 10 |
setValue(value); |
84 |
|
|
85 | 10 |
return;
|
86 |
} |
|
87 |
|
|
88 | 9 |
IPropertySelectionRenderer renderer = getRenderer(); |
89 |
|
|
90 | 9 |
if (renderer != null) |
91 |
{ |
|
92 | 0 |
renderWithRenderer(writer, cycle, renderer); |
93 | 0 |
return;
|
94 |
} |
|
95 |
|
|
96 | 9 |
writer.begin("select");
|
97 | 9 |
writer.attribute("name", name);
|
98 |
|
|
99 | 9 |
if (isDisabled())
|
100 | 0 |
writer.attribute("disabled", "disabled"); |
101 |
|
|
102 | 9 |
if (getSubmitOnChange())
|
103 | 0 |
writer.attribute("onchange", "javascript:this.form.submit();"); |
104 |
|
|
105 |
// Apply informal attributes.
|
|
106 |
|
|
107 | 9 |
renderInformalParameters(writer, cycle); |
108 |
|
|
109 | 9 |
writer.println(); |
110 |
|
|
111 | 9 |
IPropertySelectionModel model = getModel(); |
112 |
|
|
113 | 9 |
if (model == null) |
114 | 0 |
throw Tapestry.createRequiredParameterException(this, "model"); |
115 |
|
|
116 | 9 |
int count = model.getOptionCount();
|
117 | 9 |
boolean foundSelected = false; |
118 | 9 |
boolean selected = false; |
119 | 9 |
Object value = getValue(); |
120 |
|
|
121 | 9 |
for (int i = 0; i < count; i++) |
122 |
{ |
|
123 | 39 |
Object option = model.getOption(i); |
124 |
|
|
125 | 39 |
if (!foundSelected)
|
126 |
{ |
|
127 | 21 |
selected = isEqual(option, value); |
128 | 21 |
if (selected)
|
129 | 9 |
foundSelected = true;
|
130 |
} |
|
131 |
|
|
132 | 39 |
writer.begin("option");
|
133 | 39 |
writer.attribute("value", model.getValue(i));
|
134 |
|
|
135 | 39 |
if (selected)
|
136 | 9 |
writer.attribute("selected", "selected"); |
137 |
|
|
138 | 39 |
writer.print(model.getLabel(i)); |
139 |
|
|
140 | 39 |
writer.end(); |
141 |
|
|
142 | 39 |
writer.println(); |
143 |
|
|
144 | 39 |
selected = false;
|
145 |
} |
|
146 |
|
|
147 | 9 |
writer.end(); // <select>
|
148 |
|
|
149 |
} |
|
150 |
|
|
151 |
/**
|
|
152 |
* Renders the property selection using a {@link IPropertySelectionRenderer}. Support for this
|
|
153 |
* will be removed in 2.3.
|
|
154 |
*/
|
|
155 |
|
|
156 | 0 |
private void renderWithRenderer(IMarkupWriter writer, IRequestCycle cycle, |
157 |
IPropertySelectionRenderer renderer) |
|
158 |
{ |
|
159 | 0 |
renderer.beginRender(this, writer, cycle);
|
160 |
|
|
161 | 0 |
IPropertySelectionModel model = getModel(); |
162 |
|
|
163 | 0 |
int count = model.getOptionCount();
|
164 |
|
|
165 | 0 |
boolean foundSelected = false; |
166 | 0 |
boolean selected = false; |
167 |
|
|
168 | 0 |
Object value = getValue(); |
169 |
|
|
170 | 0 |
for (int i = 0; i < count; i++) |
171 |
{ |
|
172 | 0 |
Object option = model.getOption(i); |
173 |
|
|
174 | 0 |
if (!foundSelected)
|
175 |
{ |
|
176 | 0 |
selected = isEqual(option, value); |
177 | 0 |
if (selected)
|
178 | 0 |
foundSelected = true;
|
179 |
} |
|
180 |
|
|
181 | 0 |
renderer.renderOption(this, writer, cycle, model, option, i, selected);
|
182 |
|
|
183 | 0 |
selected = false;
|
184 |
} |
|
185 |
|
|
186 |
// A PropertySelection doesn't allow a body, so no need to worry about
|
|
187 |
// wrapped components.
|
|
188 |
|
|
189 | 0 |
renderer.endRender(this, writer, cycle);
|
190 |
} |
|
191 |
|
|
192 | 21 |
private boolean isEqual(Object left, Object right) |
193 |
{ |
|
194 |
// Both null, or same object, then are equal
|
|
195 |
|
|
196 | 21 |
if (left == right)
|
197 | 9 |
return true; |
198 |
|
|
199 |
// If one is null, the other isn't, then not equal.
|
|
200 |
|
|
201 | 12 |
if (left == null || right == null) |
202 | 0 |
return false; |
203 |
|
|
204 |
// Both non-null; use standard comparison.
|
|
205 |
|
|
206 | 12 |
return left.equals(right);
|
207 |
} |
|
208 |
|
|
209 |
public abstract IPropertySelectionModel getModel();
|
|
210 |
|
|
211 |
public abstract IPropertySelectionRenderer getRenderer();
|
|
212 |
|
|
213 |
/** @since 2.2 * */
|
|
214 |
|
|
215 |
public abstract boolean getSubmitOnChange(); |
|
216 |
|
|
217 |
/** @since 2.2 * */
|
|
218 |
|
|
219 |
public abstract Object getValue();
|
|
220 |
|
|
221 |
/** @since 2.2 * */
|
|
222 |
|
|
223 |
public abstract void setValue(Object value); |
|
224 |
|
|
225 |
/**
|
|
226 |
* Returns true if this PropertySelection's disabled parameter yields true. The corresponding
|
|
227 |
* HTML control(s) should be disabled.
|
|
228 |
*/
|
|
229 |
|
|
230 |
public abstract boolean isDisabled(); |
|
231 |
|
|
232 |
} |
|