1 /**
2 * Copyright 2005-2012 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.kuali.rice.krad.uif.element;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.uif.UifConstants.Position;
20 import org.kuali.rice.krad.uif.component.Component;
21 import org.kuali.rice.krad.uif.view.View;
22
23 import java.util.List;
24
25 /**
26 * Content element that renders a label
27 *
28 * <p>
29 * Contains options for adding a colon to the label along with a required message
30 * </p>
31 *
32 * @author Kuali Rice Team (rice.collab@kuali.org)
33 */
34 public class Label extends ContentElementBase {
35 private static final long serialVersionUID = -6491546893195180114L;
36
37 private String labelText;
38 private String labelForComponentId;
39
40 private boolean renderColon;
41
42 private Position requiredMessagePlacement;
43 private Message requiredMessage;
44
45 public Label() {
46 renderColon = true;
47
48 requiredMessagePlacement = Position.LEFT;
49 }
50
51 /**
52 * The following finalization is performed:
53 *
54 * <ul>
55 * <li>If label text is blank, set render to false for field</li>
56 *
57 * @see org.kuali.rice.krad.uif.component.ComponentBase#performFinalize(org.kuali.rice.krad.uif.view.View,
58 * java.lang.Object, org.kuali.rice.krad.uif.component.Component)
59 */
60 @Override
61 public void performFinalize(View view, Object model, Component parent) {
62 super.performFinalize(view, model, parent);
63
64 if (StringUtils.isBlank(getLabelText())) {
65 setRender(false);
66 }
67 }
68
69 /**
70 * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
71 */
72 @Override
73 public List<Component> getComponentsForLifecycle() {
74 List<Component> components = super.getComponentsForLifecycle();
75
76 components.add(requiredMessage);
77
78 return components;
79 }
80
81 /**
82 * Indicates the id for the component the label applies to
83 * <p>
84 * Used for setting the labelFor attribute of the corresponding HTML
85 * element. Note this gets set automatically by the framework during the
86 * initialize phase
87 * </p>
88 *
89 * @return String component id
90 */
91 public String getLabelForComponentId() {
92 return this.labelForComponentId;
93 }
94
95 /**
96 * Setter for the component id the label applies to
97 *
98 * @param labelForComponentId
99 */
100 public void setLabelForComponentId(String labelForComponentId) {
101 this.labelForComponentId = labelForComponentId;
102 }
103
104 /**
105 * Text that will display as the label
106 *
107 * @return String label text
108 */
109 public String getLabelText() {
110 return this.labelText;
111 }
112
113 /**
114 * Setter for the label text
115 *
116 * @param labelText
117 */
118 public void setLabelText(String labelText) {
119 this.labelText = labelText;
120 }
121
122 /**
123 * Indicates whether a colon should be rendered after the label text,
124 * generally used when the label appears to the left of the field's control
125 * or value
126 *
127 * @return boolean true if a colon should be rendered, false if it should
128 * not be
129 */
130 public boolean isRenderColon() {
131 return this.renderColon;
132 }
133
134 /**
135 * Setter for the render colon indicator
136 *
137 * @param renderColon
138 */
139 public void setRenderColon(boolean renderColon) {
140 this.renderColon = renderColon;
141 }
142
143 /**
144 * <code>Message</code> instance that will display a required indicator
145 *
146 * <p>
147 * To indicate a field must have a value (required input) the required
148 * message field can be set to display an indicator or message along with
149 * the label. The message field also dictates the styling of the required
150 * message
151 * </p>
152 *
153 * @return Message instance
154 */
155 public Message getRequiredMessage() {
156 return this.requiredMessage;
157 }
158
159 /**
160 * Setter for the required message field
161 *
162 * @param requiredMessage
163 */
164 public void setRequiredMessage(Message requiredMessage) {
165 this.requiredMessage = requiredMessage;
166 }
167
168 /**
169 * Indicates where the required message field should be placed in relation
170 * to the label field, valid options are 'LEFT' and 'RIGHT'
171 *
172 * @return Position the requiredMessage placement
173 */
174 public Position getRequiredMessagePlacement() {
175 return this.requiredMessagePlacement;
176 }
177
178 /**
179 * Setter for the required message field placement
180 *
181 * @param requiredMessagePlacement
182 */
183 public void setRequiredMessagePlacement(Position requiredMessagePlacement) {
184 this.requiredMessagePlacement = requiredMessagePlacement;
185 }
186
187 }