1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
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.ValidatorException; |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| public abstract class PropertySelection extends AbstractFormComponent implements ValidatableField |
48 |
| { |
49 |
| |
50 |
| |
51 |
| |
52 |
3
| protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
53 |
| { |
54 |
3
| renderDelegatePrefix(writer, cycle);
|
55 |
| |
56 |
3
| writer.begin("select");
|
57 |
3
| writer.attribute("name", getName());
|
58 |
| |
59 |
3
| if (isDisabled())
|
60 |
0
| writer.attribute("disabled", "disabled");
|
61 |
| |
62 |
3
| if (getSubmitOnChange())
|
63 |
0
| writer.attribute("onchange", "javascript: this.form.events.submit();");
|
64 |
| |
65 |
3
| renderIdAttribute(writer, cycle);
|
66 |
| |
67 |
3
| renderDelegateAttributes(writer, cycle);
|
68 |
| |
69 |
3
| getValidatableFieldSupport().renderContributions(this, writer, cycle);
|
70 |
| |
71 |
| |
72 |
3
| renderInformalParameters(writer, cycle);
|
73 |
| |
74 |
3
| writer.println();
|
75 |
| |
76 |
3
| IPropertySelectionModel model = getModel();
|
77 |
| |
78 |
3
| if (model == null)
|
79 |
0
| throw Tapestry.createRequiredParameterException(this, "model");
|
80 |
| |
81 |
3
| int count = model.getOptionCount();
|
82 |
3
| boolean foundSelected = false;
|
83 |
3
| Object value = getValue();
|
84 |
| |
85 |
3
| for (int i = 0; i < count; i++)
|
86 |
| { |
87 |
21
| Object option = model.getOption(i);
|
88 |
| |
89 |
21
| writer.begin("option");
|
90 |
21
| writer.attribute("value", model.getValue(i));
|
91 |
| |
92 |
21
| if (!foundSelected && isEqual(option, value))
|
93 |
| { |
94 |
3
| writer.attribute("selected", "selected");
|
95 |
| |
96 |
3
| foundSelected = true;
|
97 |
| } |
98 |
| |
99 |
21
| writer.print(model.getLabel(i));
|
100 |
| |
101 |
21
| writer.end();
|
102 |
| |
103 |
21
| writer.println();
|
104 |
| } |
105 |
| |
106 |
3
| writer.end();
|
107 |
| |
108 |
3
| renderDelegateSuffix(writer, cycle);
|
109 |
| } |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
3
| protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
115 |
| { |
116 |
3
| String value = cycle.getParameter(getName());
|
117 |
| |
118 |
3
| Object object = getModel().translateValue(value);
|
119 |
| |
120 |
3
| try
|
121 |
| { |
122 |
3
| getValidatableFieldSupport().validate(this, writer, cycle, object);
|
123 |
| |
124 |
3
| setValue(object);
|
125 |
| } |
126 |
| catch (ValidatorException e) |
127 |
| { |
128 |
0
| getForm().getDelegate().record(e);
|
129 |
| } |
130 |
| } |
131 |
| |
132 |
9
| private boolean isEqual(Object left, Object right)
|
133 |
| { |
134 |
| |
135 |
| |
136 |
9
| if (left == right)
|
137 |
3
| return true;
|
138 |
| |
139 |
| |
140 |
| |
141 |
6
| if (left == null || right == null)
|
142 |
0
| return false;
|
143 |
| |
144 |
| |
145 |
| |
146 |
6
| return left.equals(right);
|
147 |
| } |
148 |
| |
149 |
| public abstract IPropertySelectionModel getModel(); |
150 |
| |
151 |
| |
152 |
| public abstract boolean getSubmitOnChange(); |
153 |
| |
154 |
| |
155 |
| public abstract Object getValue(); |
156 |
| |
157 |
| |
158 |
| public abstract void setValue(Object value); |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| public abstract ValidatableFieldSupport getValidatableFieldSupport(); |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
0
| public boolean isRequired()
|
169 |
| { |
170 |
0
| return getValidatableFieldSupport().isRequired(this);
|
171 |
| } |
172 |
| } |