具体转载于 https://www.cnblogs.com/biaoyu/archive/2012/07/20/2601195.html
Eclipse RCP开发4--在Eclipse RCP中加入Console视图
有时候在编写RCP程序中,需要输出一些信息,例如log信息,调试信息,等等。这时候,如果能够将eclipse自带的console视图加入到我们自己编写的程序中,将会是一件非常美妙的事情。下面将详细的介绍如何将eclipse 自带的console视图引入到RCP程序中。
- 创建一个新的插件程序,例如起名为com.rcp.tutorial.view.console。
- 打开配置文件plugin.xml,在依赖(Dependencies)页中,在Required Plug-ins,点击Add,如下图
- 添加org.eclipse.ui.console插件,如下图
- 在扩展点(Extensions)页添加org.eclipse.console.consoleFactories扩展点,并修改扩展元素Label为Output, Class 为com.rcp.tutorial.view.console.ConsoleFactory,如下图
- 点击class,创建 com.rcp.tutorial.view.console.ConsoleFactory的实现类,该类实现org.eclipse.ui.console.IConsoleFactory接口。此类最主要的实现方法为openConsole,加入以下代码:
package com.rcp.tutorial.view.console;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleFactory;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
public class ConsoleFactory implements IConsoleFactory {
private static MessageConsole console = new MessageConsole("Output", null);
@Override
public void openConsole() {
if (console != null) {
IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
IConsole[] existing = manager.getConsoles();
boolean exists = false;
for (int i = 0; i < existing.length; i++) {
if (console == existing[i])
exists = true;
}
if(!exists){
manager.addConsoles(new IConsole[] { console });
}
manager.showConsoleView(console);
}
}
public static void closeConsole() {
IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
if (console != null) {
manager.removeConsoles(new IConsole[] { console });
}
}
public static MessageConsole getConsole(){
return console;
}
}
- 在Perspective类中加入以下代码:
package com.rcp.tutorial.view.console;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
import org.eclipse.ui.console.IConsoleConstants;
public class Perspective implements IPerspectiveFactory {
public void createInitialLayout(IPageLayout layout) {
layout.setEditorAreaVisible(true);
layout.addView(IConsoleConstants.ID_CONSOLE_VIEW, IPageLayout.BOTTOM, .4f, layout.getEditorArea());
ConsoleFactory consoleFactory = new ConsoleFactory();
consoleFactory.openConsole();
}
}
- 运行程序,可以发现,console视图已经加入到RCP程序中,如下图,
- 测试在此console中输出信息。在扩展点页加入新的扩展点,org.eclipse.ui.commands,添加一个新的command,并创建org.rcp.tutorial.view.console.TestCmd类,该类执行该command的动作,如下图
- 然后参考我的文章Eclipse RCP开发3--菜单、工具栏,创建以下菜单
- 修改TestCmd类,如下代码
package com.rcp.tutorial.view.console;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.ui.console.MessageConsoleStream;
public class TestCmd implements IHandler {
@Override
public void addHandlerListener(IHandlerListener handlerListener) {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
MessageConsoleStream printer =ConsoleFactory.getConsole().newMessageStream();
printer.println("Hello, console!");
//System.out.println("Hello, console!");
return null;
}
@Override
public boolean isEnabled() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isHandled() {
// TODO Auto-generated method stub
return true;
}
@Override
public void removeHandlerListener(IHandlerListener handlerListener) {
// TODO Auto-generated method stub
}
}
- 再次执行,如下图
- 该程序可以像自身的console输出信息!