Search code examples
smartgwtsmartgwt-pro

Handling click event on SmartGWT RibbonBar


I have a ribbonBar with 4 Button on it. Now on each of the button clicked I want to perform some action. But the problem I have is how do I get to know which button is clicked. Can somebody guide me.Below is my code and screenshot. By the way I am using Button instead of the IconButton as shown in the showcase.is it OK if i use this button??.

enter image description here

public class DBilling implements EntryPoint
{

@Override
public void onModuleLoad()
{
    VLayout vLayout = new VLayout();
    vLayout.setWidth100();

    RibbonBar ribbonBar = new RibbonBar();
    ribbonBar.setLeft(0);
    ribbonBar.setTop(0);
    ribbonBar.setWidth100();

    ribbonBar.setMembersMargin(2);
    ribbonBar.setLayoutMargin(2);

    RibbonGroup orderGroup = new RibbonGroup();
    orderGroup.setTitle("New Order");
    orderGroup.setRowHeight(60);
    orderGroup.addControl(getButton("Order", "order", false));

    RibbonGroup reportGroup = new RibbonGroup();
    reportGroup.setTitle("Report");
    reportGroup.setRowHeight(60);
    reportGroup.addControl(getButton("Report", "report", false));

    RibbonGroup productGroup = new RibbonGroup();
    productGroup.setTitle("New Product");
    productGroup.setRowHeight(60);
    productGroup.addControl(getButton("Product", "cookies", false));

    RibbonGroup systemGroup = new RibbonGroup();
    systemGroup.setTitle("System");
    systemGroup.setRowHeight(60);
    systemGroup.addControl(getButton("System", "system", false));

    ribbonBar.addMember(orderGroup);
    ribbonBar.addMember(reportGroup);
    ribbonBar.addMember(productGroup);
    ribbonBar.addMember(systemGroup);

    vLayout.addChild(ribbonBar);
    vLayout.draw();

}

private Button getButton(String title, String iconName, boolean vertical)
{
    final Button cssButton = new Button(title);  
    cssButton.setShowRollOver(true);  
    cssButton.setShowDisabled(true);  
    cssButton.setShowDown(true);  
    cssButton.setIcon(iconName + ".png"); 
    cssButton.setIconSize(32);
    cssButton.setWidth(120);
    cssButton.addClickHandler(new ClickHandler()
    {

        @Override
        public void onClick(ClickEvent event)
        {

            SC.say(event.getSource().toString());
        }
    });

    return cssButton;
}

Solution

  • Untested, but I think something like this is what you want:

    public class DBilling implements EntryPoint
    {
    
        private RibbonBar ribbonBar;
    
        @Override
        public void onModuleLoad()
        {
            VLayout vLayout = new VLayout();
            vLayout.setWidth100();
    
            this.ribbonBar = new RibbonBar();
            this.ribbonBar.setLeft(0);
            this.ribbonBar.setTop(0);
            this.ribbonBar.setWidth100();
    
            this.ribbonBar.setMembersMargin(2);
            this.ribbonBar.setLayoutMargin(2);
    
            RibbonGroup orderGroup = new RibbonGroup();
            orderGroup.setTitle("New Order");
            orderGroup.setRowHeight(60);
            orderGroup.addControl(getButton("Order", "order", false));
    
            RibbonGroup reportGroup = new RibbonGroup();
            reportGroup.setTitle("Report");
            reportGroup.setRowHeight(60);
            reportGroup.addControl(getButton("Report", "report", false));
    
            RibbonGroup productGroup = new RibbonGroup();
            productGroup.setTitle("New Product");
            productGroup.setRowHeight(60);
            productGroup.addControl(getButton("Product", "cookies", false));
    
            RibbonGroup systemGroup = new RibbonGroup();
            systemGroup.setTitle("System");
            systemGroup.setRowHeight(60);
            systemGroup.addControl(getButton("System", "system", false));
    
            this.ribbonBar.addMember(orderGroup);
            this.ribbonBar.addMember(reportGroup);
            this.ribbonBar.addMember(productGroup);
            this.ribbonBar.addMember(systemGroup);
    
            vLayout.addChild(this.ribbonBar);
            vLayout.draw();
    
        }
    
        private Button getButton(String title, String iconName, boolean vertical)
        {
            final Button cssButton = new Button(title);  
            cssButton.setShowRollOver(true);  
            cssButton.setShowDisabled(true);  
            cssButton.setShowDown(true);  
            cssButton.setIcon(iconName + ".png"); 
            cssButton.setIconSize(32);
            cssButton.setWidth(120);
            cssButton.addClickHandler(new ClickHandler()
            {
    
                @Override
                public void onClick(ClickEvent event)
                {
                    RibbonBar a;
                    for(Canvas b : a.getMembers()){
                        RibbonGroup c=(RibbonGroup)b;
                        for(Canvas d : c.getControls()){
                            Button e = (Button)d;
                            if(e.equals(event.getSource()))
                                SC.say(e.getTitle());
                        }
                    }
    
                    SC.say(event.getSource().toString());
                }
            });
    
            return cssButton;
        }
    
    }
    

    or really if you always know it will be a Button just cast event.getSource() to a Button, then you can get the button title like ((Button)event.getSource()).getTitle()