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    /** The quick launcher (initially invisible). */
073    @UiField
074    protected CmsQuickLauncher m_quickLauncher;
075
076    /** The user info button HTML. */
077    @UiField
078    protected CmsUserInfo m_userInfo;
079
080    /** The title label. */
081    private Label m_titleLabel;
082
083    /**
084     * Constructor.<p>
085     */
086    public CmsToolbar() {
087
088        initWidget(uiBinder.createAndBindUi(this));
089
090    }
091
092    /**
093     * Helper method for setting toolbar visibility.<p>
094     *
095     * @param toolbar the toolbar
096     * @param show true if the toolbar should be shown
097     * @param toolbarVisibility the style variable controlling the toolbar visibility
098     */
099    public static void showToolbar(
100        final CmsToolbar toolbar,
101        final boolean show,
102        final CmsStyleVariable toolbarVisibility) {
103
104        if (show) {
105            toolbarVisibility.setValue(null);
106        } else {
107            toolbarVisibility.setValue(I_CmsLayoutBundle.INSTANCE.toolbarCss().toolbarHide());
108        }
109    }
110
111    /**
112     * Helper method for setting toolbar visibility.<p>
113     *
114     * @param toolbar the toolbar
115     * @param show true if the toolbar should be shown
116     * @param toolbarVisibility the style variable controlling the toolbar visibility
117     * @param showClass the class which should be used for showing the toolbar
118     */
119    public static void showToolbar(
120        final CmsToolbar toolbar,
121        final boolean show,
122        final CmsStyleVariable toolbarVisibility,
123        String showClass) {
124
125        if (show) {
126            toolbarVisibility.setValue(showClass);
127        } else {
128            toolbarVisibility.setValue(I_CmsLayoutBundle.INSTANCE.toolbarCss().toolbarHide());
129        }
130    }
131
132    /**
133     * Adds a widget to the left button panel.<p>
134     *
135     * @param widget the widget to add
136     */
137    public void addLeft(Widget widget) {
138
139        m_buttonPanelLeft.add(widget);
140    }
141
142    /**
143     * Adds a widget to the left button panel.<p>
144     *
145     * @param widget the widget to add
146     */
147    public void addRight(Widget widget) {
148
149        m_buttonPanelRight.add(widget);
150
151    }
152
153    /**
154     * Returns all {@link com.google.gwt.user.client.ui.Widget} added to the tool-bar in order of addition first left than right.<p>
155     *
156     * @return all added Widgets
157     */
158    public List<Widget> getAll() {
159
160        List<Widget> all = new ArrayList<Widget>();
161        Iterator<Widget> it = m_buttonPanelLeft.iterator();
162        while (it.hasNext()) {
163            all.add(it.next());
164        }
165        it = m_buttonPanelRight.iterator();
166        while (it.hasNext()) {
167            all.add(it.next());
168        }
169        return all;
170    }
171
172    /**
173     * Returns the quick launcher.<p>
174     *
175     * @return the quick launch menu button
176     */
177    public CmsQuickLauncher getQuickLauncher() {
178
179        return m_quickLauncher;
180    }
181
182    /**
183     * Returns the user info button.<p>
184     *
185     * @return the user info button
186     */
187    public CmsUserInfo getUserInfo() {
188
189        return m_userInfo;
190    }
191
192    /**
193     * Inserts a widget into the left button panel.<p>
194     *
195     * @param widget the widget to add
196     * @param index the before index
197     */
198    public void insertLeft(Widget widget, int index) {
199
200        m_buttonPanelLeft.insert(widget, index);
201    }
202
203    /**
204     * Inserts a widget into the left button panel.<p>
205     *
206     * @param widget the widget to add
207     * @param index the before index
208     */
209    public void insertRight(Widget widget, int index) {
210
211        m_buttonPanelRight.insert(widget, index);
212    }
213
214    /**
215     * Sets the toolbar title label.<p>
216     *
217     * @param title the title
218     */
219    public void setAppTitle(String title) {
220
221        if (CmsStringUtil.isEmptyOrWhitespaceOnly(title)) {
222            if (m_titleLabel != null) {
223                m_titleLabel.removeFromParent();
224                m_titleLabel = null;
225            }
226        } else {
227
228            if (m_titleLabel == null) {
229                m_titleLabel = new Label();
230                m_titleLabel.setStyleName(I_CmsLayoutBundle.INSTANCE.toolbarCss().title());
231                m_buttonPanelLeft.insert(m_titleLabel, 0);
232            }
233            m_titleLabel.setText(title);
234        }
235    }
236
237    /**
238     * Sets the handler for the quick launch menu and turns that menu visible.<p>
239     *
240     * @param quicklaunchHandler the quick launch handler
241     */
242    public void setQuickLaunchHandler(I_QuickLaunchHandler quicklaunchHandler) {
243
244        m_quickLauncher.setQuicklaunchHandler(quicklaunchHandler);
245    }
246}