getSelectedItem() on GWT MenuBar

心已入冬 提交于 2019-12-25 08:21:00

问题


I'm new to GWT so please help me. I'm having a problem in my GWT MenuBar on how to getSelectedItem().

Here's my code:

public class Menu extends Composite implements Command {
    private MenuBar menu            = new MenuBar();
    private MenuBar priceMgt        = new MenuBar( true );
    private MenuBar salesReport     = new MenuBar( true );
    // and a lot more menubars

    private String[] itemPriceMgt = { 
        "Manage Price List", "Print Product Pricing", "Price Proof List"
    };

    private String[] itemSalesReport = { 
        "Sales Transaction List", "Cashier Report", "Media Tender Report",
        "Sales Report by Employee", "Sales Warranty Tracking", "Sales Report by Product", 
        "Sales Summary by Branch", "Sales Summary by Product", "Sales Summary by Period",
        "Product Movement Analysis", "Sales Comparison", 
        "Sales Book", "Download eSales" 
    };


    public Menu() {
        loadMenu();
    }

    private void loadMenu() {
        addSubMenuItem( priceMgt, itemPriceMgt );
        addSubMenuItem( salesReport, itemSalesReport );

        menu.addItem( "Home", false, this );
        menu.addItem( "Price Management", priceMgt );
        menu.addItem( "Sales Report", salesReport );
        menu.addItem( "Logout", false, this );

        initWidget( menu );
    }

    private void addSubMenuItem( MenuBar menubar, String[] list ) {
        for( int i = 0; i < list.length; i++ ) {
            menubar.addItem( list[i], this );
        }
    }

    public void execute() {
        // load the selected module
        // by getting the selected item
        // i can't use menu.getSelectedItem() - it's protected.
        // when i extend this class to MenuBar, adding Item says ambiguous 
        // method addItem(String,Command) for MenuBar
        // how can I get the items being selected/clicked?
    }
}

Others may say it's a not useful post but I really don't know how to figure it out. please help me. Thanks in advance.


回答1:


Here is a version that might work for you. The trick is that a specific Command needs to be associated with each MenuItem, not one command for all of them:

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.MenuBar;

public class Menu extends Composite {
    private MenuBar menu = new MenuBar();
    private MenuBar priceMgt = new MenuBar(true);
    private MenuBar salesReport = new MenuBar(true);
    // and a lot more menubars

    private String[] itemPriceMgt = { "Manage Price List",
            "Print Product Pricing", "Price Proof List" };

    private String[] itemSalesReport = { "Sales Transaction List",
            "Cashier Report", "Media Tender Report",
            "Sales Report by Employee", "Sales Warranty Tracking",
            "Sales Report by Product", "Sales Summary by Branch",
            "Sales Summary by Product", "Sales Summary by Period",
            "Product Movement Analysis", "Sales Comparison", "Sales Book",
            "Download eSales" };

    public Menu() {
        loadMenu();
    }

    private void loadMenu() {
        addSubMenuItem(priceMgt, itemPriceMgt);
        addSubMenuItem(salesReport, itemSalesReport);

        menu.addItem("Home", false, new Command() {
            @Override
            public void execute() {
                // TODO execute a Home command
            }
        });
        menu.addItem("Price Management", new Command() {
            @Override
            public void execute() {
                // TODO execute a Price Management command
            }
        });
        menu.addItem("Sales Report", new Command() {
            @Override
            public void execute() {
                // TODO execute a Sales Report command
            }
        });
        menu.addItem("Logout", false, new Command() {
            @Override
            public void execute() {
                // TODO execute a Logout command
            }
        });

        initWidget(menu);
    }

    class SubMenuItemCommand {
        private String commandName = null;

        public SubMenuItemCommand(String commandName) {
            this.commandName = commandName;
        }

        public Command subMenuItemCommand = new Command() {
            @Override
            public void execute() {
                if (commandName.equals("Manage Price List")) {
                    // TODO execute the Manage Price List command
                } else if (commandName.equals("Print Product Pricing")) {
                    // TODO execute the Print Product Pricing command
                }
                // and so on
            }
        };
    }

    private void addSubMenuItem(MenuBar menubar, String[] list) {
        for (int i = 0; i < list.length; i++) {
            menubar.addItem(list[i],
                    new SubMenuItemCommand(list[i]).subMenuItemCommand);
        }
    }
}

I've re-worked your code to use that approach, but have tried to keep as close to your original design as possible. Other people might suggest just rewriting the whole thing, but I think you'll be able to understand better how this works if we tack closely to your design.

An ugly part of this design is the SubMenuItemCommand class. I create a separate object for each submenu item, which is fine, but each object provides a unique Command with a whole bunch of code in it that executes exactly one path in it, ever. Lots of wasted bytecode there. But like I said, I wanted to stick close to your design. Once you get comfortable with it, you'll probably rewrite it yourself.

Another approach for the submenu is to create a Map of Strings to Commands, then pull the right Command from the Map to associate to each MenuItem named for that String.




回答2:


Instead of having this class implement Command and having all of your menu items use this as their argument when they are added with .addItem(), make a different Command object for each of your menu items. Then, code the specific logic for each menu item into the separate command objects.

i.e.:

 menu.addItem( "Home", false, new Command() {
      @Override
      public void execute() {
           // handle "Home" case here
      }
 );

If you really want to handle every menu item from the same piece of code, then you could make a Factory that takes the name of the menu item and produces a Command object that can reference it.



来源:https://stackoverflow.com/questions/7928310/getselecteditem-on-gwt-menubar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!