HOWTO handle right click on CellTable Header ?

366 views
Skip to first unread message

Christophe

unread,
Sep 15, 2011, 2:35:02 AM9/15/11
to Google Web Toolkit, christoph...@contactoffice.net
Hi,

I want to handle right-click on CellTable Header to display a context-
menu, but I can not find any way to do this.
Any help would be appreciated.

Thanks in advance,
Christophe

Thomas Broyer

unread,
Sep 15, 2011, 6:51:40 AM9/15/11
to google-we...@googlegroups.com, christoph...@contactoffice.net
Not tested but you should be able to do that by providing a Cell to the header that listens to the "contextmenu" event (and possibly the "click" event, you'd have to try on each browser), so that its onBrowserEvent will be called back when such an event is fired on the header's cell.
In other words, you have to do it at the Cell level (but it's relatively easy to wrap any Cell into one that additionally listens to and handles "contextmenu" events)

Jeff Larsen

unread,
Sep 15, 2011, 6:58:36 AM9/15/11
to google-we...@googlegroups.com, christoph...@contactoffice.net
if contextmenu isn't sunk by all browsers ( I haven't tested EVERYWHERE, but it does where chrome, ie8, 9, firefox) you can always grab the native event, check for the click event then nativeEvent.getButton() == NativeEvent.BUTTON_RIGHT

Christophe

unread,
Sep 15, 2011, 10:14:24 AM9/15/11
to Google Web Toolkit
I was on this, but it appears I never enter in my test. When I right-
clik on the header, it opens the standard browser context-menu.
Below my code :

private class RightClickableHeader extends SafeHtmlHeader {
public RightClickableHeader(SafeHtml headerHtml, String
columnId, String datatype) {
super(headerHtml);
sinkEvents(Event.ONMOUSEDOWN | Event.ONMOUSEUP |
Event.ONCONTEXTMENU);
}
@Override
public void onBrowserEvent(Context context, Element elem,
final NativeEvent event) {
if((event.getButton() & NativeEvent.BUTTON_RIGHT) >0) {
Window.alert("Right click!");
} else {
super.onBrowserEvent(context, elem, event);

Thomas Broyer

unread,
Sep 15, 2011, 11:23:10 AM9/15/11
to google-we...@googlegroups.com
Where is this sinkEvents from?

class SafeHtmlCellWithContextMenu<T> extends SafeHtmlCell {
  public Set<String> getConsumedEvents() { return Collections.singleton("contextmenu"); }

  public void onBrowserEvent(Cell.Context context, Element parent, SafeHtml value, NativeEvent event, ValueUpdater<SafeHtml> valueUpdater) {
    // here, event.getType() should be "contextmenu"
  }
}

Then use with: new Header(new SafeHtmlCellWithContextMenu());

(best IMO would be to turn the above into a generic Cell that can wrap any other Cell, delegating everything to the wrapped cell, except for getConsumedEvents to add "contextmenu" to the list, and onBrowserEvent to handle the "contextmenu")

Christophe

unread,
Sep 16, 2011, 4:35:50 AM9/16/11
to Google Web Toolkit
Ok, that's work. I can display my popup menu. But it doesn't prevent
default browser popup menu :
http://xemelios.org/private/many-menus.png

To stop propagation, I've tried this without any success :
public void onBrowserEvent(Cell.Context context, final Element
parent, SafeHtml value, final NativeEvent event,
ValueUpdater<SafeHtml> valueUpdater) {
event.stopPropagation();
...

Any idea ?

Thanks in advance,
Christophe

redjhawk

unread,
Sep 16, 2011, 6:35:41 AM9/16/11
to Google Web Toolkit
Have you tried with event.preventDefault() ?

On Sep 16, 10:35 am, Christophe

Christophe

unread,
Sep 16, 2011, 9:08:10 AM9/16/11
to Google Web Toolkit
Great !!!!

Thanks a lot, Thomas, for your help.
I wish you a nice week-end.

Christophe

On 16 sep, 12:35, redjhawk <jorges...@gmail.com> wrote:
> Have you tried with event.preventDefault() ?
>
> On Sep 16, 10:35 am, Christophe
>
> <christophe.march...@contactoffice.net> wrote:
> > Ok, that's work. I can display my popup menu. But it doesn't prevent
> > default browser popup menu :http://xemelios.org/private/many-menus.png
>
> > To stop propagation, I've tried this without any success :
> >         public void onBrowserEvent(Cell.Context context, final Element
> > parent, SafeHtml value, final NativeEvent event,
> > ValueUpdater<SafeHtml> valueUpdater) {
> >             event.stopPropagation();
> > ...
>
> > Any idea ?
>
> > Thanks in advance,
> > Christophe
>
> > On 15 sep, 17:23, Thomas Broyer <t.bro...@gmail.com> wrote:
>
> > > Where is this sinkEvents from?
>
> > > class SafeHtmlCellWithContextMenu<T> extends SafeHtmlCell {
> > >   public Set<String> getConsumedEvents() { return
> > > Collections.singleton("contextmenu"); }
>
> > >   public void onBrowserEvent(Cell.Context context, Element parent, SafeHtml
> > > value, NativeEvent event, ValueUpdater<SafeHtml> valueUpdater) {
> > >     // here, event.getType() should be "contextmenu"
> > >   }
>
> > > }
>
> > > Then use with: newHeader(new SafeHtmlCellWithContextMenu());
Reply all
Reply to author
Forward
0 new messages