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 | |
|
19 | |
import org.kuali.student.common.assembly.data.Metadata; |
20 | |
import org.kuali.student.common.assembly.data.MetadataInterrogator; |
21 | |
import org.kuali.student.common.ui.client.application.Application; |
22 | |
import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBinding; |
23 | |
import org.kuali.student.common.ui.client.configurable.mvc.binding.MultiplicityCompositeBinding; |
24 | |
import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityComposite; |
25 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
26 | |
import org.kuali.student.common.ui.client.mvc.HasCrossConstraints; |
27 | |
import org.kuali.student.common.ui.client.mvc.HasDataValue; |
28 | |
import org.kuali.student.common.ui.client.widgets.KSCheckBox; |
29 | |
import org.kuali.student.common.ui.client.widgets.KSTextBox; |
30 | |
import org.kuali.student.common.ui.client.widgets.RichTextEditor; |
31 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.FieldElement; |
32 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.MessageKeyInfo; |
33 | |
import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract; |
34 | |
|
35 | |
import com.google.gwt.user.client.ui.HasText; |
36 | |
import com.google.gwt.user.client.ui.HasValue; |
37 | |
import com.google.gwt.user.client.ui.Widget; |
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 | |
|
89 | |
|
90 | |
|
91 | |
public class FieldDescriptor { |
92 | |
protected String fieldKey; |
93 | |
protected Metadata metadata; |
94 | |
@SuppressWarnings("unchecked") |
95 | |
private ModelWidgetBinding modelWidgetBinding; |
96 | |
private Callback<Boolean> validationRequestCallback; |
97 | 0 | private boolean dirty = false; |
98 | 0 | private boolean hasHadFocus = false; |
99 | |
private final FieldElement fieldElement; |
100 | |
private String modelId; |
101 | |
private MessageKeyInfo messageKey; |
102 | 0 | private boolean optional = false; |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | 0 | public FieldDescriptor(String fieldKey, MessageKeyInfo messageKey, Metadata metadata) { |
111 | 0 | this.fieldKey = fieldKey; |
112 | 0 | this.metadata = metadata; |
113 | 0 | if(messageKey == null){ |
114 | 0 | messageKey = new MessageKeyInfo(""); |
115 | |
} |
116 | 0 | setMessageKey(messageKey); |
117 | 0 | fieldElement = new FieldElement(fieldKey, messageKey, createFieldWidget()); |
118 | 0 | setupField(); |
119 | |
|
120 | |
|
121 | 0 | if(getFieldWidget() instanceof HasDataValue &&!(this instanceof FieldDescriptorReadOnly)){ |
122 | 0 | Application.getApplicationContext().putPathToFieldMapping(null, Application.getApplicationContext().getParentPath()+fieldKey, this); |
123 | |
} |
124 | |
|
125 | |
|
126 | 0 | if(fieldElement.getFieldWidget() instanceof HasCrossConstraints){ |
127 | 0 | HasCrossConstraints crossConstraintWidget = (HasCrossConstraints) fieldElement.getFieldWidget(); |
128 | 0 | if(crossConstraintWidget!=null&&crossConstraintWidget.getCrossConstraints()!=null){ |
129 | 0 | for(String path:crossConstraintWidget.getCrossConstraints()){ |
130 | 0 | Application.getApplicationContext().putCrossConstraint(null, path, crossConstraintWidget); |
131 | |
} |
132 | |
} |
133 | |
} |
134 | 0 | } |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | 0 | public FieldDescriptor(String fieldKey, MessageKeyInfo messageKey, Metadata metadata, Widget fieldWidget){ |
144 | 0 | this.fieldKey = fieldKey; |
145 | 0 | this.metadata = metadata; |
146 | 0 | if(messageKey == null){ |
147 | 0 | messageKey = new MessageKeyInfo(""); |
148 | |
} |
149 | 0 | setMessageKey(messageKey); |
150 | 0 | addStyleToWidget(fieldWidget); |
151 | 0 | fieldElement = new FieldElement(fieldKey, messageKey, fieldWidget); |
152 | 0 | setupField(); |
153 | |
|
154 | |
|
155 | 0 | if(fieldWidget instanceof HasDataValue &&!(this instanceof FieldDescriptorReadOnly)){ |
156 | 0 | Application.getApplicationContext().putPathToFieldMapping(null, Application.getApplicationContext().getParentPath()+fieldKey, this); |
157 | |
} |
158 | |
|
159 | |
|
160 | 0 | if(fieldElement.getFieldWidget() instanceof HasCrossConstraints){ |
161 | 0 | HasCrossConstraints crossConstraintWidget = (HasCrossConstraints) fieldElement.getFieldWidget(); |
162 | 0 | if(crossConstraintWidget!=null&&crossConstraintWidget.getCrossConstraints()!=null){ |
163 | 0 | for(String path:crossConstraintWidget.getCrossConstraints()){ |
164 | 0 | Application.getApplicationContext().putCrossConstraint(null, path, crossConstraintWidget); |
165 | |
} |
166 | |
} |
167 | |
} |
168 | |
|
169 | 0 | } |
170 | |
|
171 | |
protected void addStyleToWidget(Widget w){ |
172 | 0 | if(fieldKey != null && !fieldKey.isEmpty() && w != null){ |
173 | 0 | String style = this.fieldKey.replaceAll("/", "-"); |
174 | 0 | w.addStyleName(style); |
175 | |
} |
176 | 0 | } |
177 | |
|
178 | |
protected void setupField() { |
179 | 0 | if(metadata != null){ |
180 | 0 | if(MetadataInterrogator.isRequired(metadata)){ |
181 | 0 | fieldElement.setRequiredString("requiredMarker", "ks-form-module-elements-required"); |
182 | |
} |
183 | 0 | else if(MetadataInterrogator.isRequiredForNextState(metadata)){ |
184 | 0 | String nextState = MetadataInterrogator.getNextState(metadata); |
185 | 0 | if(nextState != null){ |
186 | 0 | if(nextState.equalsIgnoreCase("SUBMITTED")){ |
187 | 0 | fieldElement.setRequiredString("requiredOnSubmit", "ks-form-required-for-submit"); |
188 | |
} |
189 | 0 | else if(nextState.equalsIgnoreCase("APPROVED")){ |
190 | 0 | fieldElement.setRequiredString("reqApproval", "ks-form-required-for-submit"); |
191 | |
} |
192 | 0 | else if(nextState.equalsIgnoreCase("ACTIVE")){ |
193 | 0 | fieldElement.setRequiredString("reqActivate", "ks-form-required-for-submit"); |
194 | |
} |
195 | 0 | else if(nextState.equalsIgnoreCase("INACTIVE") || |
196 | |
nextState.equalsIgnoreCase("RETIRED")){ |
197 | 0 | fieldElement.setRequiredString("reqDeactivate", "ks-form-required-for-submit"); |
198 | |
} |
199 | |
else { |
200 | 0 | fieldElement.setRequiredString("requiredOnSubmit", "ks-form-required-for-submit"); |
201 | |
} |
202 | |
|
203 | |
} |
204 | 0 | } else{ |
205 | 0 | fieldElement.clearRequiredText(); |
206 | |
} |
207 | |
} |
208 | 0 | } |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
public void hideLabel(){ |
214 | 0 | fieldElement.hideLabel(); |
215 | 0 | } |
216 | |
|
217 | |
public boolean isLabelShown(){ |
218 | 0 | return fieldElement.isLabelShown(); |
219 | |
} |
220 | |
|
221 | |
public FieldElement getFieldElement(){ |
222 | 0 | return fieldElement; |
223 | |
} |
224 | |
|
225 | |
public String getFieldKey() { |
226 | 0 | return fieldKey; |
227 | |
} |
228 | |
|
229 | |
public void setFieldKey(String fieldKey) { |
230 | 0 | this.fieldKey = fieldKey; |
231 | 0 | } |
232 | |
|
233 | |
public String getFieldLabel() { |
234 | 0 | return fieldElement.getFieldName(); |
235 | |
} |
236 | |
|
237 | |
public Widget getFieldWidget(){ |
238 | 0 | if (fieldElement.getFieldWidget() == null){ |
239 | 0 | Widget w = createFieldWidget(); |
240 | 0 | fieldElement.setWidget(w); |
241 | |
} |
242 | 0 | return fieldElement.getFieldWidget(); |
243 | |
} |
244 | |
|
245 | |
protected Widget createFieldWidget() { |
246 | 0 | if (metadata == null) { |
247 | |
|
248 | |
|
249 | 0 | Widget result = new KSTextBox(); |
250 | 0 | addStyleToWidget(result); |
251 | 0 | return result; |
252 | |
} else { |
253 | 0 | Widget result = DefaultWidgetFactory.getInstance().getWidget(this); |
254 | 0 | addStyleToWidget(result); |
255 | 0 | return result; |
256 | |
} |
257 | |
} |
258 | |
|
259 | |
public ModelWidgetBinding<?> getModelWidgetBinding() { |
260 | 0 | if(modelWidgetBinding == null){ |
261 | 0 | if(fieldElement.getFieldWidget() instanceof RichTextEditor){ |
262 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.RichTextBinding.INSTANCE; |
263 | 0 | } else if (fieldElement.getFieldWidget() instanceof KSCheckBox){ |
264 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.HasValueBinding.INSTANCE; |
265 | 0 | } else if(fieldElement.getFieldWidget() instanceof MultiplicityComposite){ |
266 | 0 | modelWidgetBinding = MultiplicityCompositeBinding.INSTANCE; |
267 | 0 | } else if (fieldElement.getFieldWidget()instanceof HasText) { |
268 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.HasTextBinding.INSTANCE; |
269 | 0 | } else if (fieldElement.getFieldWidget() instanceof KSSelectItemWidgetAbstract){ |
270 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.SelectItemWidgetBinding.INSTANCE; |
271 | 0 | } else if (fieldElement.getFieldWidget() instanceof HasDataValue){ |
272 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.HasDataValueBinding.INSTANCE; |
273 | 0 | } else if (fieldElement.getFieldWidget() instanceof HasValue){ |
274 | 0 | modelWidgetBinding = org.kuali.student.common.ui.client.configurable.mvc.binding.HasValueBinding.INSTANCE; |
275 | |
} |
276 | |
} |
277 | 0 | return modelWidgetBinding; |
278 | |
} |
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
public void setValidationCallBack(Callback<Boolean> callback){ |
286 | 0 | validationRequestCallback = callback; |
287 | 0 | } |
288 | |
|
289 | |
public Callback<Boolean> getValidationRequestCallback(){ |
290 | 0 | return validationRequestCallback; |
291 | |
} |
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
public boolean isDirty() { |
299 | 0 | return dirty; |
300 | |
} |
301 | |
|
302 | |
public void setDirty(boolean dirty) { |
303 | 0 | this.dirty = dirty; |
304 | 0 | } |
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
public boolean hasHadFocus() { |
311 | 0 | return hasHadFocus; |
312 | |
} |
313 | |
|
314 | |
public void setHasHadFocus(boolean hasHadFocus) { |
315 | 0 | this.hasHadFocus = hasHadFocus; |
316 | 0 | } |
317 | |
|
318 | |
public Metadata getMetadata() { |
319 | 0 | return metadata; |
320 | |
} |
321 | |
|
322 | |
public void setMetadata(Metadata metadata) { |
323 | 0 | this.metadata = metadata; |
324 | 0 | setupField(); |
325 | 0 | } |
326 | |
|
327 | |
public void setFieldWidget(Widget fieldWidget) { |
328 | 0 | this.fieldElement.setWidget(fieldWidget); |
329 | 0 | } |
330 | |
|
331 | |
public String getModelId() { |
332 | 0 | return modelId; |
333 | |
} |
334 | |
|
335 | |
public void setModelId(String modelId) { |
336 | 0 | this.modelId = modelId; |
337 | 0 | } |
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
public void setWidgetBinding(ModelWidgetBinding widgetBinding) { |
346 | 0 | this.modelWidgetBinding = widgetBinding; |
347 | 0 | } |
348 | |
|
349 | |
public MessageKeyInfo getMessageKey() { |
350 | 0 | return messageKey; |
351 | |
} |
352 | |
|
353 | |
public void setMessageKey(MessageKeyInfo messageKey) { |
354 | 0 | this.messageKey = messageKey; |
355 | 0 | } |
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
public void setOptional(boolean optional){ |
364 | 0 | this.optional = optional; |
365 | 0 | } |
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
public boolean isOptional(){ |
372 | 0 | return optional; |
373 | |
} |
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
public boolean isVisible() { |
379 | 0 | if (metadata != null){ |
380 | 0 | return metadata.isCanView(); |
381 | |
} else { |
382 | 0 | return true; |
383 | |
} |
384 | |
} |
385 | |
|
386 | |
} |