Inspired by a question that was asked twiced on the newsgroup about internal “API” that is missing in Eclipse 4.x although the feature is still there when interacting with the IDE
The API in question was:
… I needed an ability to allow a user to, upon request, launch a view either attached as a tab in my main window or else detached in its own window … In 3.7, I was able to meet this goal by using the internal APIs to make a call like
((WorkbenchPage)getSite().getPage()). getActivePerspective().getPresentation().detachPart(ref);
So somehow the IDE has preserved this feature but the internal API is gone. The reason is that detaching is now part of the low level Eclipse 4 Application Platform (EAP) which means the Eclipse 4 Workbench has no idea of this feature, hence the internal API is gone.
As outlined many times before the EAP does everything through services. The 2 most important ones are:
- EPartService
- EModelService
if you inspect them you find out that EModelService has a method named detach(MPartSashContainerElement mPartSashContainerElement, int x, int y,int width, int height)
which sounds very promosing.
So now we somehow have to get access to the EModelService
and Model-Element (MPart
) which represents the ViewPart which if you know it is quite easy because you can access them through getSite().getService(...)
.
So if we want to detach a ViewPart by clicking on a button within the view the code looks like this:
public class SampleView extends ViewPart { public static final String ID = "test.detach.views.SampleView"; private Button b; public void createPartControl(Composite parent) { parent.setLayout(new GridLayout()); b = new Button(parent, SWT.PUSH); b.setText("Detach me"); b.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { EModelService s = (EModelService) getSite().getService(EModelService.class); MPartSashContainerElement p = (MPart) getSite().getService(MPart.class); if (p.getCurSharedRef() != null) p = p.getCurSharedRef(); s.detach(p, 100, 100, 300, 300); } }); } public void setFocus() { b.setFocus(); } }
The only mystery here is the call to getCurSharedRef() which is needed in eclipse because views are shared between multiple areas (read perspectives).
What is the point of the line below? It seems that it is never referenced in the rest of the code. You never pass which viewpart you wanted to detach.
public static final String ID = “test.detach.views.SampleView”;
It got generated by the plugin wizard
I want to split my editor area to show two editors programatically. I can see some links that share the code to do it but they do not work in Eclipse Kepler, because the internal API’s have been hidden, as you have mentioned. So Can you please help me achieve my need. I am not able to do it.
Thanks in advance 🙂
Please ask at a newsgroup
MUIElement relToElement = null;
final EModelService service = PlatformUI.getWorkbench().getService(EModelService.class);
final MPart dragElement = ((PartSite) workbenchPage.getActiveEditor().getSite()).getModel();
relToElement = getRelativeElement(dragElement, service);
MPartSashContainerElement toInsert = (MPartSashContainerElement) dragElement;
final MStackElement stackElement = (MStackElement) dragElement;
final MPartStack newStack = BasicFactoryImpl.eINSTANCE.createPartStack();
newStack.getChildren().add(stackElement);
newStack.setSelectedElement(stackElement);
toInsert = newStack;
service.insert(toInsert, (MPartSashContainerElement) relToElement, verticalSplit ? 3 : 1, 0.5f);