001    package org.gwtbootstrap3.client.ui.base.mixin;
002    
003    /*
004     * #%L
005     * GwtBootstrap3
006     * %%
007     * Copyright (C) 2013 - 2015 GwtBootstrap3
008     * %%
009     * Licensed under the Apache License, Version 2.0 (the "License");
010     * you may not use this file except in compliance with the License.
011     * You may obtain a copy of the License at
012     * 
013     *      http://www.apache.org/licenses/LICENSE-2.0
014     * 
015     * Unless required by applicable law or agreed to in writing, software
016     * distributed under the License is distributed on an "AS IS" BASIS,
017     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018     * See the License for the specific language governing permissions and
019     * limitations under the License.
020     * #L%
021     */
022    
023    import org.gwtbootstrap3.client.ui.form.error.ErrorHandler;
024    import org.gwtbootstrap3.client.ui.form.validator.BlankValidator;
025    import org.gwtbootstrap3.client.ui.form.validator.Validator;
026    
027    import com.google.gwt.editor.client.Editor;
028    import com.google.gwt.user.client.ui.HasValue;
029    import com.google.gwt.user.client.ui.Widget;
030    
031    /**
032     * Mixin that provides the allowBlank functionality for input fields.
033     *
034     * @param <W> the generic type
035     * @param <V> the value type
036     */
037    public class BlankValidatorMixin<W extends Widget & HasValue<V> & Editor<V>, V> extends DefaultValidatorMixin<W, V> {
038    
039        private boolean allowBlank;
040    
041        /**
042         * Constructor.
043         *
044         * @param inputWidget the input widget
045         * @param errorHandler the error handler
046         */
047        public BlankValidatorMixin(W inputWidget, ErrorHandler errorHandler) {
048            super(inputWidget, errorHandler);
049        }
050    
051        /** {@inheritDoc} */
052        @Override
053        public void addValidator(Validator<V> validator) {
054            if (validator instanceof BlankValidator) {
055                allowBlank = false;
056            }
057            super.addValidator(validator);
058        }
059    
060        /**
061         * @return the allow blank
062         */
063        public boolean getAllowBlank() {
064            return allowBlank;
065        }
066    
067        /**
068         * @param allowBlank the new allow blank
069         */
070        public void setAllowBlank(boolean allowBlank) {
071            this.allowBlank = allowBlank;
072            if (!allowBlank) {
073                addValidator(new BlankValidator<V>());
074            }
075        }
076    
077    }