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