Home | History | Annotate | Download | only in ui
      1 package autotest.common.ui;
      2 
      3 import com.google.gwt.event.logical.shared.CloseHandler;
      4 import com.google.gwt.user.client.Command;
      5 import com.google.gwt.user.client.Window;
      6 import com.google.gwt.user.client.ui.MenuBar;
      7 import com.google.gwt.user.client.ui.MenuItem;
      8 import com.google.gwt.user.client.ui.PopupPanel;
      9 
     10 
     11 
     12 public class ContextMenu {
     13     private  PopupPanel popup = new PopupPanel(true);
     14     private AutoHideMenu menu = new AutoHideMenu();
     15     private boolean enabled = true;
     16 
     17     private class CommandWrapper implements Command {
     18         private Command command;
     19 
     20         CommandWrapper(Command command) {
     21             this.command = command;
     22         }
     23 
     24         public void execute() {
     25             popup.hide();
     26             command.execute();
     27         }
     28     }
     29 
     30     private class AutoHideMenu extends MenuBar {
     31         public AutoHideMenu() {
     32             super(true);
     33         }
     34 
     35         @Override
     36         public MenuItem addItem(String text, Command cmd) {
     37             return super.addItem(text, new CommandWrapper(cmd));
     38         }
     39 
     40         public void setEnabled(boolean enabled) {
     41             for (MenuItem menuItem : getItems()) {
     42                 menuItem.setEnabled(enabled);
     43             }
     44         }
     45     }
     46 
     47     public ContextMenu() {
     48         menu.setAutoOpen(true);
     49         setUseHandCursor(true);
     50         popup.add(menu);
     51     }
     52 
     53     protected void setUseHandCursor(boolean enabled) {
     54         if (enabled)
     55             menu.addStyleName("menubar-hand-cursor");
     56         else
     57             menu.removeStyleName("menubar-hand-cursor");
     58     }
     59 
     60     public void addItem(String text, Command cmd) {
     61         menu.addItem(text, new CommandWrapper(cmd));
     62     }
     63 
     64     public MenuBar addSubMenuItem(String text) {
     65         MenuBar subMenu = new AutoHideMenu();
     66         menu.addItem(text, subMenu);
     67         return subMenu;
     68     }
     69 
     70     public void showAt(int left, int top) {
     71         popup.setPopupPosition(left, top);
     72         popup.show();
     73     }
     74 
     75     public void showAtWindow(int left, int top) {
     76         showAt(left + Window.getScrollLeft(), top + Window.getScrollTop());
     77     }
     78 
     79     public void addCloseHandler(CloseHandler<PopupPanel> closeHandler) {
     80         popup.addCloseHandler(closeHandler);
     81     }
     82 
     83     public boolean isEnabled() {
     84         return enabled;
     85     }
     86 
     87     public void setEnabled(boolean enabled) {
     88         this.enabled = enabled;
     89         menu.setEnabled(enabled);
     90         setUseHandCursor(enabled);
     91     }
     92 }
     93