001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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 /** 077 * Center of the toolbar, normally for displaying the logo, but the content can be changed. 078 */ 079 @UiField 080 protected FlowPanel m_toolbarCenter; 081 082 /** The user info button HTML. */ 083 @UiField 084 protected CmsUserInfo m_userInfo; 085 086 /** The two-line title widget. */ 087 private FlowPanel m_complexTitle = new FlowPanel(); 088 089 /** Bottom row of the two-line title. */ 090 private Label m_complexTitleBottom = new Label(); 091 092 /** Top row of the two-line title. */ 093 private Label m_complexTitleTop = new Label(); 094 095 /** The title label. */ 096 private Label m_titleLabel; 097 098 /** 099 * Constructor.<p> 100 */ 101 public CmsToolbar() { 102 103 initWidget(uiBinder.createAndBindUi(this)); 104 m_complexTitle.add(m_complexTitleTop); 105 m_complexTitle.add(m_complexTitleBottom); 106 m_complexTitle.addStyleName(I_CmsLayoutBundle.INSTANCE.toolbarCss().title()); 107 m_complexTitle.addStyleName(I_CmsLayoutBundle.INSTANCE.toolbarCss().complexTitle()); 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 */ 118 public static void showToolbar( 119 final CmsToolbar toolbar, 120 final boolean show, 121 final CmsStyleVariable toolbarVisibility) { 122 123 if (show) { 124 toolbarVisibility.setValue(null); 125 } else { 126 toolbarVisibility.setValue(I_CmsLayoutBundle.INSTANCE.toolbarCss().toolbarHide()); 127 } 128 } 129 130 /** 131 * Helper method for setting toolbar visibility.<p> 132 * 133 * @param toolbar the toolbar 134 * @param show true if the toolbar should be shown 135 * @param toolbarVisibility the style variable controlling the toolbar visibility 136 * @param showClass the class which should be used for showing the toolbar 137 */ 138 public static void showToolbar( 139 final CmsToolbar toolbar, 140 final boolean show, 141 final CmsStyleVariable toolbarVisibility, 142 String showClass) { 143 144 if (show) { 145 toolbarVisibility.setValue(showClass); 146 } else { 147 toolbarVisibility.setValue(I_CmsLayoutBundle.INSTANCE.toolbarCss().toolbarHide()); 148 } 149 } 150 151 /** 152 * Adds a widget to the left button panel.<p> 153 * 154 * @param widget the widget to add 155 */ 156 public void addLeft(Widget widget) { 157 158 m_buttonPanelLeft.add(widget); 159 } 160 161 /** 162 * Adds a widget to the left button panel.<p> 163 * 164 * @param widget the widget to add 165 */ 166 public void addRight(Widget widget) { 167 168 m_buttonPanelRight.add(widget); 169 170 } 171 172 /** 173 * Returns all {@link com.google.gwt.user.client.ui.Widget} added to the tool-bar in order of addition first left than right.<p> 174 * 175 * @return all added Widgets 176 */ 177 public List<Widget> getAll() { 178 179 List<Widget> all = new ArrayList<Widget>(); 180 Iterator<Widget> it = m_buttonPanelLeft.iterator(); 181 while (it.hasNext()) { 182 all.add(it.next()); 183 } 184 it = m_buttonPanelRight.iterator(); 185 while (it.hasNext()) { 186 all.add(it.next()); 187 } 188 return all; 189 } 190 191 /** 192 * Returns the quick launcher.<p> 193 * 194 * @return the quick launch menu button 195 */ 196 public CmsQuickLauncher getQuickLauncher() { 197 198 return m_quickLauncher; 199 } 200 201 /** 202 * Gets the center area of the toolbar, which normally contains the logo. 203 * 204 * @return the center toolbar area 205 */ 206 public FlowPanel getToolbarCenter() { 207 208 return m_toolbarCenter; 209 } 210 211 /** 212 * Returns the user info button.<p> 213 * 214 * @return the user info button 215 */ 216 public CmsUserInfo getUserInfo() { 217 218 return m_userInfo; 219 } 220 221 /** 222 * Inserts a widget into the left button panel.<p> 223 * 224 * @param widget the widget to add 225 * @param index the before index 226 */ 227 public void insertLeft(Widget widget, int index) { 228 229 m_buttonPanelLeft.insert(widget, index); 230 } 231 232 /** 233 * Inserts a widget into the left button panel.<p> 234 * 235 * @param widget the widget to add 236 * @param index the before index 237 */ 238 public void insertRight(Widget widget, int index) { 239 240 m_buttonPanelRight.insert(widget, index); 241 } 242 243 /** 244 * Sets the toolbar title label.<p> 245 * 246 * @param title the title 247 */ 248 public void setAppTitle(String title) { 249 250 if (CmsStringUtil.isEmptyOrWhitespaceOnly(title)) { 251 if (m_titleLabel != null) { 252 m_titleLabel.removeFromParent(); 253 m_titleLabel = null; 254 } 255 m_complexTitle.removeFromParent(); 256 } else { 257 258 if (m_titleLabel == null) { 259 m_titleLabel = new Label(); 260 m_titleLabel.setStyleName(I_CmsLayoutBundle.INSTANCE.toolbarCss().title()); 261 m_buttonPanelLeft.insert(m_titleLabel, 0); 262 } 263 m_titleLabel.setText(title); 264 } 265 } 266 267 /** 268 * Sets a two-line title. 269 * 270 * @param top the top line text 271 * @param bottom the bottom line text 272 */ 273 public void setComplexTitle(String top, String bottom) { 274 275 setAppTitle(null); 276 if ((top == null) && (bottom == null)) { 277 return; 278 } 279 280 if (top == null) { 281 top = ""; 282 } 283 if (bottom == null) { 284 bottom = ""; 285 } 286 m_complexTitleTop.setText(top); 287 m_complexTitleBottom.setText(bottom); 288 m_buttonPanelLeft.insert(m_complexTitle, 0); 289 290 } 291 292 /** 293 * Sets the handler for the quick launch menu and turns that menu visible.<p> 294 * 295 * @param quicklaunchHandler the quick launch handler 296 */ 297 public void setQuickLaunchHandler(I_QuickLaunchHandler quicklaunchHandler) { 298 299 m_quickLauncher.setQuicklaunchHandler(quicklaunchHandler); 300 } 301}