Search code examples
eclipseeclipse-rcpe4

eclipse e4 - How are eclipse e4 views (parts) uniquely identified?


I am using PartDescriptors to create multiple instances of the same MPart. How do I identify each part ? I eclipse rcp 3.x it is easy to find a ViewPart by its primary and Seconday id. How do I achieve this in e4 rcp programming model ?


Solution

  • All the parts created from the descriptor will have the same id so it is up to you to add some additional identifying information.

    One way to do this is to add your own unique data to the part's transient data when you create the part:

    Object uniqueId = .....
    
    part.getTransientData().put("part id key", uniqueId);
    

    And read the id with:

    part.getTransientData().get("part id key");
    

    To find the part you can use one of the findElements methods of EModelService. The version with a Selector lets you code a match on the id and the data. For example if everything is in one part stack:

    MPartStack partStack = .... get part stack
    
    List<MPart> parts = modelService.findElements(partStack, MPart.class, EModelService.ANYWHERE, new FindPartSelector(id, data));
    

    where FindPartSelector is a selector you write. This just has one method:

    @Override
    public boolean select(final MApplicationElement element)
    {
      // TODO match id and transient data
    }
    

    Note that the transient data is lost when you exit your RCP. If these parts are being persisted between sessions you can use getPersistedState rather than getTransientData.