我正在学习如何开发RCP应用程序。我需要执行的任务之一是创建控制台视图。我做了一些搜索,我找到了以下代码段,据说可以创建一个控制台视图:
@PostConstruct
public void createComposite(Composite parent) {
MessageConsole myConsole = findConsole("abc");// CONSOLE_NAME);
MessageConsoleStream out = myConsole.newMessageStream();
out.println("Hello from Generic console sample action");
IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
IWorkbenchPage page = win.getActivePage();
String id = IConsoleConstants.ID_CONSOLE_VIEW;
IConsoleView view = null;
try {
view = (IConsoleView) page.showView(id);
} catch (PartInitException e) {
e.printStackTrace();
}
view.display(myConsole);
}
private MessageConsole findConsole(String name) {
ConsolePlugin plugin = ConsolePlugin.getDefault();
IConsoleManager conMan = plugin.getConsoleManager();
IConsole[] existing = conMan.getConsoles();
for (int i = 0; i < existing.length; i++)
if (name.equals(existing[i].getName()))
return (MessageConsole) existing[i];
// no console found, so create a new one
MessageConsole myConsole = new MessageConsole(name, null);
conMan.addConsoles(new IConsole[] { myConsole });
return myConsole;
}
但是我不断收到以下错误:
org.eclipse.e4.core.di.InjectionException: java.lang.IllegalStateException: Workbench 尚未创建。
我试图遵循本文(http://waheedtechblog.blogspot.com/2011/11/javalangillegalstateexception-workbench.html)中发布的建议,但无济于事。
我的问题如下:
应该怎么做才能修复上述错误?
鉴于 Eclipse 开发环境的丰富性,是否有更好的方法可以用更少的编程工作量实现相同的目标(例如,在 RCP 应用程序中切换控制台的视图)?
在 RCP 中运行现有的 Eclipse 工作台代码,则需要在应用程序中正确设置工作台。
您可以将产品定义基于现有的org.eclipse.ui.ide.workbench
应用程序:
<extension id="product" point="org.eclipse.core.runtime.products">
<product application="org.eclipse.ui.ide.workbench" name="name"/>
...
或者你可以打电话
PlatformUI.createAndRunWorkbench(display, advisor);
在你的IApplication
实现中,但你必须至少做一些org.eclipse.ui.internal.ide.application.IDEWorkbenchAdvisor
完成的工作才能使事情正常工作。