|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
PropertySelection.java | 68.8% | 87.2% | 100% | 83.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.IMarkupWriter; | |
18 | import org.apache.tapestry.IRequestCycle; | |
19 | import org.apache.tapestry.Tapestry; | |
20 | import org.apache.tapestry.valid.ValidationStrings; | |
21 | import org.apache.tapestry.valid.ValidatorException; | |
22 | ||
23 | /** | |
24 | * A component used to render a drop-down list of options that the user may select. [ <a | |
25 | * href="../../../../../ComponentReference/PropertySelection.html">Component Reference </a>] | |
26 | * <p> | |
27 | * Earlier versions of PropertySelection (through release 2.2) were more flexible, they included a | |
28 | * <b>renderer </b> property that controlled how the selection was rendered. Ultimately, this proved | |
29 | * of little value and this portion of functionality was deprecated in 2.3 and will be removed in | |
30 | * 2.3. | |
31 | * <p> | |
32 | * Typically, the values available to be selected are defined using an | |
33 | * {@link org.apache.commons.lang.enum.Enum}. A PropertySelection is dependent on an | |
34 | * {@link IPropertySelectionModel} to provide the list of possible values. | |
35 | * <p> | |
36 | * Often, this is used to select a particular {@link org.apache.commons.lang.enum.Enum} to assign to | |
37 | * a property; the {@link EnumPropertySelectionModel} class simplifies this. As of 4.0, | |
38 | * PropertySelection can indicate that it is required. Often, a drop-down list will contain an | |
39 | * initial option that serves both as a label and to represent that nothing is selected. This can | |
40 | * behavior can easily be achieved by decorating an existing {@link IPropertySelectionModel} with a | |
41 | * {@link LabeledPropertySelectionModel}. | |
42 | * | |
43 | * @author Howard Lewis Ship | |
44 | * @author Paul Ferraro | |
45 | */ | |
46 | public abstract class PropertySelection extends AbstractRequirableField | |
47 | { | |
48 | /** | |
49 | * @see org.apache.tapestry.form.validator.AbstractRequirableField#bind(org.apache.tapestry.IRequestCycle, | |
50 | * java.lang.String) | |
51 | */ | |
52 | 3 | public void bind(IMarkupWriter writer, IRequestCycle cycle) throws ValidatorException |
53 | { | |
54 | 3 | if (isDisabled()) |
55 | 0 | return; |
56 | ||
57 | 3 | setValue(getModel().translateValue(getSubmittedValue(cycle))); |
58 | } | |
59 | ||
60 | /** | |
61 | * @see org.apache.tapestry.AbstractComponent#finishLoad() | |
62 | */ | |
63 | 1 | protected void finishLoad() |
64 | { | |
65 | 1 | setRequiredMessage(ValidationStrings.getMessagePattern( |
66 | ValidationStrings.REQUIRED_SELECT_FIELD, | |
67 | getPage().getLocale())); | |
68 | } | |
69 | ||
70 | /** | |
71 | * Renders the component. The possible options, their labels, and the values to be encoded in | |
72 | * the form are provided by the {@link IPropertySelectionModel model}. | |
73 | * | |
74 | * @see org.apache.tapestry.form.validator.AbstractRequirableField#renderRequirableFormComponent(org.apache.tapestry.IMarkupWriter, | |
75 | * org.apache.tapestry.IRequestCycle) | |
76 | */ | |
77 | 3 | protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
78 | { | |
79 | 3 | super.renderFormComponent(writer, cycle); |
80 | ||
81 | 3 | renderDelegatePrefix(writer, cycle); |
82 | ||
83 | 3 | writer.begin("select"); |
84 | 3 | writer.attribute("name", getName()); |
85 | ||
86 | 3 | if (isDisabled()) |
87 | 0 | writer.attribute("disabled", "disabled"); |
88 | ||
89 | 3 | if (getSubmitOnChange()) |
90 | 0 | writer.attribute("onchange", "this.form.submit()"); |
91 | ||
92 | 3 | renderIdAttribute(writer, cycle); |
93 | ||
94 | 3 | renderDelegateAttributes(writer, cycle); |
95 | ||
96 | // Apply informal attributes. | |
97 | 3 | renderInformalParameters(writer, cycle); |
98 | ||
99 | 3 | writer.println(); |
100 | ||
101 | 3 | IPropertySelectionModel model = getModel(); |
102 | ||
103 | 3 | if (model == null) |
104 | 0 | throw Tapestry.createRequiredParameterException(this, "model"); |
105 | ||
106 | 3 | int count = model.getOptionCount(); |
107 | 3 | boolean foundSelected = false; |
108 | 3 | Object value = getValue(); |
109 | ||
110 | 3 | for (int i = 0; i < count; i++) |
111 | { | |
112 | 21 | Object option = model.getOption(i); |
113 | ||
114 | 21 | writer.begin("option"); |
115 | 21 | writer.attribute("value", model.getValue(i)); |
116 | ||
117 | 21 | if (!foundSelected && isEqual(option, value)) |
118 | { | |
119 | 3 | writer.attribute("selected", "selected"); |
120 | ||
121 | 3 | foundSelected = true; |
122 | } | |
123 | ||
124 | 21 | writer.print(model.getLabel(i)); |
125 | ||
126 | 21 | writer.end(); |
127 | ||
128 | 21 | writer.println(); |
129 | } | |
130 | ||
131 | 3 | writer.end(); // <select> |
132 | ||
133 | 3 | renderDelegateSuffix(writer, cycle); |
134 | } | |
135 | ||
136 | 9 | private boolean isEqual(Object left, Object right) |
137 | { | |
138 | // Both null, or same object, then are equal | |
139 | ||
140 | 9 | if (left == right) |
141 | 3 | return true; |
142 | ||
143 | // If one is null, the other isn't, then not equal. | |
144 | ||
145 | 6 | if (left == null || right == null) |
146 | 0 | return false; |
147 | ||
148 | // Both non-null; use standard comparison. | |
149 | ||
150 | 6 | return left.equals(right); |
151 | } | |
152 | ||
153 | public abstract IPropertySelectionModel getModel(); | |
154 | ||
155 | /** @since 2.2 * */ | |
156 | public abstract boolean getSubmitOnChange(); | |
157 | ||
158 | /** @since 2.2 * */ | |
159 | public abstract Object getValue(); | |
160 | ||
161 | /** @since 2.2 * */ | |
162 | public abstract void setValue(Object value); | |
163 | } |
|