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