Search code examples
javaeclipse-rcprcp

Stop Parts from being detachable


I would like to stop Parts from being detachable in Eclipse RCP 4.

Currently if I right click on a Part I can select detach. From this I can then close the tab even if the Part isn't Closeable. In RCP 3 when a detached View is closed it would then return back to its original location but now it closes completely.

Structure

enter image description here

Part Config

enter image description here

How can I remove the option to detach a Part?

Also how can I stop a detached Part from being closed or make it return to its original location?


Solution

  • To stop parts being detachable I used a custom stack renderer and overrode the method to populate the tab menu.

    import java.util.Arrays;
    import java.util.Optional;
    
    import javax.annotation.PostConstruct;
    import javax.inject.Inject;
    
    import org.eclipse.e4.core.contexts.IEclipseContext;
    import org.eclipse.e4.ui.internal.workbench.renderers.swt.SWTRenderersMessages;
    import org.eclipse.e4.ui.model.application.ui.basic.MPart;
    import org.eclipse.e4.ui.workbench.renderers.swt.StackRenderer;
    import org.eclipse.swt.widgets.Menu;
    import org.eclipse.swt.widgets.MenuItem;
    
    public class UndetachableStackRenderer extends StackRenderer
    {
        @Inject 
        private IEclipseContext context;
    
        @PostConstruct
        public void init() 
        {
            super.init(context);
        }
    
        @Override 
        protected void populateTabMenu(final Menu menu, final MPart part)
        {
            super.populateTabMenu(menu, part);
    
            final MenuItem[] menuItems = menu.getItems();
    
            final Optional<MenuItem> detachMenuItem = Arrays.stream(menuItems).filter(item -> item.getText().equals(SWTRenderersMessages.menuDetach)).findFirst();
    
            if(detachMenuItem.isPresent())
            {
                detachMenuItem.get().dispose();
            }
        }
    }
    

    I then added a Persisted State into the Part Stacks that I didn't want to show the Detach option.

    If anybody is having trouble setting a custom stack renderer then my question here might help.