skip to main content
TGO > Programmers documentation > Getting started > Tutorial: getting started with Rogue Wave JViews TGO > Adding interactors
 
Adding interactors
Shows you how to configure a pop-up menu that is consistent across all the components. It also shows you how to add double-click behavior for network elements and how to show an overview window with a specific keystroke.
This part of the code is referred to as Step 8.
 
void step8() {
To do Step 8:
1. Create a pop-up menu factory to use throughout the application.
 
IlpPopupMenuFactory popupMenuFactory = new IlpAbstractPopupMenuFactory(){
The interface IlpPopupMenuFactory is used to create the pop-up menu. It is implemented by IlpAbstractPopupMenuFactory. The pop-up menu factory is invoked whenever an end user right-clicks (or clicks) in a view associated with pop-up menus by the system. The pop-up menu factory is expected to return a JPopupMenu appropriate to the context in which the end user right-clicked (or clicked) or null if no menu should be shown.
2. Add the identifier of each of the selected objects to the menu.
 
public JPopupMenu createPopupMenu (IlpObjectSelectionModel
                                   ilpSelectionModel)
{
The method createPopupMenu is redefined to display a contextual menu that takes into account the business objects that are currently selected in the component where the pop-up menu is invoked.
3. Create an empty pop-up menu.
 
JPopupMenu menu = new JPopupMenu();
The new pop-up menu, menu, is an instance of the standard Java™ pop-up menu.
4. Access the selected objects from the selection model.
 
Collection selectedObjects = ilpSelectionModel.getSelectedObjects();
if (!selectedObjects.isEmpty()) {
If the selectedObjects collection is empty, no objects are selected.
5. If any objects are selected, add the identifier of each object to the pop-up menu.
 
      Iterator i=selectedObjects.iterator();
      while (i.hasNext()) {
        menu.add(((IlpObject)i.next()).getIdentifier().toString());
      }
    } else {
      menu.add("Nothing selected");
    }
    return menu;
  }
};
The code iterates through the list of business objects ( IlpObject) getting the identifier of each object and adding it to the pop-up menu until no more objects are selected. Then it returns the completed pop-up menu.
In this example, the items added to the menu do nothing. In a real application, you would probably associate an implementation of the Swing Action interface with each menu item.
6. Set the pop-up menu factory as the interactor for all graphic components.
 
IlpViewInteractor networkInteractor = networkComponent.getViewInteractor();
networkInteractor.setPopupMenuFactory(popupMenuFactory);
tableComponent.getViewInteractor().setPopupMenuFactory(popupMenuFactory);
alarmTableComponent.getViewInteractor().setPopupMenuFactory(
                                                          popupMenuFactory);
treeComponent.getViewInteractor().setPopupMenuFactory(popupMenuFactory);
First, use the method IlpNetwork. getViewInteractor to get the current interactor for the network component. A network component can have multiple interactors, only one of which is active at any one time. All view interactors implement the IlpViewInteractor interface.
Then, use the reference to the interactor retrieved above to ask the interactor to use the pop-up menu factory created in step 1 of the current procedure.
Next, set the pop-up menu factory as the view interactor of the network element table, the alarm table, and the tree.
7. Show the equipment detail view by double-clicking a network element.
First, you create an action, doubleClickAction, to show the equipment detail view.
 
Action doubleClickAction = new AbstractAction("Show equipment details") {
  public void actionPerformed(ActionEvent e) {
    if (e instanceof IlpViewActionEvent) {
      IlpViewActionEvent viewEvent = (IlpViewActionEvent)e;
      IlpObject ilpObj = viewEvent.getIlpObject();
      if (ilpObj!=null) {
        equipmentDialog.setVisible(true);
        return;
      }
      log.info("Double-click action detected at " + viewEvent.getPosition()
               + ", not on IlpObject");
    } else
    log.info("Non-CPL action detected");
  }
};
The class IlpViewActionEvent is used to discover the context of the interaction that triggered the action. When an action is triggered by a view interactor that has recognized a gesture, the action event received by the action is a view action event.
Note that the equipment dialog created in Step 7, Showing equipment details, is set to be visible.
Then, you associate the action with a double-click gesture in the interactors of the various components.
 
networkInteractor.setGestureAction(IlpGesture.BUTTON1_DOUBLE_CLICKED,
                                   doubleClickAction);
tableComponent.getViewInteractor().setGestureAction(
                                   IlpGesture.BUTTON1_DOUBLE_CLICKED,
                                   doubleClickAction);
treeComponent.getViewInteractor().setGestureAction(
                                   IlpGesture.BUTTON1_DOUBLE_CLICKED,
                                   doubleClickAction);
The first parameter, BUTTON1_DOUBLE_CLICKED, identifies the gesture that should trigger the action. A gesture is a series of one or more graphic events triggered by the end user.
The class IlpGesture contains (as static fields) a number of gestures that are recognized by some or all interactors. Its subclasses contain additional gestures that are specific to individual interactors.
8. Show the overview window by pressing the key o.
First, you create an action, overviewAction, to show the overview window.
 
Action overviewAction = new AbstractAction("Show Overview") {
  public void actionPerformed (ActionEvent e) {
    networkComponent.setOverviewVisible(true);
  }
};
When this action is performed, the overview window becomes visible.
Then, associate the keystroke o with overviewAction.
 
networkInteractor.setKeyStrokeAction
                             (KeyStroke.getKeyStroke('o'),overviewAction);
The default network view interactor has the method setKeyStrokeAction, inherited from IlpAbstractInteractor to associate a keystroke with a specified action.
The sample should now look as shown in the following figure.

Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.