Providing a Context Menu

Enhancing the Tool Behavior Provider

Context menus are defined in the tool behavior provider.

If you didn’t do so already you must first create a tool behavior provider and add it to the diagram type provider as described here.

There is one method of the tool behavior provider to overwrite:

The functionality of the context menu entries is always provided by features.

It is possible to create a context menu entry with “children”, which build a group of other context menu entries. Such a group can be shown in the context menu either as a sub-menu or as a flat list separated by separator-lines. This behaviour can be switched with a simple flag. You can also use the DynamicContextMenuEntry, which will automatically create a sub-menu, if there are more than a predefined number of children, and a flat list otherwise.

In this example we want to create one context menu entry, which offers all available custom features as menu entries in a new sub-menu. 

You can see the complete implementation of the context menu here:

 

@Override
public IContextMenuEntry[] getContextMenu(ICustomContext context) {
    // create a sub-menu for all custom features
    ContextMenuEntry subMenu = new ContextMenuEntry(null, context);
    subMenu.setText("Custom");
    subMenu.setDescription("Custom features submenu");
    // display sub-menu hierarchical or flat
    subMenu.setSubmenu(true);

    // create a menu-entry in the sub-menu for each custom feature
    ICustomFeature[] customFeatures = getFeatureProvider().getCustomFeatures(context);
    for (int i = 0; i < customFeatures.length; i++) {
         ICustomFeature customFeature = customFeatures[i];
         if (customFeature.isAvailable(context)) {
             ContextMenuEntry menuEntry = new ContextMenuEntry(customFeature, context);
             subMenu.add(menuEntry);
         }
     }

     IContextMenuEntry ret[] = new IContextMenuEntry[] { subMenu };
     return ret;
}

 

Test: Create a Connection by Drag & Drop from a Context Button

Note that previously we implemented one "Rename EClass" feature which allows renaming a EClass in a popup dialog.

Now start the editor and test this new context menu:

  1. Create or open a diagram and create a EClass in the diagram.
  2. Open the context-menu on the diagram, and verify that the "Rename EClass" feature is available but disabled in the custom sub-menu.
  3. Open the context-menu on the EClass, and verify that the "Rename EClass" feature is available and enabled in the custom sub-menu.

Next you may change the hierarchical sub-menu to a flat sub-menu (change the coding to subMenu.setSubmenu(false)). Start the editor and repeat the test to view the difference in the context-menu.