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.gwt.client.CmsCoreProvider;
031import org.opencms.gwt.client.CmsPageEditorTouchHandler;
032import org.opencms.gwt.client.I_CmsElementToolbarContext;
033import org.opencms.gwt.client.dnd.CmsDNDHandler;
034import org.opencms.gwt.client.ui.A_CmsHoverHandler;
035import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
036import org.opencms.gwt.client.util.I_CmsUniqueActiveItem;
037import org.opencms.gwt.shared.CmsGwtConstants;
038
039import java.util.Iterator;
040
041import com.google.gwt.event.dom.client.HasMouseOutHandlers;
042import com.google.gwt.event.dom.client.HasMouseOverHandlers;
043import com.google.gwt.event.dom.client.MouseOutEvent;
044import com.google.gwt.event.dom.client.MouseOutHandler;
045import com.google.gwt.event.dom.client.MouseOverEvent;
046import com.google.gwt.event.dom.client.MouseOverHandler;
047import com.google.gwt.event.shared.HandlerRegistration;
048import com.google.gwt.user.client.Timer;
049import com.google.gwt.user.client.ui.Composite;
050import com.google.gwt.user.client.ui.FlowPanel;
051import com.google.gwt.user.client.ui.Widget;
052
053/**
054 * A panel to be displayed inside a container element to provide optional functions like edit, move, remove... <p>
055 *
056 * @since 8.0.0
057 */
058public class CmsElementOptionBar extends Composite
059implements HasMouseOverHandlers, HasMouseOutHandlers, I_CmsUniqueActiveItem, I_CmsElementToolbarContext {
060
061    /**
062     * Hover handler for option bar.<p>
063     */
064    protected class HoverHandler extends A_CmsHoverHandler {
065
066        /**
067         * @see org.opencms.gwt.client.ui.A_CmsHoverHandler#onHoverIn(com.google.gwt.event.dom.client.MouseOverEvent)
068         */
069        @Override
070        protected void onHoverIn(MouseOverEvent event) {
071
072            if (!CmsPageEditorTouchHandler.get().ignoreHover()) {
073                timer = null;
074                CmsCoreProvider.get().getFlyoutMenuContainer().setActiveItem(CmsElementOptionBar.this);
075                addHighlighting();
076            }
077        }
078
079        /**
080         * @see org.opencms.gwt.client.ui.A_CmsHoverHandler#onHoverOut(com.google.gwt.event.dom.client.MouseOutEvent)
081         */
082        @Override
083        protected void onHoverOut(MouseOutEvent event) {
084
085            if (!CmsPageEditorTouchHandler.get().ignoreHover()) {
086
087                timer = new Timer() {
088
089                    @Override
090                    public void run() {
091
092                        if (timer == this) {
093                            removeHighlighting();
094                        }
095                    }
096                };
097                timer.schedule(750);
098            }
099        }
100    }
101
102    /** The timer used for hiding the option bar. */
103    /*default */static Timer timer;
104
105    /** The calculated panel width. */
106    private int m_calculatedWidth;
107
108    /** The parent container element. */
109    private CmsContainerPageElementPanel m_containerElement;
110
111    /** The panel. */
112    private FlowPanel m_panel;
113
114    /**
115     * Constructor.<p>
116     *
117     * @param containerElement the parent container element
118     */
119    public CmsElementOptionBar(CmsContainerPageElementPanel containerElement) {
120
121        m_panel = new FlowPanel(CmsGwtConstants.TAG_OC_EDITPOINT);
122        m_containerElement = containerElement;
123        initWidget(m_panel);
124        HoverHandler handler = new HoverHandler();
125        addMouseOverHandler(handler);
126        addMouseOutHandler(handler);
127        setStyleName(I_CmsElementToolbarContext.ELEMENT_OPTION_BAR_CSS_CLASS);
128        addStyleName(I_CmsLayoutBundle.INSTANCE.generalCss().opencms());
129    }
130
131    /**
132     * Creates an option-bar for the given drag element.<p>
133     *
134     * @param element the element to create the option-bar for
135     * @param dndHandler the drag and drop handler
136     * @param buttons the list of buttons to display
137     *
138     * @return the created option-bar
139     */
140    public static CmsElementOptionBar createOptionBarForElement(
141        CmsContainerPageElementPanel element,
142        CmsDNDHandler dndHandler,
143        A_CmsToolbarOptionButton... buttons) {
144
145        CmsElementOptionBar optionBar = new CmsElementOptionBar(element);
146        if (buttons != null) {
147            // add buttons, last as first
148            for (int i = buttons.length - 1; i >= 0; i--) {
149                CmsElementOptionButton option = buttons[i].createOptionForElement(element);
150                if (option == null) {
151                    continue;
152                }
153                optionBar.add(option);
154                if (buttons[i] instanceof CmsToolbarMoveButton) {
155                    option.addMouseDownHandler(dndHandler);
156                }
157            }
158        }
159        return optionBar;
160    }
161
162    /**
163     * @see org.opencms.gwt.client.I_CmsElementToolbarContext#activateToolbarContext()
164     */
165    public void activateToolbarContext() {
166
167        addHighlighting();
168    }
169
170    /**
171     * Adds another option button.<p>
172     *
173     * @param w the button to add
174     */
175    public void add(Widget w) {
176
177        m_panel.add(w);
178    }
179
180    /**
181     * @see com.google.gwt.event.dom.client.HasMouseOutHandlers#addMouseOutHandler(com.google.gwt.event.dom.client.MouseOutHandler)
182     */
183    public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
184
185        return addDomHandler(handler, MouseOutEvent.getType());
186
187    }
188
189    /**
190     * @see com.google.gwt.event.dom.client.HasMouseOverHandlers#addMouseOverHandler(com.google.gwt.event.dom.client.MouseOverHandler)
191     */
192    public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
193
194        return addDomHandler(handler, MouseOverEvent.getType());
195    }
196
197    /**
198    * Clears the bar.<p>
199    */
200    public void clear() {
201
202        m_panel.clear();
203        m_calculatedWidth = 0;
204    }
205
206    /**
207     * @see org.opencms.gwt.client.I_CmsElementToolbarContext#deactivateToolbarContext()
208     */
209    public void deactivateToolbarContext() {
210
211        try {
212            internalRemoveHighlighting();
213        } catch (Exception e) {
214            // ignore
215        }
216    }
217
218    /**
219     * Returns the calculated width of the widget.<p>
220     *
221     * @return the calculated width
222     */
223    public int getCalculatedWidth() {
224
225        return m_calculatedWidth;
226    }
227
228    /**
229     * Returns an iterator for the child widgets.<p>
230     *
231     * @return the iterator
232     */
233    public Iterator<Widget> iterator() {
234
235        return m_panel.iterator();
236    }
237
238    /**
239     * @see org.opencms.gwt.client.util.I_CmsUniqueActiveItem#onDeactivate()
240     */
241    public void onDeactivate() {
242
243        try {
244            internalRemoveHighlighting();
245        } catch (Exception e) {
246            // ignore
247
248        }
249    }
250
251    /**
252     * Removes the highlighting and option bar.<p>
253     */
254    public void removeHighlighting() {
255
256        timer = null;
257        CmsCoreProvider.get().getFlyoutMenuContainer().clearIfMatches(this);
258        internalRemoveHighlighting();
259    }
260
261    /**
262     * Adds the highlighting and option bar.<p>
263     */
264    protected void addHighlighting() {
265
266        getElement().addClassName(I_CmsLayoutBundle.INSTANCE.stateCss().cmsHovering());
267        getContainerElement().highlightElement();
268
269    }
270
271    /**
272     * Returns the parent container element.<p>
273     *
274     * @return the parent container element
275     */
276    protected CmsContainerPageElementPanel getContainerElement() {
277
278        return m_containerElement;
279    }
280
281    /**
282     * Removes the highlighting.<p>
283     */
284    protected void internalRemoveHighlighting() {
285
286        getElement().removeClassName(I_CmsLayoutBundle.INSTANCE.stateCss().cmsHovering());
287        getContainerElement().removeHighlighting();
288
289    }
290}