Search code examples
javatreeviewmouseeventcontextmenueclipse-scout

Eclipse Scout Tree View Event Handling


I'm currently writing a data visualization application with Eclipse Scout Framework. It is based on the Scout Project Template "Outline Tree and Table Form". What confuses me is the event handling in the Outline Tree. As you might know the different pages/nodes in the tree are automatically activated/created and displayed when clicking on the nodes without any custom implementation. I want to change this behaviour to the effect that a context menu opens when right-clicking on a node to delete it in a second step. For that reason I've overwritten the "execNodeClick()" method in the StandardOutline to look like this:

 @Override
  protected void execNodeClick(ITreeNode node, MouseButton mouseButton) throws ProcessingException {
    if (mouseButton == MouseButton.Right && node instanceof ConnectionNodePage) {
      ConnectionNodePage clickedConnectionNode = (ConnectionNodePage) node;
      logger.debug("Right click on ConnectionNode " + node);
      List<AbstractMenu> menuList = new ArrayList<>();
      menuList.add(new AbstractMenu() {
        @Override
        protected String getConfiguredText() {
          // TODO Auto-generated method stub
          return "delete";
        }
        @Override
        protected void execAction() throws ProcessingException {
          ServerConfigService serverConfigService = SERVICES.getService(ServerConfigService.class);
          serverConfigService.removeServerConnection(clickedConnectionNode.getConnection());
          StandardOutline.this.removeChildNode(StandardOutline.this.getRootNode(), clickedConnectionNode);
        }
      });
      clickedConnectionNode.setMenus(menuList);
    }
  }

I don't know whether this is the recommended way to dynamically add a context menu to a Tree Node, but It works somehow :P However, there are serveral problems with this approach:

  1. For some reason the nodes must be clicked/activated before (next time clicking) the context menus are opened.
  2. You can see in the RAP client that empty context menus are also opened for pages that don't fulfill the condition "node instanceof ConnectionNodePage", although logging/debugging shows that condition works fine. My assumption is that the Scout engine finds the anonymous inner menu class and does something unpredictable with it. In the SWT client you don't see context menus for wrong pages.
  3. I suspect my Event Handling to complicate with the Scout internal event handling. I dont really know what kind of event handling Scout does by default when right-clicking on tree-nodes, but it definately does something I don't want it to do. So I'd like to disable any action on mouse right-click except my custom implementation above.

I would appreciate someone to show me how this mechanism works and where I have to place the corresponding changes or at least a hint where I have to look. Thanks in advance!


Solution

  • Shame on me! Why keeping things simple when they can be complicated..? -.-

    To answer my own question and perhaps to help others might missing the forest for the trees:

    Eclipse Scout offers a built in option to add context menus to pages/nodes without the need of implementing any own mouse event handling.

    You simply need to add an inner class extending AbtractMenu/AbstractExtensibleMenu to the page you want the context menu for. Scout computes this automatically to be opened as a right-click context menu to the corresponding node in the tree.

    For a minimal menu implementation you only need to override the execAction() method of AbstractMenu to perform actions after click and override the getConfiguredText() method to set the desired display text for the menu in your new menu class. This way you avoid the weird behaviour I found with my first approach.

    I hope this answer will save somebody the hours I wasted.