【问题标题】:Eclipse 4 RCP - Application does not have an active windowEclipse 4 RCP - 应用程序没有活动窗口
【发布时间】:2014-12-21 01:00:06
【问题描述】:

我正在尝试使用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();
        }
    }

}

【问题讨论】:

标签: eclipse eclipse-rcp e4


【解决方案1】:

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

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

最好避免在处理程序中注入字段,因为它们只会在创建处理程序时注入一次。 EPartService 之类的东西会随着活动部分的变化而变化。

【讨论】:

  • 我能问一下我如何与我的 UI 通信是否是个好主意?因为我现在读到模型类可以被释放(但我不知道什么时候),所以如果 MPart 被释放,即使它显示的那个类也被释放(在我的例子中是 ExplorerView)?因为在这种情况下,对话框中的信息将丢失,因为它仅存在于该 ExplorerView 中。
  • 我通常只使用@Named(IServiceConstants.ACTIVE_PART) MPart activePart 来注入活动部分,并确保处理程序只对正确的部分有效。
  • 我在哪里存储我的信息以确保它们以后不会被处理(例如,可以是代表由 eclipse 处理的 MPart 的视图的类,或者 eclipse 将只处理该 MPart 并且稍后当该 MPart应该渲染,它将使用现有视图)?目前我只是设置了 TreeViewer。但是,如果该 TreeViewer 将被释放并再次呈现,它将没有输入。
  • 你应该使用一些非 UI 对象,你可以在任何你需要的地方注入它们。您可以创建一个 OSGi 服务来保存数据,或者只是一个您注入到应用程序上下文中的对象。
【解决方案2】:

我认为你需要这样写:

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() 之前保存上下文并在之后恢复。

【讨论】:

    【解决方案3】:

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

    如果设计情况迫使您不要在执行方法中使用 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-29
      • 2010-10-26
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      相关资源
      最近更新 更多