Interacting with the tree view

The IlpTree allows you to associate behavior with the tree view as a whole, and with the business objects it contains.
In particular, using the default view interactor, you can:
  • associate actions with mouse events and focus events,
  • associate actions with keyboard events,
  • define a pop-up menu factory to build a pop-up menu that displays in the view.
The IlpTree is associated with an IlpDefaultViewInteractor, which should satisfy most needs. You can retrieve this interactor by calling the method getViewInteractor() .

How to associate an action with a mouse event in the tree view

You can associate actions with mouse events by using either CSS or the API. The following CSS extract shows how to proceed:
Tree {
  interactor: true;
} 

Interactor {
  viewInteractor: @+viewInt;
}

Subobject#viewInt {
  class: 'ilog.cpl.interactor.IlpDefaultViewInteractor';
  action[0]: @+viewAction0;
}

Subobject#viewAction0 {
  class: 'ilog.cpl.interactor.IlpGestureAction';
  gesture: BUTTON3_CLICKED;
  action: @+myAction;
}
Subobject#myAction {
  class: MyAction;
}
The same configuration can be achieved through the API, as follows:
IlpTree tree = // ...
// Retrieve the view interactor
IlpViewInteractor viewInteractor = tree.getViewInteractor();
// Create an action
Action myAction = new MyAction();
// Clicking the 3rd mouse button will trigger myAction
viewInteractor.setGestureAction(IlpGesture.BUTTON3_CLICKED,myAction);
You can find out whether this event occurred on an IlpObject by means of the following code (which should be in the MyAction class).

How to check whether a given action occurred

// Implementation of the ActionListener interface
public void actionPerformed(ActionEvent e) {
  // JTGO interactors use IlpViewActionEvent
  IlpViewActionEvent viewEvent = (IlpViewActionEvent)e;
  // Get the IlpObject (if any) where the interaction occurred
  IlpObject ilpObj = viewEvent.getIlpObject();
  // Perform operation on the given object
}

How to associate an action with a keyboard event in the tree view

You can associate actions with keyboard events by using either CSS or the API. The following CSS extract shows how to proceed:
Tree {
  interactor: true;
} 

Interactor {
  viewInteractor: @+viewInt;
}

Subobject#viewInt {
  class: 'ilog.cpl.interactor.IlpDefaultViewInteractor';
  action[0]: @+viewAction0;
}

Subobject#viewAction0 {
  class: 'ilog.cpl.interactor.IlpKeyStrokeAction';
  keyStroke: 'typed D';
  action: @+myAction;
}

Subobject#myAction {
  class: MyAction;
}
The same configuration can be achieved through the API, as follows:
// Create an action
Action myAction = new MyAction();
// Typing CTRL+D will trigger myAction
viewInteractor.setKeyStrokeAction(
  KeyStroke.getKeyStroke('D',java.awt.Event.CTRL_MASK),myAction);

How to define a pop-up menu factory for the tree view

You can customize a pop-up menu factory for the tree view either through CSS or through the API. The following CSS extract shows how to configure a CSS file to add a pop-up menu factory:
Tree {
  interactor: true;
} 

Interactor {
  viewInteractor: @+viewInt;
}

Subobject#viewInt {
  class: 'ilog.cpl.interactor.IlpDefaultViewInteractor';
  popupMenuFactory: @+viewPopupMenuFactory;
}
Subobject#viewPopupMenuFactory {
  class: MyPopupMenuFactory;
}
The same configuration can be achieved through the API, as follows:
// Subclass IlpAbstractPopupMenuFactory, which has useful shortcuts
IlpPopupMenuFactory popupMenuFactory = new IlpAbstractPopupMenuFactory(){
  // Add the identifier of each of the selected objects to the menu
  public JPopupMenu createPopupMenu (IlpObjectSelectionModel ilpSelectionModel)
  {
    // Create an empty popup menu
    JPopupMenu menu = new JPopupMenu();
    // Access the selected objects from the selection model
    Collection selectedObjects = ilpSelectionModel.getSelectedObjects();
    // fill the menu according to the current selection
   return menu;
  }
};
The following code shows you how to associate the defined pop-up menu factory with the tree component.

How to associate a pop-up menu factory with the tree component

// Set the popup menu factory to the view interactor
viewInteractor.setPopupMenuFactory(popupMenuFactory);
Note
The selection is updated prior to invoking the pop-up menu factory. Specifically, right-clicking a selected object keeps the entire current selection, while clicking a unselected object cancels the current selection and selects the object clicked. Clicking outside any object clears the selection.
Please refer to Interacting with the graphic components for a detailed description of interactors and gestures.