001/**
002 * Copyright 2005-2018 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.control;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.datadictionary.parse.BeanTag;
020import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
021import org.kuali.rice.krad.uif.component.Component;
022import org.kuali.rice.krad.uif.field.InputField;
023import org.kuali.rice.krad.uif.util.LifecycleElement;
024
025/**
026 * Represents a HTML password text control, rendered as a input field of type
027 * 'password'. This can receive and display a mask for a single value.
028 *
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031@BeanTag(name = "passwordControl", parent = "Uif-PasswordControl")
032public class PasswordControl extends ControlBase implements SizedControl {
033    private static final long serialVersionUID = -8267606288443759880L;
034
035    private int size;
036    private Integer maxLength;
037    private Integer minLength;
038
039    private String watermarkText = StringUtils.EMPTY;
040
041    public PasswordControl() {
042        super();
043    }
044
045    /**
046     * The following actions are performed:
047     *
048     * <ul>
049     * <li>Defaults maxLength, minLength (if not set) to maxLength of parent field</li>
050     * </ul>
051     *
052     * {@inheritDoc}
053     */
054    @Override
055    public void performFinalize(Object model, LifecycleElement parent) {
056        super.performFinalize(model, parent);
057
058        if (parent instanceof InputField) {
059            InputField field = (InputField) parent;
060            if (getMaxLength() == null) {
061                setMaxLength(field.getMaxLength());
062            }
063
064            if (getMinLength() == null) {
065                setMinLength(field.getMinLength());
066            }
067        }
068    }
069
070    /**
071     * @see SizedControl#getSize()
072     */
073    @BeanTagAttribute
074    public int getSize() {
075        return this.size;
076    }
077
078    /**
079     * @see SizedControl#setSize(int)
080     */
081    public void setSize(int size) {
082        this.size = size;
083    }
084
085    /**
086     * Maximum number of characters that can be inputted
087     *
088     * <p>If not set on control, max length of field will be used</p>
089     *
090     * @return max number of characters
091     */
092    @BeanTagAttribute
093    public Integer getMaxLength() {
094        return maxLength;
095    }
096
097    /**
098     * Setter for the max number of input characters
099     *
100     * @param maxLength
101     */
102    public void setMaxLength(Integer maxLength) {
103        this.maxLength = maxLength;
104    }
105
106    /**
107     * Minimum number of characters that can be inputted
108     *
109     * <p>If not set on control, min length of field will be used</p>
110     *
111     * @return max number of characters
112     */
113    @BeanTagAttribute
114    public Integer getMinLength() {
115        return minLength;
116    }
117
118    /**
119     * Setter for the min number of input characters
120     *
121     * @param minLength
122     */
123    public void setMinLength(Integer minLength) {
124        this.minLength = minLength;
125    }
126
127    /**
128     * Gets the watermark text for this PasswordControl.
129     *
130     * <p>
131     * A watermark typically appears as light gray text within the Password input element whenever the
132     * element is empty and does not have focus. This provides a hint to the user as to what the input
133     * is used for, or the type of input that is required.
134     * </p>
135     *
136     * @return the watermarkText
137     */
138    @BeanTagAttribute
139    public String getWatermarkText() {
140        return this.watermarkText;
141    }
142
143    /**
144     * Sets the watermark text for this PasswordControl
145     *
146     * @param watermarkText the watermarkText to set
147     */
148    public void setWatermarkText(String watermarkText) {
149        //to avoid users from putting in the same value as the watermark adding some spaces here
150        //see watermark troubleshooting for more info
151        if (StringUtils.isNotEmpty(watermarkText)) {
152            watermarkText = watermarkText + "   ";
153        }
154
155        this.watermarkText = watermarkText;
156    }
157}