001package gwt.material.design.client.base.mixin; 002 003/* 004 * #%L 005 * GwtMaterial 006 * %% 007 * Copyright (C) 2015 GwtMaterialDesign 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 023import com.google.gwt.user.client.ui.HasText; 024import com.google.gwt.user.client.ui.UIObject; 025import gwt.material.design.client.base.HasError; 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 String helperText; 036 037 public ErrorMixin(final T widget, final H textObject, UIObject target) { 038 super(widget); 039 040 this.textObject = textObject; 041 this.target = target; 042 } 043 044 @Override 045 public void setError(String error) { 046 textObject.setText(error); 047 textObject.addStyleName("field-error-label"); 048 textObject.removeStyleName("field-helper-label"); 049 textObject.removeStyleName("field-success-label"); 050 if(target != null) { 051 target.addStyleName("field-error"); 052 target.removeStyleName("field-success"); 053 } 054 textObject.setVisible(true); 055 } 056 057 @Override 058 public void setSuccess(String success) { 059 textObject.setText(success); 060 textObject.addStyleName("field-success-label"); 061 textObject.removeStyleName("field-helper-label"); 062 textObject.removeStyleName("field-error-label"); 063 if(target != null) { 064 target.addStyleName("field-success"); 065 target.removeStyleName("field-error"); 066 } 067 textObject.setVisible(true); 068 } 069 070 @Override 071 public void setHelperText(String helperText) { 072 this.helperText = helperText; 073 clearErrorOrSuccess(); 074 } 075 076 @Override 077 public void clearErrorOrSuccess() { 078 textObject.setText(helperText == null ? "" : helperText); 079 if (helperText != null){ 080 textObject.addStyleName("field-helper-label"); 081 } 082 else { 083 textObject.removeStyleName("field-helper-label"); 084 } 085 textObject.removeStyleName("field-error-label"); 086 textObject.removeStyleName("field-success-label"); 087 if(target != null) { 088 target.removeStyleName("field-error"); 089 target.removeStyleName("field-success"); 090 } 091 textObject.setVisible(helperText != null); 092 } 093}