001package org.gwtbootstrap3.client.ui.form.validator;
002
003/*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 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
023import java.util.ArrayList;
024import java.util.List;
025
026import org.gwtbootstrap3.client.ui.form.error.BasicEditorError;
027
028import com.google.gwt.core.shared.GWT;
029import com.google.gwt.editor.client.Editor;
030import com.google.gwt.editor.client.EditorError;
031
032/**
033 * Common validator code.
034 *
035 * @param <T> the generic type
036 * @author Steven Jardine
037 */
038public abstract class AbstractValidator<T> implements Validator<T> {
039
040    private String invalidMessageOverride = null;
041
042    private String messageKey;
043
044    private ValidatorMessageMixin messageMixin = GWT.create(ValidatorMessageMixin.class);
045
046    private Object[] messageValueArgs;
047
048    /**
049     * Constructor. This overrides all validation message handling. Use this constructor for field specific
050     * custom validation messages.
051     *
052     * @param invalidMessageOverride the invalid message override
053     */
054    public AbstractValidator(String invalidMessageOverride) {
055        this(null, new Object[0]);
056        assert invalidMessageOverride != null;
057        this.invalidMessageOverride = invalidMessageOverride;
058    }
059
060    /**
061     * Constructor. Looks up the message using the messageKey and replacing arguments with messageValueArgs.
062     *
063     * @param messageKey the message key
064     * @param messageValueArgs the message value args
065     */
066    public AbstractValidator(String messageKey, Object[] messageValueArgs) {
067        this.messageKey = messageKey;
068        this.messageValueArgs = messageValueArgs;
069        assert this.messageValueArgs != null;
070    }
071
072    /**
073     * Creates the error list.
074     *
075     * @param editor the editor
076     * @param value the value
077     * @param messageKey the message key
078     * @return the list
079     */
080    public List<EditorError> createErrorList(Editor<T> editor, T value, String messageKey) {
081        List<EditorError> result = new ArrayList<EditorError>();
082        result.add(new BasicEditorError(editor, value, getInvalidMessage(messageKey)));
083        return result;
084    }
085
086    /**
087     * Gets the invalid message.
088     *
089     * @param key the key
090     * @return the invalid message
091     */
092    public String getInvalidMessage(String key) {
093        return invalidMessageOverride == null ? messageMixin.lookup(key, messageValueArgs) : MessageFormat.format(
094            invalidMessageOverride, messageValueArgs);
095    }
096
097    /**
098     * Checks if is valid.
099     *
100     * @param value the value
101     * @return true, if is valid
102     */
103    public abstract boolean isValid(T value);
104
105    /** {@inheritDoc} */
106    @Override
107    public final List<EditorError> validate(Editor<T> editor, T value) {
108        List<EditorError> result = new ArrayList<EditorError>();
109        if (!isValid(value)) {
110            result.add(new BasicEditorError(editor, value, getInvalidMessage(messageKey)));
111        }
112        return result;
113    }
114
115}