[英]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.