|
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 |
9
| protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
|
53 |
| { |
|
54 |
9
| renderDelegatePrefix(writer, cycle);
|
|
55 |
| |
|
56 |
9
| writer.begin("select");
|
|
57 |
9
| writer.attribute("name", getName());
|
|
58 |
| |
|
59 |
9
| if (isDisabled())
|
|
60 |
0
| writer.attribute("disabled", "disabled");
|
|
61 |
| |
|
62 |
9
| if (getSubmitOnChange())
|
|
63 |
0
| writer.attribute("onchange", "javascript: this.form.events.submit();");
|
|
64 |
| |
|
65 |
9
| renderIdAttribute(writer, cycle);
|
|
66 |
| |
|
67 |
9
| renderDelegateAttributes(writer, cycle);
|
|
68 |
| |
|
69 |
9
| getValidatableFieldSupport().renderContributions(this, writer, cycle);
|
|
70 |
| |
|
71 |
| |
|
72 |
9
| renderInformalParameters(writer, cycle);
|
|
73 |
| |
|
74 |
9
| writer.println();
|
|
75 |
| |
|
76 |
9
| IPropertySelectionModel model = getModel();
|
|
77 |
| |
|
78 |
9
| if (model == null)
|
|
79 |
0
| throw Tapestry.createRequiredParameterException(this, "model");
|
|
80 |
| |
|
81 |
9
| int count = model.getOptionCount();
|
|
82 |
9
| boolean foundSelected = false;
|
|
83 |
9
| Object value = getValue();
|
|
84 |
| |
|
85 |
9
| for (int i = 0; i < count; i++)
|
|
86 |
| { |
|
87 |
63
| Object option = model.getOption(i);
|
|
88 |
| |
|
89 |
63
| writer.begin("option");
|
|
90 |
63
| writer.attribute("value", model.getValue(i));
|
|
91 |
| |
|
92 |
63
| if (!foundSelected && isEqual(option, value))
|
|
93 |
| { |
|
94 |
9
| writer.attribute("selected", "selected");
|
|
95 |
| |
|
96 |
9
| foundSelected = true;
|
|
97 |
| } |
|
98 |
| |
|
99 |
63
| writer.print(model.getLabel(i));
|
|
100 |
| |
|
101 |
63
| writer.end();
|
|
102 |
| |
|
103 |
63
| writer.println();
|
|
104 |
| } |
|
105 |
| |
|
106 |
9
| writer.end();
|
|
107 |
| |
|
108 |
9
| renderDelegateSuffix(writer, cycle);
|
|
109 |
| } |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
9
| protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
|
115 |
| { |
|
116 |
9
| String value = cycle.getParameter(getName());
|
|
117 |
| |
|
118 |
9
| Object object = (value == null) ? null : getModel().translateValue(value);
|
|
119 |
| |
|
120 |
9
| try
|
|
121 |
| { |
|
122 |
9
| getValidatableFieldSupport().validate(this, writer, cycle, object);
|
|
123 |
| |
|
124 |
9
| setValue(object);
|
|
125 |
| } |
|
126 |
| catch (ValidatorException e) |
|
127 |
| { |
|
128 |
0
| getForm().getDelegate().record(e);
|
|
129 |
| } |
|
130 |
| } |
|
131 |
| |
|
132 |
27
| private boolean isEqual(Object left, Object right)
|
|
133 |
| { |
|
134 |
| |
|
135 |
| |
|
136 |
27
| if (left == right)
|
|
137 |
9
| return true;
|
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
18
| if (left == null || right == null)
|
|
142 |
0
| return false;
|
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
18
| 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 |
| } |