GWT MenuBar上的getSelectedItem() [英] getSelectedItem() on GWT MenuBar

查看:130
本文介绍了GWT MenuBar上的getSelectedItem()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是GWT新手,请帮助我。我在 GWT MenuBar 中遇到了如何 getSelectedItem() code>。

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.

推荐答案

以下是可能适合您的版本。诀窍是一个特定的命令需要与每个MenuItem关联,而不是一个命令对所有的命令。

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.

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

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.

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

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.

这篇关于GWT MenuBar上的getSelectedItem()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆