|
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 |
93
| public static Select get(IRequestCycle cycle)
|
|
57 |
| { |
|
58 |
93
| return (Select) cycle.getAttribute(ATTRIBUTE_NAME);
|
|
59 |
| } |
|
60 |
| |
|
61 |
| public abstract boolean isMultiple(); |
|
62 |
| |
|
63 |
90
| public boolean isRewinding()
|
|
64 |
| { |
|
65 |
90
| if (!_rendering)
|
|
66 |
0
| throw Tapestry.createRenderOnlyPropertyException(this, "rewinding");
|
|
67 |
| |
|
68 |
90
| return _rewinding;
|
|
69 |
| } |
|
70 |
| |
|
71 |
90
| public String getNextOptionId()
|
|
72 |
| { |
|
73 |
90
| if (!_rendering)
|
|
74 |
0
| throw Tapestry.createRenderOnlyPropertyException(this, "nextOptionId");
|
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
90
| return Integer.toString(_nextOptionId++);
|
|
79 |
| } |
|
80 |
| |
|
81 |
33
| public boolean isSelected(String value)
|
|
82 |
| { |
|
83 |
33
| if (_selections == null)
|
|
84 |
9
| return false;
|
|
85 |
| |
|
86 |
24
| return _selections.contains(value);
|
|
87 |
| } |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
45
| protected void prepareForRender(IRequestCycle cycle)
|
|
93 |
| { |
|
94 |
45
| if (cycle.getAttribute(ATTRIBUTE_NAME) != null)
|
|
95 |
3
| throw new ApplicationRuntimeException(Tapestry.getMessage("Select.may-not-nest"), this,
|
|
96 |
| null, null); |
|
97 |
| |
|
98 |
42
| cycle.setAttribute(ATTRIBUTE_NAME, this);
|
|
99 |
| |
|
100 |
42
| _rendering = true;
|
|
101 |
42
| _nextOptionId = 0;
|
|
102 |
| } |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
45
| protected void cleanupAfterRender(IRequestCycle cycle)
|
|
108 |
| { |
|
109 |
45
| _rendering = false;
|
|
110 |
45
| _selections = null;
|
|
111 |
| |
|
112 |
45
| cycle.removeAttribute(ATTRIBUTE_NAME);
|
|
113 |
| } |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
24
| protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
|
119 |
| { |
|
120 |
24
| _rewinding = false;
|
|
121 |
| |
|
122 |
24
| renderDelegatePrefix(writer, cycle);
|
|
123 |
| |
|
124 |
24
| writer.begin("select");
|
|
125 |
| |
|
126 |
24
| writer.attribute("name", getName());
|
|
127 |
| |
|
128 |
24
| if (isMultiple())
|
|
129 |
12
| writer.attribute("multiple", "multiple");
|
|
130 |
| |
|
131 |
24
| if (isDisabled())
|
|
132 |
3
| writer.attribute("disabled", "disabled");
|
|
133 |
| |
|
134 |
24
| renderIdAttribute(writer, cycle);
|
|
135 |
| |
|
136 |
24
| renderDelegateAttributes(writer, cycle);
|
|
137 |
| |
|
138 |
24
| getValidatableFieldSupport().renderContributions(this, writer, cycle);
|
|
139 |
| |
|
140 |
24
| renderInformalParameters(writer, cycle);
|
|
141 |
| |
|
142 |
24
| renderBody(writer, cycle);
|
|
143 |
| |
|
144 |
21
| writer.end();
|
|
145 |
| |
|
146 |
21
| renderDelegateSuffix(writer, cycle);
|
|
147 |
| } |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
12
| protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
|
153 |
| { |
|
154 |
12
| _selections = null;
|
|
155 |
12
| _rewinding = true;
|
|
156 |
| |
|
157 |
12
| String[] parameters = cycle.getParameters(getName());
|
|
158 |
| |
|
159 |
12
| try
|
|
160 |
| { |
|
161 |
12
| if (parameters != null)
|
|
162 |
| { |
|
163 |
9
| int length = parameters.length;
|
|
164 |
| |
|
165 |
9
| _selections = new HashSet((length > 30) ? 101 : 7);
|
|
166 |
| |
|
167 |
9
| for (int i = 0; i < length; i++)
|
|
168 |
15
| _selections.add(parameters[i]);
|
|
169 |
| } |
|
170 |
| |
|
171 |
12
| renderBody(writer, cycle);
|
|
172 |
| |
|
173 |
| |
|
174 |
12
| 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 |
| } |