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.ade.containerpage.client.ui;
029
030import org.opencms.ade.containerpage.client.ui.css.I_CmsLayoutBundle;
031import org.opencms.ade.containerpage.shared.rpc.I_CmsContainerpageServiceAsync;
032import org.opencms.gwt.client.rpc.CmsRpcAction;
033import org.opencms.gwt.client.util.CmsPositionBean;
034import org.opencms.gwt.client.util.CmsStyleVariable;
035
036import com.google.gwt.user.client.ui.RootPanel;
037import com.google.gwt.user.client.ui.Widget;
038
039/**
040 * This class is responsible for managing the visibility of edit points on small elements.<p>
041 */
042public class CmsSmallElementsHandler {
043
044    /** The height necessary for a container page element. */
045    public static int NECESSARY_HEIGHT = 24;
046
047    /** The height necessary for a container page element CSS rule. */
048    public static String NECESSARY_HEIGHT_RULE = NECESSARY_HEIGHT + "px !important";
049
050    /** True if currently small elements are editable. */
051    boolean m_editing;
052
053    /** The container page service instance. */
054    I_CmsContainerpageServiceAsync m_service;
055
056    /** True if any small elements have been detected. */
057    private boolean m_hasSmallElements;
058
059    /** The style variable for the display mode for small elements. */
060    private CmsStyleVariable m_smallElementsStyle;
061
062    /**
063     * Creates a new small elements handler.<p>
064     *
065     * @param service the container page service
066     */
067    public CmsSmallElementsHandler(I_CmsContainerpageServiceAsync service) {
068
069        m_smallElementsStyle = new CmsStyleVariable(RootPanel.get());
070        m_service = service;
071    }
072
073    /**
074     * Checks if  a given widget counts as 'small'.<p>
075     *
076     * @param widget the widget to check
077     *
078     * @return  true if the widget is small
079     */
080    public static boolean isSmall(Widget widget) {
081
082        assert widget.isAttached();
083        return (CmsPositionBean.generatePositionInfo(widget.getElement()).getHeight() < NECESSARY_HEIGHT)
084            && (CmsPositionBean.getBoundingClientRect(widget.getElement()).getHeight() < NECESSARY_HEIGHT);
085
086    }
087
088    /**
089     * Returns true if currently small elements are editable.<p>
090     *
091     * @return true if small elements are editable
092     */
093    public boolean areSmallElementsEditable() {
094
095        return m_editing;
096    }
097
098    /**
099     * Returns true if any small elements are present.<p>
100     *
101     * @return true if any small elements are present
102     */
103    public boolean hasSmallElements() {
104
105        return m_hasSmallElements;
106    }
107
108    /**
109     * Prepares a small element.<p>
110     *
111     * @param widget the small element
112     */
113    public void prepareSmallElement(Widget widget) {
114
115        m_hasSmallElements = true;
116        widget.addStyleName(
117            org.opencms.ade.containerpage.client.ui.css.I_CmsLayoutBundle.INSTANCE.containerpageCss().smallElement());
118
119    }
120
121    /**
122     * Enables or disables editing for small elements and optionally saves the setting.<p>
123     *
124     * @param editable if true, enables editing for small elements
125     * @param save true if the setting should be saved
126     */
127    public void setEditSmallElements(final boolean editable, boolean save) {
128
129        if (save) {
130            CmsRpcAction<Void> action = new CmsRpcAction<Void>() {
131
132                @Override
133                public void execute() {
134
135                    start(200, false);
136                    internalSetEditSmallElements(editable);
137                    m_service.setEditSmallElements(editable, this);
138
139                }
140
141                @Override
142                protected void onResponse(Void result) {
143
144                    stop(false);
145                    m_editing = editable;
146                }
147            };
148            action.execute();
149        } else {
150            internalSetEditSmallElements(editable);
151            m_editing = editable;
152        }
153    }
154
155    /**
156     * Sets the mode for displaying small elements.<p>
157     *
158     * @param editable if true, small elements will be enlarged and their edit buttons shown; if false, the edit buttons will be hidden
159     */
160    protected void internalSetEditSmallElements(boolean editable) {
161
162        String newClass = editable
163        ? I_CmsLayoutBundle.INSTANCE.containerpageCss().enlargeSmallElements()
164        : I_CmsLayoutBundle.INSTANCE.containerpageCss().ignoreSmallElements();
165        m_smallElementsStyle.setValue(newClass);
166    }
167
168}