• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CommandWrapper(Command command)20         CommandWrapper(Command command) {
21             this.command = command;
22         }
23 
execute()24         public void execute() {
25             popup.hide();
26             command.execute();
27         }
28     }
29 
30     private class AutoHideMenu extends MenuBar {
AutoHideMenu()31         public AutoHideMenu() {
32             super(true);
33         }
34 
addItem(String text, Command cmd)35         public MenuItem addItem(String text, Command cmd) {
36             return super.addItem(text, new CommandWrapper(cmd));
37         }
38 
setEnabled(boolean enabled)39         public void setEnabled(boolean enabled) {
40             for (MenuItem menuItem : getItems()) {
41                 menuItem.setEnabled(enabled);
42             }
43         }
44     }
45 
ContextMenu()46     public ContextMenu() {
47         menu.setAutoOpen(true);
48         setUseHandCursor(true);
49         popup.add(menu);
50     }
51 
setUseHandCursor(boolean enabled)52     protected void setUseHandCursor(boolean enabled) {
53         if (enabled)
54             menu.addStyleName("menubar-hand-cursor");
55         else
56             menu.removeStyleName("menubar-hand-cursor");
57     }
58 
addItem(String text, Command cmd)59     public void addItem(String text, Command cmd) {
60         menu.addItem(text, new CommandWrapper(cmd));
61     }
62 
addSubMenuItem(String text)63     public MenuBar addSubMenuItem(String text) {
64         MenuBar subMenu = new AutoHideMenu();
65         menu.addItem(text, subMenu);
66         return subMenu;
67     }
68 
showAt(int left, int top)69     public void showAt(int left, int top) {
70         popup.setPopupPosition(left, top);
71         popup.show();
72     }
73 
showAtWindow(int left, int top)74     public void showAtWindow(int left, int top) {
75         showAt(left + Window.getScrollLeft(), top + Window.getScrollTop());
76     }
77 
addCloseHandler(CloseHandler<PopupPanel> closeHandler)78     public void addCloseHandler(CloseHandler<PopupPanel> closeHandler) {
79         popup.addCloseHandler(closeHandler);
80     }
81 
isEnabled()82     public boolean isEnabled() {
83         return enabled;
84     }
85 
setEnabled(boolean enabled)86     public void setEnabled(boolean enabled) {
87         this.enabled = enabled;
88         menu.setEnabled(enabled);
89         setUseHandCursor(enabled);
90     }
91 }
92