要使用Rcp嵌入Eclipse自带的console,首先,在plugin.xml的dependencies依赖关系中添加org.eclipse.ui.console
然后在plugin.xml中扩展consoleFactories
其中ConsoleFactory.java代码如下:
- public class ConsoleFactory implements IConsoleFactory {
- static MessageConsole console = new MessageConsole("Console Info:",
- null);
- @Override
- public void openConsole() {
- showConsole();
- }
- public static void showConsole() {
- if (console != null) {
- IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
- IConsole[] existing = manager.getConsoles();
- boolean exists = false;
- //add the new MessageConsole instance into the control manager and show
- 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.java透视图中将其显示出来
public class Perspective implements IPerspectiveFactory {
public void createInitialLayout(IPageLayout layout) {
ConsoleFactory cf = new ConsoleFactory();
layout.addView(IConsoleConstants.ID_CONSOLE_VIEW, IPageLayout.BOTTOM,
0.70f, layout.getEditorArea());
cf.openConsole();
}
}
现在该console就会显示在透视图的下半部分了。但是这样加入的控制台显示的显示的信息条数是不限制的,输出的内容多了就会把RCP应用跑死,可以调用MessageConsole.setWaterMarks(5000, 8000);来限制输出条数。
定制Console View的ToolBar:
去掉RCP Console View 中的Open Console和Select Console按钮。
代码如下:
- IWorkbenchPage page = PlatformUI.getWorkbench().getWorkbenchWindows()[0].getPages()[0];
- IViewPart viewpart = page.findView(IConsoleConstants.ID_CONSOLE_VIEW);
- IActionBars actionBar = viewpart.getViewSite().getActionBars();
- IToolBarManager toolbarMgr = actionBar.getToolBarManager();
- IContributionItem[] items = toolbarMgr.getItems();
- for (IContributionItem item : items) {
- if (item instanceof ActionContributionItem) {
- IAction action = ((ActionContributionItem) item).getAction();
- String text = action.getText();
- if (text.equals("Open Console") || text.equals("Select Console")) {
- toolbarMgr.remove(item);
- }
- }
- }
- actionBar.updateActionBars();
如果不需要toobar中的任何东西,可以直接
- toolbarMgr.removeAll();
新建ConsoleHandler类:
public class ConsoleHandler{
private static MessageConsoleStream consoleStream;
public static void info(final String _message){
Display.getDefault().asyncExec(new Runnable(){
@Override
public void run() {
consoleStream = ConsoleFactory.getConsole().newMessageStream();
consoleStream.println(new SimpleDateFormat("HH:mm:ss").format(new Date())+ "(INFO)" +
" " + _message);
}
});
}
public static void error(final String _message){
Display.getDefault().asyncExec(new Runnable(){
@Override
public void run() {
consoleStream = ConsoleFactory.getConsole().newMessageStream();
consoleStream.setColor(new Color(null,255,0,0));
consoleStream.println(new SimpleDateFormat("HH:mm:ss").format(new Date())+ "(ERROR)" +
" " + _message);
}
});
}
}
然后随时可以使用ConsoleHandler的静态方法输入日志。
可以看到现在的rcp上有很多menubar,这些是我们所不需要的,可以通过在代码中添加下列代码来简化menubar
- IWorkbenchPage page = PlatformUI.getWorkbench().getWorkbenchWindows()[0]
- .getPages()[0];
- IViewPart viewpart = page.findView(IConsoleConstants.ID_CONSOLE_VIEW);
- // IActionBars actionBar = viewpart.getViewSite().getActionBars();
- IToolBarManager toolbarMgr = viewpart.getViewSite().getActionBars()
- .getToolBarManager();
- // IToolBarManager toolbarMgr = actionBar.getToolBarManager();
- IContributionItem[] items = toolbarMgr.getItems();
- for (IContributionItem item : items) {
- if (item instanceof ActionContributionItem) {
- IAction action = ((ActionContributionItem) item).getAction();
- String text = action.getText();
- if (text.equals("Open Console")
- || text.equals("Select Console")) {
- toolbarMgr.remove(item);
- }
- }
- }
- viewpart.getViewSite().getActionBars().updateActionBars();
但是这些代码如果添加在ConsoleFactory.java中,要么出现透视图无法显示,也有会出现nullpoint的错误,后来将这些代码添加到该透视图中另外一个viewpart的构造函数中,问题得到解
决。
参考地址:http://blog.csdn.net/by84788186/article/details/6332299