How to load data into TableView of one MPart, by selecting one child of TreeViewer from other Mpart

201 views Asked by At

I have simple requirement : My application contain 2 MParts. One MPart contains list of IDs displayed in TreeView from Database. By clicking on any ID, another MPart should populate/display data related to slected ID (from DB) in TableView.

I written, 1st MPart and it is displaying IDs in TreeView. On click capturing user selected ID and retrieved data related to that ID from DB. Using EPartService, i got reference of 2nd MPart. But not able to modify its TableView. (initially, i populated this TableView with empty rows. default). Basically I am trying to modify 2nd Mpart from 1st Mpart.

I am beginner, tried all related posts available in the net. Please point me right direction

2

There are 2 answers

2
christoph.keimel On

In the following answer I will asume that you are talking about JFace TreeViewer and TableViewer. If you are talking about JavaFX TreeView and TableView you will need to use a different way of capturing the selection event, but the usage of the ESelectionService remains the same.

Setting the Selection

In the part with the tree you can use dependency injection to get the ESelectionService

@Inject private ESelectionService selectionService;

You can then add a ISelectionChangedListener to the TreeViewer and forward the selection to the selection service.

ISelectionChangedListener selectionListener = new ISelectionChangedListener() {
    @Override
    public void selectionChanged(SelectionChangedEvent event) {
        IStructuredSelection selection = (IStructuredSelection) event.getSelection();
        selectionService.setSelection(selection);
    }
};
treeView.addSelectionChangedListener(selectionListener);

Instead of pushing the IStructuredSelection to the context you could also use your ID directly, i.e. by using selection.getFirstElement().

Observing the Selection

In the part with the table showing the details you can have the current selection injected. This method will be called everytime the selection changes.

@Inject
public void setSelection(@Named(IServiceConstants.ACTIVE_SELECTION) @Optional Object selection) {
    // process the selection
}

Note the use of @Optional here. This will ensure that the method is also called when the active selection is null.

More Information

For more information on the ESelectionService please refer to this tutorial: http://eclipsesource.com/blogs/tutorials/eclipse-4-e4-tutorial-part-7-services/

4
greg-449 On

When you have the MPart for the table view you can call getObject() to get your view class:

MPart tableViewPart = ....

MyTableView view = (MyTableView)tableViewPart.getObject();

// TODO call some method of MyTableView to set the table

where MyTableView is the class you defined for the view part in the Application.e4xmi.

Note: If the part has not yet been shown the object will be null. Use EPartService.showPart to show the part and create the object.