001/* 002 * #%L 003 * GwtMaterial 004 * %% 005 * Copyright (C) 2015 - 2017 GwtMaterialDesign 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.user.client.ui.HasText; 023import com.google.gwt.user.client.ui.UIObject; 024import gwt.material.design.client.base.HasError; 025import gwt.material.design.client.constants.CssName; 026 027/** 028 * @author Ben Dol 029 */ 030public class ErrorMixin<T extends UIObject & HasError, H extends UIObject & HasText> 031 extends AbstractMixin<T> implements HasError { 032 033 private H textObject; 034 private UIObject target; 035 private UIObject lblPlaceholder; 036 private String helperText; 037 038 public ErrorMixin(final T widget) { 039 this(widget, null); 040 } 041 042 public ErrorMixin(final T widget, final H textObject) { 043 this(widget, textObject, widget); 044 } 045 046 public ErrorMixin(final T widget, final H textObject, UIObject target) { 047 super(widget); 048 049 this.textObject = textObject; 050 this.target = target; 051 } 052 053 public ErrorMixin(final T widget, final H textObject, UIObject target, UIObject lblPlaceholder) { 054 this(widget, textObject, target); 055 this.lblPlaceholder = lblPlaceholder; 056 } 057 058 @Override 059 public void setError(String error) { 060 if (textObject != null) { 061 textObject.setText(error); 062 textObject.addStyleName(CssName.FIELD_ERROR_LABEL); 063 textObject.removeStyleName(CssName.FIELD_HELPER_LABEL); 064 textObject.removeStyleName(CssName.FIELD_SUCCESS_LABEL); 065 textObject.setVisible(true); 066 } 067 if (target != null) { 068 target.addStyleName(CssName.FIELD_ERROR); 069 target.removeStyleName(CssName.FIELD_SUCCESS); 070 } 071 if (lblPlaceholder != null) { 072 lblPlaceholder.removeStyleName("green-text"); 073 lblPlaceholder.addStyleName("red-text"); 074 if (error != null && !error.isEmpty()) { 075 lblPlaceholder.addStyleName(CssName.ACTIVE); 076 } 077 } 078 } 079 080 @Override 081 public void setSuccess(String success) { 082 if (textObject != null) { 083 textObject.setText(success); 084 textObject.addStyleName(CssName.FIELD_SUCCESS_LABEL); 085 textObject.removeStyleName(CssName.FIELD_HELPER_LABEL); 086 textObject.removeStyleName(CssName.FIELD_ERROR_LABEL); 087 textObject.setVisible(true); 088 } 089 if (target != null) { 090 target.addStyleName(CssName.FIELD_SUCCESS); 091 target.removeStyleName(CssName.FIELD_ERROR); 092 } 093 if (lblPlaceholder != null) { 094 lblPlaceholder.removeStyleName("red-text"); 095 lblPlaceholder.addStyleName("green-text"); 096 if (success != null && !success.isEmpty()) { 097 lblPlaceholder.addStyleName(CssName.ACTIVE); 098 } 099 } 100 } 101 102 @Override 103 public void setHelperText(String helperText) { 104 this.helperText = helperText; 105 clearErrorOrSuccess(); 106 } 107 108 @Override 109 public void clearErrorOrSuccess() { 110 if (textObject != null) { 111 textObject.setText(helperText == null ? "" : helperText); 112 if (helperText != null) { 113 textObject.addStyleName(CssName.FIELD_HELPER_LABEL); 114 } else { 115 textObject.removeStyleName(CssName.FIELD_HELPER_LABEL); 116 } 117 textObject.removeStyleName(CssName.FIELD_ERROR_LABEL); 118 textObject.removeStyleName(CssName.FIELD_SUCCESS_LABEL); 119 textObject.setVisible(helperText != null); 120 } 121 if (target != null) { 122 target.removeStyleName(CssName.FIELD_ERROR); 123 target.removeStyleName(CssName.FIELD_SUCCESS); 124 } 125 if (lblPlaceholder != null) { 126 lblPlaceholder.removeStyleName("red-text"); 127 lblPlaceholder.removeStyleName("green-text"); 128 } 129 } 130}