1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.uif.modifier;
17
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Set;
21
22 import org.kuali.rice.kns.uif.container.View;
23 import org.kuali.rice.kns.uif.core.Component;
24 import org.kuali.rice.kns.uif.util.ComponentUtils;
25 import org.kuali.rice.kns.uif.util.ObjectPropertyUtils;
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public class ComponentConvertModifier extends ComponentModifierBase {
40 private static final long serialVersionUID = -7566547737669924605L;
41
42 private Class<? extends Component> componentTypeToReplace;
43
44 private Component componentReplacementPrototype;
45
46 public ComponentConvertModifier() {
47 super();
48 }
49
50
51
52
53
54 @Override
55 public void performModification(View view, Component component) {
56 if (component == null) {
57 return;
58 }
59
60 int idSuffix = 0;
61 convertToReplacement(component, idSuffix);
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76 protected void convertToReplacement(Component component, int idSuffix) {
77 if (component == null) {
78 return;
79 }
80
81
82 List<String> componentProperties = ComponentUtils.getComponentPropertyNames(component.getClass());
83 for (String propertyPath : componentProperties) {
84 Object propValue = ObjectPropertyUtils.getPropertyValue(component, propertyPath);
85
86 if (propValue != null) {
87 if (getComponentTypeToReplace().isAssignableFrom(propValue.getClass())) {
88
89 performConversion(component, propertyPath, idSuffix++);
90 }
91 }
92 }
93
94
95 for (Component nestedComponent : component.getNestedComponents()) {
96 convertToReplacement(nestedComponent, idSuffix);
97 }
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111 protected void performConversion(Component component, String componentProperty, int idSuffix) {
112
113 Component componentReplacement = ComponentUtils.copy(getComponentReplacementPrototype(), Integer.toString(idSuffix));
114
115 ObjectPropertyUtils.setPropertyValue(component, componentProperty, componentReplacement);
116 }
117
118
119
120
121 @Override
122 public Set<Class<? extends Component>> getSupportedComponents() {
123 Set<Class<? extends Component>> components = new HashSet<Class<? extends Component>>();
124 components.add(Component.class);
125
126 return components;
127 }
128
129
130
131
132
133
134
135 public Class<? extends Component> getComponentTypeToReplace() {
136 return this.componentTypeToReplace;
137 }
138
139
140
141
142
143
144 public void setComponentTypeToReplace(Class<? extends Component> componentTypeToReplace) {
145 this.componentTypeToReplace = componentTypeToReplace;
146 }
147
148
149
150
151
152
153
154
155
156
157
158 public Component getComponentReplacementPrototype() {
159 return this.componentReplacementPrototype;
160 }
161
162
163
164
165
166
167 public void setComponentReplacementPrototype(Component componentReplacementPrototype) {
168 this.componentReplacementPrototype = componentReplacementPrototype;
169 }
170
171 }