Example: Using DataQueueDocument

Note: Read the Code example disclaimer for important legal information.
/////////////////////////////////////////////////////////////////////////
//
// Data queue document example.  This program demonstrates how to
// use a document that is associated with a server data queue.
//
// Command syntax:
//    DataQueueDocumentExample system read|write
//
/////////////////////////////////////////////////////////////////////////

import com.ibm.as400.access.*;
import com.ibm.as400.vaccess.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DataQueueDocumentExample
{
    private static DataQueueDocument    dqDocument;
    private static JTextField           text;
    private static boolean              rw;

    public static void main (String[] args)
    {
        // If a system or read|write was not specified, then display
        // help text and exit.
        if (args.length != 2)
        {
            System.out.println("Usage:  DataQueueDocumentExample system read|write");
            return;
        }

        rw = args[1].equalsIgnoreCase ("read");
        String mode = rw ? "Read" : "Write";

        try
        {
            // Create two frames.
            JFrame f = 
               new JFrame ("Data queue document example - " + mode);

            // Create an error dialog adapter.  This will display
            // any errors to the user.
            ErrorDialogAdapter errorHandler = new ErrorDialogAdapter (f);

            // Create a working cursor adapter.  This will adjust
            // the cursor whenever a data queue is read or written.
            WorkingCursorAdapter cursorAdapter = new WorkingCursorAdapter (f);

            // Create an AS400 object.  The system name was passed
            // as the first command line argument.
            AS400 system = new AS400 (args[0]);

            // Create the data queue path name.
            QSYSObjectPathName dqName = new QSYSObjectPathName ("QGPL", "JAVATALK", "DTAQ");

            // Make sure the the data queue exists.
            DataQueue dq = new DataQueue (system, dqName.getPath ());
            try
            {
                dq.create (200);
            }
            catch (Exception e)
            {
                // Ignore exceptions.  Most likely, the data queue
                // already exists.
            }

            // Create a DataQueueDocument object.
            dqDocument = new DataQueueDocument (system, dqName.getPath ());
            dqDocument.addErrorListener (errorHandler);
            dqDocument.addWorkingListener (cursorAdapter);

            // Create a text field used to present the document.
            text = new JTextField (dqDocument, "", 40);
            text.setEditable (! rw);

            // When the program runs, we need a way to control when
            // the reads and writes take place.  We will let the
            // use control this with a button.
            Button button = new Button (mode);
            button.addActionListener (new ActionListener ()
            {
                public void actionPerformed (ActionEvent event)
                {
                    if (rw)
                        dqDocument.read ();
                    else {
                        dqDocument.write ();
                        text.setText ("");
                    }
                }
            });

            // When the the frame closes, exit.
            f.addWindowListener (new WindowAdapter () {
                public void windowClosing (WindowEvent event)
                {
                    System.exit (0);
                }
            });

            // Layout the frame.
            f.getContentPane ().setLayout (new FlowLayout ());
            f.getContentPane ().add (text);
            f.getContentPane ().add (button);
            f.pack ();
            f.show ();
        }
        catch (Exception e)
        {
           System.out.println ("Error: " + e.getMessage ());
           System.exit (0);
        }
    }
}