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.contextmenu;
029
030import org.opencms.gwt.client.ui.I_CmsButton;
031import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
032import org.opencms.util.CmsStringUtil;
033
034import com.google.gwt.event.dom.client.ClickEvent;
035import com.google.gwt.user.client.ui.HTML;
036
037/**
038 * An entry in a {@link org.opencms.gwt.client.ui.contextmenu.CmsContextMenu}. Menu items can either fire a
039 * {@link com.google.gwt.user.client.Command} when they are clicked, or open a cascading sub-menu.<p>
040 *
041 * This implementation of the abstract context menu item provides a possible image in front of the text
042 * and a arrow for a sub menu entry.<p>
043 *
044 * Furthermore constructs the HTML for such a menu entry.<p>
045 *
046 * @since version 8.0.0
047 */
048public final class CmsContextMenuItem extends A_CmsContextMenuItem {
049
050    /** The command for this menu item. */
051    private I_CmsContextMenuEntry m_entry;
052
053    /** The panel containing the menu item text and optional the arrow and or a image in front of the text. */
054    private HTML m_panel;
055
056    /**
057     * Constructs a context menu item.<p>
058     *
059     * @param entry the information for this item
060     */
061    public CmsContextMenuItem(I_CmsContextMenuEntry entry) {
062
063        // call the super constructor
064        super(entry.getLabel());
065        m_entry = entry;
066
067        // get the HTML for the menu item
068        m_panel = new HTML(getMenuItemHtml(m_entry.hasSubMenu()));
069
070        // initialize the widget with the panel and set the style name for the menu item
071        initWidget(m_panel);
072        setStyleName(I_CmsLayoutBundle.INSTANCE.contextmenuCss().cmsMenuItem());
073
074        // now, if the widget is initialized, it's possible to set the item active or inactive,
075        // because the mouse handlers for the item are added or removed
076        if (!m_entry.isActive()) {
077            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_entry.getReason())) {
078                setActive(m_entry.isActive(), m_entry.getReason());
079            } else {
080                setActive(m_entry.isActive(), "");
081            }
082        }
083    }
084
085    /**
086     * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
087     */
088    @Override
089    public void onClick(ClickEvent event) {
090
091        if (m_entry != null) {
092            m_entry.execute();
093        }
094        // hide menu *after* executing the action, because hiding the menu may trigger mouseover events of the elements lying under it,
095        // and executing the action first gives it the opportunity to add a 'blocking notification' to prevent this
096        getParentMenu().hideAll();
097    }
098
099    /**
100     * Generates the HTML for a menu item.<p>
101     *
102     * @param hasSubMenu signals if this menu has a sub menu
103     *
104     * @return the HTML for the menu item
105     */
106    protected String getMenuItemHtml(boolean hasSubMenu) {
107
108        StringBuffer html = new StringBuffer();
109        if (hasSubMenu) {
110            // if this menu item has a sub menu show the arrow-icon behind the text of the icon
111            html.append("<div class=\"");
112            html.append(I_CmsLayoutBundle.INSTANCE.contextmenuCss().arrow()).append(" ").append(
113                I_CmsButton.ICON_FONT).append(" ").append(I_CmsButton.TRIANGLE_RIGHT);
114            html.append("\"></div>");
115        }
116        String iconClass = m_entry.getIconClass();
117        if (iconClass == null) {
118            iconClass = "";
119        }
120        String iconHtml = "<div class='" + iconClass + "'></div>";
121
122        html.append(
123            "<div class='" + I_CmsLayoutBundle.INSTANCE.contextmenuCss().iconBox() + "'>" + iconHtml + "</div>");
124        // add the text to the item
125        html.append("<div class=\"");
126        html.append(I_CmsLayoutBundle.INSTANCE.contextmenuCss().label());
127        html.append("\">" + getText() + "</div>");
128
129        return html.toString();
130    }
131}