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