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.CmsQuickLauncher.I_QuickLaunchHandler;
031import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
032import org.opencms.gwt.client.util.CmsStyleVariable;
033import org.opencms.util.CmsStringUtil;
034
035import java.util.ArrayList;
036import java.util.Iterator;
037import java.util.List;
038
039import com.google.gwt.core.client.GWT;
040import com.google.gwt.uibinder.client.UiBinder;
041import com.google.gwt.uibinder.client.UiField;
042import com.google.gwt.user.client.ui.Composite;
043import com.google.gwt.user.client.ui.FlowPanel;
044import com.google.gwt.user.client.ui.Label;
045import com.google.gwt.user.client.ui.Widget;
046
047/**
048 * Provides a tool-bar to be shown at the top of a page.<p>
049 *
050 * @since 8.0.0
051 */
052public class CmsToolbar extends Composite {
053
054    /**
055     * @see com.google.gwt.uibinder.client.UiBinder
056     */
057    protected interface I_CmsToolbarUiBinder extends UiBinder<Widget, CmsToolbar> {
058        // GWT interface, nothing to do here
059    }
060
061    /** The ui-binder instance for this class. */
062    private static I_CmsToolbarUiBinder uiBinder = GWT.create(I_CmsToolbarUiBinder.class);
063
064    /** Holds left-side buttons associated with the tool-bar. */
065    @UiField
066    protected FlowPanel m_buttonPanelLeft;
067
068    /** Holds right-side buttons associated with the tool-bar. */
069    @UiField
070    protected FlowPanel m_buttonPanelRight;
071
072    /**
073     * Center of the toolbar, normally for displaying the logo, but the content can be changed.
074     */
075    @UiField
076    protected FlowPanel m_toolbarCenter;
077
078    /** The quick launcher (initially invisible). */
079    @UiField
080    protected CmsQuickLauncher m_quickLauncher;
081
082    /** The user info button HTML. */
083    @UiField
084    protected CmsUserInfo m_userInfo;
085
086    /** The title label. */
087    private Label m_titleLabel;
088
089    /**
090     * Constructor.<p>
091     */
092    public CmsToolbar() {
093
094        initWidget(uiBinder.createAndBindUi(this));
095
096    }
097
098    /**
099     * Helper method for setting toolbar visibility.<p>
100     *
101     * @param toolbar the toolbar
102     * @param show true if the toolbar should be shown
103     * @param toolbarVisibility the style variable controlling the toolbar visibility
104     */
105    public static void showToolbar(
106        final CmsToolbar toolbar,
107        final boolean show,
108        final CmsStyleVariable toolbarVisibility) {
109
110        if (show) {
111            toolbarVisibility.setValue(null);
112        } else {
113            toolbarVisibility.setValue(I_CmsLayoutBundle.INSTANCE.toolbarCss().toolbarHide());
114        }
115    }
116
117    /**
118     * Helper method for setting toolbar visibility.<p>
119     *
120     * @param toolbar the toolbar
121     * @param show true if the toolbar should be shown
122     * @param toolbarVisibility the style variable controlling the toolbar visibility
123     * @param showClass the class which should be used for showing the toolbar
124     */
125    public static void showToolbar(
126        final CmsToolbar toolbar,
127        final boolean show,
128        final CmsStyleVariable toolbarVisibility,
129        String showClass) {
130
131        if (show) {
132            toolbarVisibility.setValue(showClass);
133        } else {
134            toolbarVisibility.setValue(I_CmsLayoutBundle.INSTANCE.toolbarCss().toolbarHide());
135        }
136    }
137
138    /**
139     * Adds a widget to the left button panel.<p>
140     *
141     * @param widget the widget to add
142     */
143    public void addLeft(Widget widget) {
144
145        m_buttonPanelLeft.add(widget);
146    }
147
148    /**
149     * Adds a widget to the left button panel.<p>
150     *
151     * @param widget the widget to add
152     */
153    public void addRight(Widget widget) {
154
155        m_buttonPanelRight.add(widget);
156
157    }
158
159    /**
160     * Returns all {@link com.google.gwt.user.client.ui.Widget} added to the tool-bar in order of addition first left than right.<p>
161     *
162     * @return all added Widgets
163     */
164    public List<Widget> getAll() {
165
166        List<Widget> all = new ArrayList<Widget>();
167        Iterator<Widget> it = m_buttonPanelLeft.iterator();
168        while (it.hasNext()) {
169            all.add(it.next());
170        }
171        it = m_buttonPanelRight.iterator();
172        while (it.hasNext()) {
173            all.add(it.next());
174        }
175        return all;
176    }
177
178    /**
179     * Returns the quick launcher.<p>
180     *
181     * @return the quick launch menu button
182     */
183    public CmsQuickLauncher getQuickLauncher() {
184
185        return m_quickLauncher;
186    }
187
188    /**
189     * Gets the center area of the toolbar, which normally contains the logo.
190     *
191     * @return the center toolbar area
192     */
193    public FlowPanel getToolbarCenter() {
194        return m_toolbarCenter;
195    }
196
197    /**
198     * Returns the user info button.<p>
199     *
200     * @return the user info button
201     */
202    public CmsUserInfo getUserInfo() {
203
204        return m_userInfo;
205    }
206
207    /**
208     * Inserts a widget into the left button panel.<p>
209     *
210     * @param widget the widget to add
211     * @param index the before index
212     */
213    public void insertLeft(Widget widget, int index) {
214
215        m_buttonPanelLeft.insert(widget, index);
216    }
217
218    /**
219     * Inserts a widget into the left button panel.<p>
220     *
221     * @param widget the widget to add
222     * @param index the before index
223     */
224    public void insertRight(Widget widget, int index) {
225
226        m_buttonPanelRight.insert(widget, index);
227    }
228
229    /**
230     * Sets the toolbar title label.<p>
231     *
232     * @param title the title
233     */
234    public void setAppTitle(String title) {
235
236        if (CmsStringUtil.isEmptyOrWhitespaceOnly(title)) {
237            if (m_titleLabel != null) {
238                m_titleLabel.removeFromParent();
239                m_titleLabel = null;
240            }
241        } else {
242
243            if (m_titleLabel == null) {
244                m_titleLabel = new Label();
245                m_titleLabel.setStyleName(I_CmsLayoutBundle.INSTANCE.toolbarCss().title());
246                m_buttonPanelLeft.insert(m_titleLabel, 0);
247            }
248            m_titleLabel.setText(title);
249        }
250    }
251
252    /**
253     * Sets the handler for the quick launch menu and turns that menu visible.<p>
254     *
255     * @param quicklaunchHandler the quick launch handler
256     */
257    public void setQuickLaunchHandler(I_QuickLaunchHandler quicklaunchHandler) {
258
259        m_quickLauncher.setQuicklaunchHandler(quicklaunchHandler);
260    }
261}