views:

631

answers:

1

I'm using a RichTextArea in a GWT app. I want to add a context menu to my RichTextArea:

public class MyRichTextArea extends RichTextArea implements HasContextMenuHandlers {
    public HandlerRegistration addContextMenuHandler(ContextMenuHandler h) {
        return addDomHandler(h, ContextMenuEvent.getType());
    }
}

(...)

myRichTextArea.addContextMenuHandler(new ContextMenuHandler() {
    public void onContextMenu(ContextMenuEvent event) {
        contextMenu.show();
    }
});

This works, however, the context menu only appears when I right-click on the border of the RichTextArea. If I right-click into the RichTextArea, e.g. on the contained text, the browser's default context menu is shown.

How can I display my own context menu?

A: 

I would go after a method that tells you that the rich text area has the focus, like hasfocus, or maybe better, an event listener (addFocusListener) to tell you when the focus is there on a mouse click for the right mouse button?

Does that make sense?

George Sisco
Well, the FocusEvent does not contain any information on how the widget got the focus. That is, I cannot distinguish if it got the focus because of a right or a left mouse click.
Bob
You didn't say you couldn't detect a right-click. You said your code was working on the border only. Here is a reference, but I think if you test for event.button==2 or event.button==3, you cover all your bases.http://unixpapa.com/js/mouse.htmlAnyway, if you check for either button 2 or 3 and it still doesn't work, that's when I would check for focus - after one of those two is clicked. Does that make sense?
George Sisco
Basically, I'm saying check for left click, then make sure your textarea has focus - then you would know you want to show a context menu, and if you have more than one, you should know which one (like if more than one html element has a context menu).Anyway, hope that makes sense, and that it helps you.
George Sisco