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.gwt.client.ui;
029
030import org.opencms.gwt.client.ui.css.I_CmsFloatDecoratedPanelCss;
031import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
032
033import com.google.gwt.core.client.Scheduler;
034import com.google.gwt.dom.client.Style.Float;
035import com.google.gwt.dom.client.Style.Unit;
036import com.google.gwt.dom.client.Style.Visibility;
037import com.google.gwt.user.client.ui.Composite;
038import com.google.gwt.user.client.ui.FlowPanel;
039import com.google.gwt.user.client.ui.Widget;
040
041/**
042 * A widget used for laying out multiple widgets horizontally.<p>
043 *
044 * It contains two panels, the "primary" (or main) panel and the "float" panel,
045 * to which widgets can be added. The float panel is styled so as to
046 * float left of the primary panel, and the primary panel's left margin is
047 * set to the width of the float panel. If the widget starts out as hidden,
048 * the float panel width can not be measured, so you have to call the updateLayout
049 * method  manually when the widget becomes visible.
050 *
051 * @since 8.0.0
052 */
053public class CmsFloatDecoratedPanel extends Composite implements I_CmsTruncable {
054
055    /** Css resource for this widget. */
056    static final I_CmsFloatDecoratedPanelCss CSS = I_CmsLayoutBundle.INSTANCE.floatDecoratedPanelCss();
057
058    /** The float panel. */
059    private FlowPanel m_floatBox = new FlowPanel();
060
061    /** The panel containing both the main and float panel. */
062    private FlowPanel m_panel = new FlowPanel();
063
064    /** The main panel. */
065    private FlowPanel m_primary = new FlowPanel();
066
067    /**
068     * Creates a new instance of the widget.
069     */
070    public CmsFloatDecoratedPanel() {
071
072        m_panel.setStyleName(CSS.floatDecoratedPanel());
073        m_floatBox.setStyleName(CSS.floatBox());
074        m_primary.setStyleName(CSS.primary());
075
076        m_panel.add(m_floatBox);
077        m_panel.add(m_primary);
078        m_floatBox.getElement().getStyle().setFloat(Float.LEFT);
079        initWidget(m_panel);
080        // we only make the widget visible after the layout has been updated to prevent "flickering"
081        getElement().getStyle().setVisibility(Visibility.HIDDEN);
082    }
083
084    /**
085     * Adds a widget to the main panel.<p>
086     *
087     * @param widget the widget to add
088     */
089    public void add(Widget widget) {
090
091        m_primary.add(widget);
092    }
093
094    /**
095     * Adds a widget to the float panel.<p>
096     *
097     * @param widget the widget to add
098     */
099    public void addToFloat(Widget widget) {
100
101        m_floatBox.add(widget);
102        updateLayout();
103    }
104
105    /**
106     * Adds a widget to the front of the float panel.<p>
107     *
108     * @param widget the widget to add
109     */
110    public void addToFrontOfFloat(Widget widget) {
111
112        m_floatBox.insert(widget, 0);
113    }
114
115    /**
116     * Returns the widget at the given position.<p>
117     *
118     * @param index the position
119     *
120     * @return  the widget at the given position
121     */
122    public Widget getWidget(int index) {
123
124        return m_primary.getWidget(index);
125    }
126
127    /**
128     * @see org.opencms.gwt.client.ui.I_CmsTruncable#truncate(java.lang.String, int)
129     */
130    public void truncate(String textMetricsPrefix, int widgetWidth) {
131
132        int width = widgetWidth;
133        width -= (!isAttached() ? 30 * m_floatBox.getWidgetCount() : getFloatBoxWidth());
134        for (Widget widget : m_primary) {
135            if (widget instanceof I_CmsTruncable) {
136                ((I_CmsTruncable)widget).truncate(textMetricsPrefix, width);
137            }
138        }
139    }
140
141    /**
142     * Sets the left margin of the main panel to the width of the float panel.<p>
143     */
144    public void updateLayout() {
145
146        // TODO: we should not do this kind of things...
147        if (!isAttached()) {
148            return;
149        }
150        int floatBoxWidth = getFloatBoxWidth();
151        m_primary.getElement().getStyle().setMarginLeft(floatBoxWidth, Unit.PX);
152        updateVerticalMargin();
153    }
154
155    /**
156     * Automatically calls the updateLayout method after insertion into the DOM.<p>
157     *
158     * @see com.google.gwt.user.client.ui.Widget#onLoad()
159     */
160    @Override
161    protected void onLoad() {
162
163        /* defer until children have been (hopefully) layouted. */
164        Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
165
166            /**
167             * @see com.google.gwt.user.client.Command#execute()
168             */
169            public void execute() {
170
171                updateLayout();
172                // layout update has finished, now it's OK to show the widget
173                getElement().getStyle().setVisibility(Visibility.VISIBLE);
174            }
175        });
176    }
177
178    /**
179     * Returns the width of the float box.<p>
180     *
181     * @return a width
182     */
183    private int getFloatBoxWidth() {
184
185        return m_floatBox.getOffsetWidth();
186    }
187
188    /**
189     * Updates the vertical margin of the float box such that its vertical middle point coincides
190     * with the vertical middle point of the primary panel.<p>
191     */
192    private void updateVerticalMargin() {
193
194        int floatHeight = m_floatBox.getOffsetHeight();
195        int primaryHeight = m_primary.getOffsetHeight();
196        int verticalOffset = (primaryHeight - floatHeight) / 2;
197        m_floatBox.getElement().getStyle().setMarginTop(verticalOffset, Unit.PX);
198    }
199}