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.contextmenu.CmsContextMenuEntry;
031import org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuCommand;
032import org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuEntry;
033import org.opencms.gwt.client.ui.contextmenu.I_CmsValidatingContextMenuCommand;
034import org.opencms.gwt.shared.CmsContextMenuEntryBean;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.util.CmsUUID;
037
038import java.util.ArrayList;
039import java.util.List;
040
041/**
042 * Abstract class which implements the common part of all toolbar handler functionality.<p>
043 *
044 * @since 8.0.0
045 */
046public abstract class A_CmsToolbarHandler implements I_CmsToolbarHandler {
047
048    /**
049     * Transforms a list of context menu entry beans to a list of context menu entries.<p>
050     *
051     * @param menuBeans the list of context menu entry beans
052     * @param structureId the id of the resource for which to transform the context menu entries
053     *
054     * @return a list of context menu entries
055     */
056    public List<I_CmsContextMenuEntry> transformEntries(
057        List<CmsContextMenuEntryBean> menuBeans,
058        final CmsUUID structureId) {
059
060        List<I_CmsContextMenuEntry> menuEntries = new ArrayList<I_CmsContextMenuEntry>();
061        for (CmsContextMenuEntryBean bean : menuBeans) {
062            I_CmsContextMenuEntry entry = transformSingleEntry(structureId, bean);
063            if (entry != null) {
064                menuEntries.add(entry);
065            }
066        }
067        return menuEntries;
068    }
069
070    /**
071     * Creates a single context menu entry from a context menu entry bean.<p>
072     *
073     * @param structureId the structure id of the resource
074     * @param bean the context menu entry bean
075     *
076     * @return the created context menu entry
077     */
078    protected I_CmsContextMenuEntry transformSingleEntry(final CmsUUID structureId, CmsContextMenuEntryBean bean) {
079
080        String name = bean.getName();
081        I_CmsContextMenuCommand command = null;
082        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(name)) {
083            command = getContextMenuCommands().get(name);
084        }
085        if ((command == null) && !bean.hasSubMenu()) {
086            return null;
087        }
088        if (command instanceof I_CmsValidatingContextMenuCommand) {
089            boolean ok = ((I_CmsValidatingContextMenuCommand)command).validate(bean);
090            if (!ok) {
091                return null;
092            }
093        }
094        CmsContextMenuEntry entry = new CmsContextMenuEntry(this, structureId, command);
095        entry.setBean(bean);
096        if (bean.hasSubMenu()) {
097            entry.setSubMenu(transformEntries(bean.getSubMenu(), structureId));
098            if (entry.getSubMenu().isEmpty()) {
099                return null;
100            }
101        }
102        return entry;
103    }
104
105}