How to add perspective listeners

旧城冷巷雨未停 提交于 2019-12-10 10:13:21

问题


In our application, we have 4 views (View 1, 2, 3 & 4) and 3 perspectives (perspective 1, 2 and 3).

The perspectives are built by implementing IPerspectivefactory and views are built by extending viewPart.

We are facing a situation where we want to set the focus to a particular view in case of any of the perspective is selected.

For ex, Consider View 1 as a common view which will be displayed under all the 3 perspectives. We want to set focus to View1 whenever we select any perspective.

How to add listeners to the perspectives?

We know that we have to add IPerspectiveListerner to listen to the perspective changes but we are not sure where to add it so that we get the execution control inside the methods which we have implemented under IPerspectiveListener.


回答1:


You can use the following code:

PlatformUI.getWorkbench().getActiveWorkbenchWindow().addPerspectiveListener(new IPerspectiveListener() {
  @Override
  public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective, String changeId) {
    try {
      page.showView("...", null, IWorkbenchPage.VIEW_VISIBLE);
    } catch (PartInitException ex) {
      ex.printStackTrace();
    }
  }

  @Override
  public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
  }
});

EDIT: since this is tied to view1 you could place this code in the Activator for the view1 plugin. Of course it won't show up until some code is loaded from the view1 plugin. Something like:

Job job = new UIJob("Add Perspective listener") {
    public IStatus runInUIThread(IProgressMonitor monitor) {
        addPerspectiveListener();
        return Status.OK_STATUS;
    }
};
job.setSystem(true);
job.schedule();


来源:https://stackoverflow.com/questions/9412448/how-to-add-perspective-listeners

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!