001/* 002 * #%L 003 * GwtBootstrap3 004 * %% 005 * Copyright (C) 2015 - 2017 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.mixin; 021 022import com.google.gwt.editor.client.EditorError; 023import com.google.gwt.editor.client.HasEditorErrors; 024import com.google.gwt.user.client.ui.Widget; 025import gwt.material.design.client.base.error.DefaultErrorHandler; 026import gwt.material.design.client.base.error.ErrorHandler; 027import gwt.material.design.client.base.error.ErrorHandlerType; 028import gwt.material.design.client.base.error.HasErrorHandler; 029 030import java.util.List; 031 032/** 033 * Mixin to handle error handler support. 034 * 035 * @param <V> the type of editor value. 036 */ 037public class ErrorHandlerMixin<V> implements HasEditorErrors<V>, HasErrorHandler { 038 039 private ErrorHandler errorHandler; 040 041 private ErrorHandlerType errorHandlerType = ErrorHandlerType.DEFAULT; 042 043 private Widget inputWidget = null; 044 045 /** 046 * Mixin for the {@link ErrorHandler} implementation. 047 * 048 * @param widget the widget 049 */ 050 public ErrorHandlerMixin(Widget widget) { 051 inputWidget = widget; 052 errorHandler = new DefaultErrorHandler(inputWidget); 053 } 054 055 /** 056 * Clear the errors. 057 */ 058 public void clearErrors() { 059 if (errorHandler != null) { 060 errorHandler.clearErrors(); 061 } 062 } 063 064 @Override 065 public ErrorHandler getErrorHandler() { 066 return errorHandler; 067 } 068 069 @Override 070 public ErrorHandlerType getErrorHandlerType() { 071 return errorHandlerType; 072 } 073 074 @Override 075 public void setErrorHandler(ErrorHandler handler) { 076 errorHandlerType = null; 077 errorHandler = handler; 078 } 079 080 @Override 081 public void setErrorHandlerType(ErrorHandlerType type) { 082 if (errorHandler != null) { 083 errorHandler.cleanup(); 084 } 085 errorHandlerType = type == null ? ErrorHandlerType.DEFAULT : type; 086 switch (errorHandlerType) { 087 case NONE: 088 errorHandler = null; 089 break; 090 case DEFAULT: 091 errorHandler = new DefaultErrorHandler(inputWidget); 092 } 093 } 094 095 @Override 096 public void showErrors(List<EditorError> errors) { 097 if (errorHandler != null) { 098 if (errors == null || errors.isEmpty()) { 099 errorHandler.clearErrors(); 100 return; 101 } 102 errorHandler.showErrors(errors); 103 } 104 } 105}