001    /**
002     * Copyright 2005-2013 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     */
016    package org.kuali.rice.krad.uif.control;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.krad.datadictionary.parse.BeanTag;
020    import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
021    import org.kuali.rice.krad.datadictionary.parse.BeanTags;
022    import org.kuali.rice.krad.uif.component.Component;
023    import org.kuali.rice.krad.uif.field.InputField;
024    import org.kuali.rice.krad.uif.view.View;
025    import org.kuali.rice.krad.uif.widget.DatePicker;
026    
027    import java.util.List;
028    
029    /**
030     * Represents a HTML Text control, generally rendered as a input field of type
031     * 'text'. This can display and receive a single value
032     *
033     * @author Kuali Rice Team (rice.collab@kuali.org)
034     */
035    @BeanTags({@BeanTag(name = "textControl-bean", parent = "Uif-TextControl"),
036            @BeanTag(name = "smallTextControl-bean", parent = "Uif-SmallTextControl"),
037            @BeanTag(name = "mediumTextControl-bean", parent = "Uif-MediumTextControl"),
038            @BeanTag(name = "largeTextControl-bean", parent = "Uif-LargeTextControl"),
039            @BeanTag(name = "currencyTextControl-bean", parent = "Uif-CurrencyTextControl"),
040            @BeanTag(name = "dateControl-bean", parent = "Uif-DateControl")})
041    public class TextControl extends ControlBase implements SizedControl {
042        private static final long serialVersionUID = -8267606288443759880L;
043    
044        private int size;
045        private Integer maxLength;
046        private Integer minLength;
047    
048        private DatePicker datePicker;
049        private String watermarkText = StringUtils.EMPTY;
050        private boolean textExpand;
051    
052        public TextControl() {
053            super();
054        }
055    
056        /**
057         * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
058         */
059        @Override
060        public List<Component> getComponentsForLifecycle() {
061            List<Component> components = super.getComponentsForLifecycle();
062    
063            components.add(datePicker);
064    
065            return components;
066        }
067    
068        /**
069         * The following actions are performed:
070         *
071         * <ul>
072         * <li>Defaults maxLength, minLength (if not set) to maxLength of parent field</li>
073         * </ul>
074         *
075         * @see org.kuali.rice.krad.uif.component.ComponentBase#performFinalize(org.kuali.rice.krad.uif.view.View,
076         *      java.lang.Object, org.kuali.rice.krad.uif.component.Component)
077         */
078        @Override
079        public void performFinalize(View view, Object model, Component parent) {
080            super.performFinalize(view, model, parent);
081    
082            if (parent instanceof InputField) {
083                InputField field = (InputField) parent;
084                if (getMaxLength() == null) {
085                    setMaxLength(field.getMaxLength());
086                }
087    
088                if (getMinLength() == null) {
089                    setMinLength(field.getMinLength());
090                }
091            }
092        }
093    
094        /**
095         * @see org.kuali.rice.krad.uif.control.SizedControl#getSize()
096         */
097        @BeanTagAttribute(name = "size")
098        public int getSize() {
099            return this.size;
100        }
101    
102        /**
103         * @see org.kuali.rice.krad.uif.control.SizedControl#setSize(int)
104         */
105        public void setSize(int size) {
106            this.size = size;
107        }
108    
109        /**
110         * Maximum number of characters that can be inputted
111         *
112         * <p>If not set on control, max length of field will be used</p>
113         *
114         * @return int max number of characters
115         */
116        @BeanTagAttribute(name = "maxLength")
117        public Integer getMaxLength() {
118            return maxLength;
119        }
120    
121        /**
122         * Setter for the max number of input characters
123         *
124         * @param maxLength
125         */
126        public void setMaxLength(Integer maxLength) {
127            this.maxLength = maxLength;
128        }
129    
130        /**
131         * Minimum number of characters that can be inputted
132         *
133         * <p>If not set on control, min length of field will be used</p>
134         *
135         * @return int max number of characters
136         */
137        @BeanTagAttribute(name = "minLength")
138        public Integer getMinLength() {
139            return minLength;
140        }
141    
142        /**
143         * Setter for the min number of input characters
144         *
145         * @param minLength
146         */
147        public void setMinLength(Integer minLength) {
148            this.minLength = minLength;
149        }
150    
151        /**
152         * Renders a calendar that can be used to select a date value for the text
153         * control. The <code>Calendar</code> instance contains configuration such
154         * as the date format string
155         *
156         * @return Calendar
157         */
158        @BeanTagAttribute(name = "datePicker", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
159        public DatePicker getDatePicker() {
160            return this.datePicker;
161        }
162    
163        /**
164         * Setter for the date picker
165         *
166         * @param datePicker
167         */
168        public void setDatePicker(DatePicker datePicker) {
169            this.datePicker = datePicker;
170        }
171    
172        /**
173         * Gets the watermark text for this TextControl.
174         *
175         * <p>
176         * A watermark typically appears as light gray text within the text input element whenever the
177         * element is empty and does not have focus. This provides a hint to the user as to what the input
178         * is used for, or the type of input that is required.
179         * </p>
180         *
181         * @return the watermarkText
182         */
183        @BeanTagAttribute(name = "watermarkText")
184        public String getWatermarkText() {
185            return this.watermarkText;
186        }
187    
188        /**
189         * Sets the watermark text for this TextControl
190         *
191         * @param watermarkText the watermarkText to set
192         */
193        public void setWatermarkText(String watermarkText) {
194            //to avoid users from putting in the same value as the watermark adding some spaces here
195            //see watermark troubleshooting for more info
196            if (StringUtils.isNotEmpty(watermarkText)) {
197                watermarkText = watermarkText + "   ";
198            }
199            this.watermarkText = watermarkText;
200        }
201    
202        /**
203         * If set to true, this control will have a button which can be clicked to expand the text area through
204         * a popup window so the user has more space to type and see the data they are entering in this text field.
205         *
206         * @return the textExpand
207         */
208        @BeanTagAttribute(name = "textExpand")
209        public boolean isTextExpand() {
210            return this.textExpand;
211        }
212    
213        /**
214         * Sets whether this control will have a button to expand the text area through a popup window.
215         *
216         * @param textExpand the textExpand to set
217         */
218        public void setTextExpand(boolean textExpand) {
219            this.textExpand = textExpand;
220        }
221    
222    }