Search code examples
javaswteclipse-rcprcp

How to take active parts with EPartService?


I want to take active parts by using EPartService. Can I use EPartService for this?


Solution

  • If you want to get the active part in something like a command handler you can inject it as a parameter:

    @Execute
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart)
    {
      ... handler code
    }
    

    If you want a list of all currently displayed parts you can use the EPartService. Something like:

    @Inject
    MApplication app;
    
    @Inject
    EPartService partService;
    
    // Find all the `MPart` objects in the current presentation
    
    Collection<MPart> parts = partService.getParts();
    
    // Filter the list to include just the parts that are current being displayed (rendered)
    
    parts = parts.stream().filter(MPart::isToBeRendered).collect(Collectors.toList());
    

    Note: This code requires Java 8