001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.uif.field;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.uif.UifConstants.Position;
020import org.kuali.rice.krad.uif.component.Component;
021import org.kuali.rice.krad.uif.view.View;
022
023import java.util.List;
024
025/**
026 * Contains a label for another <code>Field</code> instance
027 *
028 * <p>
029 * The <code>LabelField</code> exists so that the label can be placed separate
030 * from the component in a layout manager such as the
031 * <code>GridLayoutManager</code>. It addition it can be used to style the label
032 * (from the inherited styleClass and style properties)
033 * </p>
034 * 
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 */
037public class LabelField extends FieldBase {
038    private static final long serialVersionUID = -6491546893195180114L;
039
040    private String labelText;
041    private String labelForComponentId;
042
043    private boolean renderColon;
044
045    private Position requiredMessagePlacement;
046    private MessageField requiredMessageField;
047
048    public LabelField() {
049        renderColon = true;
050
051        requiredMessagePlacement = Position.LEFT;
052    }
053
054    /**
055         * The following finalization is performed:
056         *
057         * <ul>
058         * <li>If label text is blank, set render to false for field</li>
059         *
060         * @see org.kuali.rice.krad.uif.component.ComponentBase#performFinalize(org.kuali.rice.krad.uif.view.View,
061         *      java.lang.Object, org.kuali.rice.krad.uif.component.Component)
062         */
063    @Override
064    public void performFinalize(View view, Object model, Component parent) {
065        super.performFinalize(view, model, parent);
066
067        if (StringUtils.isBlank(getLabelText())) {
068            setRender(false);
069        }
070    }
071
072    /**
073     * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
074     */
075    @Override
076    public List<Component> getComponentsForLifecycle() {
077        List<Component> components = super.getComponentsForLifecycle();
078
079        components.add(requiredMessageField);
080
081        return components;
082    }
083
084    /**
085     * Indicates the id for the component the label applies to
086     * <p>
087     * Used for setting the labelFor attribute of the corresponding HTML
088     * element. Note this gets set automatically by the framework during the
089     * initialize phase
090     * </p>
091     * 
092     * @return String component id
093     */
094    public String getLabelForComponentId() {
095        return this.labelForComponentId;
096    }
097
098    /**
099     * Setter for the component id the label applies to
100     * 
101     * @param labelForComponentId
102     */
103    public void setLabelForComponentId(String labelForComponentId) {
104        this.labelForComponentId = labelForComponentId;
105    }
106
107    /**
108     * Text that will display as the label
109     * 
110     * @return String label text
111     */
112    public String getLabelText() {
113        return this.labelText;
114    }
115
116    /**
117     * Setter for the label text
118     * 
119     * @param labelText
120     */
121    public void setLabelText(String labelText) {
122        this.labelText = labelText;
123    }
124
125    /**
126     * Indicates whether a colon should be rendered after the label text,
127     * generally used when the label appears to the left of the field's control
128     * or value
129     * 
130     * @return boolean true if a colon should be rendered, false if it should
131     *         not be
132     */
133    public boolean isRenderColon() {
134        return this.renderColon;
135    }
136
137    /**
138     * Setter for the render colon indicator
139     * 
140     * @param renderColon
141     */
142    public void setRenderColon(boolean renderColon) {
143        this.renderColon = renderColon;
144    }
145
146    /**
147     * <code>MessageField</code> instance that will display a required indicator
148     * 
149     * <p>
150     * To indicate a field must have a value (required input) the required
151     * message field can be set to display an indicator or message along with
152     * the label. The message field also dictates the styling of the required
153     * message
154     * </p>
155     * 
156     * @return MessageField instance
157     */
158    public MessageField getRequiredMessageField() {
159        return this.requiredMessageField;
160    }
161
162    /**
163     * Setter for the required message field
164     * 
165     * @param requiredMessageField
166     */
167    public void setRequiredMessageField(MessageField requiredMessageField) {
168        this.requiredMessageField = requiredMessageField;
169    }
170
171    /**
172     * Indicates where the required message field should be placed in relation
173     * to the label field, valid options are 'LEFT' and 'RIGHT'
174     * 
175     * @return Position the requiredMessage placement
176     */
177    public Position getRequiredMessagePlacement() {
178        return this.requiredMessagePlacement;
179    }
180
181    /**
182     * Setter for the required message field placement
183     * 
184     * @param requiredMessagePlacement
185     */
186    public void setRequiredMessagePlacement(Position requiredMessagePlacement) {
187        this.requiredMessagePlacement = requiredMessagePlacement;
188    }
189
190}