ProgramCallButton Example
/////////////////////////////////////////////////////////////////////////
//
// Program call button example. This program demonstrates how to
// use a button that calls an AS/400 program. It will exchange data
// with the AS/400 program through an input and output parameter.
//
// Command syntax:
// ProgramCallButtonExample system
//
/////////////////////////////////////////////////////////////////////////
//
// This source is an example of AS/400 Toolbox for Java "ProgramCallButton".
// IBM grants you a nonexclusive license to use this as an example
// from which you can generate similar function tailored to
// your own specific needs.
//
// This sample code is provided by IBM for illustrative purposes
// only. These examples have not been thoroughly tested under all
// conditions. IBM, therefore, cannot guarantee or imply
// reliability, serviceability, or function of these programs.
//
// All programs contained herein are provided to you "AS IS"
// without any warranties of any kind. The implied warranties of
// merchantablility and fitness for a particular purpose are
// expressly disclaimed.
//
// AS/400 Toolbox for Java
// (C) Copyright IBM Corp. 1997, 1998
// All rights reserved.
// US Government Users Restricted Rights -
// Use, duplication, or disclosure restricted
// by GSA ADP Schedule Contract with IBM Corp.
//
/////////////////////////////////////////////////////////////////////////
import com.ibm.as400.access.*;
import com.ibm.as400.vaccess.*;
import com.sun.java.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ProgramCallButtonExample
{
private static ProgramParameter parm1, parm2, parm3, parm4, parm5;
private static JTextField cpuField;
private static JTextField dasdField;
private static JTextField jobsField;
public static 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 its AS/400 format.
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 its AS/400 format.
AS400Text parm3Converter = new AS400Text (8);
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);
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)
{
// Get the data from the first parameter.
// It is in AS/400 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));
}
});
// 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);
}
}
}