001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.acacia.client;
029
030import org.opencms.acacia.client.css.I_CmsLayoutBundle;
031import org.opencms.acacia.client.ui.CmsAttributeValueView;
032
033import com.google.gwt.event.dom.client.MouseDownEvent;
034import com.google.gwt.event.dom.client.MouseDownHandler;
035import com.google.gwt.event.dom.client.MouseOutEvent;
036import com.google.gwt.event.dom.client.MouseOutHandler;
037import com.google.gwt.event.dom.client.MouseOverEvent;
038import com.google.gwt.event.dom.client.MouseOverHandler;
039import com.google.gwt.event.shared.HandlerRegistration;
040import com.google.gwt.user.client.ui.RootPanel;
041import com.google.gwt.user.client.ui.Widget;
042
043/**
044 * The attribute value view highlighting handler.<p>
045 */
046public final class CmsValueFocusHandler implements MouseOverHandler, MouseOutHandler, MouseDownHandler {
047
048    /** The handler instance. */
049    private static CmsValueFocusHandler INSTANCE;
050
051    /** The on button hover highlighted element. */
052    private CmsAttributeValueView m_currentButtonFocus;
053
054    /** The currently selected value view. */
055    private CmsAttributeValueView m_currentFocus;
056
057    /** The handler registration. */
058    private HandlerRegistration m_handlerRegistration;
059
060    /**
061     * Constructor.<p>
062     */
063    private CmsValueFocusHandler() {
064
065        m_handlerRegistration = RootPanel.get().addDomHandler(this, MouseDownEvent.getType());
066    }
067
068    /**
069     * Returns the highlighting handler instance.<p>
070     *
071     * @return the highlighting handler instance
072     */
073    public static CmsValueFocusHandler getInstance() {
074
075        if (INSTANCE == null) {
076            INSTANCE = new CmsValueFocusHandler();
077        }
078        return INSTANCE;
079    }
080
081    /**
082     * Clears the static instance reference.<p>
083     */
084    private static void clearInstance() {
085
086        INSTANCE = null;
087    }
088
089    /**
090     * Removes all focus highlighting.<p>
091     */
092    public void clearFocus() {
093
094        if ((m_currentFocus != null)) {
095            m_currentFocus.toggleFocus(false);
096            m_currentFocus = null;
097        }
098    }
099
100    /**
101     * Destroys the current handler instance.<p>
102     */
103    public void destroy() {
104
105        m_currentFocus = null;
106        if (m_handlerRegistration != null) {
107            m_handlerRegistration.removeHandler();
108        }
109        m_handlerRegistration = null;
110        clearInstance();
111    }
112
113    /**
114     * Hides all help bubbles.<p>
115     *
116     * @param formPanel the form panel
117     * @param hide <code>true</code> to hide the help bubbles
118     */
119    public void hideHelpBubbles(Widget formPanel, boolean hide) {
120
121        if (hide) {
122            formPanel.addStyleName(I_CmsLayoutBundle.INSTANCE.form().hideHelpBubbles());
123        } else {
124            formPanel.removeStyleName(I_CmsLayoutBundle.INSTANCE.form().hideHelpBubbles());
125        }
126    }
127
128    /**
129     * @see com.google.gwt.event.dom.client.MouseDownHandler#onMouseDown(com.google.gwt.event.dom.client.MouseDownEvent)
130     */
131    public void onMouseDown(MouseDownEvent event) {
132
133        event.stopPropagation();
134        if (RootPanel.get().equals(event.getSource())) {
135            com.google.gwt.dom.client.Element element = com.google.gwt.dom.client.Element.as(
136                event.getNativeEvent().getEventTarget());
137            if ((m_currentFocus != null) && !m_currentFocus.owns(element)) {
138                m_currentFocus.toggleFocus(false);
139                m_currentFocus = null;
140            }
141        } else {
142            if (event.getSource().equals(m_currentFocus)) {
143                return;
144            }
145            if ((m_currentFocus != null)) {
146                m_currentFocus.toggleFocus(false);
147            }
148            m_currentFocus = (CmsAttributeValueView)event.getSource();
149            m_currentFocus.toggleFocus(true);
150        }
151    }
152
153    /**
154     * @see com.google.gwt.event.dom.client.MouseOutHandler#onMouseOut(com.google.gwt.event.dom.client.MouseOutEvent)
155     */
156    public void onMouseOut(MouseOutEvent event) {
157
158        if (!(event.getSource() instanceof CmsAttributeValueView)) {
159            if ((m_currentButtonFocus != null)
160                && m_currentButtonFocus.getElement().isOrHasChild(((Widget)event.getSource()).getElement())) {
161                if (!m_currentButtonFocus.equals(m_currentFocus)) {
162                    m_currentButtonFocus.toggleFocus(false);
163                }
164                m_currentButtonFocus = null;
165            }
166            return;
167        }
168    }
169
170    /**
171     * @see com.google.gwt.event.dom.client.MouseOverHandler#onMouseOver(com.google.gwt.event.dom.client.MouseOverEvent)
172     */
173    public void onMouseOver(MouseOverEvent event) {
174
175        if (!(event.getSource() instanceof CmsAttributeValueView)) {
176            CmsAttributeValueView parentView = null;
177            Widget source = ((Widget)event.getSource()).getParent();
178            while (parentView == null) {
179                if (source instanceof CmsAttributeValueView) {
180                    parentView = (CmsAttributeValueView)source;
181                } else {
182                    source = source.getParent();
183                }
184            }
185            if (m_currentButtonFocus != null) {
186                if (m_currentButtonFocus.equals(parentView)) {
187                    return;
188                } else if (!m_currentButtonFocus.equals(m_currentFocus)) {
189                    m_currentButtonFocus.toggleFocus(false);
190                }
191            }
192            m_currentButtonFocus = parentView;
193            m_currentButtonFocus.toggleFocus(true);
194            return;
195        }
196    }
197
198    /**
199     * Sets the given attribute value view focused.<p>
200     *
201     * @param target the target attribute value view
202     */
203    public void setFocus(CmsAttributeValueView target) {
204
205        if (m_currentFocus == target) {
206            return;
207        }
208
209        if ((m_currentFocus != null)) {
210            m_currentFocus.toggleFocus(false);
211        }
212        m_currentFocus = target;
213        m_currentFocus.toggleFocus(true);
214    }
215}