views:

43

answers:

1

I'm trying to capture right-clicks on a widget, to popup my own context menu instead of the browser's. There are a couple references on this, but the most popular one here is a little dated, although some of the comments contain more recent code snippets.

I've pieced together bits and I've got it working in Chrome and FF but not IE. In IE it doesn't display the default browser context menu, but it doesn't display my menu. I'm just getting into GWT so I'm assuming I'm not doing something right with the right kinds of handlers or events. I'm also using the gwt-graphics module, that's where the Rectangle class that I'm extending comes from, in case that's relevant.

Here's my code:

public class RectangleRightClickable extends Rectangle {

public RectangleRightClickable(int x, int y, int width, int height) {
    super(x, y, width, height);
    sinkEvents(Event.ONCONTEXTMENU);
}

public void onBrowserEvent(Event event) {
    GWT.log("onBrowserEvent");
    event.stopPropagation();
    event.preventDefault();
    GWT.log("event type : " + DOM.eventGetType(event));
    switch(DOM.eventGetType(event)) {
    case Event.ONCONTEXTMENU:
        if (DOM.eventGetButton(event) == Event.BUTTON_RIGHT) {
            GWT.log("Event.BUTTON_RIGHT", null);
            showMenu();
        }
        break;
    default:
        GWT.log(event.toString());
        break;
    }
}   

protected void showMenu() {
    final RectangleRightClickable parent = this;
    final PopupMenu popMenu = new PopupMenu();
    popMenu.addMenuItem(new Label("Add thing"));
    popMenu.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
        public void setPosition(int offsetWidth, int offsetHeight) {
            int left = parent.getX() + parent.getWidth();
            int top = parent.getY() + parent.getWidth();
            popMenu.setPopupPosition(left, top);
        }
    });

}
}
A: 

Got this response on the GWT google groups list, which worked:

addDomHandler(new ContextMenuHandler() 
{ 
    @Override 
    public void onContextMenu(ContextMenuEvent event) 
    { 
        showMenu(); 
        event.preventDefault(); 
    } 
}, ContextMenuEvent.getType());