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