GWT MenuBar上的getSelectedItem()

时间:2011-10-28 10:55:53

标签: gwt menuitem menubar

我是GWT的新手所以请帮助我。我在GWT MenuBar中遇到了关于如何getSelectedItem()的问题。

这是我的代码:

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?
    }
}

其他人可能会说这不是一个有用的帖子,但我真的不知道该怎么弄清楚。请帮我。提前谢谢。

2 个答案:

答案 0 :(得分:1)

这是一个可能适合您的版本。诀窍是特定的Command需要与每个MenuItem相关联,而不是所有这些命令的一个命令:

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);
        }
    }
}

我重新使用了您的代码来使用这种方法,但我们尽量保持尽可能接近原始设计。其他人可能会建议重写整个事情,但我认为如果我们严格遵守您的设计,您将能够更好地理解其工作原理。

这个设计的一个丑陋部分是SubMenuItemCommand类。我为每个子菜单项创建了一个单独的对象,这很好,但是每个对象都提供了一个唯一的Command,其中包含一大堆代码,它始终只执行一个路径。那里浪费了很多字节码。但就像我说的那样,我想要贴近你的设计。一旦你对它感到满意,你可能会自己重写它。

子菜单的另一种方法是创建一个字符串映射到命令,然后从Map中拉出正确的命令以关联为该字符串命名的每个MenuItem。

答案 1 :(得分:0)

在添加Command时,不要使用此类工具this并使所有菜单项都使用.addItem()作为参数,而是创建不同的Command对象对于每个菜单项。然后,将每个菜单项的特定逻辑编码到单独的命令对象中。

即:

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

如果你真的想要处理同一段代码中的每个菜单项,那么你可以创建一个Factory,它接受菜单项的名称并生成一个可以引用它的Command对象。