Eclipse hints, tips, and random musings

Wayne Beaton’s blog about Eclipse.

Displaying the console in your RCP application

This question has been asked on eclipse.newcomer a couple of times now, so I thought it might be worthwhile to put a response in the blogosphere.

How do I display the System.out console in a view in my Eclipse RCP application?

The answer has two parts. To start, you can redirect console output to an arbitrary PrintStream using System.setOut(somePrintStream);. The second part is that you need a useful PrintStream to write on.

It’s relatively easy to make a plug-in that does exactly what we need. Here’s how you construct the view.

package org.eclipse.console.ui.views;

import java.io.IOException;import java.io.OutputStream;import java.io.PrintStream;

import org.eclipse.swt.SWT;import org.eclipse.swt.events.DisposeEvent;import org.eclipse.swt.events.DisposeListener;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Text;import org.eclipse.ui.part.ViewPart;

public class LightweightConsoleView extends ViewPart { private Text text;

 public void createPartControl(Composite parent) {  text = new Text(parent, SWT.READ_ONLY | SWT.MULTI);  OutputStream out = new OutputStream() {   @Override   public void write(int b) throws IOException {    if (text.isDisposed()) return;    text.append(String.valueOf((char) b));   }  };  final PrintStream oldOut = System.out;  System.setOut(new PrintStream(out));  text.addDisposeListener(new DisposeListener() {   public void widgetDisposed(DisposeEvent e) {    System.setOut(oldOut);   }  }); }

 public void setFocus() {  text.setFocus(); }}

You’ll notice that I’ve created a read-only Text to contain the output. Then I create an anonymous OutputStream that–when told to dump some bytes–appends output to the end of the Text; I wrap that OutputStream in a PrintStream, and we’re off to the races.

The one part that I’m not so comfortable with is making things right again after the console view closes. In this implementation, I remember where output used to be directed and reset it after the view closes. As long as this view is the only one that tries to mess with the console, this will work. As soon as another view tries, things will get a little spooky.

Posted October 19th, 2006 by Wayne Beaton in category: Other
You can skip to the end and leave a response. Pinging is currently not allowed.

26 Responses to “Displaying the console in your RCP application”


  1. Jeroen Leenarts Says:

    A logical follow-up would be this one: Any ideas on how to implement a “power-user console”?

    Sort of to satisfy any text based itches one might have? I can imagine that it can be usefull to be able to edit/query an application’s model through a mini language. :D

    Should be easy to implement the laguage side with any java based scripting language. (The name of one of those is sitting at the tip of my tongue, can’t remember… :( )


  2. Anonymous Says:

    Converting every byte in the stream to a string and then appending the string to a text field (which is going to cause a repaint) has got to be slow…


  3. Dominique Boucher Says:

    Wayne,

    I translated your Java code in Scheme to show how one can script Eclipse using the SchemeScript plugin.


  4. Anonymous Says:

    Not bad. But I think that would be more interested in seeying how to integrate the *eclipse console view* in your RCP application than seeying how to reimplement the wheel.


  5. Anonymous Says:

    Hello,
    I’m using MessageConsole (RCP) and ConsoleManager.
    There is a bar that you can open new consoles and I want to DO NOT open new consoles.
    Anyone have ideas?

    Thanks


  6. spocket Says:

    My version of the write-method. This one can also handle calls from threads other than the UI-Thread.

    @Override
    public void write(int b) throws IOException
    {
    final String toAppend = String.valueOf( (char) b );
    display.asyncExec( new Runnable()
    {
    public void run()
    {
    if (text.isDisposed())
    return;
    text.append( toAppend );
    }
    } );
    }


  7. Wayne Says:

    Very nice. That should also handle multiple concurrent threads attempting to write to System.out.


  8. Anonymous Says:

    I needed to do this in my RCP application, but I wanted the features/feel of the Eclipse console… So, in my Application.java run() I have the following before creating and running the workbench:

    MessageConsole console = new MessageConsole(”System Output”, null);
    ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { console });
    ConsolePlugin.getDefault().getConsoleManager().showConsoleView(console);
    MessageConsoleStream stream = console.newMessageStream();

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

    logger = LoggerFactory.getLogger(Application.class); // Previously declared.

    This makes System.out, System.err, and anything that any subclass in my RCP app logs using logback magically appear in the standard Eclipse console view. I haven’t tested performance, but it seems fine so far. Feedback is welcome!


  9. Sebastien Says:

    Hello!

    I am currently developing a console that will recieve logging events (for logback, too).

    So far, I’ve been able to send content to a MessageConsole, and add it to the ConsolePlugin tab.

    What I would like to do is 3 things:

    1. Integrate the MessageConsole in my view, and not add it to the ConsoleView.

    2. Color some logging statements

    3. Remove old lines, so that my view does not eat all the memory available if 10000 events are recieved.

    Could anybody give me some hints on how to procede? The first of the previous points is what I’m on at the moment and I am not that close to having a nice result… :(

    Thanks! :)


  10. acorona Says:

    Very useful tip. But I found it kind of slow. So I changed the implementation of the OutputStream to buffer the output causing a quicker display of the output. Here is my version of the OutputStream:

    OutputStream out = new OutputStream() {
    StringBuffer buffer = new StringBuffer();

    @Override
    public void write(final int b) throws IOException {
    if (text.isDisposed())
    return;
    buffer.append((char) b);
    }
    @Override
    public void write(byte[] b, int off, int len) throws IOException {
    super.write(b, off, len);
    flush();
    }
    @Override
    public void flush() throws IOException {
    final String newText = buffer.toString();
    top.getDisplay().syncExec(new Runnable() {
    public void run() {
    text.append(newText);

    }
    });
    buffer = new StringBuffer();
    }

    };


  11. acorona Says:

    The OutputStream I posted before has a problem with multiple threads. I’ve since changed the code and this seems to work better.
    —–
    OutputStream out = new OutputStream() {
    private StringBuffer buffer = new StringBuffer();
    private final Object obj = new Object();

    @Override
    public void write(final int b) throws IOException {
    synchronized (obj) {
    if (text.isDisposed())
    return;

    buffer.append((char) b);
    }
    }
    @Override
    public void write(byte[] b, int off, int len) throws IOException {
    super.write(b, off, len);
    flush();
    }
    @Override
    public void flush() throws IOException {
    synchronized (obj) {
    final String newText = buffer.toString();
    text.append(newText);

    buffer = new StringBuffer();
    }
    }

    };


  12. Albert G Says:

    Bookmarks…

    How I add this article to Digg?…


  13. MOBY Says:

    Bookmarks…

    I can’t add your post to Digg. How I do this?…


  14. A blue Says:

    A blue…

    NetscapeBot: Add your article to Netscape.com…


  15. America the Beautiful Flag Silver Charm Says:

    America the Beautiful Flag Silver Charm…

    NetscapeBot: Add your article to Netscape.com…


  16. Mr.Rick Says:

    Add Page…

    I don’t added your post to del.icio.us…


  17. Lavender Awareness Ribbon Photo Charm Says:

    Lavender Awareness Ribbon Photo Charm…

    I don’t see admin e-mail…


  18. Kenneth Killin Says:

    British Columbia Aerial Photography, Vancouver, Photo, Aerial Video - Flightpath Film, Video and Stills - Flightnet…

    Useful, thank you!…


  19. roll control bolsters Says:

    roll control bolsters…

    I don’t see Subscribe to this blog…


  20. design padding specialized Says:

    design padding specialized…

    I don’t see Subscribe to this blog…


  21. Mr.Roy Says:

    Add Article…

    How I add article to your blog?…


  22. Isaac Baber Says:

    Hotels and Travel Services in Turkey…

    Thank you for your post!…


  23. joel bolster Says:

    joel bolster…

    Where I see gallery?…


  24. pillow talk meaning Says:

    pillow talk meaning…

    Where I see gallery?…


  25. que bebo se que fumo Says:

    que bebo se que fumo…

    Wellcome to Bebo online ?ommunity, allowing you to share photos and blogs…


  26. register for bebo Says:

    register for bebo…

    Wellcome to Bebo online ?ommunity, allowing you to share photos and blogs…

Leave a Reply

You must be logged in using your Eclipse Bugzilla account to post a comment.

Recent Posts

Archives

Categories

Meta