All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gwt.material.design.client.base.error.DefaultErrorHandler Maven / Gradle / Ivy

/*
 * #%L
 * GwtBootstrap3
 * %%
 * Copyright (C) 2015 GwtBootstrap3
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package gwt.material.design.client.base.error;

import com.google.gwt.editor.client.EditorError;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import gwt.material.design.client.base.HasErrorText;
import gwt.material.design.client.base.HasStatusText;
import gwt.material.design.client.ui.MaterialHelpBlock;

import java.util.List;

/**
 * This is the default {@link ErrorHandler} implementation.
 * If there is a {@link MaterialHelpBlock} that is a child then error messages
 * will be displayed in the {@link MaterialHelpBlock}.
 * 

* Example: *

*

{@code
 * 
 *     
 *     
 * 
 * }
* * @author Steven Jardine * @author Ben Dol */ public class DefaultErrorHandler implements ErrorHandler { private boolean initialized = false; private final Widget inputWidget; private MaterialHelpBlock helpBlock = null; /** * Default error handler. * * @param widget the parent of this error handler. */ public DefaultErrorHandler(Widget widget) { super(); assert widget != null; this.inputWidget = widget; this.inputWidget.addAttachHandler(event -> { if (event.isAttached()) { init(); } }); } @Override public void cleanup() { } @Override public void clearErrors() { if (inputWidget instanceof HasStatusText) { ((HasStatusText) inputWidget).clearStatusText(); } if (helpBlock != null) { helpBlock.removeStyleName("field-error-label"); helpBlock.removeStyleName("field-success-label"); helpBlock.clear(); } } /** * Find the sibling {@link MaterialHelpBlock}. * * @param widget the {@link Widget} to search. * @return the found {@link MaterialHelpBlock} of null if not found. */ protected MaterialHelpBlock findHelpBlock(Widget widget) { if (widget != null) { if (widget instanceof MaterialHelpBlock) { return (MaterialHelpBlock) widget; } // Try and find the MaterialHelpBlock in the children of the given widget. if (widget instanceof HasWidgets) { for (Widget w : (HasWidgets) widget) { if (w instanceof MaterialHelpBlock) { return (MaterialHelpBlock) w; } } } // Try and find the MaterialHelpBlock in the parent of widget. return findHelpBlock(widget.getParent()); } return null; } /** * Initialize the instance. */ public void init() { if (initialized) { return; } Widget parent = inputWidget.getParent(); while (parent != null && !parent.getClass().getName().equals("com.google.gwt.user.client.ui.Widget")) { helpBlock = findHelpBlock(inputWidget); if (helpBlock != null) { break; } else { parent = parent.getParent(); } } initialized = true; } @Override public void showErrors(List errors) { init(); String errorMsg = ""; for (int index = 0; index < errors.size(); index++) { errorMsg = errors.get(0).getMessage(); if (index + 1 < errors.size()) { errorMsg += "; "; } } if (inputWidget instanceof HasErrorText) { ((HasErrorText) inputWidget).setErrorText(errorMsg); } if (helpBlock != null) { helpBlock.addStyleName("field-error-label"); helpBlock.removeStyleName("field-success-label"); helpBlock.setText(errorMsg); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy