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;
029
030import org.opencms.gwt.client.util.CmsDomUtil;
031
032import com.google.gwt.core.client.JavaScriptObject;
033import com.google.gwt.dom.client.Element;
034import com.google.gwt.dom.client.NativeEvent;
035import com.google.gwt.user.client.Event;
036import com.google.gwt.user.client.Event.NativePreviewEvent;
037import com.google.gwt.user.client.Event.NativePreviewHandler;
038
039/**
040 * Singleton class that manages showing / hiding of toolbars in touch-only mode in the page editor.
041 */
042public class CmsPageEditorTouchHandler {
043
044    /** The instance of the class. */
045    private static CmsPageEditorTouchHandler instance = new CmsPageEditorTouchHandler();
046
047    /** The currently active toolbar context. */
048    private I_CmsElementToolbarContext m_activeContext;
049
050    /**
051     * Creates a new instance.
052     */
053    private CmsPageEditorTouchHandler() {
054
055        // Clicks on non-toolbar elements should deactivate the currently activated toolbar
056        // when in touch-only mode. We catch click events and check the target's ancestors
057        // for the absence of the CSS class for toolbars to detect this case.
058        if (CmsCoreProvider.isTouchOnly()) {
059            Event.addNativePreviewHandler(new NativePreviewHandler() {
060
061                public void onPreviewNativeEvent(NativePreviewEvent event) {
062
063                    if (m_activeContext != null) {
064                        NativeEvent nativeEvent = event.getNativeEvent();
065                        if (event.getTypeInt() == Event.ONCLICK) {
066                            JavaScriptObject target = nativeEvent.getEventTarget();
067                            if (Element.is(target)) {
068                                Element element = Element.as(nativeEvent.getEventTarget());
069                                Element optionBar = CmsDomUtil.getAncestor(
070                                    element,
071                                    I_CmsElementToolbarContext.ELEMENT_OPTION_BAR_CSS_CLASS);
072                                if (optionBar == null) {
073                                    deactivateContext();
074                                }
075                            }
076                        }
077                    }
078                }
079            });
080        }
081
082    }
083
084    /**
085     * Gets the singleton instance of the handler.
086     *
087     * @return the singleton instance
088     */
089    public static CmsPageEditorTouchHandler get() {
090
091        return instance;
092    }
093
094    /**
095     * This method is called to give this class a chance to process clicks on toolbar buttons by itself, for the purpose
096     * of showing / hiding toolbars in touch-only mode. If this class decided to handle the click event, returns true, otherwise false.
097     *
098     * @param context the toolbar context
099     * @return true if the click was handled by this class
100     */
101    public boolean eatClick(I_CmsElementToolbarContext context) {
102
103        if (!CmsCoreProvider.isTouchOnly()) {
104            return false;
105        }
106
107        if (m_activeContext == context) {
108            return false;
109        } else {
110            deactivateContext();
111            m_activeContext = context;
112            m_activeContext.activateToolbarContext();
113            return true;
114        }
115    }
116
117    /**
118     * Check if hover events should be ignored.
119     *
120     * @return true if hover events should be ignored
121     */
122    public boolean ignoreHover() {
123
124        return CmsCoreProvider.isTouchOnly();
125    }
126
127    /**
128     * Deactivates and clears the current context, if it is set.
129     */
130    private void deactivateContext() {
131
132        if (m_activeContext != null) {
133            m_activeContext.deactivateToolbarContext();
134            m_activeContext = null;
135        }
136    }
137
138}