Time for action – adding context menus

A context menu can be added to the TimeZoneTableView class and respond to it dynamically in the view's creation. The typical pattern for Eclipse 3 applications is to create a hookContextMenu() method, which is used to wire up the context menu operation with displaying the menu. A default implementation can be seen by creating an example view, or one can be created from first principles.

Eclipse menus are managed by a MenuManager. This is a specialized subclass of a more general ContributionManager, which looks after a dynamic set of contributions that can be made from other sources. When the menu manager is connected to a control, it responds in the standard ways for the platform for showing the menu (typically a context-sensitive click or short key). Menus can also be displayed in other locations, such as a view's or the workspace's coolbar (toolbar). The same MenuManager approach works in these different locations.

  1. Open the TimeZoneTableView class and go to the createPartControl() method.
  2. At the bottom of the method, add a new MenuManager with the ID #PopupMenu and associate it to the viewer's control.
    MenuManager manager = new MenuManager("#PopupMenu");
    Menu menu = manager.createContextMenu(tableViewer.getControl());
    tableViewer.getControl().setMenu(menu);
  3. If the Menu is empty, the MenuManager won't show any content, so this currently has no effect. To demonstrate this, an Action will be added to the Menu. An Action has text (for rendering in the pop-up menu, or the menu at the top of the screen), as well as a state (enabled/disabled, selected) and a behavior. These are typically created as subclasses and (although the Action doesn't strictly require it) an implementation of the run() method. Add this to the bottom of the createPartControl() method.
    Action deprecated = new Action() {
      public void run() {
        MessageDialog.openInformation(null, "Hello", "World");
      }
    };
    deprecated.setText("Hello");
    manager.add(deprecated);
  4. Run the Eclipse instance, open the Time Zone Table View, and right-click on the table. The Hello menu can be seen, and when selected, an informational dialog is shown.

What just happened?

The MenuManager (with the id #PopupMenu) was bound to the control, which means when that particular control's context sensitive menu is invoked, the manager will be able to ask to display a menu. The manager is associated with a single Menu object (which is also stamped on the underlying control itself) and is responsible for updating the status of the menu.

Note

Actions are deprecated. They are included here since examples on the Internet may have preferred references to them, but it's important to note that while they still work, the way of building user interfaces are with the commands and handlers, shown in the next section.

When the menu is shown, the actions that the menu contains are rendered in the order in which they are added. Action are usually subclasses that implement a run() method, which performs a certain operation, and have text which is displayed.

Action instances also have other metadata, such as whether they are enabled or disabled. Although it is tempting to override the accessor methods, this behavior doesn't work—the setters cause an event to be sent out to registered listeners, which causes side effects, such as updating any displayed controls.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset