Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    TreeGrid context menu depending on node type

    Hello
    What is the best way to show a different context menu when the user right-clicks on different nodes of a TreeGrid?

    My tree:

    Code:
    Home
     |-Room 1
     --Room 2
          |-Chair
          --Table
    When right clicking on Home node, menu should show "Add new room", when clicked on Room node it should show "Add new furniture" etc.

    HomeNode, RoomNode, FurnitureNode are all subclasses from TreeNode.

    Thanks for answers

    #2
    Just to give some more details, here's my code. As you can see, I would like to react with a different context menu, with different actions depending on the node. How should I do this?

    Code:
    final Tree tree = new Tree();
    TreeNode home = new HomeNode("Home");
    tree.setRoot(home);
    tree.setShowRoot(true);
    
    final TreeGrid treeGrid = new TreeGrid();
    treeGrid.setData(tree);
    treeGrid.setShowRoot(true);
    
    RoomNode room1 = new RoomNode(1);
    tree.add(room1, home);
    RoomNode room2 = new RoomNode(2);
    tree.add(room2, home);
    
    tree.add(new ProductNode("Chair"), room2);
    tree.add(new ProductNode("Table"), room2);
    
    Menu contextMenu = new Menu();
    MenuItem menuAdd = new MenuItem("Add");
    contextMenu.addItem(menuAdd);
    
    menuAdd.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(MenuItemClickEvent event) {
        TreeNode node = (TreeNode) treeGrid.getSelectedRecord();
        if (node instanceof HomeNode) {
          RoomDialogSW dialogSW = new RoomDialogSW(tree, node);
          dialogSW.show();
        } else if (node instanceof RoomNode) {
          ProductDialog productDialog = new ProductDialog(tree, node);
          productDialog.show();
        } 
      }
    });
    
    treeGrid.setContextMenu(contextMenu);
    
    treeGrid.addCellContextClickHandler(new CellContextClickHandler() {
      public void onCellContextClick(CellContextClickEvent event) {
        if (event.getColNum() == 0) {
          treeGrid.getContextMenu().showContextMenu();
        }
      }
    });
    Currently I'm using a (terrible) instanceof ladder... Thanks for your input.

    Comment


      #3
      Well could add/remove menu items depending on the node before showing it or just have a set of different menus and show one depending on the node. About the same amount of work either way really, but a bit more reuse with the first.

      Comment


        #4
        I've had moderate luck using addCellOverHandler() to change the context menu as the user moves over the TreeGrid.
        The problem with that, however, is that if the user right clicks on an element and the context menu is being displayed the OnCellOver handler does not get invoked while the menu is still active. The user has to close the menu and then right click on a different tree node to get a different menu.

        I've been experimenting with CellMouseDownHandler but haven't figured out how to change the context menu. It always displays the default menu.

        Comment


          #5
          Originally posted by evanross
          I've been experimenting with CellMouseDownHandler but haven't figured out how to change the context menu. It always displays the default menu.
          As I'm also currently experimenting with different context menus for different tree node types, I realized that the MouseDownEvents and ClickEvents are only triggered for the left mouse button. So it is not possible to catch the click, change the context menu and then see the correct context menu.

          I wondered if not the menu has some sort of intern method that one can override. So it could first get the row of the tree that is currently selected and its record, check the type and then display the according MenuItems. Until now, I did not find a suiting method to override within the Menu class.

          Comment


            #6
            See getCellContextMenuItems, it's an override point.

            Comment

            Working...
            X