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.ade.containerpage.client.ui;
029
030import org.opencms.ade.containerpage.client.CmsContainerpageController;
031import org.opencms.ade.containerpage.client.CmsContainerpageHandler;
032import org.opencms.ade.containerpage.client.ui.css.I_CmsLayoutBundle;
033import org.opencms.gwt.client.ui.I_CmsButton;
034import org.opencms.gwt.client.ui.contextmenu.CmsShowPage;
035import org.opencms.util.CmsStringUtil;
036
037import java.util.ArrayList;
038import java.util.Arrays;
039import java.util.List;
040
041import com.google.common.base.Joiner;
042import com.google.gwt.event.dom.client.ClickEvent;
043
044/**
045 * The edit button holding all edit related methods.<p>
046 *
047 * @since 8.0.0
048 */
049public class CmsToolbarEditButton extends A_CmsToolbarOptionButton {
050
051    /** List of function types. */
052    private static final List<String> functionTypes = Arrays.asList("function", "function_config");
053
054    /**
055     * Constructor.<p>
056     *
057     * @param handler the container-page handler
058     */
059    public CmsToolbarEditButton(CmsContainerpageHandler handler) {
060
061        super(I_CmsButton.ButtonData.EDIT, handler);
062    }
063
064    /**
065     * @see org.opencms.ade.containerpage.client.ui.A_CmsToolbarOptionButton#createOptionForElement(org.opencms.ade.containerpage.client.ui.CmsContainerPageElementPanel)
066     */
067    @Override
068    public CmsElementOptionButton createOptionForElement(CmsContainerPageElementPanel element) {
069
070        CmsElementOptionButton button = super.createOptionForElement(element);
071        button.setImageClass(I_CmsButton.ButtonData.SELECTION.getIconClass());
072        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(element.getNoEditReason())) {
073
074            List<String> cssClasses = new ArrayList<>();
075            cssClasses.add(I_CmsButton.ButtonData.SELECTION.getIconClass());
076            if (functionTypes.contains(element.getResourceType())) {
077                cssClasses.add(I_CmsLayoutBundle.INSTANCE.containerpageCss().functionElement());
078                cssClasses.add(I_CmsLayoutBundle.INSTANCE.containerpageCss().lockedElement());
079                button.setTitle(element.getNoEditReason());
080            } else if (element.hasWritePermission()
081                && !((element instanceof CmsGroupContainerElementPanel)
082                    && ((CmsGroupContainerElementPanel)element).isInheritContainer())) {
083                cssClasses.add(I_CmsLayoutBundle.INSTANCE.containerpageCss().lockedElement());
084                button.setTitle(element.getNoEditReason());
085            } else {
086                button.disable(element.getNoEditReason());
087            }
088            button.setImageClass(Joiner.on(" ").join(cssClasses));
089        }
090        return button;
091    }
092
093    /**
094     * @see org.opencms.ade.containerpage.client.ui.A_CmsToolbarOptionButton#onElementClick(com.google.gwt.event.dom.client.ClickEvent, org.opencms.ade.containerpage.client.ui.CmsContainerPageElementPanel)
095     */
096    @Override
097    public void onElementClick(ClickEvent event, CmsContainerPageElementPanel element) {
098
099        if (CmsStringUtil.isEmptyOrWhitespaceOnly(element.getNoEditReason())) {
100            if (!CmsContainerpageController.get().getData().isModelGroup() && element.isModelGroup()) {
101                new CmsShowPage().execute(element.getModelGroupId(), null, null);
102            } else {
103                openEditor(element);
104            }
105        } else {
106            openLockReport(element);
107        }
108        event.stopPropagation();
109        event.preventDefault();
110
111    }
112
113    /**
114     * Opens the element editor.<p>
115     *
116     * @param element the element
117     */
118    private void openEditor(CmsContainerPageElementPanel element) {
119
120        getHandler().openEditorForElement(element, false, element.isNew());
121    }
122
123    /**
124     * Opens the lock report for locked elements.<p>
125     *
126     * @param element the element
127     */
128    private void openLockReport(CmsContainerPageElementPanel element) {
129
130        getHandler().openLockReportForElement(element);
131    }
132}