user964147
user964147

Reputation: 739

How to get active perspective name in Eclipse plugin development

I am very new to Eclipse plugin development. I want to get (access) the active perspective name using Java. How do I do that?

Upvotes: 6

Views: 7022

Answers (3)

Campa
Campa

Reputation: 4495

The new E4 way relies on the EModelService.

Simply as:

import javax.inject.Inject;
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.e4.ui.workbench.modeling.EModelService;

@Inject EModelService modelService;    
@Inject MWindow window;

MPerspective p =  modelService.getActivePerspective(window);
System.out.println("ACTIVE PERSPECTIVE: " + p.getLabel());

Upvotes: 1

katsharp
katsharp

Reputation: 2551

IWorkbench wb = PlatformUI.getWorkbench();

IWorkbenchWindow win = wb.getActiveWorkbenchWindow();

IWorkbenchPage page = win.getActivePage();

IPerspectiveDescriptor perspective = page.getPerspective();

String label = perspective.getLabel();

You can also access the description and the id of the perspective using the methods on IPerspectiveDescriptor.

Upvotes: 15

Heinrich
Heinrich

Reputation: 408

Have a look at "Using Perspectives in the Eclipse UI".

If you have access to an object of type IWorkbenchWindow:

window.getActivePage().getPerspective().getLabel()

Upvotes: 1

Related Questions