MultiSelectable Applet Example

In this example, the multiselectable applet, SampleApplet, must initialize the following two arrays of data when it is selected:

  • An array of package data to be initialized when the first applet in the package becomes active

  • An array of private applet data to be initialized upon applet instance activation

You can make these distinctions in your code because the MultiSelectable interface allows the applet to recognize the circumstances under which it is selected.

Also, the applet has the following requirements:

  • Clear the package data once no applet in the package is active

  • Clear the applet private data when the applet instance is deselected

The following methods are responsible for clearing and setting the data:

//dataType parameter as above
final static byte DATA_PRIVATE      = (byte)01;
final static byte DATA_PACKAGE      = (byte)02;
...
public void initData(byte[] dataArray, byte dataType) {
	...
}
public void clearData(byte[] dataArray) {
	...
}

To achieve the behavior specified above, you must modify the selection and deselection methods in your sample applet.

The code for Applet.select(), which is invoked when this applet is the first to become active in the package, can be implemented like this:

public boolean select() {
 
    // First applet to be selected in package, so 
    // initialize package data and applet data
    initData(packageData, DATA_PACKAGE);
    initData(privateData, DATA_PRIVATE);
    return true;
}

Likewise, the implementation of the method MultiSelectable.select(boolean) must determine whether the applet is already active. According to its definition, this method is called when another applet within this package is active. MultiSelectable.select(boolean) can be implemented in a way that if appInstAlreadySelected is false, the applet private data can be initialized. For example:

public boolean select(boolean appInstAlreadySelected) {
    // If boolean parameter is false, 
    // then we have applet activation
    // Otherwise, no applet activation occurs.
    if (appInstAlreadySelected == false) {
        // Initialize applet private data, upon activation
        initData(privateData, DATA_PRIVATE);
    }
    return true;
}

In the case of deselection, the applet data must be cleared. The method MultiSelectable.deselect(boolean) can be implemented in a way that it clears applet data only if the applet is no longer active. For example:

public void deselect(boolean appInstStillSelected) {
 
    // If boolean parameter is false, then applet is no longer
    // active.  It is O.K. to clear applet private data.
    if (appInstStillSelected == false) {
        clearData(privateData);
    }
}

If this applet is the last one to be deactivated from the package, it also must clear package data. This situation results in a call to Applet.deselect(). This method can be implemented like this:


public void deselect() {
    // This call means that the applet is no longer active and
    // that no other applet in the package is.  Data for both
    // applet and package must be cleared.
    clearData(packageData);
    clearData(privateData);
}