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.I_CmsButton.ButtonColor; 031import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle; 032import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle; 033import org.opencms.gwt.client.util.CmsDomUtil; 034 035import com.google.gwt.user.client.DOM; 036import com.google.gwt.user.client.Event; 037import com.google.gwt.user.client.ui.HasHorizontalAlignment; 038import com.google.gwt.user.client.ui.ToggleButton; 039 040/** 041 * Tool-bar button class.<p> 042 * 043 * @since 8.0.0 044 */ 045public class CmsToggleButton extends ToggleButton implements HasHorizontalAlignment { 046 047 /** The current horizontal alignment. */ 048 private HorizontalAlignmentConstant m_align; 049 050 /** Stores the button style. */ 051 private ButtonStyle m_buttonStyle; 052 053 /** Stores the button color. */ 054 private I_CmsButton.ButtonColor m_color; 055 056 /** The down face image class. */ 057 private String m_downImageClass; 058 059 /** The image class. */ 060 private String m_imageClass; 061 062 /** Flag to indicate the button was reenalbled. Set until the next mouse up, over or out event. */ 063 private boolean m_isReenabled; 064 065 /** The button size. */ 066 private I_CmsButton.Size m_size; 067 068 /** The button text. */ 069 private String m_text; 070 071 /** The title. */ 072 private String m_title; 073 074 /** Use minimum width flag. */ 075 private boolean m_useMinWidth; 076 077 /** 078 * Constructor.<p> 079 */ 080 public CmsToggleButton() { 081 082 super(); 083 m_align = HasHorizontalAlignment.ALIGN_RIGHT; 084 setStyleName(I_CmsLayoutBundle.INSTANCE.buttonCss().cmsState()); 085 setButtonStyle(ButtonStyle.TEXT, ButtonColor.BLUE); 086 setSize(I_CmsButton.Size.medium); 087 } 088 089 /** 090 * Disables the button and changes the button title attribute to the disabled reason.<p> 091 * 092 * @param disabledReason the disabled reason 093 */ 094 public void disable(String disabledReason) { 095 096 setEnabled(false); 097 super.setTitle(disabledReason); 098 } 099 100 /** 101 * Enables the button, switching the button title attribute from the disabled reason to the original title.<p> 102 */ 103 public void enable() { 104 105 m_isReenabled = true; 106 setEnabled(true); 107 super.setTitle(m_title); 108 } 109 110 /** 111 * Returns the image class of the down face.<p> 112 * 113 * @return the image class of the down face 114 */ 115 public String getDownImageClass() { 116 117 return m_downImageClass; 118 } 119 120 /** 121 * This is the alignment of the text in reference to the image, possible values are left or right.<p> 122 * 123 * @see com.google.gwt.user.client.ui.HasHorizontalAlignment#getHorizontalAlignment() 124 */ 125 public HorizontalAlignmentConstant getHorizontalAlignment() { 126 127 return m_align; 128 } 129 130 /** 131 * Returns the imageClass.<p> 132 * 133 * @return the imageClass 134 */ 135 public String getImageClass() { 136 137 return m_imageClass; 138 } 139 140 /** 141 * Returns the size.<p> 142 * 143 * @return the size 144 */ 145 public I_CmsButton.Size getSize() { 146 147 return m_size; 148 } 149 150 /** 151 * Returns the text.<p> 152 * 153 * @return the text 154 */ 155 @Override 156 public String getText() { 157 158 return m_text; 159 } 160 161 /** 162 * Returns the title.<p> 163 * 164 * @return the title 165 */ 166 @Override 167 public String getTitle() { 168 169 return m_title; 170 } 171 172 /** 173 * Returns the useMinWidth.<p> 174 * 175 * @return the useMinWidth 176 */ 177 public boolean isUseMinWidth() { 178 179 return m_useMinWidth; 180 } 181 182 /** 183 * @see com.google.gwt.user.client.ui.CustomButton#onBrowserEvent(com.google.gwt.user.client.Event) 184 */ 185 @Override 186 public void onBrowserEvent(Event event) { 187 188 // if the button is enabled while the mouse-pointer is within the button element, 189 // the mouse-over element will not get triggered again 190 // this may prevent correct handling of the click event 191 if (isEnabled() && m_isReenabled) { 192 int type = DOM.eventGetType(event); 193 switch (type) { 194 case Event.ONMOUSEUP: 195 m_isReenabled = false; 196 CmsDomUtil.ensureMouseOver(getElement()); 197 break; 198 case Event.ONMOUSEOVER: 199 case Event.ONMOUSEOUT: 200 m_isReenabled = false; 201 break; 202 default: 203 } 204 } 205 206 super.onBrowserEvent(event); 207 } 208 209 /** 210 * Sets the button style.<p> 211 * 212 * @param style the style to set 213 * @param color the color to set 214 */ 215 public void setButtonStyle(I_CmsButton.ButtonStyle style, I_CmsButton.ButtonColor color) { 216 217 if (m_buttonStyle != null) { 218 for (String styleName : m_buttonStyle.getAdditionalClasses()) { 219 removeStyleName(styleName); 220 } 221 } 222 if (style == ButtonStyle.TRANSPARENT) { 223 setSize(null); 224 } 225 addStyleName(style.getCssClassName()); 226 m_buttonStyle = style; 227 228 if (m_color != null) { 229 removeStyleName(m_color.getClassName()); 230 } 231 if (color != null) { 232 addStyleName(color.getClassName()); 233 } 234 m_color = color; 235 } 236 237 /** 238 * @see com.google.gwt.user.client.ui.CustomButton#setDown(boolean) 239 */ 240 @Override 241 public void setDown(boolean down) { 242 243 super.setDown(down); 244 } 245 246 /** 247 * Sets the down face text and image.<p> 248 * 249 * @param text the down face text to set, set to <code>null</code> to not show any 250 * @param imageClass the down face image class to use, set to <code>null</code> to not show any 251 */ 252 public void setDownFace(String text, String imageClass) { 253 254 m_downImageClass = imageClass; 255 getDownFace().setHTML(getFaceHtml(text, imageClass)); 256 } 257 258 /** 259 * Sets the imageClassDown.<p> 260 * 261 * @param imageClass the imageClass to set 262 */ 263 public void setDownImageClass(String imageClass) { 264 265 setDownFace(m_text, imageClass); 266 } 267 268 /** 269 * This is the alignment of the text in reference to the image, possible values are left or right.<p> 270 * 271 * @see com.google.gwt.user.client.ui.HasHorizontalAlignment#setHorizontalAlignment(com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant) 272 */ 273 public void setHorizontalAlignment(HorizontalAlignmentConstant align) { 274 275 if (align.equals(HasHorizontalAlignment.ALIGN_CENTER)) { 276 // ignore center alignment 277 return; 278 } 279 m_align = align; 280 } 281 282 /** 283 * Sets the imageClass.<p> 284 * 285 * @param imageClass the imageClass to set 286 */ 287 public void setImageClass(String imageClass) { 288 289 setUpFace(m_text, imageClass); 290 } 291 292 /** 293 * Sets the size.<p> 294 * 295 * @param size the size to set 296 */ 297 public void setSize(I_CmsButton.Size size) { 298 299 if (m_size != null) { 300 removeStyleName(m_size.getCssClassName()); 301 } 302 if (size != null) { 303 addStyleName(size.getCssClassName()); 304 } 305 m_size = size; 306 } 307 308 /** 309 * Sets the text.<p> 310 * 311 * @param text the text to set 312 */ 313 @Override 314 public void setText(String text) { 315 316 setUpFace(text, m_imageClass); 317 m_text = text; 318 setTitle(text); 319 } 320 321 /** 322 * @see com.google.gwt.user.client.ui.UIObject#setTitle(java.lang.String) 323 */ 324 @Override 325 public void setTitle(String title) { 326 327 super.setTitle(title); 328 m_title = title; 329 } 330 331 /** 332 * Sets the up face text and image.<p> 333 * 334 * @param text the up face text to set, set to <code>null</code> to not show any 335 * @param imageClass the up face image class to use, set to <code>null</code> to not show any 336 */ 337 public void setUpFace(String text, String imageClass) { 338 339 m_text = text; 340 m_imageClass = imageClass; 341 getUpFace().setHTML(getFaceHtml(text, imageClass)); 342 } 343 344 /** 345 * Sets the useMinWidth.<p> 346 * 347 * @param useMinWidth the useMinWidth to set 348 */ 349 public void setUseMinWidth(boolean useMinWidth) { 350 351 if (useMinWidth != m_useMinWidth) { 352 if (useMinWidth) { 353 addStyleName(I_CmsLayoutBundle.INSTANCE.buttonCss().cmsMinWidth()); 354 } else { 355 removeStyleName(I_CmsLayoutBundle.INSTANCE.buttonCss().cmsMinWidth()); 356 } 357 m_useMinWidth = useMinWidth; 358 } 359 } 360 361 /** 362 * Convenience method to assemble the HTML to use for a button face.<p> 363 * 364 * @param text text the up face text to set, set to <code>null</code> to not show any 365 * @param imageClass the up face image class to use, set to <code>null</code> to not show any 366 * 367 * @return the HTML 368 */ 369 protected String getFaceHtml(String text, String imageClass) { 370 371 return CmsDomUtil.createFaceHtml(text, imageClass, m_align); 372 } 373}