我正在尝试使用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
这样的事情随着活跃部分的变化而变化。
我想你需要写这样的东西:
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。
如果设计情况迫使您不在执行方法中使用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