RCP e4 hide parts

I implemented an e4 RCP application and i want to hide parts for special users.

In e3 i know that i can do that with a perspective class that implements the IPerspectiveFactory.

My current sollution is:

@PostConstruct
private void init(EPartService partService) {
    MPart part = partService.findPart("part.id.activity");
    partService.hidePart(part, true);
}

I do this in the part class and when i run my application i get this injectionexcetion:

!ENTRY org.eclipse.e4.ui.workbench 4 0 2017-08-01 09:08:06.139
!MESSAGE Unable to create class ‘application.parts.activityPart.ActivityPart’ from bundle ‘135’
!STACK 0
org.eclipse.e4.core.di.InjectionException: Unable to process “ActivityPart#createComposite()”: no actual value was found for the argument “Composite”.
at org.eclipse.e4.core.internal.di.InjectorImpl.reportUnresolvedArgument(InjectorImpl.java:476)
at org.eclipse.e4.core.internal.di.InjectorImpl.processAnnotated(InjectorImpl.java:987)
at org.eclipse.e4.core.internal.di.InjectorImpl.inject(InjectorImpl.java:124)
at org.eclipse.e4.core.internal.di.InjectorImpl.internalMake(InjectorImpl.java:399)
at org.eclipse.e4.core.internal.di.InjectorImpl.make(InjectorImpl.java:318)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.make(ContextInjectionFactory.java:162)
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.createFromBundle(ReflectionContributionFactory.java:105)
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.doCreate(ReflectionContributionFactory.java:74)
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.create(ReflectionContributionFactory.java:56)
at org.eclipse.e4.ui.workbench.renderers.swt.ContributedPartRenderer.createWidget(ContributedPartRenderer.java:129)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createWidget(PartRenderingEngine.java:997)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:666)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:772)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$0(PartRenderingEngine.java:743)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$2.run(PartRenderingEngine.java:737)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java:721)
at org.eclipse.e4.ui.workbench.renderers.swt.StackRenderer.showTab(StackRenderer.java:1289)
at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer.lambda$0(LazyStackRenderer.java:68)
at org.eclipse.e4.ui.services.internal.events.UIEventHandler$1.run(UIEventHandler.java:40)
at org.eclipse.swt.widgets.Synchronizer.syncExec(Synchronizer.java:233)
at org.eclipse.swt.widgets.Display.syncExec(Display.java:4889)

Don’t try and hide a part during the initialization of the part.

If you know that you don’t want to show a part during the application start up you can just turn off its ‘To be rendered’ flag in your LifeCycle class.

For example:

@ProcessAdditions
public void processAdditions(MApplication app, EModelService modelService)
{
  MUIElement el = modelService.find("part.id.activity", app);
  if (el != null) {
    el.setToBeRendered(false);
  }
}