1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.configurable.mvc; |
17 | |
|
18 | |
import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBinding; |
19 | |
import org.kuali.student.common.ui.client.configurable.mvc.binding.MultiplicityCompositeBinding; |
20 | |
import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityComposite; |
21 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
22 | |
import org.kuali.student.common.ui.client.mvc.HasDataValue; |
23 | |
import org.kuali.student.common.ui.client.widgets.KSCheckBox; |
24 | |
import org.kuali.student.common.ui.client.widgets.KSTextBox; |
25 | |
import org.kuali.student.common.ui.client.widgets.RichTextEditor; |
26 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.FieldElement; |
27 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.MessageKeyInfo; |
28 | |
import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract; |
29 | |
import org.kuali.student.core.assembly.data.Metadata; |
30 | |
import org.kuali.student.core.assembly.data.MetadataInterrogator; |
31 | |
|
32 | |
import com.google.gwt.user.client.ui.HasText; |
33 | |
import com.google.gwt.user.client.ui.HasValue; |
34 | |
import com.google.gwt.user.client.ui.Widget; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public class FieldDescriptor { |
89 | |
protected String fieldKey; |
90 | |
protected Metadata metadata; |
91 | |
@SuppressWarnings("unchecked") |
92 | |
private ModelWidgetBinding modelWidgetBinding; |
93 | |
private Callback<Boolean> validationRequestCallback; |
94 | 0 | private boolean dirty = false; |
95 | 0 | private boolean hasHadFocus = false; |
96 | |
private final FieldElement fieldElement; |
97 | |
private String modelId; |
98 | |
private MessageKeyInfo messageKey; |
99 | 0 | private boolean optional = false; |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | 0 | public FieldDescriptor(String fieldKey, MessageKeyInfo messageKey, Metadata metadata) { |
108 | 0 | this.fieldKey = fieldKey; |
109 | 0 | this.metadata = metadata; |
110 | 0 | if(messageKey == null){ |
111 | 0 | messageKey = new MessageKeyInfo(""); |
112 | |
} |
113 | 0 | setMessageKey(messageKey); |
114 | 0 | fieldElement = new FieldElement(fieldKey, messageKey, createFieldWidget()); |
115 | 0 | setupField(); |
116 | 0 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | 0 | public FieldDescriptor(String fieldKey, MessageKeyInfo messageKey, Metadata metadata, Widget fieldWidget){ |
126 | 0 | this.fieldKey = fieldKey; |
127 | 0 | this.metadata = metadata; |
128 | 0 | if(messageKey == null){ |
129 | 0 | messageKey = new MessageKeyInfo(""); |
130 | |
} |
131 | 0 | setMessageKey(messageKey); |
132 | 0 | addStyleToWidget(fieldWidget); |
133 | 0 | fieldElement = new FieldElement(fieldKey, messageKey, fieldWidget); |
134 | 0 | setupField(); |
135 | 0 | } |
136 | |
|
137 | |
protected void addStyleToWidget(Widget w){ |
138 | 0 | if(fieldKey != null && !fieldKey.isEmpty() && w != null){ |
139 | 0 | String style = this.fieldKey.replaceAll("/", "-"); |
140 | 0 | w.addStyleName(style); |
141 | |
} |
142 | 0 | } |
143 | |
|
144 | |
protected void setupField() { |
145 | 0 | if(metadata != null){ |
146 | 0 | if(MetadataInterrogator.isRequired(metadata)){ |
147 | 0 | fieldElement.setRequiredString("requiredMarker", "ks-form-module-elements-required"); |
148 | |
} |
149 | 0 | else if(MetadataInterrogator.isRequiredForNextState(metadata)){ |
150 | 0 | String nextState = MetadataInterrogator.getNextState(metadata); |
151 | 0 | if(nextState != null){ |
152 | 0 | if(nextState.equalsIgnoreCase("SUBMITTED")){ |
153 | 0 | fieldElement.setRequiredString("requiredOnSubmit", "ks-form-required-for-submit"); |
154 | |
} |
155 | 0 | else if(nextState.equalsIgnoreCase("APPROVED")){ |
156 | 0 | fieldElement.setRequiredString("reqApproval", "ks-form-required-for-submit"); |
157 | |
} |
158 | 0 | else if(nextState.equalsIgnoreCase("ACTIVE")){ |
159 | 0 | fieldElement.setRequiredString("reqActivate", "ks-form-required-for-submit"); |
160 | |
} |
161 | 0 | else if(nextState.equalsIgnoreCase("INACTIVE") || |
162 | |
nextState.equalsIgnoreCase("RETIRED")){ |
163 | 0 | fieldElement.setRequiredString("reqDeactivate", "ks-form-required-for-submit"); |
164 | |
} |
165 | |
else { |
166 | 0 | fieldElement.setRequiredString("requiredOnSubmit", "ks-form-required-for-submit"); |
167 | |
} |
168 | |
|
169 | |
} |
170 | 0 | } else{ |
171 | 0 | fieldElement.clearRequiredText(); |
172 | |
} |
173 | |
} |
174 | 0 | } |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
public void hideLabel(){ |
180 | 0 | fieldElement.hideLabel(); |
181 | 0 | } |
182 | |
|
183 | |
public boolean isLabelShown(){ |
184 | 0 | return fieldElement.isLabelShown(); |
185 | |
} |
186 | |
|
187 | |
public FieldElement getFieldElement(){ |
188 | 0 | return fieldElement; |
189 | |
} |
190 | |
|
191 | |
public String getFieldKey() { |
192 | 0 | return fieldKey; |
193 | |
} |
194 | |
|
195 | |
public void setFieldKey(String fieldKey) { |
196 | 0 | this.fieldKey = fieldKey; |
197 | 0 | } |
198 | |
|
199 | |
public String getFieldLabel() { |
200 | 0 | return fieldElement.getFieldName(); |
201 | |
} |
202 | |
|
203 | |
public Widget getFieldWidget(){ |
204 | 0 | if (fieldElement.getFieldWidget() == null){ |
205 | 0 | Widget w = createFieldWidget(); |
206 | 0 | fieldElement.setWidget(w); |
207 | |
} |
208 | 0 | return fieldElement.getFieldWidget(); |
209 | |
} |
210 | |
|
211 | |
protected Widget createFieldWidget() { |
212 | 0 | if (metadata == null) { |
213 | |
|
214 | |
|
215 | 0 | Widget result = new KSTextBox(); |
216 | 0 | addStyleToWidget(result); |
217 | 0 | return result; |
218 | |
} else { |
219 | 0 | Widget result = DefaultWidgetFactory.getInstance().getWidget(this); |
220 | 0 | addStyleToWidget(result); |
221 | 0 | return result; |
222 | |
} |
223 | |
} |
224 | |
|
225 | |
public ModelWidgetBinding<?> getModelWidgetBinding() { |
226 | 0 | if(modelWidgetBinding == null){ |
227 | 0 | if(fieldElement.getFieldWidget() instanceof RichTextEditor){ |
228 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.RichTextBinding.INSTANCE; |
229 | 0 | } else if (fieldElement.getFieldWidget() instanceof KSCheckBox){ |
230 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.HasValueBinding.INSTANCE; |
231 | 0 | } else if(fieldElement.getFieldWidget() instanceof MultiplicityComposite){ |
232 | 0 | modelWidgetBinding = MultiplicityCompositeBinding.INSTANCE; |
233 | 0 | } else if (fieldElement.getFieldWidget()instanceof HasText) { |
234 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.HasTextBinding.INSTANCE; |
235 | 0 | } else if (fieldElement.getFieldWidget() instanceof KSSelectItemWidgetAbstract){ |
236 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.SelectItemWidgetBinding.INSTANCE; |
237 | 0 | } else if (fieldElement.getFieldWidget() instanceof HasDataValue){ |
238 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.HasDataValueBinding.INSTANCE; |
239 | 0 | } else if (fieldElement.getFieldWidget() instanceof HasValue){ |
240 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.HasValueBinding.INSTANCE; |
241 | |
} |
242 | |
} |
243 | 0 | return modelWidgetBinding; |
244 | |
} |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
public void setValidationCallBack(Callback<Boolean> callback){ |
252 | 0 | validationRequestCallback = callback; |
253 | 0 | } |
254 | |
|
255 | |
public Callback<Boolean> getValidationRequestCallback(){ |
256 | 0 | return validationRequestCallback; |
257 | |
} |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
public boolean isDirty() { |
265 | 0 | return dirty; |
266 | |
} |
267 | |
|
268 | |
public void setDirty(boolean dirty) { |
269 | 0 | this.dirty = dirty; |
270 | 0 | } |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
public boolean hasHadFocus() { |
277 | 0 | return hasHadFocus; |
278 | |
} |
279 | |
|
280 | |
public void setHasHadFocus(boolean hasHadFocus) { |
281 | 0 | this.hasHadFocus = hasHadFocus; |
282 | 0 | } |
283 | |
|
284 | |
public Metadata getMetadata() { |
285 | 0 | return metadata; |
286 | |
} |
287 | |
|
288 | |
public void setMetadata(Metadata metadata) { |
289 | 0 | this.metadata = metadata; |
290 | 0 | setupField(); |
291 | 0 | } |
292 | |
|
293 | |
public void setFieldWidget(Widget fieldWidget) { |
294 | 0 | this.fieldElement.setWidget(fieldWidget); |
295 | 0 | } |
296 | |
|
297 | |
public String getModelId() { |
298 | 0 | return modelId; |
299 | |
} |
300 | |
|
301 | |
public void setModelId(String modelId) { |
302 | 0 | this.modelId = modelId; |
303 | 0 | } |
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
public void setWidgetBinding(ModelWidgetBinding widgetBinding) { |
312 | 0 | this.modelWidgetBinding = widgetBinding; |
313 | 0 | } |
314 | |
|
315 | |
public MessageKeyInfo getMessageKey() { |
316 | 0 | return messageKey; |
317 | |
} |
318 | |
|
319 | |
public void setMessageKey(MessageKeyInfo messageKey) { |
320 | 0 | this.messageKey = messageKey; |
321 | 0 | } |
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
public void setOptional(boolean optional){ |
330 | 0 | this.optional = optional; |
331 | 0 | } |
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
public boolean isOptional(){ |
338 | 0 | return optional; |
339 | |
} |
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
public boolean isVisible() { |
345 | 0 | if (metadata != null){ |
346 | 0 | return metadata.isCanView(); |
347 | |
} else { |
348 | 0 | return true; |
349 | |
} |
350 | |
} |
351 | |
|
352 | |
} |