IBM Books

Screen Customizer Component Interface for Java


Writing Your First Screen Customizer Component Interface Applet and Application

The following examples demonstrate how simple writing SCCI applets and applications can be:

HelloWorldApplet

Below is a simple HelloWorld program that will change the captions of all the GUI components on the screen. Please refer to classpath, compiling, and executing for all the details on how to compile and run HelloWorldApplet.java.

//HelloWorld will change the captions of all the GUI objects on the current
//screen to "Hello World!!!"  HelloWorld implements CustomInterface and
//uses a CustomTerminal to get all the GUI objects from the current screen
//and then steps through each of them setting the caption.

import com.ibm.eNetwork.beans.HOD.*;               //picks up HostTerminal
import com.ibm.eNetwork.HOD.*;                     //picks up HOD classes
import com.ibm.hi.customizer.beans.*;              //picks up CustomTerminal
import com.ibm.hi.customizer.beans.scci.*;         //picks up SCCustomComponents

//All user applet programs wishing to access to the GUI components (SCCI components) must
//implement CustomInterface found in the com.ibm.eNetwork.HOD package
public class HelloWorldApplet implements CustomInterface{

    //default constructor
    //HelloWorld will be instantiated using the default constructor
    //after the default constructor returns init (see below) will be called
    public HelloWorldApplet(){
        //not implemented
    }

    //This init method is defined in CustomInterface and is called immediately after
    //the CustomInterface is instantiated.  This method should be used as a starting
    //point to hook up communication to the underlying session object and the component
    //objects.  A reference to a HIFramework is passed into init() and from this
    //reference it is possible to access all GUI components among, other things, over
    //the course of a few simple steps.  See below on how to get a CustomTerminal from
    //the HIFramework and then get the SCCustomComponents from the CustomTerminal.
    public void init(HIFramework h){

        //the getHostTerminal method in HIFramework will return the HostTerminal
        //instance for the current session
        HostTerminal hostTerminal = h.getHostTerminal();

        //in order to gain access to SCCI components, a CustomTerminal is needed.
        //fortunately, the HostTerminal returned above may be cast to a
        //CustomTerminal in the scope of all CustomInterface programs running
        //under Screen Customizer.
        CustomTerminal customTerminal = (CustomTerminal)hostTerminal;

        //one of the methods in CustomTerminal will return an array of
        //SCCustomComponents.  These are the components currently displayed on the
        //screen.  See the javadoc for com.ibm.hi.customizer.beans.scci for a
        //complete listing and description of the available methods for all
        //SCCustomComponents
        SCCustomComponent [] comps = customTerminal.getCustomComponents();

        //step through all the components and set the caption to "Hello World!!!"
        for(int i=0; null != comps && i<comps.length; i++){
            comps[i].setCaption("Hello World!!!");
        }
    }

    //this update method is defined in CustomInterface but is not used here
    public void update(HIFramework h){
        //not implemented
    }
}

HelloWorldApplet2

If you ran HelloWorldApplet you may have noticed that some of the SCLabels and SCTextfields may have changed colors. This is because the String "Hello World!!!" is longer than the available space in the SCLabel or SCTextfield, so that the entire String cannot be displayed; Screen Customizer changes the color of the field to warn you. You also may have noticed that, if you selected Refresh from the Actions menu item, or you navigated away from the initial screen, HelloWorldApplet ceased to to have influence on the SCCustomComponents.

HelloWorldApplet2 addresses these limitations of HelloWorldApplet by checking the size of the labels and textfields, and by listening for ScreenEvents.

//HelloWorldApplet2 behaves similarly to HelloWorld, however
//only addresses SCLabels and SCTextfields and persists over
//the entire duration of the currentSession.
import com.ibm.eNetwork.beans.HOD.*;
import com.ibm.eNetwork.HOD.*;
import com.ibm.hi.customizer.beans.*;
import com.ibm.hi.customizer.beans.scci.*;
import com.ibm.hi.customizer.beans.event.*;        //picks up CustomListener and the CustomEvents
//HelloWorld2 implements CustomListener which enables it to listen for CustomEvents.
public class HelloWorldApplet2 implements CustomInterface, CustomListener{

    private HostTerminal   hostTerminal   = null;  //hang onto HostTerminal and CustomTerminal
    private CustomTerminal customTerminal = null;

    //default constructor
    public HelloWorldApplet2(){
        //not implemented
    }

    public void init(HIFramework h){
        hostTerminal = h.getHostTerminal();
        customTerminal = (CustomTerminal)hostTerminal;

        //here we add HelloWorld2 as a listener for CustomEvents fired from
        //CustomTerminal, see customScreenEvent(), customStatusEvent(), and
        //customMacroEvent() below
        customTerminal.addCustomListener(this);
        sayHello();
    }

    public void update(HIFramework h){
        //not implemented
    }

    //sayHello is a private method that changes the text only on
    //SCTextfields and SClabels
    private void sayHello(){

        //note that arrays of individual components may be retrieved
        //from CustomTerminal
        SCTextfield [] textfields = customTerminal.getTextfields();

        //step through each of the SCTextfields and say hello
        //note that this is not an optimal solution
        for(int i=0; null != textfields && i<textfields.length; i++){
            String hello = "Hello Textfield "+i+"!!!";
            int columns = textfields[i].getColumns(); //the width of the field
            //trim our hello to the length of the field
            if(hello.length() > columns){
                hello = hello.substring(0, columns);
            }
            //say hello
            textfields[i].setCaption(hello);
        }

        //retrieve and step through each of the SCLabels and say hello
        SCLabel [] labels = customTerminal.getLabels();
        for(int i=0; null != labels && i<labels.length; i++){
            String hello = "Hello Label "+i+"!!!";
            int columns = labels[i].getColumns(); //the width of the field
            //trim our hello to the length of the field
            if(hello.length() > columns){
                hello = hello.substring(0, columns);
            }
            //say hello
            labels[i].setCaption(hello);
        }

        //SCButtons [] buttons = customTerminal.getButtons()...
        //each type of SCCustomComponent may be gotten from
        //CustomTerminal
    }

    //customScreenEvent is defined in CustomListener
    public void customScreenEvent(CustomScreenEvent evt){
        //get the eventType out of the CustomScreenEvent using getScreenType()
        //and if it is a SCREEN_READY event, sayHello
        if(CustomScreenEvent.SCREEN_READY == evt.getScreenType())
            sayHello();
    }

    //this customStatusEvent method is defined in CustomListener but is not used here
    public void customStatusEvent(CustomStatusEvent evt){
        //not implemented
    }

    //this customMacroEvent method is defined in CustomListener but is not used here
    public void customMacroEvent(CustomMacroEvent evt){
        //not implemented
    }
}

HelloWorldApplication

In addition to writing SCCI applets, you can write SCCI applications to run standalone. There are a few fundamental differences. The first is that there are no interfaces to implement. The second is that your application needs to create its own CustomTerminal.


import com.ibm.eNetwork.beans.HOD.*;
import com.ibm.eNetwork.HOD.*;
import com.ibm.hi.customizer.beans.*;
import com.ibm.hi.customizer.beans.scci.*;
import com.ibm.hi.customizer.beans.event.*;

//It is not necessary for applications using a CustomTerminal to access
//SCCustomComponents to implement any interfaces.  In this example,
//HelloWorldApplication implements CustomListener.  HelloWorldApplication
//differs from HelloWorld2 in only that there are neither init() nor
//update() methods as required by CustomInterface, the CustomTerminal is
//created in the default constructor (it was extracted from the HIFramework
//passed in to init() in HelloWorld2), and the addition of a main() method
//to drive HelloWorldApplication
public class HelloWorldApplication implements CustomListener{

    private CustomTerminal customTerminal = null;

    public HelloWorldApplication(String hostName){

        //create a properties object to pass into the CustomTerminal constructor
        java.util.Properties properties = new java.util.Properties();
        properties.put(Session.HOST, hostName);

        try{
            customTerminal = new CustomTerminal(properties);
            customTerminal.addCustomListener(this);
            //notice that we are not yet attempting to get the SCCustomComponents
            //from our CustomTerminal.  The CustomTerminal constructor is
            //asynchronous and may return before communication has been set up
            //successfully which would mean that a call requesting the
            //SCCustomComponents may return null.
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    private void sayHello(){
        SCTextfield [] textfields = customTerminal.getTextfields();
        for(int i=0; null != textfields && i<textfields.length; i++){
            String hello = "Hello Textfield "+i+"!!!";
            int columns = textfields[i].getColumns();
            if(hello.length() > columns){
                hello = hello.substring(0, columns);
            }
            textfields[i].setCaption(hello);
        }
        SCLabel [] labels = customTerminal.getLabels();
        for(int i=0; null != labels && i<labels.length; i++){
            String hello = "Hello Label "+i+"!!!";
            int columns = labels[i].getColumns();
            if(hello.length() > columns){
                hello = hello.substring(0, columns);
            }
            labels[i].setCaption(hello);
        }
    }

    public void customScreenEvent(CustomScreenEvent evt){
        if(CustomScreenEvent.SCREEN_READY == evt.getScreenType())
            sayHello();
    }

    public void customStatusEvent(CustomStatusEvent evt){
    }
    public void customMacroEvent(CustomMacroEvent evt){
    }

    //a accessory method for our customTerminal
    public CustomTerminal getCustomTerminal(){
        return customTerminal;
    }

    //A simple main method
    public static void main(String [] args){

        //get the hostName
        String hostName = null;
        if((args.length < 1) || ((hostName = args[0]) == null)){
            System.out.println("invalid hostName");
            System.out.println("usage: java HelloWorldApplication hostName");
            System.out.println("eg: java HelloWorldApplication clemson.clemson.edu");
            System.exit(1);
        }

        //Create a HelloWorldApplication
        HelloWorldApplication hello = new HelloWorldApplication(hostName);

        //Define a simple Frame that will listen to window Events
        class HelloFrame extends java.awt.Frame{
            public HelloFrame(){
                addWindowListener(new java.awt.event.WindowAdapter(){
                    public void windowClosing(java.awt.event.WindowEvent e){
                        System.exit(0);
                        }
                });
            }
        }
        //Create a frame our newly defined frame
        HelloFrame helloFrame = new HelloFrame();
        //Get the CustomTerminal from our HelloWorld Application
        //and add it to the frame
        helloFrame.add(hello.getCustomTerminal());
        //Set the size of the frame and show it
        helloFrame.setSize(helloFrame.getPreferredSize());
        helloFrame.show();
    }

}

Please refer to classpath and compiling to build HelloWorldApplication; to execute it, from a command prompt, type:

c:\hostondemand\hod>c:\jdk\bin\java HelloWorldApplication

HelloWorldApplication2

This final HelloWorld program acts as an application from the standpoint that it runs standalone, but it may be run through a web browser as a java.applet.Applet.

import com.ibm.eNetwork.beans.HOD.*;
import com.ibm.eNetwork.HOD.*;
import com.ibm.hi.customizer.beans.*;
import com.ibm.hi.customizer.beans.scci.*;
import com.ibm.hi.customizer.beans.event.*;
import java.applet.*;
import java.awt.*;

//HelloWorldApplication2 does not add any functionality over HelloWorldApplication.
//The only difference is that it is an Applet (a java.applet.Applet) and my be run
//from a webpage.
public class HelloWorldApplication2 extends Applet implements CustomListener{
    private CustomTerminal customTerminal = null;

    public HelloWorldApplication2(){
    }

    public void init(){

        //Gets the hostName out of the html file.
        String hostName = null;
        if((hostName = getParameter("hostname")) == null){
            System.out.println("invalid hostName");
            System.out.println("be sure include a valid hostname as a parameter in your html file");
            System.exit(1);
        }

        java.util.Properties properties = new java.util.Properties();
        properties.put(Session.HOST, hostName);
        try{
            customTerminal = new CustomTerminal(properties);
            customTerminal.addCustomListener(this);
            //Because we are an java.applet.Applet now we add the custom
            //terminal directly to the applet
            setLayout(new BorderLayout());
            add(BorderLayout.CENTER, customTerminal);
            setBackground(customTerminal.getBackground());
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    private void sayHello(){
        SCTextfield [] textfields = customTerminal.getTextfields();
        for(int i=0; null != textfields && i<textfields.length; i++){
            String hello = "Hello Textfield "+i+"!!!";
            int columns = textfields[i].getColumns();
            if(hello.length() > columns){
                hello = hello.substring(0, columns);
            }
            textfields[i].setCaption(hello);
        }
        SCLabel [] labels = customTerminal.getLabels();
        for(int i=0; null != labels && i<labels.length; i++){
            String hello = "Hello Label "+i+"!!!";
            int columns = labels[i].getColumns();
            if(hello.length() > columns){
                hello = hello.substring(0, columns);
            }
            labels[i].setCaption(hello);
        }
    }

    public void customScreenEvent(CustomScreenEvent evt){
        if(CustomScreenEvent.SCREEN_READY == evt.getScreenType())
            sayHello();
    }

    public void customStatusEvent(CustomStatusEvent evt){
    }

    public void customMacroEvent(CustomMacroEvent evt){
    }

}

Here is a simple HTML file to run HelloWorldApplication2

<HTML>
<BODY>
<APPLET CODE="HelloWorldApplication2.class" WIDTH=650 HEIGHT=500>
</APPLET>
</BODY>
</HTML>

Please refer to classpath and compiling to build HelloWorldApplication2; to execute it, from a command prompt, type:

c:\hostondemand\hod>c:\jdk\bin\appletviewer HelloWorldApplication2.html



[ Top of Page | Previous Page | Next Page | Table of Contents]