user2987728
user2987728

Reputation: 1

Eclipse RCP console

I have integrated a console to my rcp application but I would like to display some important informations in red colour rather than in blue but I don't know how to do it(can I add some if conditions?)this the class for my console view .please help me to solve my problem.

public DebugConsole()   
{   
    super("stdio/stderr", null);   

    outMessageStream = newMessageStream();   
    outMessageStream.setColor(Display.getCurrent().getSystemColor(   
            SWT.COLOR_BLUE));   

    errMessageStream = newMessageStream(); 

    errMessageStream.setColor(Display.getCurrent().getSystemColor(   
            SWT.COLOR_RED));   

    System.setOut(new PrintStream(outMessageStream));   
    System.setErr(new PrintStream(errMessageStream));   

} 

Upvotes: 0

Views: 702

Answers (1)

Chandrayya G K
Chandrayya G K

Reputation: 8849

Suggest the below.

From your code if newMessageStream() method returns valid MessageConsoleStream then all System.out.print messages will be displayed in BLUE colour and System.err.print messages will be displayed in RED colour

  1. Use ConsolePatternListener to display matching messages in different colour on console.

  2. Change stream colour before you use System.out.print or System.err.print statements. In the last two lines of you code make PrintSteam instances as class public fields(or private with getter and setters) and get these streams and set colour. Remember to reset the colour back after you used System.out.print or System.err.print statement.

public class DebugConsole {

    private PrintStream outStream;
    private PrintStream errStream;

    public DebugConsole() {
        super("stdio/stderr", null);

        outMessageStream = newMessageStream();
        outMessageStream.setColor(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));

        errMessageStream = newMessageStream();

        errMessageStream.setColor(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
        outStream = new PrintStream(outMessageStream);
        errStream = new PrintStream(errMessageStream);

        System.setOut(outStream);
        System.setErr(errStream);
    }

    public PrintStream getOutStream() {
        return outStream;
    }
    public void setOutStream(PrintStream outStream) {
        this.outStream = outStream;
    }
    public PrintStream getErrStream() {
        return errStream;
    }
    public void setErrStream(PrintStream errStream) {
        this.errStream = errStream;
    }       
}

Test class:

public class TestConsole {
    public static void main(String[] args) {
        DebugConsole console = new DebugConsole();
        MessageConsoleStream errStream = (MessageConsoleStream)console.getErrStream();  
        Color oldColor = errStream.getColor();
        errStream.setColor(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GREEN));
        //All the below message will be displayed in green color
        System.err.println("This is in green color");
        System.err.println("This is in green color");
        //Reset color back
        errStream.setColor(oldColor);

        //Do the same for output stream         

    }
}

Or Use Grep console Plugin See here

Upvotes: 1

Related Questions