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