Search code examples
gwttreecell

GWT Cell Tree Right Click Selection


So I have created a CellTree and what I want to do is select the cell that receives a right click so that when I open my context menu to do things, I will know what cell I am working with. Maybe I am going about it the wrong way, I can override the onBrowserEvent method and detect when someone right clicks on the tree but I can't figure out which cell is being clicked so I can manually select it. Has anyone found a solution for this problem?


Solution

  • I found a solution, I hope this helps others as I have been searching for this for a long time. There might be a better way, but here is how I accomplished the functionality that I desired:

    In the cells that I used inside my tree, I did an override on the onbrowserevent to catch the mouse events and set the selection model. With abstract cells you can sink events you want it to listen to and in my case I chose mouse down.

    public class CustomContactCell extends AbstractCell<ContactInfo> {
    
        private SetSelectionModel<ContactInfo> selectionModel;
    
        public CustomContactCell(SetSelectionModel<ContactInfo> selectionModel) {
            super("mousedown");
            this.selectionModel = selectionModel;
        }
    
        @Override
        public void render(Context context, ContactInfo value, SafeHtmlBuilder sb) {
            ...
        }
    
        @Override
        public void onBrowserEvent(com.google.gwt.cell.client.Cell.Context context, Element parent, ContactInfo value, NativeEvent event, ValueUpdater<ContactInfo> valueUpdater) {
            if (event.getButton() == NativeEvent.BUTTON_RIGHT) {
                if (selectionModel != null) {
                    selectionModel.clear();
                    selectionModel.setSelected(value, true);
                }
            }
            super.onBrowserEvent(context, parent, value, event, valueUpdater);
        }
    }