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