Search code examples
eclipse-plugineclipse-rcpe4

How to access an compatibility layer IViewPart’s (e4) MPart


I am porting an existing Eclipse plug-in to e4. From within a non-ported IViewPart I would like to access the view’s corresponding e4 MPart but couldn’t get the following to work reliably:

EPartService partService = (EPartService) PlatformUI.getWorkbench().getService(EPartService.class);
MPart part = partService.findPart(getSite().getId());

Placed in createPartControlComposite) I sometimes get an IllegalStateException (“Application does not have an active window“).


Solution

  • Avoid using the part service from the workbench as this only works when there is an active window.

    Instead use the part service for the current window (which might not be active). You can get this in the IViewPart using:

    EPartService partService = getSite().getService(EPartService.class);
    
    MPart part = partService.findPart(getSite().getId());