001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.gwt.client.ui.input; 029 030import org.opencms.gwt.client.ui.css.I_CmsInputLayoutBundle; 031import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle; 032import org.opencms.gwt.client.util.CmsFadeAnimation; 033import org.opencms.util.CmsStringUtil; 034 035import com.google.gwt.user.client.Command; 036import com.google.gwt.user.client.ui.Composite; 037import com.google.gwt.user.client.ui.Label; 038 039/** 040 * Helper class for displaying errors.<p> 041 * 042 * @since 8.0.0 043 */ 044public class CmsErrorWidget extends Composite { 045 046 /** The internal label that displays the error message. */ 047 private Label m_label; 048 049 /** 050 * Creates a new instance.<p> 051 */ 052 public CmsErrorWidget() { 053 054 m_label = createErrorLabel(); 055 initWidget(m_label); 056 setText(null); 057 } 058 059 /** 060 * Creates the a new error label.<p> 061 * 062 * @return a label with the appropriate style for an error label 063 */ 064 private static Label createErrorLabel() { 065 066 Label label = new Label(); 067 label.addStyleName(I_CmsLayoutBundle.INSTANCE.generalCss().cornerAll()); 068 label.addStyleName(I_CmsInputLayoutBundle.INSTANCE.inputCss().error()); 069 return label; 070 } 071 072 /** 073 * Returns <code>true</code> if a error message is set.<p> 074 * 075 * @return <code>true</code> if a error message is set 076 */ 077 public boolean hasError() { 078 079 return CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_label.getText()); 080 } 081 082 /** 083 * Sets the text for the error message.<p> 084 * 085 * If the text parameter is null, the error message will be hidden. 086 * @param text 087 */ 088 public void setText(final String text) { 089 090 setErrorVisible(false); 091 m_label.setText(text); 092 } 093 094 /** 095 * Hides the error.<p> 096 */ 097 protected void hideError() { 098 099 if (hasError()) { 100 CmsFadeAnimation.fadeOut(m_label.getElement(), new Command() { 101 102 /** 103 * @see com.google.gwt.user.client.Command#execute() 104 */ 105 public void execute() { 106 107 setErrorVisible(false); 108 } 109 }, 300); 110 } 111 } 112 113 /** 114 * Sets the visibility of the error label.<p> 115 * 116 * @param visible <code>true</code> if the error should be visible 117 */ 118 protected void setErrorVisible(boolean visible) { 119 120 m_label.setVisible(visible); 121 } 122 123 /** 124 * Shows the error.<p> 125 */ 126 protected void showError() { 127 128 if (hasError()) { 129 m_label.getElement().getStyle().clearDisplay(); 130 CmsFadeAnimation.fadeIn(m_label.getElement(), new Command() { 131 132 /** 133 * @see com.google.gwt.user.client.Command#execute() 134 */ 135 public void execute() { 136 137 // noop 138 } 139 }, 300); 140 } 141 } 142}