Graphic Components > Tree Component > Tree Component Services > 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:

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) {
  // ILOG 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 nonselected 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.