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 (origComponent == null) {
144 throw new RuntimeException(id + " not found in view index try setting p:persistInSession=\"true\" in xml");
145 }
146
147 if (view.getViewIndex().getInitialComponentStates().containsKey(origComponent.getFactoryId())) {
148 component = view.getViewIndex().getInitialComponentStates().get(origComponent.getFactoryId());
149 } else {
150 component = (Component) KRADServiceLocatorWeb.getDataDictionaryService().getDictionaryObject(
151 origComponent.getFactoryId());
152 }
153
154 if (component != null) {
155 component = ComponentUtils.copyObject(component);
156 component.setId(origComponent.getFactoryId());
157 }
158
159 return component;
160 }
161
162
163
164
165
166
167
168 public static Component getNewComponentInstance(String beanId) {
169 Component component = (Component) KRADServiceLocatorWeb.getDataDictionaryService().getDictionaryObject(beanId);
170
171
172 component.setId(null);
173 component.setFactoryId(null);
174
175 return component;
176 }
177
178 public static TextControl getTextControl() {
179 return (TextControl) getNewComponentInstance(TEXT_CONTROL);
180 }
181
182 public static TextAreaControl getTextAreaControl() {
183 return (TextAreaControl) getNewComponentInstance(TEXTAREA_CONTROL);
184 }
185
186 public static CheckboxControl getCheckboxControl() {
187 return (CheckboxControl) getNewComponentInstance(CHECKBOX_CONTROL);
188 }
189
190 public static HiddenControl getHiddenControl() {
191 return (HiddenControl) getNewComponentInstance(HIDDEN_CONTROL);
192 }
193
194 public static SelectControl getSelectControl() {
195 return (SelectControl) getNewComponentInstance(SELECT_CONTROL);
196 }
197
198 public static CheckboxGroupControl getCheckboxGroupControl() {
199 return (CheckboxGroupControl) getNewComponentInstance(CHECKBOX_GROUP_CONTROL);
200 }
201
202 public static CheckboxGroupControl getCheckboxGroupControlHorizontal() {
203 return (CheckboxGroupControl) getNewComponentInstance(CHECKBOX_GROUP_CONTROL_HORIZONTAL);
204 }
205
206 public static RadioGroupControl getRadioGroupControl() {
207 return (RadioGroupControl) getNewComponentInstance(RADIO_GROUP_CONTROL);
208 }
209
210 public static RadioGroupControl getRadioGroupControlHorizontal() {
211 return (RadioGroupControl) getNewComponentInstance(RADIO_GROUP_CONTROL_HORIZONTAL);
212 }
213
214 public static FileControl getFileControl() {
215 return (FileControl) getNewComponentInstance(FILE_CONTROL);
216 }
217
218 public static TextControl getDateControl() {
219 return (TextControl) getNewComponentInstance(DATE_CONTROL);
220 }
221
222 public static TextControl getUserControl() {
223 return (TextControl) getNewComponentInstance(USER_CONTROL);
224 }
225
226 public static TextControl getGroupControl() {
227 return (TextControl) getNewComponentInstance(GROUP_CONTROL);
228 }
229
230 public static DataField getDataField() {
231 return (DataField) getNewComponentInstance(DATA_FIELD);
232 }
233
234 public static DataField getDataField(String propertyName, String label) {
235 DataField field = (DataField) getNewComponentInstance(DATA_FIELD);
236
237 field.setPropertyName(propertyName);
238 field.setLabel(label);
239
240 return field;
241 }
242
243 public static InputField getInputField() {
244 return (InputField) getNewComponentInstance(INPUT_FIELD);
245 }
246
247 public static InputField getInputField(String propertyName, String label) {
248 InputField field = (InputField) getNewComponentInstance(INPUT_FIELD);
249
250 field.setPropertyName(propertyName);
251 field.setLabel(label);
252
253 return field;
254 }
255
256 public static InputField getInputField(String propertyName, String label, UifConstants.ControlType controlType) {
257 InputField field = (InputField) getNewComponentInstance(INPUT_FIELD);
258
259 field.setPropertyName(propertyName);
260 field.setLabel(label);
261 field.setControl(getControl(controlType));
262
263 return field;
264 }
265
266 public static InputField getInputField(String propertyName, String label, UifConstants.ControlType controlType,
267 String defaultValue) {
268 InputField field = (InputField) getNewComponentInstance(INPUT_FIELD);
269
270 field.setPropertyName(propertyName);
271 field.setLabel(label);
272 field.setControl(getControl(controlType));
273 field.setDefaultValue(defaultValue);
274
275 return field;
276 }
277
278 public static InputField getInputField(String propertyName, String label, UifConstants.ControlType controlType,
279 Class<? extends KeyValuesFinder> optionsFinderClass) {
280 InputField field = (InputField) getNewComponentInstance(INPUT_FIELD);
281
282 field.setPropertyName(propertyName);
283 field.setLabel(label);
284 field.setControl(getControl(controlType));
285 field.setOptionsFinderClass(optionsFinderClass);
286
287 return field;
288 }
289
290 public static InputField getInputField(String propertyName, String label, UifConstants.ControlType controlType,
291 List<KeyValue> options) {
292 InputField field = (InputField) getNewComponentInstance(INPUT_FIELD);
293
294 field.setPropertyName(propertyName);
295 field.setLabel(label);
296
297 Control control = getControl(controlType);
298 if (control instanceof MultiValueControl) {
299 ((MultiValueControl) control).setOptions(options);
300 } else {
301 throw new RuntimeException("Control is not instance of multi-value control, cannot set options");
302 }
303
304 return field;
305 }
306
307 public static InputField getInputField(String propertyName, String label, UifConstants.ControlType controlType,
308 int size, int maxLength, int minLength) {
309 InputField field = (InputField) getNewComponentInstance(INPUT_FIELD);
310
311 field.setPropertyName(propertyName);
312 field.setLabel(label);
313
314 Control control = getControl(controlType);
315 if (control instanceof SizedControl) {
316 ((SizedControl) control).setSize(size);
317 } else {
318 throw new RuntimeException("Control does not support the size property");
319 }
320
321 field.setMaxLength(maxLength);
322 field.setMinLength(minLength);
323
324 return field;
325 }
326
327
328
329
330
331
332
333
334
335
336
337
338
339 public static InputField translateRemotableField(RemotableAttributeField remotableField) {
340 InputField inputField = getInputField();
341
342 inputField.setPropertyName(remotableField.getName());
343 inputField.setShortLabel(remotableField.getShortLabel());
344 inputField.setLabel(remotableField.getLongLabel());
345 inputField.setHelpSummary(remotableField.getHelpSummary());
346 inputField.setHelpDescription(remotableField.getHelpDescription());
347 inputField.setConstraintText(remotableField.getConstraintText());
348 inputField.setPerformUppercase(remotableField.isForceUpperCase());
349 inputField.setMinLength(remotableField.getMinLength());
350 inputField.setMaxLength(remotableField.getMaxLength());
351
352
353 if (remotableField.getMinValue() != null) {
354 inputField.setExclusiveMin(remotableField.getMinValue().toString());
355 }
356 if (remotableField.getMaxValue() != null) {
357 inputField.setInclusiveMax(remotableField.getMaxValue().toString());
358 }
359 inputField.setRequired(remotableField.isRequired());
360
361 if ((remotableField.getDefaultValues() != null) && !remotableField.getDefaultValues().isEmpty()) {
362 inputField.setDefaultValue(remotableField.getDefaultValues().iterator().next());
363 }
364
365 if (StringUtils.isNotBlank(remotableField.getRegexConstraint())) {
366 ValidCharactersConstraint constraint = new ValidCharactersConstraint();
367 constraint.setValue(remotableField.getRegexConstraint());
368 inputField.setValidCharactersConstraint(constraint);
369
370 }
371
372 RemotableDatepicker remotableDatepicker = null;
373 RemotableTextExpand remotableTextExpand = null;
374 RemotableQuickFinder remotableQuickFinder = null;
375 for (RemotableAbstractWidget remoteWidget : remotableField.getWidgets()) {
376 if (remoteWidget instanceof RemotableDatepicker) {
377 remotableDatepicker = (RemotableDatepicker) remoteWidget;
378 } else if (remoteWidget instanceof RemotableTextExpand) {
379 remotableTextExpand = (RemotableTextExpand) remoteWidget;
380 } else if (remoteWidget instanceof RemotableQuickFinder) {
381 remotableQuickFinder = (RemotableQuickFinder) remoteWidget;
382 }
383 }
384
385 if (remotableQuickFinder != null) {
386 inputField.getFieldLookup().setBaseLookupUrl(remotableQuickFinder.getBaseLookupUrl());
387 inputField.getFieldLookup().setDataObjectClassName(remotableQuickFinder.getDataObjectClass());
388 inputField.getFieldLookup().setLookupParameters(remotableQuickFinder.getLookupParameters());
389 inputField.getFieldLookup().setFieldConversions(remotableQuickFinder.getFieldConversions());
390 }
391
392 if (remotableField.getControl() != null) {
393 Control control = null;
394
395 RemotableControlContract remotableControl = remotableField.getControl();
396 if (remotableControl instanceof RemotableHiddenInput) {
397 control = getHiddenControl();
398 } else if (remotableControl instanceof RemotableRadioButtonGroup) {
399 RemotableRadioButtonGroup remotableRadioButtonGroup = (RemotableRadioButtonGroup) remotableControl;
400 control = getRadioGroupControl();
401 ((RadioGroupControl) control).setOptions(buildKeyValuePairs(remotableRadioButtonGroup.getKeyLabels()));
402 } else if (remotableControl instanceof RemotableSelect) {
403 RemotableSelect remotableSelect = (RemotableSelect) remotableControl;
404 control = getSelectControl();
405
406 Map<String, String> keyLabels = new HashMap<String, String>();
407 if ((remotableSelect.getGroups() != null) && (!remotableSelect.getGroups().isEmpty())) {
408 for (RemotableSelectGroup remotableSelectGroup : remotableSelect.getGroups()) {
409 keyLabels.putAll(remotableSelectGroup.getKeyLabels());
410 }
411 } else {
412 keyLabels = remotableSelect.getKeyLabels();
413 }
414
415 ((SelectControl) control).setOptions(buildKeyValuePairs(keyLabels));
416 if (remotableSelect.getSize() != null) {
417 ((SelectControl) control).setSize(remotableSelect.getSize());
418 }
419 ((SelectControl) control).setMultiple(remotableSelect.isMultiple());
420 } else if (remotableControl instanceof RemotableCheckboxGroup) {
421 RemotableCheckboxGroup remotableCheckboxGroup = (RemotableCheckboxGroup) remotableControl;
422 control = getCheckboxGroupControl();
423 ((CheckboxGroupControl) control).setOptions(buildKeyValuePairs(remotableCheckboxGroup.getKeyLabels()));
424 } else if (remotableControl instanceof RemotableCheckbox) {
425 control = getCheckboxControl();
426 } else if (remotableControl instanceof RemotableTextarea) {
427 RemotableTextarea remotableTextarea = (RemotableTextarea) remotableControl;
428 control = getTextAreaControl();
429
430 if (remotableTextExpand != null) {
431 ((TextAreaControl) control).setTextExpand(true);
432 }
433 ((TextAreaControl) control).setRows(remotableTextarea.getRows());
434 ((TextAreaControl) control).setCols(remotableTextarea.getCols());
435 ((TextAreaControl) control).setWatermarkText(remotableTextarea.getWatermark());
436
437 } else if (remotableControl instanceof RemotableTextInput) {
438 RemotableTextInput remotableTextInput = (RemotableTextInput) remotableControl;
439
440 if (remotableDatepicker != null) {
441 control = getDateControl();
442 } else {
443 control = getTextControl();
444 }
445
446 if (remotableTextExpand != null) {
447 ((TextAreaControl) control).setTextExpand(true);
448 }
449 ((TextControl) control).setSize(remotableTextInput.getSize());
450 ((TextControl) control).setWatermarkText(remotableTextInput.getWatermark());
451 }
452
453 inputField.setControl(control);
454 }
455
456 return inputField;
457 }
458
459
460
461
462
463
464
465
466
467 public static List<InputField> translateRemotableFields(List<RemotableAttributeField> remotableFields) {
468 List<InputField> inputFields = new ArrayList<InputField>();
469
470 for (RemotableAttributeField remotableField : remotableFields) {
471 inputFields.add(translateRemotableField(remotableField));
472 }
473
474 return inputFields;
475 }
476
477 protected static List<KeyValue> buildKeyValuePairs(Map<String, String> optionsMap) {
478 List<KeyValue> options = new ArrayList<KeyValue>();
479
480 for (Map.Entry<String, String> optionEntry : optionsMap.entrySet()) {
481 KeyValue keyValue = new ConcreteKeyValue(optionEntry.getKey(), optionEntry.getValue());
482 options.add(keyValue);
483 }
484
485 return options;
486 }
487
488 protected static Control getControl(UifConstants.ControlType controlType) {
489 Control control = null;
490 switch (controlType) {
491 case CHECKBOX:
492 control = getCheckboxControl();
493 case CHECKBOXGROUP:
494 control = getCheckboxGroupControl();
495 case FILE:
496 control = getFileControl();
497 case GROUP:
498 control = getGroupControl();
499 case HIDDEN:
500 control = getHiddenControl();
501 case RADIOGROUP:
502 control = getRadioGroupControl();
503 case SELECT:
504 control = getSelectControl();
505 case TEXTAREA:
506 control = getTextAreaControl();
507 case TEXT:
508 control = getTextControl();
509 case USER:
510 control = getUserControl();
511 }
512
513 return control;
514 }
515
516 public static ErrorsField getErrorsField() {
517 return (ErrorsField) getNewComponentInstance(ERRORS_FIELD);
518 }
519
520 public static ActionField getActionField() {
521 return (ActionField) getNewComponentInstance(ACTION_FIELD);
522 }
523
524 public static ActionField getActionLinkField() {
525 return (ActionField) getNewComponentInstance(ACTION_LINK_FIELD);
526 }
527
528 public static LinkField getLinkField() {
529 return (LinkField) getNewComponentInstance(LINK_FIELD);
530 }
531
532 public static IframeField getIframeField() {
533 return (IframeField) getNewComponentInstance(IFRAME_FIELD);
534 }
535
536 public static ImageField getImageField() {
537 return (ImageField) getNewComponentInstance(IMAGE_FIELD);
538 }
539
540 public static BlankField getBlankField() {
541 return (BlankField) getNewComponentInstance(BLANK_FIELD);
542 }
543
544 public static GenericField getGenericField() {
545 return (GenericField) getNewComponentInstance(GENERIC_FIELD);
546 }
547
548 public static LabelField getLabelField() {
549 return (LabelField) getNewComponentInstance(LABEL_FIELD);
550 }
551
552 public static MessageField getMessageField() {
553 return (MessageField) getNewComponentInstance(MESSAGE_FIELD);
554 }
555
556 public static FieldGroup getFieldGroup() {
557 return (FieldGroup) getNewComponentInstance(FIELD_GROUP);
558 }
559
560 public static FieldGroup getHorizontalFieldGroup() {
561 return (FieldGroup) getNewComponentInstance(HORIZONTAL_FIELD_GROUP);
562 }
563
564 public static Group getGroup() {
565 return (Group) getNewComponentInstance(GROUP);
566 }
567
568 public static PageGroup getPageGroup() {
569 return (PageGroup) getNewComponentInstance(PAGE_GROUP);
570 }
571
572 public static Group getGroupGridLayout() {
573 return (Group) getNewComponentInstance(GROUP_GRID_LAYOUT);
574 }
575
576 public static Group getGroupBodyOnly() {
577 return (Group) getNewComponentInstance(GROUP_BODY_ONLY);
578 }
579
580 public static Group getGroupGridBodyOnly() {
581 return (Group) getNewComponentInstance(GROUP_GRID_BODY_ONLY);
582 }
583
584 public static TabGroup getTabGroup() {
585 return (TabGroup) getNewComponentInstance(TAB_GROUP);
586 }
587
588 public static NavigationGroup getNavigationGroup() {
589 return (NavigationGroup) getNewComponentInstance(NAVIGATION_GROUP);
590 }
591
592 public static TreeGroup getTreeGroup() {
593 return (TreeGroup) getNewComponentInstance(TREE_GROUP);
594 }
595
596 public static LinkGroup getLinkGroup() {
597 return (LinkGroup) getNewComponentInstance(LINK_GROUP);
598 }
599
600 public static CollectionGroup getCollectionGroup() {
601 return (CollectionGroup) getNewComponentInstance(COLLECTION_GROUP);
602 }
603
604 public static CollectionGroup getCollectionGroupTableLayout() {
605 return (CollectionGroup) getNewComponentInstance(COLLECTION_GROUP_TABLE_LAYOUT);
606 }
607
608 public static CollectionGroup getListGroup() {
609 return (CollectionGroup) getNewComponentInstance(LIST_GROUP);
610 }
611
612 public static HeaderField getHeader() {
613 return (HeaderField) getNewComponentInstance(HEADER);
614 }
615
616 public static Group getFooter() {
617 return (Group) getNewComponentInstance(FOOTER);
618 }
619
620 public static Group getFooterSaveCloseCancel() {
621 return (Group) getNewComponentInstance(FOOTER_SAVECLOSECANCEL);
622 }
623
624 }