以编程方式打开透视图 [英] Open Perspective programmatically

查看:76
本文介绍了以编程方式打开透视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提供一个命令/处理程序以切换到特定的视角。

I am trying to provide a command/ handler to switch to a specific Perspective.

我想到了以下课程:

public class OpenPerspectiveHandler {

    private static final Logger logger = Logger.getLogger(OpenPerspectiveHandler.class);
    @Inject
    private MApplication application;
    @Inject
    private EModelService modelService;
    @Inject
    private EPartService partService;
    private final String perspectiveId;

    public OpenPerspectiveHandler(String perspectiveId) {
        super();
        this.perspectiveId = perspectiveId;
    }

    public void changePerspective(String perspectiveId) {

        Optional<MPerspective> perspective = findPerspective();
        if(perspective.isPresent()) {
            partService.switchPerspective(perspective.get());
        } else {
            logger.debug("Perspective not found (" + perspectiveId + ")");
        }
    }

    @Execute
    public void execute() {

        changePerspective(perspectiveId);
    }

    private Optional<MPerspective> findPerspective() {

        MUIElement element = modelService.find(perspectiveId, application);
        if(element instanceof MPerspective) {
            return Optional.of((MPerspective)element);
        } else {
            logger.debug("Wrong type " + element);
        }
        return Optional.empty();
    }

    @Override
    public String toString() {

        return "OpenPerspectiveHandler [application=" + application + ", modelService=" + modelService + ", partService=" + partService + ", perspectiveId=" + perspectiveId + "]";
    }
}

有趣的是,该方法仅工作一次。一种解决方法是找到 MPerspective 后立即对其进行缓存,而不再使用 modelService.find(perspectiveId,application)

Interestingly, this works only once. A workaround is to cache MPerspective once it was found and not to use modelService.find(perspectiveId, application) again.

为什么只工作一次? modelService.find(perspectiveId,application)首次执行后返回 null

Why does it work only once? modelService.find(perspectiveId, application) returns null after the first execution.

编辑:

另一种方法(由greg-449建议)如下:

Another approach (as suggested by greg-449) is the following:

public class OpenPerspectiveHandler {

    private static final Logger logger = Logger.getLogger(OpenPerspectiveHandler.class);
    private final String perspectiveId;

    public OpenPerspectiveHandler(String perspectiveId) {
        super();
        this.perspectiveId = perspectiveId;
    }

    @Execute
    public void changePerspective(MApplication application, EModelService modelService, EPartService partService) {

        Optional<MPerspective> perspective = findPerspective(application, modelService);
        if(perspective.isPresent()) {
            partService.switchPerspective(perspective.get());
        } else {
            logger.debug("Perspective not found (" + perspectiveId + ")");
        }
    }

    private Optional<MPerspective> findPerspective(MApplication application, EModelService modelService) {

        MUIElement element = modelService.find(perspectiveId, application);
        if(element instanceof MPerspective) {
            return Optional.of((MPerspective)element);
        } else {
            logger.debug("Wrong type " + element);
        }
        return Optional.empty();
    }
}

但是这种方法也只改变了一次视角。 modelService.find(perspectiveId,application); 首次执行后返回 null

But this approach also changes the perspective only once. modelService.find(perspectiveId, application); returns null after the first execution.

推荐答案

EPartService 取决于处理程序所运行的上下文。在某些情况下,您可以获得应用程序零件服务仅在有活动窗口的情况下才起作用。

The EPartService varies depending on the context that the handler runs in. In some cases you get the Application part service which only works if there is an active window.

您可以使用以下方式为该窗口获取零件服务:

You can get the part service for that window using something like:

MWindow window = (MWindow)modelService.find("top window id", application);

IEclipseContext windowContext = window.getContext();

EPartService windowPartService = windowContext.get(EPartService.class);

这篇关于以编程方式打开透视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆