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;
029
030import org.opencms.gwt.client.ui.CmsNotification.Type;
031import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
032import org.opencms.gwt.client.ui.I_CmsButton.Size;
033import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
034import org.opencms.util.CmsStringUtil;
035
036import com.google.gwt.core.client.GWT;
037import com.google.gwt.dom.client.Element;
038import com.google.gwt.event.dom.client.ClickEvent;
039import com.google.gwt.uibinder.client.UiBinder;
040import com.google.gwt.uibinder.client.UiField;
041import com.google.gwt.uibinder.client.UiHandler;
042import com.google.gwt.user.client.ui.Composite;
043import com.google.gwt.user.client.ui.Widget;
044
045/**
046 * Widget for a single notification message.<p>
047 */
048public class CmsNotificationMessage extends Composite {
049
050    /** The UI binder interface. */
051    interface I_CmsNotificationMessageUiBinder extends UiBinder<Widget, CmsNotificationMessage> {
052        // nothing to do
053    }
054
055    /** The UI binder instance. */
056    private static I_CmsNotificationMessageUiBinder uiBinder = GWT.create(I_CmsNotificationMessageUiBinder.class);
057
058    /** The close button. */
059    @UiField(provided = true)
060    CmsPushButton m_closeButton;
061
062    /** The message content DIV. */
063    @UiField
064    Element m_messageContent;
065
066    /** The notification mode. */
067    private CmsNotification.Mode m_mode;
068
069    /**
070     * Constructor.<p>
071     *
072     * @param mode the notification mode
073     * @param type the message type
074     * @param message the message content
075     */
076    public CmsNotificationMessage(CmsNotification.Mode mode, CmsNotification.Type type, String message) {
077        m_mode = mode;
078        m_closeButton = new CmsPushButton(I_CmsButton.CLOSE);
079        m_closeButton.setButtonStyle(ButtonStyle.FONT_ICON, null);
080        m_closeButton.setSize(Size.small);
081        initWidget(uiBinder.createAndBindUi(this));
082        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(message)) {
083            m_messageContent.setInnerHTML(message);
084            addStyleName(classForType(type));
085            if (!CmsNotification.Mode.BROADCAST.equals(m_mode)) {
086                m_closeButton.setVisible(false);
087            }
088        } else {
089            setVisible(false);
090        }
091    }
092
093    /**
094     * Returns if the given message is of a blocking mode.<p>
095     *
096     * @return <code>true</code> if the given message is of a blocking mode
097     */
098    public boolean isBlockingMode() {
099
100        return CmsNotification.Mode.BUSY.equals(m_mode);
101    }
102
103    /**
104     * Returns if the given message is of a busy mode.<p>
105     *
106     * @return <code>true</code> if the given message is of a busy mode
107     */
108    public boolean isBusyMode() {
109
110        return CmsNotification.Mode.BUSY.equals(m_mode);
111    }
112
113    /**
114     * Handles the close button click event.<p>
115     *
116     * @param event the click event
117     */
118    @UiHandler("m_closeButton")
119    public void onCloseClick(ClickEvent event) {
120
121        CmsNotification.get().removeMessage(this);
122    }
123
124    /**
125     * Returns the class name for the given type.<p>
126     *
127     * @param type the type
128     *
129     * @return the class name
130     */
131    private String classForType(Type type) {
132
133        switch (type) {
134            case ERROR:
135                return I_CmsLayoutBundle.INSTANCE.notificationCss().notificationError();
136            case NORMAL:
137                return I_CmsLayoutBundle.INSTANCE.notificationCss().notificationNormal();
138            case WARNING:
139                return I_CmsLayoutBundle.INSTANCE.notificationCss().notificationWarning();
140            default:
141                return I_CmsLayoutBundle.INSTANCE.notificationCss().notificationNormal();
142        }
143    }
144
145}