ContextMenu.java revision 5524adf7fc9d923ac3b6f7e7fa72158a5625460b
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 addItem(String text, Command cmd) {
46        menu.addItem(text, new CommandWrapper(cmd));
47    }
48
49    public MenuBar addSubMenuItem(String text) {
50        MenuBar subMenu = new AutoHideMenu();
51        menu.addItem(text, subMenu);
52        return subMenu;
53    }
54
55    public void showAt(int left, int top) {
56        popup.setPopupPosition(left, top);
57        popup.show();
58    }
59
60    public void showAtWindow(int left, int top) {
61        showAt(left + Window.getScrollLeft(), top + Window.getScrollTop());
62    }
63
64    public void addPopupListener(PopupListener popupListener) {
65        popup.addPopupListener(popupListener);
66    }
67
68}
69