GWT阻止弹出面板打开

时间:2014-09-04 10:17:03

标签: java gwt uibinder

我有 MenuItem ,附加了 ScheduledCommand 。当用户单击菜单时,会出现一个启用了autoHide的新 PopupPanel 。现在,当用户在弹出窗口打开时单击 MenuItem 时,面板将关闭,但会立即再次打开,因为 PopupPanel 的关闭事件将作为单击事件触发菜单项。有人可以告诉我如何在这种情况下阻止PopupPanel打开?

我的代码是这样的:

@UiField
protected MenuItem menuItem;

....

    menuItem.setScheduledCommand(new ScheduledCommand() {
        @Override
        public void execute() {
            PopupPanel window = new PopupPanel();
            window.init();
            window.addCloseHandler(new CloseHandler<PopupPanel>() {
                @Override
                public void onClose(final CloseEvent<PopupPanel> event) {
                    // TODO Maybe something here?
                }
            });                
            window.show();
        }
    });

2 个答案:

答案 0 :(得分:0)

为弹出窗口创建单个实例,并使用PopupPanel#isShowing方法隐藏或显示弹出窗口。

public class MyMenuClass{

    private PopupPanel window;
    ....
    ....
    menuItem.setScheduledCommand(new ScheduledCommand() {
        @Override
        public void execute() {
             if(window==null){
                window = new PopupPanel(true);
                window.add(new Label("Hello close me!!!"));
            }
            if(window.isShowing()){
               window.hide();   
            }else{
                window.show();
            }
        }
}

答案 1 :(得分:0)

好的,我设法通过检查菜单栏上的最后一个悬停元素是否是打开窗口的menuItem来实现这一点。要做到这一点,我必须继承默认的 MenuBar 类并公开getSelectedItem()方法(它默认受保护,为什么?)

@UiField
MyMenuBar myMenuBar;

....

menuItem.setScheduledCommand(new ScheduledCommand() {
            @Override
            public void execute() {
                if (!wasHoveredWhenClosed) {
                    window.init();
                    window.addCloseHandler(new CloseHandler<PopupPanel>() {
                        @Override
                        public void onClose(final CloseEvent<PopupPanel> event) {
                            wasHoveredWhenClosed = myMenuBar.getSelectedItem() != menuItem;
                        }
                    });
                    window.show();
                } else {
                    wasHoveredWhenClosed = false;
                }
            }
        });