1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.util;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.uif.RemotableAbstractWidget;
20 import org.kuali.rice.core.api.uif.RemotableAttributeField;
21 import org.kuali.rice.core.api.uif.RemotableCheckbox;
22 import org.kuali.rice.core.api.uif.RemotableCheckboxGroup;
23 import org.kuali.rice.core.api.uif.RemotableControlContract;
24 import org.kuali.rice.core.api.uif.RemotableDatepicker;
25 import org.kuali.rice.core.api.uif.RemotableHiddenInput;
26 import org.kuali.rice.core.api.uif.RemotableQuickFinder;
27 import org.kuali.rice.core.api.uif.RemotableRadioButtonGroup;
28 import org.kuali.rice.core.api.uif.RemotableSelect;
29 import org.kuali.rice.core.api.uif.RemotableSelectGroup;
30 import org.kuali.rice.core.api.uif.RemotableTextExpand;
31 import org.kuali.rice.core.api.uif.RemotableTextInput;
32 import org.kuali.rice.core.api.uif.RemotableTextarea;
33 import org.kuali.rice.core.api.util.ConcreteKeyValue;
34 import org.kuali.rice.core.api.util.KeyValue;
35 import org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersConstraint;
36 import org.kuali.rice.krad.keyvalues.KeyValuesFinder;
37 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
38 import org.kuali.rice.krad.uif.UifConstants;
39 import org.kuali.rice.krad.uif.container.CollectionGroup;
40 import org.kuali.rice.krad.uif.container.Group;
41 import org.kuali.rice.krad.uif.container.LinkGroup;
42 import org.kuali.rice.krad.uif.container.NavigationGroup;
43 import org.kuali.rice.krad.uif.container.PageGroup;
44 import org.kuali.rice.krad.uif.container.TabGroup;
45 import org.kuali.rice.krad.uif.container.TreeGroup;
46 import org.kuali.rice.krad.uif.control.CheckboxControl;
47 import org.kuali.rice.krad.uif.control.CheckboxGroupControl;
48 import org.kuali.rice.krad.uif.control.Control;
49 import org.kuali.rice.krad.uif.control.FileControl;
50 import org.kuali.rice.krad.uif.control.HiddenControl;
51 import org.kuali.rice.krad.uif.control.MultiValueControl;
52 import org.kuali.rice.krad.uif.control.RadioGroupControl;
53 import org.kuali.rice.krad.uif.control.SelectControl;
54 import org.kuali.rice.krad.uif.control.SizedControl;
55 import org.kuali.rice.krad.uif.control.TextAreaControl;
56 import org.kuali.rice.krad.uif.control.TextControl;
57 import org.kuali.rice.krad.uif.component.Component;
58 import org.kuali.rice.krad.uif.field.ActionField;
59 import org.kuali.rice.krad.uif.field.DataField;
60 import org.kuali.rice.krad.uif.field.InputField;
61 import org.kuali.rice.krad.uif.field.BlankField;
62 import org.kuali.rice.krad.uif.field.ErrorsField;
63 import org.kuali.rice.krad.uif.field.FieldGroup;
64 import org.kuali.rice.krad.uif.field.GenericField;
65 import org.kuali.rice.krad.uif.field.HeaderField;
66 import org.kuali.rice.krad.uif.field.IframeField;
67 import org.kuali.rice.krad.uif.field.ImageField;
68 import org.kuali.rice.krad.uif.field.LabelField;
69 import org.kuali.rice.krad.uif.field.LinkField;
70 import org.kuali.rice.krad.uif.field.MessageField;
71 import org.kuali.rice.krad.uif.view.View;
72
73 import java.util.ArrayList;
74 import java.util.HashMap;
75 import java.util.List;
76 import java.util.Map;
77
78
79
80
81
82
83
84 public class ComponentFactory {
85
86 protected static final String TEXT_CONTROL = "Uif-TextControl";
87 protected static final String CHECKBOX_CONTROL = "Uif-CheckboxControl";
88 protected static final String HIDDEN_CONTROL = "Uif-HiddenControl";
89 protected static final String TEXTAREA_CONTROL = "Uif-TextAreaControl";
90 protected static final String SELECT_CONTROL = "Uif-DropdownControl";
91 protected static final String CHECKBOX_GROUP_CONTROL = "Uif-VerticalCheckboxesControl";
92 protected static final String CHECKBOX_GROUP_CONTROL_HORIZONTAL = "Uif-HorizontalCheckboxesControl";
93 protected static final String RADIO_GROUP_CONTROL = "Uif-VerticalRadioControl";
94 protected static final String RADIO_GROUP_CONTROL_HORIZONTAL = "Uif-HorizontalRadioControl";
95 protected static final String FILE_CONTROL = "Uif-FileControl";
96 protected static final String DATE_CONTROL = "Uif-DateControl";
97 protected static final String USER_CONTROL = "Uif-KimPersonControl";
98 protected static final String GROUP_CONTROL = "Uif-KimGroupControl";
99
100 protected static final String DATA_FIELD = "Uif-DataField";
101 protected static final String INPUT_FIELD = "Uif-InputField";
102 protected static final String ERRORS_FIELD = "Uif-FieldValidationMessages";
103 protected static final String ACTION_FIELD = "Uif-PrimaryActionButton";
104 protected static final String ACTION_LINK_FIELD = "Uif-ActionLink";
105 protected static final String LINK_FIELD = "Uif-Link";
106 protected static final String IFRAME_FIELD = "Uif-IframeField";
107 protected static final String IMAGE_FIELD = "Uif-ImageField";
108 protected static final String BLANK_FIELD = "Uif-EmptyField";
109 protected static final String GENERIC_FIELD = "Uif-CustomTemplateField";
110 protected static final String LABEL_FIELD = "Uif-Label";
111 protected static final String MESSAGE_FIELD = "Uif-MessageField";
112 protected static final String FIELD_GROUP = "Uif-VerticalFieldGroup";
113 protected static final String HORIZONTAL_FIELD_GROUP = "Uif-HorizontalFieldGroup";
114
115 protected static final String GROUP = "Uif-BoxSection";
116 protected static final String PAGE_GROUP = "Uif-Page";
117 protected static final String GROUP_GRID_LAYOUT = "Uif-GridSection";
118 protected static final String GROUP_BODY_ONLY = "Uif-BoxGroup";
119 protected static final String GROUP_GRID_BODY_ONLY = "Uif-GridGroup";
120 protected static final String TAB_GROUP = "Uif-TabSection";
121 protected static final String NAVIGATION_GROUP = "Uif-NavigationGroupBase";
122 protected static final String TREE_GROUP = "Uif-TreeSection";
123 protected static final String LINK_GROUP = "Uif-LinkGroup";
124 protected static final String COLLECTION_GROUP = "Uif-StackedCollectionSection";
125 protected static final String COLLECTION_GROUP_TABLE_LAYOUT = "Uif-TableCollectionSection";
126 protected static final String LIST_GROUP = "Uif-ListCollectionSection";
127
128 protected static final String HEADER = "Uif-HeaderFieldBase";
129 protected static final String FOOTER = "Uif-FooterBase";
130 protected static final String FOOTER_SAVECLOSECANCEL = "Uif-FormFooter";
131
132
133
134
135
136
137
138
139 public static Component getNewInstanceForRefresh(View view, String id) {
140 Component component = null;
141 Component origComponent = view.getViewIndex().getComponentById(id);
142
143 if (view.getViewIndex().getInitialComponentStates().containsKey(origComponent.getFactoryId())) {
144 component = view.getViewIndex().getInitialComponentStates().get(origComponent.getFactoryId());
145 } else {
146 component = (Component) KRADServiceLocatorWeb.getDataDictionaryService().getDictionaryObject(
147 origComponent.getFactoryId());
148 }
149
150 if (component != null) {
151 component = ComponentUtils.copyObject(component);
152 component.setId(origComponent.getFactoryId());
153 }
154
155 return component;
156 }
157
158
159
160
161
162
163
164 public static Component getNewComponentInstance(String beanId) {
165 Component component = (Component) KRADServiceLocatorWeb.getDataDictionaryService().getDictionaryObject(beanId);
166
167
168 component.setId(null);
169 component.setFactoryId(null);
170
171 return component;
172 }
173
174 public static TextControl getTextControl() {
175 return (TextControl) getNewComponentInstance(TEXT_CONTROL);
176 }
177
178 public static TextAreaControl getTextAreaControl() {
179 return (TextAreaControl) getNewComponentInstance(TEXTAREA_CONTROL);
180 }
181
182 public static CheckboxControl getCheckboxControl() {
183 return (CheckboxControl) getNewComponentInstance(CHECKBOX_CONTROL);
184 }
185
186 public static HiddenControl getHiddenControl() {
187 return (HiddenControl) getNewComponentInstance(HIDDEN_CONTROL);
188 }
189
190 public static SelectControl getSelectControl() {
191 return (SelectControl) getNewComponentInstance(SELECT_CONTROL);
192 }
193
194 public static CheckboxGroupControl getCheckboxGroupControl() {
195 return (CheckboxGroupControl) getNewComponentInstance(CHECKBOX_GROUP_CONTROL);
196 }
197
198 public static CheckboxGroupControl getCheckboxGroupControlHorizontal() {
199 return (CheckboxGroupControl) getNewComponentInstance(CHECKBOX_GROUP_CONTROL_HORIZONTAL);
200 }
201
202 public static RadioGroupControl getRadioGroupControl() {
203 return (RadioGroupControl) getNewComponentInstance(RADIO_GROUP_CONTROL);
204 }
205
206 public static RadioGroupControl getRadioGroupControlHorizontal() {
207 return (RadioGroupControl) getNewComponentInstance(RADIO_GROUP_CONTROL_HORIZONTAL);
208 }
209
210 public static FileControl getFileControl() {
211 return (FileControl) getNewComponentInstance(FILE_CONTROL);
212 }
213
214 public static TextControl getDateControl() {
215 return (TextControl) getNewComponentInstance(DATE_CONTROL);
216 }
217
218 public static TextControl getUserControl() {
219 return (TextControl) getNewComponentInstance(USER_CONTROL);
220 }
221
222 public static TextControl getGroupControl() {
223 return (TextControl) getNewComponentInstance(GROUP_CONTROL);
224 }
225
226 public static DataField getDataField() {
227 return (DataField) getNewComponentInstance(DATA_FIELD);
228 }
229
230 public static DataField getDataField(String propertyName, String label) {
231 DataField field = (DataField) getNewComponentInstance(DATA_FIELD);
232
233 field.setPropertyName(propertyName);
234 field.setLabel(label);
235
236 return field;
237 }
238
239 public static InputField getInputField() {
240 return (InputField) getNewComponentInstance(INPUT_FIELD);
241 }
242
243 public static InputField getInputField(String propertyName, String label) {
244 InputField field = (InputField) getNewComponentInstance(INPUT_FIELD);
245
246 field.setPropertyName(propertyName);
247 field.setLabel(label);
248
249 return field;
250 }
251
252 public static InputField getInputField(String propertyName, String label, UifConstants.ControlType controlType) {
253 InputField field = (InputField) getNewComponentInstance(INPUT_FIELD);
254
255 field.setPropertyName(propertyName);
256 field.setLabel(label);
257 field.setControl(getControl(controlType));
258
259 return field;
260 }
261
262 public static InputField getInputField(String propertyName, String label, UifConstants.ControlType controlType,
263 String defaultValue) {
264 InputField field = (InputField) getNewComponentInstance(INPUT_FIELD);
265
266 field.setPropertyName(propertyName);
267 field.setLabel(label);
268 field.setControl(getControl(controlType));
269 field.setDefaultValue(defaultValue);
270
271 return field;
272 }
273
274 public static InputField getInputField(String propertyName, String label, UifConstants.ControlType controlType,
275 Class<? extends KeyValuesFinder> optionsFinderClass) {
276 InputField field = (InputField) getNewComponentInstance(INPUT_FIELD);
277
278 field.setPropertyName(propertyName);
279 field.setLabel(label);
280 field.setControl(getControl(controlType));
281 field.setOptionsFinderClass(optionsFinderClass);
282
283 return field;
284 }
285
286 public static InputField getInputField(String propertyName, String label, UifConstants.ControlType controlType,
287 List<KeyValue> options) {
288 InputField field = (InputField) getNewComponentInstance(INPUT_FIELD);
289
290 field.setPropertyName(propertyName);
291 field.setLabel(label);
292
293 Control control = getControl(controlType);
294 if (control instanceof MultiValueControl) {
295 ((MultiValueControl) control).setOptions(options);
296 } else {
297 throw new RuntimeException("Control is not instance of multi-value control, cannot set options");
298 }
299
300 return field;
301 }
302
303 public static InputField getInputField(String propertyName, String label, UifConstants.ControlType controlType,
304 int size, int maxLength, int minLength) {
305 InputField field = (InputField) getNewComponentInstance(INPUT_FIELD);
306
307 field.setPropertyName(propertyName);
308 field.setLabel(label);
309
310 Control control = getControl(controlType);
311 if (control instanceof SizedControl) {
312 ((SizedControl) control).setSize(size);
313 } else {
314 throw new RuntimeException("Control does not support the size property");
315 }
316
317 field.setMaxLength(maxLength);
318 field.setMinLength(minLength);
319
320 return field;
321 }
322
323
324
325
326
327
328
329
330
331
332
333
334
335 public static InputField translateRemotableField(RemotableAttributeField remotableField) {
336 InputField inputField = getInputField();
337
338 inputField.setPropertyName(remotableField.getName());
339 inputField.setShortLabel(remotableField.getShortLabel());
340 inputField.setLabel(remotableField.getLongLabel());
341 inputField.setHelpSummary(remotableField.getHelpSummary());
342 inputField.setHelpDescription(remotableField.getHelpDescription());
343 inputField.setConstraintText(remotableField.getConstraintText());
344 inputField.setPerformUppercase(remotableField.isForceUpperCase());
345 inputField.setMinLength(remotableField.getMinLength());
346 inputField.setMaxLength(remotableField.getMaxLength());
347
348
349 if (remotableField.getMinValue() != null) {
350 inputField.setExclusiveMin(remotableField.getMinValue().toString());
351 }
352 if (remotableField.getMaxValue() != null) {
353 inputField.setInclusiveMax(remotableField.getMaxValue().toString());
354 }
355 inputField.setRequired(remotableField.isRequired());
356
357 if ((remotableField.getDefaultValues() != null) && !remotableField.getDefaultValues().isEmpty()) {
358 inputField.setDefaultValue(remotableField.getDefaultValues().iterator().next());
359 }
360
361 if (StringUtils.isNotBlank(remotableField.getRegexConstraint())) {
362 ValidCharactersConstraint constraint = new ValidCharactersConstraint();
363 constraint.setValue(remotableField.getRegexConstraint());
364 inputField.setValidCharactersConstraint(constraint);
365
366 }
367
368 RemotableDatepicker remotableDatepicker = null;
369 RemotableTextExpand remotableTextExpand = null;
370 RemotableQuickFinder remotableQuickFinder = null;
371 for (RemotableAbstractWidget remoteWidget : remotableField.getWidgets()) {
372 if (remoteWidget instanceof RemotableDatepicker) {
373 remotableDatepicker = (RemotableDatepicker) remoteWidget;
374 } else if (remoteWidget instanceof RemotableTextExpand) {
375 remotableTextExpand = (RemotableTextExpand) remoteWidget;
376 } else if (remoteWidget instanceof RemotableQuickFinder) {
377 remotableQuickFinder = (RemotableQuickFinder) remoteWidget;
378 }
379 }
380
381 if (remotableQuickFinder != null) {
382 inputField.getFieldLookup().setBaseLookupUrl(remotableQuickFinder.getBaseLookupUrl());
383 inputField.getFieldLookup().setDataObjectClassName(remotableQuickFinder.getDataObjectClass());
384 inputField.getFieldLookup().setLookupParameters(remotableQuickFinder.getLookupParameters());
385 inputField.getFieldLookup().setFieldConversions(remotableQuickFinder.getFieldConversions());
386 }
387
388 if (remotableField.getControl() != null) {
389 Control control = null;
390
391 RemotableControlContract remotableControl = remotableField.getControl();
392 if (remotableControl instanceof RemotableHiddenInput) {
393 control = getHiddenControl();
394 } else if (remotableControl instanceof RemotableRadioButtonGroup) {
395 RemotableRadioButtonGroup remotableRadioButtonGroup = (RemotableRadioButtonGroup) remotableControl;
396 control = getRadioGroupControl();
397 ((RadioGroupControl) control).setOptions(buildKeyValuePairs(remotableRadioButtonGroup.getKeyLabels()));
398 } else if (remotableControl instanceof RemotableSelect) {
399 RemotableSelect remotableSelect = (RemotableSelect) remotableControl;
400 control = getSelectControl();
401
402 Map<String, String> keyLabels = new HashMap<String, String>();
403 if ((remotableSelect.getGroups() != null) && (!remotableSelect.getGroups().isEmpty())) {
404 for (RemotableSelectGroup remotableSelectGroup : remotableSelect.getGroups()) {
405 keyLabels.putAll(remotableSelectGroup.getKeyLabels());
406 }
407 } else {
408 keyLabels = remotableSelect.getKeyLabels();
409 }
410
411 ((SelectControl) control).setOptions(buildKeyValuePairs(keyLabels));
412 if (remotableSelect.getSize() != null) {
413 ((SelectControl) control).setSize(remotableSelect.getSize());
414 }
415 ((SelectControl) control).setMultiple(remotableSelect.isMultiple());
416 } else if (remotableControl instanceof RemotableCheckboxGroup) {
417 RemotableCheckboxGroup remotableCheckboxGroup = (RemotableCheckboxGroup) remotableControl;
418 control = getCheckboxGroupControl();
419 ((CheckboxGroupControl) control).setOptions(buildKeyValuePairs(remotableCheckboxGroup.getKeyLabels()));
420 } else if (remotableControl instanceof RemotableCheckbox) {
421 control = getCheckboxControl();
422 } else if (remotableControl instanceof RemotableTextarea) {
423 RemotableTextarea remotableTextarea = (RemotableTextarea) remotableControl;
424 control = getTextAreaControl();
425
426 if (remotableTextExpand != null) {
427 ((TextAreaControl) control).setTextExpand(true);
428 }
429 ((TextAreaControl) control).setRows(remotableTextarea.getRows());
430 ((TextAreaControl) control).setCols(remotableTextarea.getCols());
431 ((TextAreaControl) control).setWatermarkText(remotableTextarea.getWatermark());
432
433 } else if (remotableControl instanceof RemotableTextInput) {
434 RemotableTextInput remotableTextInput = (RemotableTextInput) remotableControl;
435
436 if (remotableDatepicker != null) {
437 control = getDateControl();
438 } else {
439 control = getTextControl();
440 }
441
442 if (remotableTextExpand != null) {
443 ((TextAreaControl) control).setTextExpand(true);
444 }
445 ((TextControl) control).setSize(remotableTextInput.getSize());
446 ((TextControl) control).setWatermarkText(remotableTextInput.getWatermark());
447 }
448
449 inputField.setControl(control);
450 }
451
452 return inputField;
453 }
454
455
456
457
458
459
460
461
462
463 public static List<InputField> translateRemotableFields(List<RemotableAttributeField> remotableFields) {
464 List<InputField> inputFields = new ArrayList<InputField>();
465
466 for (RemotableAttributeField remotableField : remotableFields) {
467 inputFields.add(translateRemotableField(remotableField));
468 }
469
470 return inputFields;
471 }
472
473 protected static List<KeyValue> buildKeyValuePairs(Map<String, String> optionsMap) {
474 List<KeyValue> options = new ArrayList<KeyValue>();
475
476 for (Map.Entry<String, String> optionEntry : optionsMap.entrySet()) {
477 KeyValue keyValue = new ConcreteKeyValue(optionEntry.getKey(), optionEntry.getValue());
478 options.add(keyValue);
479 }
480
481 return options;
482 }
483
484 protected static Control getControl(UifConstants.ControlType controlType) {
485 Control control = null;
486 switch (controlType) {
487 case CHECKBOX:
488 control = getCheckboxControl();
489 case CHECKBOXGROUP:
490 control = getCheckboxGroupControl();
491 case FILE:
492 control = getFileControl();
493 case GROUP:
494 control = getGroupControl();
495 case HIDDEN:
496 control = getHiddenControl();
497 case RADIOGROUP:
498 control = getRadioGroupControl();
499 case SELECT:
500 control = getSelectControl();
501 case TEXTAREA:
502 control = getTextAreaControl();
503 case TEXT:
504 control = getTextControl();
505 case USER:
506 control = getUserControl();
507 }
508
509 return control;
510 }
511
512 public static ErrorsField getErrorsField() {
513 return (ErrorsField) getNewComponentInstance(ERRORS_FIELD);
514 }
515
516 public static ActionField getActionField() {
517 return (ActionField) getNewComponentInstance(ACTION_FIELD);
518 }
519
520 public static ActionField getActionLinkField() {
521 return (ActionField) getNewComponentInstance(ACTION_LINK_FIELD);
522 }
523
524 public static LinkField getLinkField() {
525 return (LinkField) getNewComponentInstance(LINK_FIELD);
526 }
527
528 public static IframeField getIframeField() {
529 return (IframeField) getNewComponentInstance(IFRAME_FIELD);
530 }
531
532 public static ImageField getImageField() {
533 return (ImageField) getNewComponentInstance(IMAGE_FIELD);
534 }
535
536 public static BlankField getBlankField() {
537 return (BlankField) getNewComponentInstance(BLANK_FIELD);
538 }
539
540 public static GenericField getGenericField() {
541 return (GenericField) getNewComponentInstance(GENERIC_FIELD);
542 }
543
544 public static LabelField getLabelField() {
545 return (LabelField) getNewComponentInstance(LABEL_FIELD);
546 }
547
548 public static MessageField getMessageField() {
549 return (MessageField) getNewComponentInstance(MESSAGE_FIELD);
550 }
551
552 public static FieldGroup getFieldGroup() {
553 return (FieldGroup) getNewComponentInstance(FIELD_GROUP);
554 }
555
556 public static FieldGroup getHorizontalFieldGroup() {
557 return (FieldGroup) getNewComponentInstance(HORIZONTAL_FIELD_GROUP);
558 }
559
560 public static Group getGroup() {
561 return (Group) getNewComponentInstance(GROUP);
562 }
563
564 public static PageGroup getPageGroup() {
565 return (PageGroup) getNewComponentInstance(PAGE_GROUP);
566 }
567
568 public static Group getGroupGridLayout() {
569 return (Group) getNewComponentInstance(GROUP_GRID_LAYOUT);
570 }
571
572 public static Group getGroupBodyOnly() {
573 return (Group) getNewComponentInstance(GROUP_BODY_ONLY);
574 }
575
576 public static Group getGroupGridBodyOnly() {
577 return (Group) getNewComponentInstance(GROUP_GRID_BODY_ONLY);
578 }
579
580 public static TabGroup getTabGroup() {
581 return (TabGroup) getNewComponentInstance(TAB_GROUP);
582 }
583
584 public static NavigationGroup getNavigationGroup() {
585 return (NavigationGroup) getNewComponentInstance(NAVIGATION_GROUP);
586 }
587
588 public static TreeGroup getTreeGroup() {
589 return (TreeGroup) getNewComponentInstance(TREE_GROUP);
590 }
591
592 public static LinkGroup getLinkGroup() {
593 return (LinkGroup) getNewComponentInstance(LINK_GROUP);
594 }
595
596 public static CollectionGroup getCollectionGroup() {
597 return (CollectionGroup) getNewComponentInstance(COLLECTION_GROUP);
598 }
599
600 public static CollectionGroup getCollectionGroupTableLayout() {
601 return (CollectionGroup) getNewComponentInstance(COLLECTION_GROUP_TABLE_LAYOUT);
602 }
603
604 public static CollectionGroup getListGroup() {
605 return (CollectionGroup) getNewComponentInstance(LIST_GROUP);
606 }
607
608 public static HeaderField getHeader() {
609 return (HeaderField) getNewComponentInstance(HEADER);
610 }
611
612 public static Group getFooter() {
613 return (Group) getNewComponentInstance(FOOTER);
614 }
615
616 public static Group getFooterSaveCloseCancel() {
617 return (Group) getNewComponentInstance(FOOTER_SAVECLOSECANCEL);
618 }
619
620 }