All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class jcsp.awt.event.ItemEventHandler

java.lang.Object
   |
   +----jcsp.awt.event.ItemEventHandler

public class ItemEventHandler
extends Object
implements CSProcess, ItemListener

Process Diagram

   __________________
  |                  | event
  | ItemEventHandler |--->---
  |__________________|
 

Description

The ItemEventHandler class is not intended to be used by software constructors, instead it is used in the construction of new Active AWT components that generate ItemEvents (i.e. ActiveCheckbox, ActiveChoice).

The ItemEventHandler class implements the ItemListener interface and can be used for any component which allows the addition of ItemListeners. When the component is created a new instance of the ItemEventHandler should be created and added as an ItemListeners to the component. The process must be set running (either asynchronously using ProcessNetwork or as part of a Parallel construct).

The main body of this process waits to be notified by a component invoking the itemStateChanged() method. The process then wakes up and sends a Boolean indicating whether the event was caused because the item had been selected or deselected followed by new Integer set to the current value of the component down the event Channel. Any further calls to itemStateChanged() while the process is writing will be discarded so as not to block the main Java event Thread. When another process reads from the Channel this process loops round and waits to be notified again.

NOTE: As the component generates two Objects for each event the event Channel must be a One2OneChannel.

Channel Protocols

Input Channels
Output Channels
event Boolean followed by Object This component sends two Objects for each event. The first is a Boolean which will be true if the event was for selection false if it was for deselection. The second is an Object representing the item that changed.

Example

Extending an Existing Component

 import java.awt.*;
 import jcsp.lang.*;
 import jcsp.awt.event.*;
 public class ChannelCheckbox extends Checkbox implements CSProcess {
   private ItemEventHandler handler;
   public ChannelCheckbox(ChannelOutput event) {
     super();
     if (event !=null) {
       handler = new ItemEventHandler(event);
       addItemListener(handler);
     }
   }
   public void run() {
     if (handler != null) {
       handler.run();
     }
   }
 }
 
NOTE: This is in fact how the ActiveCheckbox class is implemented. Except it includes an extra CSProcess and Channel for configuring the Component.

Listening to a Component

 import java.awt.*;
 import jcsp.awt.event.*;
 import jcsp.lang.*;
        :
        :
   {
     final Channel event = new One2OneChannel();
     Checkbox checkbox = new Checkbox();
     add(checkbox);
     ItemEventHandler handler = new ItemEventHandler(event);
     checkbox.addItemListener(handler);
     new Parallel(new CSProcess[] {
       checkbox,
       handler,
       new CSProcess() {
         public void run() {
           while (true) {
             Bolean state = (Boolean)event.read();
             Integer value = (Integer)event.read();
             System.out.println("Chackbox state=" + state + " value='" + value);
           }
         }
       }
     }).run();
   }
 }
 

Author:
P.D.Austin

Constructor Index

 o ItemEventHandler(ChannelOutput)
constructs a new ItemEventHandler with the specified event Channel.

Method Index

 o itemStateChanged(ItemEvent)
Invoked when an item change occurs on the component the event handler is listening to.
 o run()
The main body of the process.

Constructors

 o ItemEventHandler
 public ItemEventHandler(ChannelOutput event)
constructs a new ItemEventHandler with the specified event Channel.

Parameters:
event - The Channel ItemEvent notifications are sent down.

Methods

 o itemStateChanged
 public void itemStateChanged(ItemEvent e)
Invoked when an item change occurs on the component the event handler is listening to. Notifies the event process that an ItemEvent has occurred. Some notifications will be lost so there are no guarantees that all events generated will be processed.

Parameters:
e - The parameters associated with this event
 o run
 public void run()
The main body of the process. Executes the functionality described above.


All Packages  Class Hierarchy  This Package  Previous  Next  Index