|
|||||||||||||||||||
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 | |||||||||||||||
Select.java | 86.4% | 95.5% | 100% | 93.2% |
|
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 java.util.HashSet;
|
|
18 |
import java.util.Set;
|
|
19 |
|
|
20 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
21 |
import org.apache.tapestry.IForm;
|
|
22 |
import org.apache.tapestry.IMarkupWriter;
|
|
23 |
import org.apache.tapestry.IRequestCycle;
|
|
24 |
import org.apache.tapestry.Tapestry;
|
|
25 |
import org.apache.tapestry.request.RequestContext;
|
|
26 |
|
|
27 |
/**
|
|
28 |
* Implements a component that manages an HTML <select> form element.
|
|
29 |
* The most common situation, using a <select> to set a specific
|
|
30 |
* property of some object, is best handled using a {@link PropertySelection} component.
|
|
31 |
*
|
|
32 |
* [<a href="../../../../../ComponentReference/Select.html">Component Reference</a>]
|
|
33 |
*
|
|
34 |
* <p>Otherwise, this component is very similar to {@link RadioGroup}.
|
|
35 |
*
|
|
36 |
*
|
|
37 |
* @author Howard Lewis Ship
|
|
38 |
*
|
|
39 |
**/
|
|
40 |
|
|
41 |
public abstract class Select extends AbstractFormComponent |
|
42 |
{ |
|
43 |
private boolean _rewinding; |
|
44 |
private boolean _rendering; |
|
45 |
|
|
46 |
private Set _selections;
|
|
47 |
private int _nextOptionId; |
|
48 |
|
|
49 |
/**
|
|
50 |
* Used by the <code>Select</code> to record itself as a
|
|
51 |
* {@link IRequestCycle} attribute, so that the
|
|
52 |
* {@link Option} components it wraps can have access to it.
|
|
53 |
*
|
|
54 |
**/
|
|
55 |
|
|
56 |
private final static String ATTRIBUTE_NAME = "org.apache.tapestry.active.Select"; |
|
57 |
|
|
58 | 37 |
public static Select get(IRequestCycle cycle) |
59 |
{ |
|
60 | 37 |
return (Select) cycle.getAttribute(ATTRIBUTE_NAME);
|
61 |
} |
|
62 |
|
|
63 |
public abstract boolean isDisabled(); |
|
64 |
|
|
65 |
public abstract boolean isMultiple(); |
|
66 |
|
|
67 | 36 |
public boolean isRewinding() |
68 |
{ |
|
69 | 36 |
if (!_rendering)
|
70 | 0 |
throw Tapestry.createRenderOnlyPropertyException(this, "rewinding"); |
71 |
|
|
72 | 36 |
return _rewinding;
|
73 |
} |
|
74 |
|
|
75 | 36 |
public String getNextOptionId()
|
76 |
{ |
|
77 | 36 |
if (!_rendering)
|
78 | 0 |
throw Tapestry.createRenderOnlyPropertyException(this, "nextOptionId"); |
79 |
|
|
80 |
// Return it as a hex value.
|
|
81 |
|
|
82 | 36 |
return Integer.toString(_nextOptionId++);
|
83 |
} |
|
84 |
|
|
85 | 11 |
public boolean isSelected(String value) |
86 |
{ |
|
87 | 11 |
if (_selections == null) |
88 | 3 |
return false; |
89 |
|
|
90 | 8 |
return _selections.contains(value);
|
91 |
} |
|
92 |
|
|
93 |
/**
|
|
94 |
* Renders the <option> element, or responds when the form containing the element
|
|
95 |
* is submitted (by checking {@link IForm#isRewinding()}.
|
|
96 |
**/
|
|
97 |
|
|
98 | 16 |
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
99 |
{ |
|
100 | 16 |
IForm form = getForm(cycle); |
101 |
|
|
102 | 15 |
if (cycle.getAttribute(ATTRIBUTE_NAME) != null) |
103 | 1 |
throw new ApplicationRuntimeException( |
104 |
Tapestry.getMessage("Select.may-not-nest"),
|
|
105 |
this,
|
|
106 |
null,
|
|
107 |
null);
|
|
108 |
|
|
109 |
// It isn't enough to know whether the cycle in general is rewinding, need to know
|
|
110 |
// specifically if the form which contains this component is rewinding.
|
|
111 |
|
|
112 | 14 |
_rewinding = form.isRewinding(); |
113 |
|
|
114 |
// Used whether rewinding or not.
|
|
115 |
|
|
116 | 14 |
String name = form.getElementId(this);
|
117 |
|
|
118 | 14 |
cycle.setAttribute(ATTRIBUTE_NAME, this);
|
119 |
|
|
120 | 14 |
if (_rewinding)
|
121 |
{ |
|
122 | 5 |
_selections = buildSelections(cycle, name); |
123 |
} |
|
124 |
else
|
|
125 |
{ |
|
126 | 9 |
writer.begin("select");
|
127 |
|
|
128 | 9 |
writer.attribute("name", name);
|
129 |
|
|
130 | 9 |
if (isMultiple())
|
131 | 4 |
writer.attribute("multiple", "multiple"); |
132 |
|
|
133 | 9 |
if (isDisabled())
|
134 | 1 |
writer.attribute("disabled", "disabled"); |
135 |
|
|
136 | 9 |
renderInformalParameters(writer, cycle); |
137 |
} |
|
138 |
|
|
139 | 14 |
_rendering = true;
|
140 | 14 |
_nextOptionId = 0; |
141 |
|
|
142 | 14 |
renderBody(writer, cycle); |
143 |
|
|
144 | 13 |
if (!_rewinding)
|
145 |
{ |
|
146 | 8 |
writer.end(); |
147 |
} |
|
148 |
|
|
149 | 13 |
cycle.removeAttribute(ATTRIBUTE_NAME); |
150 |
|
|
151 |
} |
|
152 |
|
|
153 | 16 |
protected void cleanupAfterRender(IRequestCycle cycle) |
154 |
{ |
|
155 | 16 |
_rendering = false;
|
156 | 16 |
_selections = null;
|
157 |
|
|
158 | 16 |
super.cleanupAfterRender(cycle);
|
159 |
} |
|
160 |
|
|
161 |
/**
|
|
162 |
* Cut-and-paste with {@link RadioGroup}!
|
|
163 |
*
|
|
164 |
**/
|
|
165 |
|
|
166 | 5 |
private Set buildSelections(IRequestCycle cycle, String parameterName)
|
167 |
{ |
|
168 | 5 |
RequestContext context = cycle.getRequestContext(); |
169 |
|
|
170 | 5 |
String[] parameters = context.getParameters(parameterName); |
171 |
|
|
172 | 5 |
if (parameters == null) |
173 | 1 |
return null; |
174 |
|
|
175 | 4 |
int length = parameters.length;
|
176 |
|
|
177 | 4 |
int size = (parameters.length > 30) ? 101 : 7;
|
178 |
|
|
179 | 4 |
Set result = new HashSet(size);
|
180 |
|
|
181 | 4 |
for (int i = 0; i < length; i++) |
182 | 6 |
result.add(parameters[i]); |
183 |
|
|
184 | 4 |
return result;
|
185 |
} |
|
186 |
} |
|