我正在尝试使用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();
}
}
}
答案 0 :(得分:4)
尝试将EPartService
作为参数注入execute
方法:
@Execute
public void execute(EPartService fPartService, @Named(IServiceConstants.ACTIVE_SHELL) Shell shell)
最好避免在处理程序中注入字段,因为它们只会在创建处理程序时注入一次。 EPartService
之类的内容随着活动部分的变化而变化。
答案 1 :(得分:0)
我认为你需要写下这样的东西:
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()
之前保存上下文并在之后恢复。
答案 2 :(得分:0)
如greg-449所述,最好将EPartService注入execute方法中。
如果设计情况迫使您不要在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