Package org.jcsp.awt

Class ActiveMenuItem

  • All Implemented Interfaces:
    java.io.Serializable, javax.accessibility.Accessible, CSProcess

    public class ActiveMenuItem
    extends java.awt.MenuItem
    implements CSProcess
    java.awt.MenuItem with a channel interface.

    Process Diagram

    Description

    ActiveMenuItem is a process extension of java.awt.MenuItem with channels for run-time configuration and event notification. The event channel should be connected to an application-specific server process (instead of registering a passive object as a Listener to this component).

    The configure and event channels are settable from a constructor. The event channel delivers the current label on the ActiveMenuItem whenever it is selected. 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 the event channel and configure the component -- no deadlock can occur.

    IMPORTANT: it is essential that a (non-null) event channel from this process is 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 myMenuItemEvent = Channel.one2one (new OverWriteOldestBuffer (n));
     
       final ActiveMenuItem myMenuItem =
         new ActiveMenuItem (null, myMenuItemEvent.out (), "Choose Me");
     
    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 Change the label on the ActiveMenuItem to the value of the String
    java.awt.MenuShortcut Sets the MenuShortcut for the ActiveMenuItem
    Boolean
    1. If this is the Boolean.TRUE object, the menuItem is made active
    2. If this is the Boolean.FALSE object, the menuItem is made inactive
    3. Other Boolean objects are ignored
    ActiveMenuItem.Configure Invoke the user-defined Configure.configure method on the menuItem.
    Output Channels
    event String The label on the ActiveMenuItem (when the item is selected)

    Example

     import java.awt.*;
     import org.jcsp.lang.*;
     import org.jcsp.util.*;
     import org.jcsp.awt.*;
     
     public class ActiveMenuItemExample {
     
       public static void main (String argv[]) {
     
         final ActiveClosingFrame activeClosingFrame =
           new ActiveClosingFrame ("ActiveMenuItem Example");
     
         final ActiveFrame frame = activeClosingFrame.getActiveFrame ();
     
         final MenuBar menuBar = new MenuBar ();
         frame.setMenuBar (menuBar);
     
         final Menu fileMenu = new Menu ("File");
         final Menu langMenu = new Menu ("Language");
         menuBar.add (fileMenu);
         menuBar.add (langMenu);
     
         final String[] fileOptions = {"Hello World", "Rocket Science", "CSP",
                                       "Monitors", "Ignore Me", "Goodbye World"};
         final String[] langOptions = {"occam-pi", "Java", "Smalltalk", "Algol-60",
                                       "Pascal", "Haskell", "SML", "Lisp"};
     
         final Any2OneChannel event[] = Channel.any2oneArray (2, new OverWriteOldestBuffer (10));
     
         final ActiveMenuItem[] fileMenuItem = new ActiveMenuItem[fileOptions.length];
         for (int i = 0; i < fileOptions.length; i++) {
           fileMenuItem[i] = new ActiveMenuItem (null, event[0].out (), fileOptions[i]);
           fileMenu.add (fileMenuItem[i]);
         }
     
         final ActiveMenuItem[] langMenuItem = new ActiveMenuItem[langOptions.length];
         for (int i = 0; i < langOptions.length; i++) {
           langMenuItem[i] = new ActiveMenuItem (null, event[1].out (), langOptions[i]);
           langMenu.add (langMenuItem[i]);
         }
     
         frame.setSize (300, 200);
         frame.setBackground (Color.green);
         frame.setVisible (true);
     
         new Parallel (
           new CSProcess[] {
             activeClosingFrame,
             new Parallel (fileMenuItem),
             new Parallel (langMenuItem),
             new CSProcess () {
               public void run () {
                 boolean running = true;
                 while (running) {
                   final String s = (String) event[0].in ().read ();
                   System.out.println ("File ==> `" + s + "' selected ...");
                   running = (s != fileOptions[fileOptions.length - 1]);
                 }
                 frame.setVisible (false);
                 System.exit (0);
               }
             },
             new CSProcess () {
               public void run () {
                 while (true) {
                   final String s = (String) event[1].in ().read ();
                   System.out.println ("Language ==> `" + s + "' selected ...");
                 }
               }
             }
           }
         ).run ();
     
       }
     
     }
     
    Author:
    P.D. Austin and P.H. Welch
    See Also:
    MenuItem, ComponentEvent, FocusEvent, KeyEvent, MouseEvent, OverWriteOldestBuffer, Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  ActiveMenuItem.Configure
      This enables general configuration of this component.
      • Nested classes/interfaces inherited from class java.awt.MenuItem

        java.awt.MenuItem.AccessibleAWTMenuItem
      • Nested classes/interfaces inherited from class java.awt.MenuComponent

        java.awt.MenuComponent.AccessibleAWTMenuComponent
    • Constructor Summary

      Constructors 
      Constructor Description
      ActiveMenuItem()
      Constructs a new ActiveMenuItem with no label and no shortcut and no configuration or event channels.
      ActiveMenuItem​(java.lang.String s)
      Constructs a new ActiveMenuItem with no shortcut and no configuration or event channels.
      ActiveMenuItem​(java.lang.String s, java.awt.MenuShortcut ms)
      Constructs a new ActiveMenuItem with no configuration or event channels.
      ActiveMenuItem​(ChannelInput configure, ChannelOutput event)
      Constructs a new ActiveMenuItem with no label and no shortcut.
      ActiveMenuItem​(ChannelInput configure, ChannelOutput event, java.lang.String s)
      Constructs a new ActiveMenuItem with no shortcut.
      ActiveMenuItem​(ChannelInput configure, ChannelOutput event, java.lang.String s, java.awt.MenuShortcut ms)
      Constructs a new ActiveMenuItem.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void run()
      The main body of this process.
      void setConfigureChannel​(ChannelInput configure)
      Sets the configuration channel for this ActiveMenuItem.
      • Methods inherited from class java.awt.MenuItem

        addActionListener, addNotify, deleteShortcut, disable, disableEvents, enable, enable, enableEvents, getAccessibleContext, getActionCommand, getActionListeners, getLabel, getListeners, getShortcut, isEnabled, paramString, processActionEvent, processEvent, removeActionListener, setActionCommand, setEnabled, setLabel, setShortcut
      • Methods inherited from class java.awt.MenuComponent

        dispatchEvent, getFont, getName, getParent, getTreeLock, postEvent, removeNotify, setFont, setName, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ActiveMenuItem

        public ActiveMenuItem()
        Constructs a new ActiveMenuItem with no label and no shortcut and no configuration or event channels.
      • ActiveMenuItem

        public ActiveMenuItem​(java.lang.String s)
        Constructs a new ActiveMenuItem with no shortcut and no configuration or event channels.
        Parameters:
        s - the initial label displayed on the menuItem.
      • ActiveMenuItem

        public ActiveMenuItem​(java.lang.String s,
                              java.awt.MenuShortcut ms)
        Constructs a new ActiveMenuItem with no configuration or event channels.
        Parameters:
        s - the initial label displayed on the menuItem.
        ms - the MenuShortcut for the menuItem.
      • ActiveMenuItem

        public ActiveMenuItem​(ChannelInput configure,
                              ChannelOutput event)
        Constructs a new ActiveMenuItem with no label and no shortcut.
        Parameters:
        configure - the channel for configuration events -- can be null if no configuration is required.
        event - the current label will be output when the menuItem is selected -- can be null if no notification is required.
      • ActiveMenuItem

        public ActiveMenuItem​(ChannelInput configure,
                              ChannelOutput event,
                              java.lang.String s)
        Constructs a new ActiveMenuItem with no shortcut.
        Parameters:
        configure - the channel for configuration events -- can be null if no configuration is required.
        event - the current label will be output when the menuItem is selected -- can be null if no notification is required.
        s - the initial label displayed on the menuItem.
      • ActiveMenuItem

        public ActiveMenuItem​(ChannelInput configure,
                              ChannelOutput event,
                              java.lang.String s,
                              java.awt.MenuShortcut ms)
        Constructs a new ActiveMenuItem.
        Parameters:
        configure - the channel for configuration events -- can be null if no configuration is required.
        event - the current label will be output when the menuItem is selected -- can be null if no notification is required.
        s - the initial label displayed on the menuItem.
        ms - the MenuShortcut for the menuItem.
    • Method Detail

      • setConfigureChannel

        public void setConfigureChannel​(ChannelInput configure)
        Sets the configuration channel for this ActiveMenuItem. 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.
      • run

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