001/* 002 * #%L 003 * GwtBootstrap3 004 * %% 005 * Copyright (C) 2015 GwtBootstrap3 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package gwt.material.design.client.base.validator; 021 022import com.google.gwt.core.shared.GWT; 023import com.google.gwt.editor.client.Editor; 024import com.google.gwt.editor.client.EditorError; 025import gwt.material.design.client.base.error.BasicEditorError; 026 027import java.util.ArrayList; 028import java.util.List; 029 030/** 031 * Common validator code. 032 * 033 * @param <T> the generic type 034 * @author Steven Jardine 035 */ 036public abstract class AbstractValidator<T> implements Validator<T> { 037 038 private String invalidMessageOverride = null; 039 040 private String messageKey; 041 042 private ValidatorMessageMixin messageMixin = GWT.create(ValidatorMessageMixin.class); 043 044 private Object[] messageValueArgs; 045 046 /** 047 * Constructor. This overrides all validation message handling. Use this constructor for field specific 048 * custom validation messages. 049 * 050 * @param invalidMessageOverride the invalid message override 051 */ 052 public AbstractValidator(String invalidMessageOverride) { 053 this(null, new Object[0]); 054 assert invalidMessageOverride != null; 055 this.invalidMessageOverride = invalidMessageOverride; 056 } 057 058 /** 059 * Constructor. Looks up the message using the messageKey and replacing arguments with messageValueArgs. 060 * 061 * @param messageKey the message key 062 * @param messageValueArgs the message value args 063 */ 064 public AbstractValidator(String messageKey, Object[] messageValueArgs) { 065 this.messageKey = messageKey; 066 this.messageValueArgs = messageValueArgs; 067 assert this.messageValueArgs != null; 068 } 069 070 /** 071 * Creates the error list. 072 * 073 * @param editor the editor 074 * @param value the value 075 * @param messageKey the message key 076 * @return the list 077 */ 078 public List<EditorError> createErrorList(Editor<T> editor, T value, String messageKey) { 079 List<EditorError> result = new ArrayList<>(); 080 result.add(new BasicEditorError(editor, value, getInvalidMessage(messageKey))); 081 return result; 082 } 083 084 /** 085 * Gets the invalid message. 086 * 087 * @param key the key 088 * @return the invalid message 089 */ 090 public String getInvalidMessage(String key) { 091 return invalidMessageOverride == null ? messageMixin.lookup(key, messageValueArgs) : MessageFormat.format( 092 invalidMessageOverride, messageValueArgs); 093 } 094 095 /** 096 * Checks if is valid. 097 * 098 * @param value the value 099 * @return true, if is valid 100 */ 101 public abstract boolean isValid(T value); 102 103 /** 104 * {@inheritDoc} 105 */ 106 @Override 107 public final List<EditorError> validate(Editor<T> editor, T value) { 108 List<EditorError> result = new ArrayList<>(); 109 if (!isValid(value)) { 110 result.add(new BasicEditorError(editor, value, getInvalidMessage(messageKey))); 111 } 112 return result; 113 } 114 115}