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