DevFaqOWColorText

Revision as of 14:25, 8 December 2010 by Javydreamercsw (Talk | contribs)

How to use color text in Output Window

You can use IO extension classes like IOColorPrint, IOColorLines. Default colors can be changed via IOColors.

InputOutput io = IOProvider.getDefault().getIO("test");

// change default color for output in corresponding tab
if (IOColors.isSupported(io)) {
    IOColors.setColor(io, IOColors.OutputType.OUTPUT, Color.GRAY);
}

// print line in specific color
if (IOColorLines.isSupported(io)) {
    IOColorLines.println(io, "Green line", Color.GREEN);
}


public class L implements OutputListener {
...
}

// print parts of line in specific color
if (IOColorPrint.isSupported(io)) {
    IOColorPrint.print(io, "Green part", Color.GREEN);
    IOColorPrint.print(io, " pink part", Color.PINK);
    IOColorPrint.print(io, " hyperlink with custom color", new L(), false, Color.MAGENTA);
}

Here's a class that takes care of the dirty work and even handles OutputListeners:

import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Logger;
import org.openide.awt.StatusDisplayer;
import org.openide.util.Exceptions;
import org.openide.windows.IOColorLines;
import org.openide.windows.IOProvider;
import org.openide.windows.InputOutput;
import org.openide.windows.OutputListener;

/**
 * This class handles the output of messages to the output tabs within the platform.
 * @author Javier A. Ortiz Bultrón <javier.ortiz.78@gmail.com>
 */
public final class OutputHandler {

    private OutputHandler() {
    }
    private static ArrayList<String> outputMap = new ArrayList<String>();

    /**
     * Display output
     * @param name
     * @param mess
     * @param c
     */
    public static void output(String name, String mess, Color c) {
        output(name, mess, c, null);
    }

    /**
     * Display output
     * @param name
     * @param mess
     * @param listener
     */
    public static void output(String name, String mess, OutputListener listener) {
        output(name, mess, Color.BLACK, listener);
    }

    /**
     * Print in specified color
     * @param name Tab name
     * @param mess Message to display
     * @param c Color to display the method
     * @param listener
     */
    public static void output(String name, String mess, Color c, OutputListener listener) {
        boolean select = getIO(name) || !outputMap.contains(name);
        InputOutput io = IOProvider.getDefault().getIO(name, select);
        if (select) {
            io.select();
        }
        io.setFocusTaken(false);
        if (mess == null || mess.trim().isEmpty()) {
            if (listener == null) {
                io.getOut().println(mess);
            } else {
                try {
                    io.getOut().println(mess, listener);
                } catch (IOException ex1) {
                    Logger.getLogger(OutputHandler.class.getSimpleName(), ex1.getMessage());
                    io.getOut().println(mess);
                }
            }
        } else {
            if (c != null && c != Color.BLACK) {
                try {
                    if (listener == null) {
                        IOColorLines.println(io, mess, c);
                    } else {
                        IOColorLines.println(io, mess, listener, true, c);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(OutputHandler.class.getSimpleName(), ex.getMessage());
                    if (listener == null) {
                        io.getOut().println(mess);
                    } else {
                        try {
                            io.getOut().println(mess, listener);
                        } catch (IOException ex1) {
                            Logger.getLogger(OutputHandler.class.getSimpleName(), ex1.getMessage());
                            io.getOut().println(mess);
                        }
                    }
                }
            } else {
                //Just print in black as default
                if (listener == null) {
                    io.getOut().println(mess);
                } else {
                    try {
                        io.getOut().println(mess, listener);
                    } catch (IOException ex1) {
                        Logger.getLogger(OutputHandler.class.getSimpleName(), ex1.getMessage());
                        io.getOut().println(mess);
                    }
                }
            }
        }
    }

    /**
     * Print in default color black
     * @param name Tab name
     * @param mess Message to display
     */
    public static void output(String name, String mess) {
        output(name, mess, Color.BLACK, null);
    }

    /**
     * Clear a tab (close and so it'll be recreated next time)
     * @param name
     */
    public static void clear(String name) {
        if (outputMap.contains(name)) {
            try {
                //Remove it
                IOProvider.getDefault().getIO(name, false).getOut().reset();
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }

    private static boolean getIO(String name) {
        boolean create = true;
        if (outputMap.contains(name)) {
            create = false;
        } else {
            outputMap.add(name);
        }
        return create;
    }

    /**
     * Close all I/O tabs (defined)
     */
    public static void closeOutputs() {
        for (String name : outputMap) {
            IOProvider.getDefault().getIO(name, false).closeInputOutput();
        }
        outputMap.clear();
    }

    /**
     * Select an output tab (if it exists)
     * @param name
     */
    public static void select(String name) {
        if (outputMap.contains(name)) {
            IOProvider.getDefault().getIO(name, getIO(name)).select();
        }
    }

    /**
     * Update the message in the status windows of the platform
     * @param mess
     */
    public static void setStatus(String mess) {
        StatusDisplayer.getDefault().setStatusText(mess);
    }
}

Applies to: NetBeans 6.7 or later, (IOColorPrint 6.8 or later).

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo