Package org.jcsp.awt

Class ActiveChoice

All Implemented Interfaces:
ImageObserver, ItemSelectable, MenuContainer, Serializable, Accessible, CSProcess

public class ActiveChoice extends Choice implements CSProcess
java.awt.Choice with a channel interface.

Process Diagram

Description

ActiveChoice is a process extension of java.awt.Choice with channels for run-time configuration and event notification. The event channels should be connected to one or more application-specific server processes (instead of registering a passive object as a Listener to this component).

All channels are optional. The configure and event channels are settable from a constructor. The event channel delivers the generated java.awt.ItemEvent whenever an item from the ActiveChoice is selected. Other event channels can be added to notify the occurrence of any other events the component generates (by calling the appropriate addXXXEventChannel method before the process is run). Messages can be sent down the configure channel at any time to configure the component. See the table below for details.

All channels are managed by independent internal handler processes. It is, therefore, safe for a serial application process both to service an event channel and configure the component -- no deadlock can occur.

IMPORTANT: it is essential that event channels from this process are always serviced -- otherwise the Java Event Thread will be blocked and the GUI will stop responding. A simple way to guarantee this is to use channels configured with overwriting buffers. For example:

   final One2OneChannel myChoiceEvent = Channel.one2one (new OverWriteOldestBuffer (n));
 
   final ActiveChoice myChoice =
     new ActiveChoice (null, myChoiceEvent.out ());
 
This will ensure that the Java Event Thread will never be blocked. Slow or inattentive readers may miss rapidly generated events, but the n most recent events will always be available.

Channel Protocols

Input Channels
configure String Sets the selected item in this ActiveChoice menu to be the item whose name is equal to the specified ActiveChoice. If more than one item matches, the one with the smallest index is selected.
Integer Sets the selected item in this ActiveChoice menu to be the item at the specified position.
Boolean
  1. If this is the Boolean.TRUE object, the choice is made active
  2. If this is the Boolean.FALSE object, the choice is made inactive
  3. Other Boolean objects are ignored
ActiveChoice.Configure Invoke the user-defined Configure.configure method on the choice.
Output Channels
event ItemEvent When the ActiveChoice is operated, the generated java.awt.ItemEvent is written down this channel.
componentEvent ComponentEvent See the addComponentEventChannel method.
focusEvent FocusEvent See the addFocusEventChannel method.
keyEvent KeyEvent See the addKeyEventChannel method.
mouseEvent MouseEvent See the addMouseEventChannel method.
mouseMotionEvent MouseEvent See the addMouseMotionEventChannel method.

Example

 import java.awt.*;
 import java.awt.event.*;
 import org.jcsp.lang.*;
 import org.jcsp.util.*;
 import org.jcsp.awt.*;
 
 public class ActiveChoiceExample {
 
   public static void main (String argv[]) {
 
     final Frame root = new Frame ("ActiveChoice Example");
 
     final One2OneChannel close = Channel.one2one (new OverWriteOldestBuffer (1));
 
     final ActiveChoice choice = new ActiveChoice (null, close.out ());
 
     final String[] menu = {"Hello World", "Rocket Science", "CSP",
                            "Monitors", "Ignore Me", "Goodbye World"};
 
     for (int i = 0; i < menu.length; i++) {
       choice.add (menu[i]);
     }
 
     root.setSize (200, 100);
     root.add (choice);
     root.setVisible (true);
 
     new Parallel (
       new CSProcess[] {
         choice,
         new CSProcess () {
           public void run () {
             boolean running = true;
             while (running) {
               ItemEvent e = (ItemEvent) close.in ().read ();
               String item = (String) e.getItem ();
               System.out.println ("Selected ==> `" + item + "'");
               running = (item != menu[menu.length - 1]);
             }
             root.setVisible (false);
             System.exit (0);
           }
         }
       }
     ).run ();
 
   }
 
 }
 
See Also:
  • Field Details

    • vec

      private Vector vec
      The Vector construct containing the handlers.
    • configure

      private ChannelInput configure
      The channel from which configuration messages arrive.
  • Constructor Details

    • ActiveChoice

      public ActiveChoice()
      Constructs an ActiveChoice with no configuration or event channels.
    • ActiveChoice

      public ActiveChoice(ChannelInput configure, ChannelOutput event)
      Constructs an ActiveChoice with configuration and event channels.
      Parameters:
      configure - the channel for configuration events -- can be null if no configuration is required.
      event - the ItemEvent will be output when the choice is exercised -- can be null if no notification is required.
  • Method Details

    • setConfigureChannel

      public void setConfigureChannel(ChannelInput configure)
      Sets the configuration channel for this ActiveChoice. This method overwrites any configuration channel set in the constructor.
      Parameters:
      configure - the channel for configuration events -- can be null if no configuration is required.
    • addComponentEventChannel

      public void addComponentEventChannel(ChannelOutput componentEvent)
      Add a new channel to this component that will be used to notify that a ComponentEvent has occurred. This should be used instead of registering a ComponentListener with the component. It is possible to add more than one channel by calling this method multiple times If the channel passed is null, no action will be taken.

      NOTE: This method must be called before this process is run.

      Parameters:
      componentEvent - the channel down which to send ComponentEvents.
    • addFocusEventChannel

      public void addFocusEventChannel(ChannelOutput focusEvent)
      Add a new channel to this component that will be used to notify that a FocusEvent has occurred. This should be used instead of registering a FocusListener with the component. It is possible to add more than one channel by calling this method multiple times If the channel passed is null, no action will be taken.

      NOTE: This method must be called before this process is run.

      Parameters:
      focusEvent - the channel down which to send FocusEvents.
    • addKeyEventChannel

      public void addKeyEventChannel(ChannelOutput keyEvent)
      Add a new channel to this component that will be used to notify that a KeyEvent has occurred. This should be used instead of registering a KeyListener with the component. It is possible to add more than one channel by calling this method multiple times If the channel passed is null, no action will be taken.

      NOTE: This method must be called before this process is run.

      Parameters:
      keyEvent - the channel down which to send KeyEvents.
    • addMouseEventChannel

      public void addMouseEventChannel(ChannelOutput mouseEvent)
      Add a new channel to this component that will be used to notify that a MouseEvent has occurred. This should be used instead of registering a MouseListener with the component. It is possible to add more than one channel by calling this method multiple times If the channel passed is null, no action will be taken.

      NOTE: This method must be called before this process is run.

      Parameters:
      mouseEvent - the channel down which to send MouseEvents.
    • addMouseMotionEventChannel

      public void addMouseMotionEventChannel(ChannelOutput mouseMotionEvent)
      Add a new channel to this component that will be used to notify that a MouseMotionEvent has occurred. This should be used instead of registering a MouseMotionListener with the component. It is possible to add more than one channel by calling this method multiple times If the channel passed is null, no action will be taken.

      NOTE: This method must be called before this process is run.

      Parameters:
      mouseMotionEvent - the channel down which to send MouseMotionEvents.
    • run

      public void run()
      The main body of this process.
      Specified by:
      run in interface CSProcess