001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.ui.contextmenu;
029
030import org.opencms.main.OpenCms;
031import org.opencms.ui.A_CmsUI;
032import org.opencms.ui.CmsVaadinUtils;
033import org.opencms.ui.I_CmsDialogContext;
034import org.opencms.ui.contextmenu.CmsContextMenu.ContextMenuItem;
035import org.opencms.ui.contextmenu.CmsContextMenu.ContextMenuItemClickEvent;
036import org.opencms.ui.contextmenu.CmsContextMenu.ContextMenuItemClickListener;
037import org.opencms.util.CmsStringUtil;
038import org.opencms.util.CmsTreeNode;
039
040import java.util.ArrayList;
041import java.util.List;
042
043import com.vaadin.ui.themes.ValoTheme;
044
045/**
046 * Context menu builder for resource items.<p>
047 */
048public class CmsResourceContextMenuBuilder implements I_CmsContextMenuBuilder {
049
050    /** The menu item providers. */
051    private List<I_CmsContextMenuItemProvider> m_itemProviders;
052
053    /** Tree builder used to build the tree of menu items. */
054    private CmsContextMenuTreeBuilder m_treeBuilder;
055
056    /**
057     * Constructor.<p>
058     */
059    public CmsResourceContextMenuBuilder() {
060
061        m_itemProviders = new ArrayList<I_CmsContextMenuItemProvider>();
062    }
063
064    /**
065     * Adds a menu item provider.<p>
066     *
067     * @param provider the menu item provider to add
068     */
069    public void addMenuItemProvider(I_CmsContextMenuItemProvider provider) {
070
071        m_itemProviders.add(provider);
072    }
073
074    /**
075     * @see org.opencms.ui.contextmenu.I_CmsContextMenuBuilder#buildContextMenu(org.opencms.ui.I_CmsDialogContext, org.opencms.ui.contextmenu.CmsContextMenu)
076     */
077    public void buildContextMenu(I_CmsDialogContext context, CmsContextMenu menu) {
078
079        CmsContextMenuTreeBuilder treeBuilder = new CmsContextMenuTreeBuilder(context);
080        m_treeBuilder = treeBuilder;
081        CmsTreeNode<I_CmsContextMenuItem> tree = treeBuilder.buildAll(getMenuItems());
082        I_CmsContextMenuItem defaultActionItem = treeBuilder.getDefaultActionItem();
083        for (CmsTreeNode<I_CmsContextMenuItem> node : tree.getChildren()) {
084            createItem(menu, node, context, defaultActionItem);
085        }
086    }
087
088    /**
089     * @see org.opencms.ui.contextmenu.I_CmsContextMenuItemProvider#getMenuItems()
090     */
091    public List<I_CmsContextMenuItem> getMenuItems() {
092
093        if (m_itemProviders.isEmpty()) {
094            return OpenCms.getWorkplaceAppManager().getMenuItemProvider().getMenuItems();
095        } else {
096            List<I_CmsContextMenuItem> items = new ArrayList<I_CmsContextMenuItem>();
097            for (I_CmsContextMenuItemProvider provider : m_itemProviders) {
098                items.addAll(provider.getMenuItems());
099            }
100            return items;
101        }
102    }
103
104    /**
105     * Gets the localized title for the context menu item by resolving any message key macros in the raw title using the current locale.<p>
106     *
107     * @param item the unlocalized title
108     * @return the localized title
109     */
110    String getTitle(I_CmsContextMenuItem item) {
111
112        return CmsVaadinUtils.localizeString(item.getTitle(A_CmsUI.get().getLocale()));
113    }
114
115    /**
116     * Creates a context menu item.<p>
117     *
118     * @param parent the parent (either the context menu itself, or a parent item)
119     * @param node the node which should be added as a context menu item
120     * @param context the dialog context
121     * @param defaultAction the default action item if available
122     *
123     * @return the created item
124     */
125    private ContextMenuItem createItem(
126        Object parent,
127        CmsTreeNode<I_CmsContextMenuItem> node,
128        final I_CmsDialogContext context,
129        I_CmsContextMenuItem defaultAction) {
130
131        final I_CmsContextMenuItem data = node.getData();
132        ContextMenuItem guiMenuItem = null;
133        if (parent instanceof CmsContextMenu) {
134            guiMenuItem = ((CmsContextMenu)parent).addItem(getTitle(data));
135        } else {
136            guiMenuItem = ((ContextMenuItem)parent).addItem(getTitle(data));
137        }
138        if (m_treeBuilder.getVisibility(data).isInActive()) {
139            guiMenuItem.setEnabled(false);
140            String key = m_treeBuilder.getVisibility(data).getMessageKey();
141            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(key)) {
142                guiMenuItem.setDescription(CmsVaadinUtils.getMessageText(key));
143            }
144        }
145        if (node.getChildren().size() > 0) {
146            for (CmsTreeNode<I_CmsContextMenuItem> childNode : node.getChildren()) {
147                createItem(guiMenuItem, childNode, context, defaultAction);
148            }
149        } else {
150            guiMenuItem.addItemClickListener(new ContextMenuItemClickListener() {
151
152                public void contextMenuItemClicked(ContextMenuItemClickEvent event) {
153
154                    data.executeAction(context);
155                }
156            });
157
158        }
159        // highlight the default action
160        if (data.equals(defaultAction)) {
161            guiMenuItem.addStyleName(ValoTheme.LABEL_BOLD);
162        }
163        return guiMenuItem;
164    }
165}