1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.rice.krad.uif.field;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.exception.RiceRuntimeException;
20  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
21  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
22  import org.kuali.rice.krad.datadictionary.parse.BeanTags;
23  import org.kuali.rice.krad.uif.UifConstants;
24  import org.kuali.rice.krad.uif.UifConstants.Position;
25  import org.kuali.rice.krad.uif.component.ComponentSecurity;
26  import org.kuali.rice.krad.uif.element.Label;
27  import org.kuali.rice.krad.uif.util.MessageStructureUtils;
28  import org.kuali.rice.krad.uif.view.View;
29  import org.kuali.rice.krad.uif.component.Component;
30  import org.kuali.rice.krad.uif.component.ComponentBase;
31  import org.kuali.rice.krad.uif.util.ComponentFactory;
32  
33  import java.util.List;
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  @BeanTags({@BeanTag(name = "fieldBase-bean", parent = "Uif-FieldBase"),
51      @BeanTag(name = "fieldBase-withLabel-bean", parent = "Uif-FieldBase-withLabel")})
52  public class FieldBase extends ComponentBase implements Field {
53      private static final long serialVersionUID = -5888414844802862760L;
54  
55      private String shortLabel;
56      private Label fieldLabel;
57  
58      private Position labelPlacement;
59  
60      private boolean labelRendered;
61  
62      public FieldBase() {
63          super();
64  
65          labelRendered = false;
66          labelPlacement = Position.LEFT;
67      }
68  
69      
70  
71  
72  
73  
74  
75  
76  
77      @Override
78      public void performInitialization(View view, Object model) {
79          super.performInitialization(view, model);
80      }
81  
82      
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97      @Override
98      public void performFinalize(View view, Object model, Component parent) {
99          super.performFinalize(view, model, parent);
100 
101         if (fieldLabel != null) {
102             fieldLabel.setLabelForComponentId(this.getId());
103 
104             if ((getRequired() != null) && getRequired().booleanValue()) {
105                 fieldLabel.getRequiredMessage().setRender(!isReadOnly());
106             } else {
107                 setRequired(new Boolean(false));
108                 fieldLabel.getRequiredMessage().setRender(true);
109 
110                 String prefixStyle = "";
111                 if (StringUtils.isNotBlank(fieldLabel.getRequiredMessage().getStyle())) {
112                     prefixStyle = fieldLabel.getRequiredMessage().getStyle();
113                 }
114                 fieldLabel.getRequiredMessage().setStyle(prefixStyle + ";" + "display: none;");
115             }
116 
117             if (labelPlacement.equals(Position.RIGHT)) {
118                 fieldLabel.setRenderColon(false);
119             }
120 
121             if (labelPlacement.equals(Position.TOP) || labelPlacement.equals(Position.BOTTOM)){
122                 fieldLabel.addStyleClass("uif-labelBlock");
123             }
124 
125             fieldLabel.addDataAttribute(UifConstants.DataAttributes.LABEL_FOR, this.getId());
126             if(StringUtils.isNotBlank(this.getFieldLabel().getLabelText())){
127                 this.addDataAttribute(UifConstants.DataAttributes.LABEL,
128                         MessageStructureUtils.translateStringMessage(this.getFieldLabel().getLabelText()));
129             }
130         }
131     }
132 
133     
134 
135 
136 
137 
138 
139     protected void setNestedComponentIdAndSuffix(Component component, String suffix) {
140         if (component != null) {
141             String fieldId = getId();
142             fieldId += suffix;
143 
144             component.setId(fieldId);
145         }
146     }
147 
148     
149 
150 
151     @Override
152     public final String getComponentTypeName() {
153         return "field";
154     }
155 
156     
157 
158 
159     @Override
160     public List<Component> getComponentsForLifecycle() {
161         List<Component> components = super.getComponentsForLifecycle();
162 
163         components.add(fieldLabel);
164 
165         return components;
166     }
167 
168     
169 
170 
171     @BeanTagAttribute(name = "label")
172     public String getLabel() {
173         if (fieldLabel != null) {
174             return fieldLabel.getLabelText();
175         }
176 
177         return null;
178     }
179 
180     
181 
182 
183     public void setLabel(String labelText) {
184         if (StringUtils.isNotBlank(labelText) && this.fieldLabel == null) {
185             this.fieldLabel = ComponentFactory.getLabel();
186         }
187 
188         if (this.fieldLabel != null) {
189             this.fieldLabel.setLabelText(labelText);
190         }
191     }
192 
193     
194 
195 
196     @BeanTagAttribute(name="labelStyleClasses",type= BeanTagAttribute.AttributeType.LISTVALUE)
197     public List<String> getLabelStyleClasses() {
198         if (fieldLabel != null) {
199             return fieldLabel.getCssClasses();
200         }
201 
202         return null;
203     }
204 
205     
206 
207 
208     public void setLabelStyleClasses(List<String> labelStyleClasses) {
209         if (labelStyleClasses != null && this.fieldLabel == null) {
210             this.fieldLabel = ComponentFactory.getLabel();
211         }
212 
213         if (this.fieldLabel != null) {
214             this.fieldLabel.setCssClasses(labelStyleClasses);
215         }
216     }
217 
218     
219 
220 
221     @BeanTagAttribute(name="labelColSpan")
222     public int getLabelColSpan() {
223         if (fieldLabel != null) {
224             return fieldLabel.getColSpan();
225         }
226 
227         return 1;
228     }
229 
230     
231 
232 
233     public void setLabelColSpan(int labelColSpan) {
234         if (this.fieldLabel == null) {
235             this.fieldLabel = ComponentFactory.getLabel();
236         }
237 
238         if (this.fieldLabel != null) {
239             this.fieldLabel.setColSpan(labelColSpan);
240         }
241     }
242 
243     
244 
245 
246     @BeanTagAttribute(name="shortLabel")
247     public String getShortLabel() {
248         return this.shortLabel;
249     }
250 
251     
252 
253 
254     public void setShortLabel(String shortLabel) {
255         this.shortLabel = shortLabel;
256     }
257 
258     
259 
260 
261 
262 
263 
264 
265 
266 
267 
268 
269     public void setShowLabel(boolean showLabel) {
270         if (fieldLabel != null) {
271             fieldLabel.setRender(showLabel);
272         }
273     }
274 
275     
276 
277 
278     @BeanTagAttribute(name="fieldLabel",type= BeanTagAttribute.AttributeType.SINGLEBEAN)
279     public Label getFieldLabel() {
280         return this.fieldLabel;
281     }
282 
283     
284 
285 
286     public void setFieldLabel(Label fieldLabel) {
287         this.fieldLabel = fieldLabel;
288     }
289 
290     
291 
292 
293 
294 
295 
296     @BeanTagAttribute(name="labelPlacement",type= BeanTagAttribute.AttributeType.SINGLEBEAN)
297     public Position getLabelPlacement() {
298         return this.labelPlacement;
299     }
300 
301     
302 
303 
304 
305 
306     public void setLabelPlacement(Position labelPlacement) {
307         this.labelPlacement = labelPlacement;
308     }
309 
310     
311 
312 
313     @BeanTagAttribute(name="labelRendered")
314     public boolean isLabelRendered() {
315         return this.labelRendered;
316     }
317 
318     
319 
320 
321     public void setLabelRendered(boolean labelRendered) {
322         this.labelRendered = labelRendered;
323     }
324     
325     
326 
327 
328     public FieldSecurity getFieldSecurity() {
329         return (FieldSecurity) super.getComponentSecurity();
330     }
331 
332     
333 
334 
335 
336 
337     @Override
338     public void setComponentSecurity(ComponentSecurity componentSecurity) {
339         if ((componentSecurity != null) && !(componentSecurity instanceof FieldSecurity)) {
340             throw new RiceRuntimeException("Component security for Field should be instance of FieldSecurity");
341         }
342 
343         super.setComponentSecurity(componentSecurity);
344     }
345 
346     
347 
348 
349     @Override
350     protected Class<? extends ComponentSecurity> getComponentSecurityClass() {
351         return FieldSecurity.class;
352     }
353 
354     
355 
356 
357     @Override
358     protected <T> void copyProperties(T component) {
359         super.copyProperties(component);
360         FieldBase fieldBaseCopy = (FieldBase) component;
361         fieldBaseCopy.setShortLabel(this.shortLabel);
362         fieldBaseCopy.setLabelRendered(this.labelRendered);
363 
364         if (this.fieldLabel != null) {
365             fieldBaseCopy.setFieldLabel((Label)this.fieldLabel.copy());
366         }
367 
368         fieldBaseCopy.setLabelPlacement(this.labelPlacement);
369     }
370 }