2011-10-28 27 views
0

我是GWT新手,所以请帮助我。我在GWTMenuBar上遇到问题,请登录getSelectedItem()GWT上的getSelectedItem()MenuBar

这里是我的代码:

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

其他人可能会说这是一个没有用的职位,但我真的不知道如何计算出来。请帮帮我。提前致谢。

回答

1

这是一个可能适合你的版本。诀窍是,一个特定的命令需要与每个菜单项,为所有这些不是一个命令相关:

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 to Strings to Commands,然后从Map中拉出右边的Command以关联到为该String命名的每个MenuItem。

+0

谢谢你回答这个问题。如果你自己做而不考虑我的设计,你会如何实现它? – wens

0

而不必这个类实现Command,并且具有所有的菜单项的使用this当作它们的参数,当他们与.addItem()增加,做出不同的Command对象为每个菜单项。然后,将每个菜单项的特定逻辑编码到单独的命令对象中。

即:

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

如果你真的要处理来自同一段代码每一个菜单项,然后你可以做一个厂,是以菜单项的名称,并产生一个Command对象,可以参考它。