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.editprovider.client;
029
030import org.opencms.gwt.client.A_CmsEntryPoint;
031import org.opencms.gwt.client.CmsBroadcastTimer;
032import org.opencms.gwt.client.CmsCoreProvider;
033import org.opencms.gwt.client.rpc.CmsRpcAction;
034import org.opencms.gwt.client.ui.CmsPushButton;
035import org.opencms.gwt.client.ui.CmsToolbar;
036import org.opencms.gwt.client.ui.CmsToolbarContextButton;
037import org.opencms.gwt.client.ui.I_CmsToolbarButton;
038import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
039import org.opencms.gwt.client.ui.css.I_CmsToolbarButtonLayoutBundle;
040import org.opencms.gwt.client.util.CmsDomUtil;
041import org.opencms.gwt.client.util.CmsDomUtil.Tag;
042import org.opencms.gwt.client.util.CmsPositionBean;
043import org.opencms.gwt.client.util.CmsStyleVariable;
044import org.opencms.gwt.shared.CmsCoreData.AdeContext;
045import org.opencms.gwt.shared.CmsGwtConstants;
046
047import java.util.HashMap;
048import java.util.List;
049import java.util.Map;
050
051import com.google.common.collect.Lists;
052import com.google.common.collect.Maps;
053import com.google.gwt.core.client.Scheduler;
054import com.google.gwt.core.client.Scheduler.ScheduledCommand;
055import com.google.gwt.dom.client.Element;
056import com.google.gwt.event.dom.client.ClickEvent;
057import com.google.gwt.event.dom.client.ClickHandler;
058import com.google.gwt.event.logical.shared.ResizeEvent;
059import com.google.gwt.event.logical.shared.ResizeHandler;
060import com.google.gwt.user.client.Window;
061import com.google.gwt.user.client.ui.RootPanel;
062
063/**
064 * The entry point class for the org.opencms.ade.editprovider.EditProvider module.<p>
065 *
066 * @since 8.0.0
067 */
068public class CmsDirectEditEntryPoint extends A_CmsEntryPoint {
069
070    /** The map of button bar positions. */
071    protected Map<String, CmsPositionBean> m_buttonPositions = new HashMap<String, CmsPositionBean>();
072
073    /** The map of editable element positions. */
074    protected Map<String, CmsPositionBean> m_positions = new HashMap<String, CmsPositionBean>();
075
076    /** The toolbar. */
077    protected CmsToolbar m_toolbar;
078
079    /** A style variable to control toolbar visibility. */
080    protected CmsStyleVariable m_toolbarVisibility;
081
082    /** The dierect edit buttons. */
083    private Map<String, CmsDirectEditButtons> m_directEditButtons = Maps.newHashMap();
084
085    /**
086     * Initializes the direct edit buttons.<p>
087     */
088    public void initializeButtons() {
089
090        List<Element> editableElements = CmsDomUtil.getElementsByClass(CmsGwtConstants.CLASS_EDITABLE, Tag.div);
091        List<CmsDirectEditButtons> editables = Lists.newArrayList();
092
093        for (Element elem : editableElements) {
094            CmsPositionBean pos = CmsDomUtil.getEditablePosition(elem);
095            m_positions.put(elem.getId(), pos);
096        }
097
098        CmsEditablePositionCalculator posCalc = new CmsEditablePositionCalculator(m_positions);
099        m_buttonPositions = posCalc.calculatePositions();
100
101        for (Element elem : editableElements) {
102            CmsDirectEditButtons directEdit = processEditableElement(elem);
103            m_directEditButtons.put(elem.getId(), directEdit);
104            editables.add(directEdit);
105        }
106        Window.addResizeHandler(new ResizeHandler() {
107
108            public void onResize(ResizeEvent event) {
109
110                repositionButtons();
111            }
112        });
113    }
114
115    /**
116     * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
117     */
118    @Override
119    public void onModuleLoad() {
120
121        super.onModuleLoad();
122        org.opencms.gwt.client.ui.css.I_CmsLayoutBundle.INSTANCE.directEditCss().ensureInjected();
123        RootPanel.get().addStyleName(I_CmsToolbarButtonLayoutBundle.INSTANCE.toolbarButtonCss().editButtonsVisible());
124        installToolbar();
125        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
126
127            public void execute() {
128
129                initializeButtons();
130
131            }
132        });
133        CmsBroadcastTimer.start();
134    }
135
136    /**
137     * Writes the tool-bar visibility into the session cache.<p>
138     *
139     * @param visible <code>true</code> if the tool-bar is visible
140     */
141    public void saveToolbarVisibility(final boolean visible) {
142
143        CmsRpcAction<Void> action = new CmsRpcAction<Void>() {
144
145            /**
146             * @see org.opencms.gwt.client.rpc.CmsRpcAction#execute()
147             */
148            @Override
149            public void execute() {
150
151                CmsCoreProvider.getService().setToolbarVisible(visible, this);
152            }
153
154            /**
155             * @see org.opencms.gwt.client.rpc.CmsRpcAction#onResponse(java.lang.Object)
156             */
157            @Override
158            protected void onResponse(Void result) {
159
160                //nothing to do
161            }
162        };
163        action.execute();
164    }
165
166    /**
167     * Adds the toolbar to the browser window.<p>
168     */
169    protected void installToolbar() {
170
171        ClickHandler clickHandler = new ClickHandler() {
172
173            /**
174             * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
175             */
176            public void onClick(ClickEvent event) {
177
178                I_CmsToolbarButton source = (I_CmsToolbarButton)event.getSource();
179                source.onToolbarClick();
180                if (source instanceof CmsPushButton) {
181                    ((CmsPushButton)source).clearHoverState();
182                }
183            }
184        };
185        m_toolbar = new CmsToolbar();
186        RootPanel root = RootPanel.get();
187        m_toolbarVisibility = new CmsStyleVariable(m_toolbar);
188        root.add(m_toolbar);
189        boolean initiallyVisible = CmsCoreProvider.get().isToolbarVisible();
190        if (initiallyVisible) {
191            m_toolbarVisibility.setValue(I_CmsLayoutBundle.INSTANCE.toolbarCss().simpleToolbarShow());
192        } else {
193            m_toolbarVisibility.setValue(I_CmsLayoutBundle.INSTANCE.toolbarCss().toolbarHide());
194        }
195
196        CmsDirectEditToolbarHandler handler = new CmsDirectEditToolbarHandler(this);
197
198        CmsToolbarPublishButton publish = new CmsToolbarPublishButton(handler);
199        publish.addClickHandler(clickHandler);
200        m_toolbar.addLeft(publish);
201
202        CmsToolbarSelectionButton selection = new CmsToolbarSelectionButton(handler);
203        selection.setActive(initiallyVisible);
204        selection.addClickHandler(clickHandler);
205        m_toolbar.addLeft(selection);
206        CmsToolbarContextButton contextMenuButton = new CmsToolbarContextButton(handler);
207        contextMenuButton.setMenuContext(AdeContext.editprovider);
208        contextMenuButton.addClickHandler(clickHandler);
209        m_toolbar.addRight(contextMenuButton);
210        handler.setContextMenuButton(contextMenuButton);
211
212    }
213
214    /**
215     * Checks if the toolbar is visible.<p>
216     *
217     * @return true if the toolbar is visible
218     */
219    protected boolean isToolbarVisible() {
220
221        return m_toolbarVisibility.getValue().equals(I_CmsLayoutBundle.INSTANCE.toolbarCss().simpleToolbarShow());
222    }
223
224    /**
225     * Adds the direct edit buttons for a single editable element.<p>
226     *
227     * @param elem the data container element
228     *
229     * @return the direct edit buttons widget which was created for the element
230     */
231    protected CmsDirectEditButtons processEditableElement(Element elem) {
232
233        RootPanel root = RootPanel.get();
234        CmsDirectEditButtons result = new CmsDirectEditButtons(elem, null);
235        root.add(result);
236        result.setPosition(m_positions.get(elem.getId()), m_buttonPositions.get(elem.getId()), elem.getParentElement());
237        return result;
238    }
239
240    /**
241     * Repositions the direct edit buttons.<p>
242     */
243    protected void repositionButtons() {
244
245        for (Map.Entry<String, CmsDirectEditButtons> entry : m_directEditButtons.entrySet()) {
246            CmsDirectEditButtons buttons = entry.getValue();
247            Element tag = buttons.getMarkerTag();
248            CmsPositionBean newPos = CmsDomUtil.getEditablePosition(tag);
249            m_positions.put(tag.getId(), newPos);
250        }
251        CmsEditablePositionCalculator posCalc = new CmsEditablePositionCalculator(m_positions);
252        m_buttonPositions = posCalc.calculatePositions();
253        for (CmsDirectEditButtons buttons : m_directEditButtons.values()) {
254            String id = buttons.getMarkerTag().getId();
255            buttons.setPosition(
256                m_positions.get(id),
257                m_buttonPositions.get(id),
258                buttons.getMarkerTag().getParentElement());
259        }
260
261    }
262
263    /**
264     * Toggles the visibility of the toolbar.<p>
265     *
266     * @param show <code>true</code> to show the toolbar
267     */
268    protected void toggleToolbar(boolean show) {
269
270        if (show) {
271            CmsToolbar.showToolbar(
272                m_toolbar,
273                true,
274                m_toolbarVisibility,
275                I_CmsLayoutBundle.INSTANCE.toolbarCss().simpleToolbarShow());
276            saveToolbarVisibility(true);
277        } else {
278            CmsToolbar.showToolbar(
279                m_toolbar,
280                false,
281                m_toolbarVisibility,
282                I_CmsLayoutBundle.INSTANCE.toolbarCss().simpleToolbarShow());
283            saveToolbarVisibility(false);
284        }
285
286    }
287
288}