BookNBPlatformCookbookCH0209

Contents

NetBeans Platform Cookbook Chapter 02 09

Using Output

NetBeans Platform provides I/O APIs module to display messages of a tasks. You can even add a hyperlink, listen to events, print by wished color or add your actions - you know it from NetBeans IDE. The Run output window has Cancel or Rerun buttons, Refactorig or JUnit window has more buttons.

Preparation

Do not print log messages by standard System.out or System.err but by standard logger API or by InputOutput object offered by the Platform. These objects are provided by factory service IOProvider. You can implement your own implementation as a service.

To get its instance call getDefault() - it returns the default implementation from the Platform. The IOProvider.get(provider-name) returns the provider service of given name, if it does not exist the default one is returned. If the I/O APIs module is not available the standard java System.out and System.err are used.

How to

Simple printing to output

    InputOutput io = IOProvider.getDefault().getIO( “MyMessages”, false );
    io.getOut().println( “Some normal message” );
    io.getErr().println( “Error message” );
    io.getOut().close();
    io.getErr().close();
    io.select();   

First you ask for service implementation by IOProvider.getDefault() or IOProvider.get(“IOimplname”). Retrieve the InputOutput object by getIO() its method.

Pass name of the io object (and of the resulted tab). Set the second parameter to true to enable reuse previously used object if any. Then you get output or error PrintWriter instance by io.getOut() or io.getErr() method. Now you can print as usually.

It is important to close the writer. At end call io.select() to ensure the just used io tab will be selected and displayed in the Output window.

Adding actions to output

You can add your actions to the toolbar of output tab. Use variant of getIO() method with actions parameter.

   getIO(String name, Action[] actions)

Descripton from the documentation: Array of actions that are added to the toolbar. Can be empty array, but not null. The number of actions should not exceed 5 and each should have the Action.SMALL_ICON property defined. Example is copied from the Definitive Guide to NetBeans Platform book:

 
public class StopTask extends AbstractAction {
   public StopTask() {
      putValue( SMALL_ICON,
                new ImageIcon(
               ImageUtilities.loadImage("icon.gif", true)));
   }

   public void actionPerformed(ActionEvent evt) {
      // stop the task
   }
}

The StopTask button will be added right on the output tab.

Colored printing

For colored printing you can use IOColorLines to print lines of given color and IOColorPrint to print parts of lines by different color. First is necessary check if colored printing is supported. Printing with IOColorPrint is similar but you must explicitly print new line char.

if ( IOColorLines.isSupported(io) ) {
    IOColorLines.println(io, text, new OutListener(oname)
          , true    // important line
          , c); 
}
else {
    io.getOut().println(  text, new OutListener(oname)
          , true );
}

The parameter important line marks the line as important. Makes the UI respond appropriately, e. g. stop the automatic scrolling or highlight the hyperlink (copied from the documentation).

The empty OutListener looks like:

    private class OutListener implements OutputListener {

        public OutListener() { 
        }

        /** Called when a line is selected.         */ 
        public void outputLineSelected(OutputEvent ev) {
            // do something with line text = ev.getLine()            
        }

        /** Called when some sort of action is performed on a line.         */
        public void outputLineAction(OutputEvent ev) {
            // do something with line text = ev.getLine()
        }

        /** Called when a line is cleared from the buffer of known lines.  */
        public void outputLineCleared(OutputEvent ev) {
            // nothing
        }
    } // OutListener

Good illustration how to use color printing and useful class you find on NetBeans wiki page “How to use color text in Output Window” on http://wiki.netbeans.org/DevFaqOWColorText. Explore the OutputHandler class written by Javier A. Ortiz Bultrón.

image:nbpcook_02_15_output.png

Figure 2.15 Colored Output Window


Notes/Tips - properties

Text and sources were created under NB 6.8, 6.9, 7.0, 7.1, 7.2.

Navigation

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