ConsoleViewPart.java

package com.wbq.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 ConsoleViewPart implements IConsoleFactory {

private static MessageConsole console = new MessageConsole("报文监控窗口", null);


public void openConsole() {
showConsole();
}


public static void showConsole() {
// try {
if (console != null) {
//得到默认控制台管理器
IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
//得到所有的控制台实例
IConsole[] existing = manager.getConsoles();
boolean exists = false;
//新创建的MessageConsole实例不存在就加入到控制台管理器,并显示出来
for (int i = 0; i < existing.length; i++) {
if (console == existing[i])
exists = true;
}
if(!exists){
manager.addConsoles(new IConsole[] { console });
}
manager.showConsoleView(console);


// System.setOut(new PrintStream(stream));


}
// } catch (IOException e) {
// e.printStackTrace();
// }
}

public static void closeConsole() {
IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
if (console != null) {
manager.removeConsoles(new IConsole[] { console });
}
}

public static MessageConsole getConsole(){
return console;
}

}