So you used internal API


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).

About these ads

4 Responses to “So you used internal API”

  1. 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”;

  2. 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 :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 708 other followers

%d bloggers like this: