RCP添加控制台Console

时间:2023-01-31 08:37:49

RCP,SWT,插件开发【qq群】336280109

1.添加依赖org.eclipse.ui.console;bundle-version="3.5.100";

2.添加扩展点 

  <extension
         point="org.eclipse.ui.console.consoleFactories">
      <consoleFactory
            class="test.console.my.ConsoleFactory"
            label="Console">
      </consoleFactory>
   </extension>

3.创建类ConsoleFactory

import java.io.PrintStream;

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;
import org.eclipse.ui.console.MessageConsoleStream;

public class ConsoleFactory implements IConsoleFactory {

private static MessageConsole console = new MessageConsole("Test Infomation", null);

private static PrintStream initConsolePrintStream = System.out;

private static PrintStream newConsolePrintStream;

public static void setPrintStreamInConsole() {
System.setOut(newConsolePrintStream);
}

public static void revertPrintStream() {
System.setOut(initConsolePrintStream);
}

@Override
public void openConsole() {
showConsole();
}

public static void showConsole() {
if (console != null) {
final IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
final 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);
final MessageConsoleStream stream = console.newMessageStream();
newConsolePrintStream = new PrintStream(stream);
setPrintStreamInConsole();
}
}

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

public static MessageConsole getConsole() {
return console;
}
}



4.在Perspective中修改代码

@Override
public void createInitialLayout(final IPageLayout layout) {
layout.setEditorAreaVisible(false);

final ConsoleFactory cf = new ConsoleFactory();
cf.openConsole();
}


5.完成