繁体   English   中英

Eclipse 4 RCP-应用程序没有活动窗口

[英]Eclipse 4 RCP - Application does not have an active window

我正在尝试使用EPartService设置我的视图,但是仍然通过findPart调用在该行上获取异常。 我做对了吗?

例外:

Caused by: java.lang.IllegalStateException: Application does not have an active window
    at org.eclipse.e4.ui.internal.workbench.ApplicationPartServiceImpl.getActiveWindowService(ApplicationPartServiceImpl.java:36)
    at org.eclipse.e4.ui.internal.workbench.ApplicationPartServiceImpl.findPart(ApplicationPartServiceImpl.java:87)

码:

package cz.vutbr.fit.xhriba01.bc.handler;

import javax.inject.Inject;
import javax.inject.Named;

import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;

import cz.vutbr.fit.xhriba01.bc.ui.ExplorerView;
import cz.vutbr.fit.xhriba01.bc.ui.dialogs.NewFromDirectoryDialog;

public class NewFromDirectoryHandler {

    @Inject 
    private EPartService fPartService;

    @Execute
    public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {

        NewFromDirectoryDialog dialog = new NewFromDirectoryDialog(shell);
        dialog.create();
        if (dialog.open() == Window.OK) {
            String sourceDir = dialog.getSourceDir();
            String classDir = dialog.getClassDir();
            TreeViewer tv = ((ExplorerView)fPartService.findPart("bc.part.explorer").getObject()).getTreeViewer();
        }
    }

}

尝试将EPartService注入为execute方法的参数:

@Execute
public void execute(EPartService fPartService, @Named(IServiceConstants.ACTIVE_SHELL) Shell shell)

最好避免在处理程序中注入字段,因为在创建处理程序时它们只会被注入一次。 随着活动零件的更改,诸如EPartService之类的内容EPartService更改。

我认为您需要编写如下内容:

public class NewFromDirectoryHandler {

    @Inject 
    private EPartService fPartService;

    @Inject 
    MApplication application

    @Execute
    public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {

        NewFromDirectoryDialog dialog = new NewFromDirectoryDialog(shell);
        dialog.create();
        IEclipseContext activeWindowContext = application.getContext().getActiveChild();
        if (dialog.open() == Window.OK) {
            activeWindowContext.activate();
            String sourceDir = dialog.getSourceDir();
            String classDir = dialog.getClassDir();
            TreeViewer tv = ((ExplorerView)fPartService.findPart("bc.part.explorer").getObject()).getTreeViewer();
        }
    }

之前保存上下文,然后在dialog.open()之后还原。

正如greg-449所说,最好在execute方法中注入EPartService。

如果设计情况迫使您不要在execute方法中使用EPartService,则可以使用以下方法。

IEclipseContext context = application.getContext();
MTrimmedWindow window = (MTrimmedWindow) application.getChildren().get(0);
IEclipseContext windowContext = window.getContext();
context.activate();
windowContext.setParent(context);
windowContext.activateBrach();
EPartService partService = windowContext.get(EPartService.class);

然后,该partService可以用于findPart或createPart。

我认为的问题是,应用程序上下文没有活动子级。 EPartService中的代码看到活动叶不同于活动子叶。

活跃叶子也可以通过以下方式获得:

IEclipseContext activeLeaf = PlatformUI.getWorkbench().getService(IEclipseContext.class).getActiveLeaf(); 

如果叶子已经处于活动状态,那么该叶子也可以用于获取EPartService

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM