Search code examples
javaeventsgwtflextable

Pure GWT, events converting


So, I have a ‘FlexTable’ in GWT and I need to drop down list of actions on right click on some cell. On left mouse click to retrieve ‘rowIndex’ of my cell I simply use ClickEvent method ‘getCellForEvent(event).getRowIndex()’. But there is no right click handler in pure GWT. So I decided to use ContextMenuHandler which requires ContextMenuEvent. And, of course, I can’t put ContextMenuEvent into ClickEvent method ‘getCellForEvent’. Is there any solution to this case? Or maybe someone knows easier way to drop down list on rightClick on ‘FlexTable’.


Solution

  • If you use a CellTable or DataGrid Widget, you can handle the CellPreviewEvent that gets fired when a cell is clicked, and that can also tell you which row/cell the event happened in. Here is some code:

    private void makeItemRightClickListener()
    {
        grid.addCellPreviewHandler(new CellPreviewEvent.Handler<T>() {
            @Override
            public void onCellPreview(CellPreviewEvent<T> event)
            {
                if (event.getNativeEvent().getButton() !=
                            NativeEvent.BUTTON_RIGHT)
                    return;
    
                // Prevent browser's own context menu from appearing
                event.getNativeEvent().preventDefault();
                event.getNativeEvent().stopPropagation();
    
                handleItemRightClickEvent(event);
            }
        });
    }
    
    private void handleItemRightClickEvent(CellPreviewEvent<T> event)
    {
        NativeEvent nativeClickEvent = event.getNativeEvent();
        // Get the data (type T) that is being displayed in the cell
        // by the CellTable or DataGrid widget.
        T rowClicked = event.getValue();
    
        // Create PopupPanel for menu
        PopupPanel popup = ...
    
        // Show the popup menu at the click position
        UIObject target = new MousePointUIObject(nativelickEvent);
        popup.showRelativeTo(target);
    }
    
    private static class MousePointUIObject extends UIObject
    {
        private NativeEvent mouseEvent;
    
        public MousePointUIObject(NativeEvent mouseEvent)
        {
            this.mouseEvent = mouseEvent;
        }
    
        @Override
        public int getAbsoluteLeft()
        {
            return mouseEvent.getClientX() + Window.getScrollLeft();
        }
    
        @Override
        public int getAbsoluteTop()
        {
            return mouseEvent.getClientY() + Window.getScrollTop();
        }
    
        @Override
        public int getOffsetWidth()
        {
            return 0;
        }
    
        @Override
        public int getOffsetHeight()
        {
            return 0;
        }
    }