以编程方式打开Perspective

时间:2017-07-05 13:42:07

标签: eclipse-rcp e4

我正在尝试提供一个命令/处理程序来切换到特定的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)

为什么它只能运作一次?第一次执行后modelService.find(perspectiveId, application)返回null

修改

另一种方法(如greg-449所示)如下:

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

1 个答案:

答案 0 :(得分:0)

EPartService根据处理程序运行的上下文而有所不同。在某些情况下,您将获得Application part服务,该服务仅在存在活动窗口时才有效。

您可以使用以下内容获取该窗口的零件服务:

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

IEclipseContext windowContext = window.getContext();

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