Top
Interstage Studio J Business Kit User's Guide
FUJITSU Software

2.3.10 Notifying Applet Activation and Inactivation

This section describes the notification of applet activation and inactivation used in JBK Plugin.

An HTML document can have several applets in it, so the browser may display several applets in its HTML document window at a time. Therefore, the active and inactive status of each applet does not always match those of the browser window.

JBK Plugin defines the active and inactive status of the applet on its own terms and notifies the change of the status to the applet. When constructing system with several applets, this functionality gives the ability of changing some environment for executing applets according to which applet is activated.

When an Applet Becomes Active or Inactive?

In the following cases, JBK Plugin considers that an applet becomes active:

On the other hand, JBK Plugin considers that an applet becomes inactive in the following cases:

Notification Event for the Applet Activation and Inactivation

When an active applet becomes inactive or an inactive one becomes active, JBK Plugin notifies the PluginAppletEvent to the applet. The PluginAppletEvent class is provided by JBK Plugin.

The API of the PluginAppletEvent class is as shown below:

Methods to Receive the Notification Event

When receiving a PluginAppletEvent, follow this procedure:

  1. First, create an event listener to receive the PluginAppletEvent.

    The listener has to implement the PluginAppletListener interface, which is provided by JBK Plugin.

    The API of the PluginAppletEvent interface is as shown below:

    • Interface

      • com.fujitsu.jbk.plugin.browser.PluginAppletListener
        (extends java.util.EventListener)

    • Methods

      • eventOccurred

        receives the event notified from JBK Plugin.

        public abstract void eventOccurred(PluginAppletEvent event)

        event

        the event notified to the applet

  2. Next, register the listener in the applet context (PluginAppletContext).

    The listener created has to be registered in the applet context. The applet context of JBK Plugin (PluginAppletContext class) provides the methods for adding and removing the listener in it.

    The API is shown below:

    • Interface

      • com.fujitsu.jbk.plugin.browser.PluginAppletContext
        (extends java.applet.AppletContext)

    • Methods

      • addPluginAppletListener

        Adds an event listener to receive the notification event from JBK Plugin.

        public abstract void addPluginAppletListener(PluginAppletListener listener)

        listener

        an event listener to be added

      • removePluginAppletListener

        Removes an event listener to receive the notification event from JBK Plugin.

        public abstract void removePluginAppletListener(PluginAppletListener listener)

        listener

        an event listener to be removed

      • activateApplet

        If the given applet belongs to this applet context, this method activates it. If it does not, this method does nothing.

        public abstract void activateApplet(Applet applet)

        applet

        an applet to be activated

The class file for the PluginAppletEvent class, the PluginAppletListener interface and the PluginAppletContext interface is stored in the jar file for the development of the JBK plugin ("classes\jbkstd.jar" in the JBK installation folder). When compiling the class of an applet that uses the PluginAppletContext interface, check whether the jar file for the development of the JBK plugin is included in the classpath.

Example

An example of handling the notification event of the active and inactive status

The applet context of JBK Plugin can be retrieved using the getAppletContext() method of java.applet.Applet class. An example of handling the notification events is shown below.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

import com.fujitsu.jbk.plugin.browser.PluginAppletEvent;
import com.fujitsu.jbk.plugin.browser.PluginAppletListener;
import com.fujitsu.jbk.plugin.browser.PluginAppletContext;

/**
 * Sample applet for handling its active and inactive status.
 * This applet implements the PluginAppletListener interface and 
 * receives the PluginAppletEvent event.
 */
public class ActivateSample extends Applet implements PluginAppletListener {

    /**
     * Initializes the applet.
     */
    public void init() {
        setBackground(Color.lightGray);
        // Gets the applet context.
        // When this applet runs in JBK Plugin,
        // getAppletContext() returns an instanceof the PluginAppletContext.
        AppletContext context = getAppletContext();
        try {
            // Checks whether the applet context is an instance of PluginAppletContext class.
            if (context instanceof PluginAppletContext) {
                PluginAppletContext plgContext = (PluginAppletContext)context;
                // Adds an event listener to receive PluginAppletEvent
                    plgContext.addPluginAppletListener(this);
            }
        } catch (Throwable ignore) {
            // do nothing
        }
    }

    /**
     * Receives the notification event (PluginAppletEvent) from JBK Plugin.
     * This method changes the background color of the applet
     * according to its active or inactive status.
     */
    public void eventOccurred(PluginAppletEvent e) {
        switch (e.getID()) {
            case PluginAppletEvent.PLUGINAPPLET_ACTIVATE:
                // The applet becomes active.
                setBackground(Color.red);
                repaint();
                break;
            case PluginAppletEvent.PLUGINAPPLET_DEACTIVATE:
                // The applet becomes inactive.
                setBackground(Color.lightGray);
                repaint();
                break;
            default;
                break;
        }
    }
}

Point

Forcibly activating an applet when the browser becomes active

You can configure the browser such that an applet is forcibly activated when the browser becomes active. To do so, include the following line in jbkplugin.properties:

jbk.plugin.sw.fbc.force_activate=<Specifies whether or not to activate an applet when the browser becomes active>

Assign one of the following values to the <Specifies whether or not to activate an applet when the browser becomes active> option:

true

Activates an applet when the browser becomes active.

false

Does not activate an applet when the browser becomes active.

By default, false is assigned to jbk.plugin.sw.fbc.force_activate. If true is specified and the browser window has more than one applet, the applet that is activated cannot be predicted.

Note that in Internet Explorer 7 and later, focus returns to the part that had focus immediately previously when the browser is active, so the above setting is generally not necessary.