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