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.CmsContainerpageHandler;
031import org.opencms.gwt.client.CmsPageEditorTouchHandler;
032import org.opencms.gwt.client.ui.A_CmsToolbarButton;
033import org.opencms.gwt.client.ui.I_CmsButton;
034
035import com.google.gwt.event.dom.client.ClickEvent;
036import com.google.gwt.event.dom.client.ClickHandler;
037
038/**
039 * Abstract button class implementing common methods of {@link org.opencms.gwt.client.ui.I_CmsToolbarButton}
040 * for container-page tool-bar buttons with element functions.<p>
041 *
042 * @since 8.0.0
043 */
044public abstract class A_CmsToolbarOptionButton extends A_CmsToolbarButton<CmsContainerpageHandler> {
045
046    /** The click handler for all tool-bar buttons. */
047    private static ClickHandler m_elementClickHandler = new ClickHandler() {
048
049        /**
050         * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
051         */
052        public void onClick(ClickEvent event) {
053
054            CmsElementOptionButton source = (CmsElementOptionButton)event.getSource();
055            if (!(source.getToolbarButton() instanceof CmsToolbarEditButton)
056                || !CmsPageEditorTouchHandler.get().eatClick(source.getContainerElement().getElementOptionBar())) {
057                source.getToolbarButton().onElementClick(event, source.getContainerElement());
058                source.getContainerElement().getElementOptionBar().removeHighlighting();
059                source.clearHoverState();
060            }
061        }
062    };
063
064    /**
065     * Constructor.<p>
066     *
067     * @param buttonData the button data
068     * @param handler the container-page handler
069     */
070    protected A_CmsToolbarOptionButton(I_CmsButton.ButtonData buttonData, CmsContainerpageHandler handler) {
071
072        super(buttonData, handler);
073    }
074
075    /**
076     * Creates an element options button associated with this button and assigns the click-handler.<p>
077     * If this method returns null, no option button should be shown.<p>
078     *
079     * @param element the element to create the button for
080     *
081     * @return the created button
082     */
083    public CmsElementOptionButton createOptionForElement(CmsContainerPageElementPanel element) {
084
085        if (!isOptionAvailable(element)) {
086            return null;
087        }
088        CmsElementOptionButton button = new CmsElementOptionButton(this, element);
089        button.addClickHandler(m_elementClickHandler);
090        return button;
091    }
092
093    /**
094     * Checks whether an option button should be shown for a container page element.<p>
095     *
096     * @param element a container page element
097     * @return true if the option should be shown for the given element
098     */
099    public boolean isOptionAvailable(CmsContainerPageElementPanel element) {
100
101        return true;
102    }
103
104    /**
105     * Method is executed when the element option button is clicked.<p>
106     *
107     * @param event the mouse event (stop propagation if appropriate)
108     * @param element the element the option button is associated to
109     */
110    public abstract void onElementClick(ClickEvent event, CmsContainerPageElementPanel element);
111
112    /**
113     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarActivate()
114     */
115    public void onToolbarActivate() {
116
117        // nothing to do
118    }
119
120    /**
121     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarDeactivate()
122     */
123    public void onToolbarDeactivate() {
124
125        // nothing to do
126    }
127}