ContextMenu.java revision a901ba9a627c31238bd1b674a532d2e860a1a348
1package autotest.common.ui;
2
3import com.google.gwt.user.client.Command;
4import com.google.gwt.user.client.Window;
5import com.google.gwt.user.client.ui.MenuBar;
6import com.google.gwt.user.client.ui.MenuItem;
7import com.google.gwt.user.client.ui.PopupListener;
8import com.google.gwt.user.client.ui.PopupPanel;
9
10
11
12public class ContextMenu {
13    private  PopupPanel popup = new PopupPanel(true);
14    private MenuBar menu = new AutoHideMenu();
15
16    private class CommandWrapper implements Command {
17        private Command command;
18
19        CommandWrapper(Command command) {
20            this.command = command;
21        }
22
23        public void execute() {
24            popup.hide();
25            command.execute();
26        }
27    }
28
29    private class AutoHideMenu extends MenuBar {
30        public AutoHideMenu() {
31            super(true);
32        }
33
34        @Override
35        public MenuItem addItem(String text, Command cmd) {
36            return super.addItem(text, new CommandWrapper(cmd));
37        }
38    }
39
40    public ContextMenu() {
41        menu.setAutoOpen(true);
42        popup.add(menu);
43    }
44
45    public void useHandCursor() {
46        menu.addStyleName("menubar-hand-cursor");
47    }
48
49    public void addItem(String text, Command cmd) {
50        menu.addItem(text, new CommandWrapper(cmd));
51    }
52
53    public MenuBar addSubMenuItem(String text) {
54        MenuBar subMenu = new AutoHideMenu();
55        menu.addItem(text, subMenu);
56        return subMenu;
57    }
58
59    public void showAt(int left, int top) {
60        popup.setPopupPosition(left, top);
61        popup.show();
62    }
63
64    public void showAtWindow(int left, int top) {
65        showAt(left + Window.getScrollLeft(), top + Window.getScrollTop());
66    }
67
68    public void addPopupListener(PopupListener popupListener) {
69        popup.addPopupListener(popupListener);
70    }
71
72}
73