1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.kuali.rice.krad.uif.util; |
12 | |
|
13 | |
import org.apache.commons.lang.StringUtils; |
14 | |
import org.kuali.rice.krad.service.DataDictionaryService; |
15 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
16 | |
import org.kuali.rice.krad.uif.container.CollectionGroup; |
17 | |
import org.kuali.rice.krad.uif.container.Group; |
18 | |
import org.kuali.rice.krad.uif.control.RadioGroupControl; |
19 | |
import org.kuali.rice.krad.uif.control.TextControl; |
20 | |
import org.kuali.rice.krad.uif.core.Component; |
21 | |
import org.kuali.rice.krad.uif.field.AttributeField; |
22 | |
import org.kuali.rice.krad.uif.field.Field; |
23 | |
import org.kuali.rice.krad.uif.field.GroupField; |
24 | |
import org.kuali.rice.krad.uif.field.LabelField; |
25 | |
import org.kuali.rice.krad.uif.field.MessageField; |
26 | |
import org.kuali.rice.krad.web.form.UifFormBase; |
27 | |
|
28 | |
import java.util.HashMap; |
29 | |
import java.util.List; |
30 | |
import java.util.Map; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | public class ComponentFactory { |
39 | |
|
40 | 0 | private static final Map<String, Component> componentDefinitions = new HashMap<String, Component>(); |
41 | |
|
42 | |
protected static final String LABEL_FIELD = "LabelField"; |
43 | |
protected static final String MESSAGE_FIELD = "MessageField"; |
44 | |
protected static final String TEXT_CONTROL = "TextControl"; |
45 | |
protected static final String RADIO_GROUP_CONTROL = "RadioGroupControl"; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public static void addComponentDefinition(String componentDictionaryId, Component component) { |
54 | 0 | componentDefinitions.put(componentDictionaryId, ComponentUtils.copyObject(component)); |
55 | 0 | } |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public static Component getNewComponentInstance(String componentDictionaryId) { |
66 | 0 | if (componentDefinitions.containsKey(componentDictionaryId)) { |
67 | 0 | Component component = componentDefinitions.get(componentDictionaryId); |
68 | |
|
69 | 0 | return ComponentUtils.copyObject(component); |
70 | |
} |
71 | |
|
72 | 0 | return null; |
73 | |
} |
74 | |
|
75 | |
public static LabelField getLabelField() { |
76 | 0 | return (LabelField) getNewComponentInstance(LABEL_FIELD); |
77 | |
} |
78 | |
|
79 | |
public static MessageField getMessageField() { |
80 | 0 | return (MessageField) getNewComponentInstance(MESSAGE_FIELD); |
81 | |
} |
82 | |
|
83 | |
public static TextControl getTextControl() { |
84 | 0 | return (TextControl) getNewComponentInstance(TEXT_CONTROL); |
85 | |
} |
86 | |
|
87 | |
public static RadioGroupControl getRadioGroupControl() { |
88 | 0 | return (RadioGroupControl) getNewComponentInstance(RADIO_GROUP_CONTROL); |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public static Component getComponentById(UifFormBase form, String id) { |
99 | 0 | Component origComponent = form.getView().getViewIndex().getComponentById(id); |
100 | 0 | Component component = getNewComponentInstance(origComponent.getBaseId()); |
101 | |
|
102 | 0 | return component; |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
public static Component getComponentByIdWithLifecycle(UifFormBase form, String id) { |
114 | 0 | Component origComponent = form.getView().getViewIndex().getComponentById(id); |
115 | 0 | Component component = getComponentById(form, id); |
116 | |
|
117 | 0 | form.getView().getViewHelperService().performComponentLifecycle(form, component, id); |
118 | |
|
119 | |
|
120 | 0 | if(component instanceof Field){ |
121 | 0 | ((Field) component).setLabelFieldRendered(((Field)origComponent).isLabelFieldRendered()); |
122 | |
} |
123 | |
|
124 | 0 | if(component instanceof AttributeField){ |
125 | 0 | ((AttributeField) component).setBindingInfo(((AttributeField)origComponent).getBindingInfo()); |
126 | |
} |
127 | |
|
128 | 0 | if(component instanceof CollectionGroup){ |
129 | 0 | ((CollectionGroup) component).setBindingInfo(((CollectionGroup)origComponent).getBindingInfo()); |
130 | |
} |
131 | |
|
132 | 0 | if(component instanceof Group || component instanceof GroupField){ |
133 | 0 | List<AttributeField> fields = ComponentUtils.getComponentsOfTypeDeep(component, AttributeField.class); |
134 | 0 | String suffix = StringUtils.replaceOnce(component.getId(), component.getBaseId(), ""); |
135 | 0 | for(AttributeField field: fields){ |
136 | 0 | AttributeField origField = (AttributeField)form.getView().getViewIndex().getComponentById(StringUtils.replaceOnce(field.getId(), field.getBaseId(), field.getBaseId() + suffix)); |
137 | 0 | if(origField != null){ |
138 | 0 | field.setBindingInfo(origField.getBindingInfo()); |
139 | |
} |
140 | 0 | field.setLabelFieldRendered(origField.isLabelFieldRendered()); |
141 | 0 | } |
142 | |
|
143 | 0 | List<CollectionGroup> collections = ComponentUtils.getComponentsOfTypeDeep(component, CollectionGroup.class); |
144 | 0 | for(CollectionGroup collection: collections){ |
145 | 0 | CollectionGroup origField = (CollectionGroup)form.getView().getViewIndex().getComponentById(StringUtils.replaceOnce(collection.getId(), collection.getBaseId(), collection.getBaseId() + suffix)); |
146 | 0 | if(origField != null){ |
147 | 0 | collection.setBindingInfo(origField.getBindingInfo()); |
148 | |
} |
149 | 0 | } |
150 | |
} |
151 | |
|
152 | 0 | form.getView().getViewIndex().indexComponent(component); |
153 | |
|
154 | 0 | return component; |
155 | |
} |
156 | |
|
157 | |
|
158 | |
|
159 | |
protected static DataDictionaryService getDataDictionaryService() { |
160 | 0 | return KRADServiceLocatorWeb.getDataDictionaryService(); |
161 | |
} |
162 | |
} |