1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
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.IMarkupWriter; |
22 |
| import org.apache.tapestry.IRequestCycle; |
23 |
| import org.apache.tapestry.Tapestry; |
24 |
| import org.apache.tapestry.valid.ValidatorException; |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| public abstract class Select extends AbstractFormComponent implements ValidatableField |
40 |
| { |
41 |
| private boolean _rewinding; |
42 |
| |
43 |
| private boolean _rendering; |
44 |
| |
45 |
| private Set _selections; |
46 |
| |
47 |
| private int _nextOptionId; |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| private final static String ATTRIBUTE_NAME = "org.apache.tapestry.active.Select"; |
55 |
| |
56 |
62
| public static Select get(IRequestCycle cycle)
|
57 |
| { |
58 |
62
| return (Select) cycle.getAttribute(ATTRIBUTE_NAME);
|
59 |
| } |
60 |
| |
61 |
| public abstract boolean isMultiple(); |
62 |
| |
63 |
60
| public boolean isRewinding()
|
64 |
| { |
65 |
60
| if (!_rendering)
|
66 |
0
| throw Tapestry.createRenderOnlyPropertyException(this, "rewinding");
|
67 |
| |
68 |
60
| return _rewinding;
|
69 |
| } |
70 |
| |
71 |
60
| public String getNextOptionId()
|
72 |
| { |
73 |
60
| if (!_rendering)
|
74 |
0
| throw Tapestry.createRenderOnlyPropertyException(this, "nextOptionId");
|
75 |
| |
76 |
| |
77 |
| |
78 |
60
| return Integer.toString(_nextOptionId++);
|
79 |
| } |
80 |
| |
81 |
22
| public boolean isSelected(String value)
|
82 |
| { |
83 |
22
| if (_selections == null)
|
84 |
6
| return false;
|
85 |
| |
86 |
16
| return _selections.contains(value);
|
87 |
| } |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
30
| protected void prepareForRender(IRequestCycle cycle)
|
93 |
| { |
94 |
30
| if (cycle.getAttribute(ATTRIBUTE_NAME) != null)
|
95 |
2
| throw new ApplicationRuntimeException(Tapestry.getMessage("Select.may-not-nest"), this,
|
96 |
| null, null); |
97 |
| |
98 |
28
| cycle.setAttribute(ATTRIBUTE_NAME, this);
|
99 |
| |
100 |
28
| _rendering = true;
|
101 |
28
| _nextOptionId = 0;
|
102 |
| } |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
30
| protected void cleanupAfterRender(IRequestCycle cycle)
|
108 |
| { |
109 |
30
| _rendering = false;
|
110 |
30
| _selections = null;
|
111 |
| |
112 |
30
| cycle.removeAttribute(ATTRIBUTE_NAME);
|
113 |
| } |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
16
| protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
119 |
| { |
120 |
16
| _rewinding = false;
|
121 |
| |
122 |
16
| renderDelegatePrefix(writer, cycle);
|
123 |
| |
124 |
16
| writer.begin("select");
|
125 |
| |
126 |
16
| writer.attribute("name", getName());
|
127 |
| |
128 |
16
| if (isMultiple())
|
129 |
8
| writer.attribute("multiple", "multiple");
|
130 |
| |
131 |
16
| if (isDisabled())
|
132 |
2
| writer.attribute("disabled", "disabled");
|
133 |
| |
134 |
16
| renderIdAttribute(writer, cycle);
|
135 |
| |
136 |
16
| renderDelegateAttributes(writer, cycle);
|
137 |
| |
138 |
16
| getValidatableFieldSupport().renderContributions(this, writer, cycle);
|
139 |
| |
140 |
16
| renderInformalParameters(writer, cycle);
|
141 |
| |
142 |
16
| renderBody(writer, cycle);
|
143 |
| |
144 |
14
| writer.end();
|
145 |
| |
146 |
14
| renderDelegateSuffix(writer, cycle);
|
147 |
| } |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
8
| protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
153 |
| { |
154 |
8
| _selections = null;
|
155 |
8
| _rewinding = true;
|
156 |
| |
157 |
8
| String[] parameters = cycle.getParameters(getName());
|
158 |
| |
159 |
8
| try
|
160 |
| { |
161 |
8
| if (parameters != null)
|
162 |
| { |
163 |
6
| int length = parameters.length;
|
164 |
| |
165 |
6
| _selections = new HashSet((length > 30) ? 101 : 7);
|
166 |
| |
167 |
6
| for (int i = 0; i < length; i++)
|
168 |
10
| _selections.add(parameters[i]);
|
169 |
| } |
170 |
| |
171 |
8
| renderBody(writer, cycle);
|
172 |
| |
173 |
| |
174 |
8
| getValidatableFieldSupport().validate(this, writer, cycle, parameters);
|
175 |
| } |
176 |
| catch (ValidatorException e) |
177 |
| { |
178 |
0
| getForm().getDelegate().record(e);
|
179 |
| } |
180 |
| } |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
| public abstract ValidatableFieldSupport getValidatableFieldSupport(); |
186 |
| |
187 |
| |
188 |
| |
189 |
| |
190 |
0
| public boolean isRequired()
|
191 |
| { |
192 |
0
| return getValidatableFieldSupport().isRequired(this);
|
193 |
| } |
194 |
| } |