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.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        m_itemProviders = new ArrayList<I_CmsContextMenuItemProvider>();
061    }
062
063    /**
064     * Adds a menu item provider.<p>
065     *
066     * @param provider the menu item provider to add
067     */
068    public void addMenuItemProvider(I_CmsContextMenuItemProvider provider) {
069
070        m_itemProviders.add(provider);
071    }
072
073    /**
074     * @see org.opencms.ui.contextmenu.I_CmsContextMenuBuilder#buildContextMenu(org.opencms.ui.I_CmsDialogContext, org.opencms.ui.contextmenu.CmsContextMenu)
075     */
076    public void buildContextMenu(I_CmsDialogContext context, CmsContextMenu menu) {
077
078        CmsContextMenuTreeBuilder treeBuilder = new CmsContextMenuTreeBuilder(context);
079        m_treeBuilder = treeBuilder;
080        CmsTreeNode<I_CmsContextMenuItem> tree = treeBuilder.buildAll(getMenuItems());
081        I_CmsContextMenuItem defaultActionItem = treeBuilder.getDefaultActionItem();
082        for (CmsTreeNode<I_CmsContextMenuItem> node : tree.getChildren()) {
083            createItem(menu, node, context, defaultActionItem);
084        }
085    }
086
087    /**
088     * @see org.opencms.ui.contextmenu.I_CmsContextMenuItemProvider#getMenuItems()
089     */
090    public List<I_CmsContextMenuItem> getMenuItems() {
091
092        if (m_itemProviders.isEmpty()) {
093            return OpenCms.getWorkplaceAppManager().getMenuItemProvider().getMenuItems();
094        } else {
095            List<I_CmsContextMenuItem> items = new ArrayList<I_CmsContextMenuItem>();
096            for (I_CmsContextMenuItemProvider provider : m_itemProviders) {
097                items.addAll(provider.getMenuItems());
098            }
099            return items;
100        }
101    }
102
103    /**
104     * Gets the localized title for the context menu item by resolving any message key macros in the raw title using the current locale.<p>
105     *
106     * @param item the unlocalized title
107     * @return the localized title
108     */
109    String getTitle(I_CmsContextMenuItem item) {
110
111        return CmsVaadinUtils.localizeString(item.getTitle(A_CmsUI.get().getLocale()));
112    }
113
114    /**
115     * Creates a context menu item.<p>
116     *
117     * @param parent the parent (either the context menu itself, or a parent item)
118     * @param node the node which should be added as a context menu item
119     * @param context the dialog context
120     * @param defaultAction the default action item if available
121     *
122     * @return the created item
123     */
124    private ContextMenuItem createItem(
125        Object parent,
126        CmsTreeNode<I_CmsContextMenuItem> node,
127        final I_CmsDialogContext context,
128        I_CmsContextMenuItem defaultAction) {
129
130        final I_CmsContextMenuItem data = node.getData();
131        ContextMenuItem guiMenuItem = null;
132        if (parent instanceof CmsContextMenu) {
133            guiMenuItem = ((CmsContextMenu)parent).addItem(getTitle(data));
134        } else {
135            guiMenuItem = ((ContextMenuItem)parent).addItem(getTitle(data));
136        }
137        if (m_treeBuilder.getVisibility(data).isInActive()) {
138            guiMenuItem.setEnabled(false);
139            String key = m_treeBuilder.getVisibility(data).getMessageKey();
140            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(key)) {
141                guiMenuItem.setDescription(CmsVaadinUtils.getMessageText(key));
142            }
143        }
144        if (node.getChildren().size() > 0) {
145            for (CmsTreeNode<I_CmsContextMenuItem> childNode : node.getChildren()) {
146                createItem(guiMenuItem, childNode, context, defaultAction);
147            }
148        } else {
149            guiMenuItem.addItemClickListener(new ContextMenuItemClickListener() {
150
151                public void contextMenuItemClicked(ContextMenuItemClickEvent event) {
152
153                    data.executeAction(context);
154                }
155            });
156
157        }
158        // highlight the default action
159        if (data.equals(defaultAction)) {
160            guiMenuItem.addStyleName(ValoTheme.LABEL_BOLD);
161        }
162        return guiMenuItem;
163    }
164}