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