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, create a new one.
MessageConsole console=new MessageConsole(name,null);
conMan.addConsoles(new IConsole[]{console});
return console;
}
private void writeConsole(){
MessageConsole console=findConsole("lpid=10");
MessageConsoleStream out=console.newMessageStream();
out.println("hello from generic console");
}
private void showConsole(){
IConsole console=....console;
IWorkbenchPage page=...;
String id=IConsoleConstants.ID_CONSOLE_VIEW;
IConsoleView view=(IConsoleView) page.showView(id);
view.display(console);
}
The org.eclipse.debug.ui.console.IConsole has a method connect(IStreamsProxy streamsProxy), which will connects this console to the given streams proxy. This associates the standard in, out, and error streams with the console. Keyboard input will be written to the given proxy.
//////////////////////////////////////////////////////////
Note:
In the following, all "process" means the real process, not the Java wrapper Process. It is in fact is the wrapped process. And the outputStream of wrapper is piped into the inputStream of the wrapped process. The stdout of the wrapped process is piped into the wrapper's inputStream.
IStreamMonitor : manages the contents of the stream a process is writing to, and notifies registered listeners of changes in the stream.
public void addListener(IStreamListener listener);
public String getContents();
public void removeListener(IStreamListener listener);
IStreamListener: has only one method which will be called when text has been appended to the given stream monitor.
public void streamAppended(String text, IStreamMonitor monitor);
IStreamProxy : Acts as proxy between the streams of a process and interested clients. It allows implementation of IProcess to handle I/O related to the stdin/out/err associated with a process.
public IStreamMonitor getErrorStreamMonitor();
public IStreamMonitor getOutputStreamMonitor(); // The monitor is connected to the output stream of the associated process.
public void write(String input) throws IOException; // Writes the given text to the stdin of the proxy's process.
IProcess (1)---->(1) IStreamProxy (1)---->(2) IStreamMonitor (1)---->(m) IStreamListener
org.eclipse.debug.ui.console.IConsole: A console displays output and writes input to a process.
public void connect(IStreamsProxy streamsProxy); // associates the stdin/out/err with console.
public void connect(IStreamMonitor streamMonitor, String streamIdentifer);
public void addLink(IHyperlink link, int offset, int length);
public IRegion getRegion(IHyperlink link);
public IDocument getDocument();
public IProcess getProcess();
public void addPatternMatchListener(IPatternMatchListener matchListener);
public void removePatternMatchListener(IPatternMatchListener matchListener);
public IOConsoleOutputStream getStream(String streamIdentifier);
When connect(), the stream listener will be created and added to the fStreamListners list. The StreamListener will be called later when the process outputstream changes, and then the listener will write (anything he wants) to the Console throught the output stream which is passed when the listener's constructor.
To summary, to output the process' output message to the Console, you must first create a IConsole, and register it to the ConsoleManager. You should also monitor the process's output stream, so that when there is any output message from the process, you can then write the message to the console.
To implement the StreamsProxy, please refer to the org.eclipse.debug.internal.core.StreamsProxy.