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 java.util.ArrayList;
024    import java.util.List;
025    import java.util.Set;
026    import java.util.TreeSet;
027    
028    import org.gwtbootstrap3.client.ui.form.error.ErrorHandler;
029    import org.gwtbootstrap3.client.ui.form.validator.HasValidators;
030    import org.gwtbootstrap3.client.ui.form.validator.Validator;
031    import org.gwtbootstrap3.client.ui.form.validator.ValidatorWrapper;
032    
033    import com.google.gwt.editor.client.Editor;
034    import com.google.gwt.editor.client.EditorError;
035    import com.google.gwt.event.dom.client.BlurEvent;
036    import com.google.gwt.event.dom.client.BlurHandler;
037    import com.google.gwt.event.shared.HandlerRegistration;
038    import com.google.gwt.user.client.ui.FocusWidget;
039    import com.google.gwt.user.client.ui.HasValue;
040    import com.google.gwt.user.client.ui.Widget;
041    
042    /**
043     * Abstract validator mixin. Contains all of the validation logic.
044     *
045     * @param <W> the generic type
046     * @param <V> the value type
047     */
048    public class DefaultValidatorMixin<W extends Widget & HasValue<V> & Editor<V>, V> implements HasValidators<V> {
049    
050        private HandlerRegistration blurHandler;
051    
052        protected ErrorHandler errorHandler = null;
053    
054        private W inputWidget;
055    
056        private boolean validateOnBlur = false;
057    
058        protected Set<ValidatorWrapper<V>> validators = new TreeSet<ValidatorWrapper<V>>();
059    
060        /**
061         * Instantiates a new abstract validator mixin.
062         *
063         * @param inputWidget the input widget
064         * @param errorHandler the error handler
065         */
066        public DefaultValidatorMixin(W inputWidget, ErrorHandler errorHandler) {
067            super();
068            this.inputWidget = inputWidget;
069            this.errorHandler = errorHandler;
070        }
071    
072        /** {@inheritDoc} */
073        @Override
074        public void addValidator(Validator<V> validator) {
075            validators.add(new ValidatorWrapper<V>(validator, validators.size()));
076        }
077    
078        /** {@inheritDoc} */
079        @Override
080        public boolean getValidateOnBlur() {
081            return validateOnBlur;
082        }
083    
084        /**
085         * Sets the error handler.
086         *
087         * @param errorHandler the new error handler
088         */
089        public void setErrorHandler(ErrorHandler errorHandler) {
090            this.errorHandler = errorHandler;
091        }
092    
093        /** {@inheritDoc} */
094        @Override
095        public void setValidateOnBlur(boolean vob) {
096            validateOnBlur = vob;
097            if (validateOnBlur && inputWidget instanceof FocusWidget) {
098                blurHandler = inputWidget.addDomHandler(new BlurHandler() {
099                    @Override
100                    public void onBlur(BlurEvent event) {
101                        validate();
102                    }
103                }, BlurEvent.getType());
104            } else if (blurHandler != null) {
105                blurHandler.removeHandler();
106                blurHandler = null;
107            }
108        }
109    
110        /** {@inheritDoc} */
111        @Override
112        public void setValidators(Validator<V>... newValidators) {
113            validators.clear();
114            for (Validator<V> validator : newValidators) {
115                addValidator(validator);
116            }
117        }
118    
119        /** {@inheritDoc} */
120        @Override
121        public boolean validate() {
122            return validate(true);
123        }
124    
125        /** {@inheritDoc} */
126        @Override
127        public boolean validate(boolean show) {
128            if (errorHandler == null) { return true; }
129            List<EditorError> errors = new ArrayList<EditorError>();
130            for (ValidatorWrapper<V> wrapper : validators) {
131                Validator<V> validator = wrapper.getValidator();
132                List<EditorError> result = validator.validate(inputWidget, inputWidget.getValue());
133                if (result != null && !result.isEmpty()) {
134                    errors.addAll(result);
135                }
136            }
137            if (errors.size() > 0) {
138                errorHandler.showErrors(errors);
139                return false;
140            }
141            errorHandler.clearErrors();
142            return true;
143        }
144    
145        /** {@inheritDoc} */
146        @Override
147        public void reset() {
148            if (errorHandler != null) {
149                errorHandler.clearErrors();
150            }
151        }
152    
153    }