Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * This program is free software; you can redistribute it and/or modify it under the 
   * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software 
   * Foundation.
   *
   * You should have received a copy of the GNU Lesser General Public License along with this 
   * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html 
   * or from the Free Software Foundation, Inc., 
   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU Lesser General Public License for more details.
  *
  * Copyright 2008 Pentaho Corporation.  All rights reserved.
  */
 package org.pentaho.gwt.widgets.client.toolbar;
 
 
 import  com.google.gwt.user.client.Command;
 import  com.google.gwt.user.client.ui.ClickListener;
 import  com.google.gwt.user.client.ui.DockPanel;
 import  com.google.gwt.user.client.ui.FocusPanel;
 import  com.google.gwt.user.client.ui.Image;
 import  com.google.gwt.user.client.ui.Label;
 import  com.google.gwt.user.client.ui.MouseListener;
 import  com.google.gwt.user.client.ui.Widget;

Manages a PushButton in the common toolbar Toolbar. Note, it's not a subclass of PushButton as the PushButton api does not allow the changing of the image after instantiation. It is not a decorator because the GWT PushButton does not implement an interface. If these limitations change, please change this class.

Author(s):
nbaker
 
 @SuppressWarnings("deprecation")
 public class ToolbarButton {
   protected DockPanel button = new DockPanel();
   protected boolean enabled = true;
   protected boolean visible = true;
   protected String text;
   protected Image image;
   protected Image disabledImage;
   protected Image currentImage;
   protected Label label = new Label();
   protected FocusPanel eventWrapper = new FocusPanel();
   protected String stylePrimaryName = "toolbar-button";    //$NON-NLS-1$
   protected Command command;
   protected String toolTip;
   protected Image downImage;
   protected Image downImageDisabled;

  
Constructs a toolbar button with an image and a label

Parameters:
img GWT Image object
label String containing an option label
 
   public ToolbarButton(Image imgString label){
     this(img);
     this..setText(label);
     .add(this., DockPanel.EAST);
   }
  
  
Constructs a toolbar button with an enabled image, disabled image and a label

Parameters:
img GWT Image object
disabledImage GWT Image object
label String containing an option label
 
   public ToolbarButton(Image img, Image disabledImageString label){
     this(imglabel);
     this. = disabledImage;
   }
  

  
Constructs a toolbar button with an enabled image, disabled image and a label

Parameters:
img GWT Image object
disabledImage GWT Image object
label String containing an option label
 
   public ToolbarButton(Image img, Image disabledImage){
     this(img);
     this. = disabledImage;
   }
  
  
Constructs a toolbar button with an image, currently hardcoded to 16x16

Parameters:
img GWT Image object
 
  public ToolbarButton(Image img){
    this. = img;
    this. = img;
    .add(this., DockPanel.CENTER);
    .setCellHorizontalAlignment(this., DockPanel.ALIGN_CENTER);
    .setCellVerticalAlignment(this., DockPanel.ALIGN_MIDDLE);
    .setStyleName();
    .add();
    
  }
  
  public void setId(String id){
    if(( != null) && (.getElement()!=null)){
      .getElement().setId(id.concat("_btn"));
    }
    if(( != null) && (.getElement()!=null)){
      .getElement().setId(id);
    }
  }
  
  public void setStylePrimaryName(String styleName){
    this. = styleName;
    .setStylePrimaryName(styleName);
    
  }
  
  protected void addStyleMouseListener(){
    // a click listener is more appropriate here to fire the click events
    // rather than a mouse-up because the focus panel can (and does) sometimes
    // receive mouse up events if a widget 'above' it has been clicked and
    // dismissed (on mouse-down).  The ensures that only a true click will
    // fire a button's command
    .addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        if(!){
          //ElementUtils.blur(ToolbarButton.this.eventWrapper.getElement());
          return;
        }
        .removeStyleName(+"-down");   //$NON-NLS-1$
        .removeStyleName(+"-hovering");   //$NON-NLS-1$
        .execute();
        //ElementUtils.blur(ToolbarButton.this.eventWrapper.getElement());
      }
    });
    .addMouseListener(new MouseListener(){
      public void onMouseDown(Widget wint arg1int arg2) {
        if(!){
          return;
        }
        .addStyleName(+"-down");    //$NON-NLS-1$
      }
      public void onMouseEnter(Widget arg0) {
        if(!){
          return;
        }
        .addStyleName(+"-hovering");    //$NON-NLS-1$
      }
      public void onMouseLeave(Widget arg0) {
        if(!){
          return;
        }
        .removeStyleName(+"-hovering");   //$NON-NLS-1$
      }
      public void onMouseUp(Widget arg0int arg1int arg2) {
        if(!){
          //ElementUtils.blur(ToolbarButton.this.eventWrapper.getElement());
          return;
        }
        .removeStyleName(+"-down");   //$NON-NLS-1$
        .removeStyleName(+"-hovering");   //$NON-NLS-1$
        //ElementUtils.blur(ToolbarButton.this.eventWrapper.getElement());
      }
      public void onMouseMove(Widget arg0int arg1int arg2) {}
    });
  }
  
  
Gets the enabled status of the button.

Returns:
boolean flag
  public boolean isEnabled() {
    return ;  
  }

  
Sets the enabled status of the button.

Parameters:
enabled boolean flag
  public void setEnabled(boolean enabled) {
    boolean prevState = this.;
    this. = enabled;
    if(enabled){
      .removeStyleName(+"-disabled");    //$NON-NLS-1$
      if(prevState == false &&  != null){
        //was disabled, remove old image and put in the enabled one
        .remove();
        .add(calculateApporiateImage(), DockPanel.CENTER);
        .setCellHorizontalAlignment(this., DockPanel.ALIGN_CENTER);
        .setCellVerticalAlignment(this., DockPanel.ALIGN_MIDDLE);
      }
      
    } else {
      .addStyleName(+"-disabled");    //$NON-NLS-1$
      
      if(prevState == true &&  != null){
        //was enabled, remove old image and put in the disabled one
        .remove();
        .add(calculateApporiateImage(), DockPanel.CENTER);
        .setCellHorizontalAlignment(this., DockPanel.ALIGN_CENTER);
        .setCellVerticalAlignment(this., DockPanel.ALIGN_MIDDLE);
      }
    }
  }
  public void setTempDisabled(boolean disable) {
    .setStyleName(
        this. && !disable
        ? 
        : +"-disabled"              //$NON-NLS-1$
    );
  }
  
  
Gets the visibility of the button]

Returns:
boolean flag
  public boolean isVisible() {
    return ;
  }

  
Sets the visibility of the button

Parameters:
visible boolean flag
  public void setVisible(boolean visible) {
    this. = visible;
    .setVisible(visible);
  }
  
  
Returns the managed PushButton object

Returns:
PushButton concreate object
  public FocusPanel getPushButton(){
    return ;
  }

  
Returns the image displayed on this button.

Returns:
GWT Image
  public Image getImage() {
    return ;
  }

  
Returns the image displayed on this button.

Returns:
GWT Image
  public Image getDisabledImage() {
    return ;
  }

  
Sets the image displayed on this button.

Parameters:
img GWT Image
  public void setImage(Image img) {
    this. = img;
    .remove();
    Image curImage = calculateApporiateImage();
    .add(curImage, DockPanel.CENTER);
    .setCellHorizontalAlignment(curImage, DockPanel.ALIGN_CENTER);
    .setCellVerticalAlignment(curImage, DockPanel.ALIGN_MIDDLE);
  }

  
Sets the image to be displayed on this button when disabled (greyed out).

Parameters:
img GWT Image
  public void setDisabledImage(Image img) {
    if(!isEnabled()){
      //was enabled, remove old image and put in the disabled one
      this. = img;
      .remove();
      Image curImage = calculateApporiateImage();
      .add(curImage, DockPanel.CENTER);
      .setCellHorizontalAlignment(curImage, DockPanel.ALIGN_CENTER);
      .setCellVerticalAlignment(curImage, DockPanel.ALIGN_MIDDLE);
    }
  }
  
  
Returns the optional text to be displayed on this button.

Returns:
String
  public String getText() {
    return ;
  }

  
Sets the optional text to be displayed on this button.

Parameters:
text String to be displayed
  public void setText(String text) {
    this. = text;
    .setText(text);
  }

  
Returns the click listener attached to the button instance.

Returns:
ClickListener
  public Command getCommand() {
    return ;
  }

  
Sets the ClickListener on the button. If a ClickListener was previously added to the button, it will be removed (let Nick know if you don't like this behavior).

Parameters:
clickListener
  public void setCommand(Command cmd) {
    this. = cmd;
  }
  
  
Sets the text to be displayed in a hover-tip when a user hovers over the button

Parameters:
toolTip String
  public void setToolTip(String toolTip){
    this. = toolTip;
    .addMouseListener(new ToolTip(toolTip, 1000));
  }
  
  
Gets the text to be displayed in a hover-tip when a user hovers over the button

Returns:
String tooltip
  public String getToolTip(){
    return ;
  }
  
  protected Image calculateApporiateImage(){
     = (! &&  != null)?  : ;
    return ;
  }

  
Gets the image to be displayed on this button when depressed
  public Image getDownImage() {
  
    return ;
  }

  
Sets the image to be displayed on this button when depressed

Parameters:
img GWT Image
  public void setDownImage(Image downImage) {
  
    this. = downImage;
  }
  public Image getDownImageDisabled() {
  
    return ;
  }
  public void setDownImageDisabled(Image downImageDisabled) {
  
    this. = downImageDisabled;
  }
  
  
New to GrepCode? Check out our FAQ X