All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class jcsp.awt.event.ActionEventHandler

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

public class ActionEventHandler
extends Object
implements CSProcess, ActionListener

Process Diagram

   ____________________
  |                    | event
  | ActionEventHandler |--->---
  |____________________|
 

Description

The ActionEventHandler class is not intended to be used by software constructors, instead it is used in the construction of new Active AWT components that generate Action Events (i.e. ActiveButton, ActiveMenuItem).

The ActionEventHandler class implements the ActionListener interface and can be used for any component which allows the addition of ActionListeners. When the component is created a new instance of the ActionEventHandler should be created and added as an ActionListener 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 actionPerformed() method. The process then wakes up and sends the String representing the Action down the event Channel. Any further calls to actionPerformed() 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.

Channel Protocols

Input Channels
Output Channels
event String The string representing the ActionEvent from the component that generated the event.

Example

Extending an Existing Component

 import java.awt.*;
 import jcsp.lang.*;
 import jcsp.awt.event.*;
 public class ChannelButton extends Button implements CSProcess {
   private ActionEventHanlder handler;
   public ChannelButton(ChannelOutput event, String label) {
     super(s);
     if (event !=null) {
       handler = new ActionEventHandler(event);
       addActionListener(handler);
     }
   }
   public void run() {
     if (handler != null) {
       handler.run();
     }
   }
 }
 
NOTE: This is in fact how the ActiveButton and ActiveMenuItem classes are implemented. Except they include 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();
     Button button = new Button("Active");
     add(button);
     ActionEventHandler handler = new ActionEventHandler(event);
     button.addActionListener(handler);
     new Parallel(new CSProcess[] {
       button,
       handler,
       new CSProcess() {
         public void run() {
           while (true) {
             Object o = event.read();
             System.out.println("Button '" + o + "' Pressed");
           }
         }
       }
     }).run();
   }
 }
 

Author:
P.D.Austin

Constructor Index

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

Method Index

 o actionPerformed(ActionEvent)
Invoked when an action occurs on the component the event handler is listening to.
 o run()
The main body of the process.

Constructors

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

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

Methods

 o actionPerformed
 public void actionPerformed(ActionEvent e)
Invoked when an action occurs on the component the event handler is listening to. Notifies the event process that an ActionEvent 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