Example: Using a button to call a program on the server

Note: Read the Code example disclaimer for important legal information.
/////////////////////////////////////////////////////////////////////////
//
// Program call button example.  This program demonstrates how to
// use a button that calls a program on the server.  It will exchange data
// with the server program via an input and output parameter.
//
// Command syntax:
//    ProgramCallButtonExample system
//
// This source is an example of IBM Toolbox for Java "ProgramCallButton".
//
/////////////////////////////////////////////////////////////////////////

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

public class ProgramCallButtonExample
{

    private ProgramParameter   parm1, parm2, parm3, parm4, parm5;
    private JTextField         cpuField;
    private JTextField         dasdField;
    private JTextField         jobsField;


    // Create a ProgramCallButtonExample object, then call the
    // non-static version of main().  If we don't to this then
    // the class variables (parm1, parm2, ...) must be declared
    // static.  If they are static they cannot be used by the
    // action completed listener in Java 1.1.7 or 1.1.8.
    public static void main (String[] args)
    {
       ProgramCallButtonExample me = new ProgramCallButtonExample();
       me.Main(args);
    }

    public void Main (String[] args)
    {
        // If a system was not specified, then display help text and
        // exit.
        if (args.length != 1)
        {
            System.out.println("Usage:  ProgramCallButtonExample system");
            return;
        }

        try
        {
            // Create a frame.
            JFrame f = new JFrame ("Program call button example");

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

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

            // Create the program path name.
            QSYSObjectPathName programName = new QSYSObjectPathName ("QSYS",
                "QWCRSSTS", "PGM");

            // Create a ProgramCallButton object.  The button
            // will have the text "Refresh" and no icon.
            ProgramCallButton button = new ProgramCallButton ("Refresh", null);
            button.setSystem (system);
            button.setProgram (programName.getPath ());
            button.addErrorListener (errorHandler);

            // The first parameter is an 64 byte output parameter.
            parm1 = new ProgramParameter (64);
            button.addParameter (parm1);

            // We use the second parameter to set the buffer size
            // of the first parameter.  We will always set this to
            // 64.  Remember that we need to convert the Java int
            // value 64 to the format used on the server.
            AS400Bin4 parm2Converter = new AS400Bin4 ();
            byte[] parm2Bytes = parm2Converter.toBytes (64);
            parm2 = new ProgramParameter (parm2Bytes);
            button.addParameter (parm2);

            // The third parameter is the status format.  We will
            // always use "SSTS0200".  This is a String value, and
            // again we need to convert it to the format used on the server.
            AS400Text parm3Converter = new AS400Text (8, system);
            byte[] parm3Bytes = parm3Converter.toBytes ("SSTS0200");
            parm3 = new ProgramParameter (parm3Bytes);
            button.addParameter (parm3);

            // The fourth parameter is the reset statistics parameter.
            // We will always pass "*NO" as a 10 character String.
            AS400Text parm4Converter = new AS400Text (10, system);
            byte[] parm4Bytes = parm4Converter.toBytes ("*NO       ");
            parm4 = new ProgramParameter (parm4Bytes);
            button.addParameter (parm4);

            // The fifth parameter is for error information.  It
            // is an input/output parameter.  We will not use it
            // for this example, but we need to set it to something,
            // or else the number of parameters will not match
            // what the server is expecting.
            byte[] parm5Bytes = new byte[32];
            parm5 = new ProgramParameter (parm5Bytes, 0);
            button.addParameter (parm5);

            // When the program runs, we will get a bunch of data.
            // We need a way to display that data to the user.
            // In this case, we will just use simple labels and text
            // fields.
            JLabel cpuLabel = new JLabel ("CPU Utilitization: ");
            cpuField = new JTextField (10);
            cpuField.setEditable (false);

            JLabel dasdLabel = new JLabel ("DASD Utilitization: ");
            dasdField = new JTextField (10);
            dasdField.setEditable (false);

            JLabel jobsLabel = new JLabel ("Number of active jobs: ");
            jobsField = new JTextField (10);
            jobsField.setEditable (false);

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

            // When the program is called, we need to process the
            // information that comes back in the first parameter.
            // The format of the data in this parameter was documented
            // by the program we are calling.
            button.addActionCompletedListener (new ActionCompletedListener ()
            {
                public void actionCompleted (ActionCompletedEvent event)
                {
                    try
                    {
                       // Get the data from the first parameter.
                       // It is in the server format.
                       byte[] parm1Bytes = parm1.getOutputData ();

                       // Each of the pieces of data that we need
                       // is an int.  We can create one converter
                       // to do all of our conversions.
                       AS400Bin4 parm1Converter = new AS400Bin4 ();

                       // Get the CPU utilitization starting at byte 32.
                       // Set this value in the corresponding text field.
                       int cpu = parm1Converter.toInt (parm1Bytes, 32);
                       cpuField.setText (Integer.toString (cpu / 10) + "%");

                       // Get the DASD utilitization starting at byte 52.
                       // Set this value in the corresponding text field.
                       int dasd = parm1Converter.toInt (parm1Bytes, 52);
                       dasdField.setText (Integer.toString (dasd / 10000) + "%");

                       // Get the number of active jobs starting at byte 36.
                       // Set this value in the corresponding text field.
                       int jobs = parm1Converter.toInt (parm1Bytes, 36);
                       jobsField.setText (Integer.toString (jobs));
                    }
                    catch (Exception e) { e.printStackTrace(); }
                }
            });

            // Layout the frame.
            JPanel outputPanel = new JPanel ();
            outputPanel.setLayout (new GridLayout (3, 2, 5, 5));
            outputPanel.add (cpuLabel);
            outputPanel.add (cpuField);
            outputPanel.add (dasdLabel);
            outputPanel.add (dasdField);
            outputPanel.add (jobsLabel);
            outputPanel.add (jobsField);

            Panel buttonPanel = new Panel ();
            buttonPanel.add (button);

            f.getContentPane ().setLayout (new BorderLayout ());
            f.getContentPane ().add ("Center", outputPanel);
            f.getContentPane ().add ("South", buttonPanel);
            f.pack ();
            f.show ();
        }
        catch (Exception e)
        {
           System.out.println ("Error: " + e.getMessage ());
           System.exit (0);
        }
    }


}