Package org.jcsp.plugNplay

# Class FramedButtonArray

java.lang.Object
org.jcsp.plugNplay.FramedButtonArray

All Implemented Interfaces:
  `CSProcess`

public final class FramedButtonArray extends Object implements CSProcess
A free-standing array of button processes in their own frame, with configure and event channels.

## Process Diagram

Please check out the process diagram for a framed single button in `FramedButton`. Imagine here an array of these, each with individual configure and event channels.

## Description

This process provides a free-standing array of button processes in their own frame. They are just `ActiveButton`s wrapped in an `ActiveClosingFrame`, but save us the trouble of constructing them. They may be displayed in a row or column.

Wire them to application processes with configure channels (for setting labels, enabling/disabling and all other configuration options) and event channels (on which the current label on any button is sent when that button is clicked). Note that all the events may be streamed to the same channel, provided an `Any2*Channel` is used (as in the example below).

Initially, all button labels are empty `java.lang.String`s. To set a button label, send a `java.lang.String` down the appropriate configure channel.

Initially, all buttons are enabled. To disable a button, send `java.lang.Boolean.FALSE` down the appropriate configure channel. To enable, send `java.lang.Boolean.TRUE`.

For other configuration options, send objects implementing the `ActiveButton.Configure` interface.

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 myButtonEvent =
         Channel.one2one (new OverWriteOldestBuffer (n));

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.

## Example

This runs a framed button array in parallel with a simple application process (in-lined in the ``Parallel`` below). All event channels from the buttons are mulitplexed through an `Any2OneChannel` to the application process. The application configures the buttons with their labels, then reports each time any of them is pressed. The application ends when the button labelled `Goodbye World' is pressed.

     import org.jcsp.lang.*;
     import org.jcsp.util.*;
     import org.jcsp.plugNplay.*;

     public class FramedButtonArrayExample {

       public static void main (String argv[]) {

         // labels for the array of buttons

         final String[] label = {"JCSP", "Rocket Science", "occam-pi", "Goodbye World"};

         final int nButtons = label.length;

         // row or column?

         final boolean horizontal = true;

         // initial pixel sizes for the frame for the button array

         final int pixDown = 20 + (horizontal ? 120 : nButtons*120);
         final int pixAcross = horizontal ? nButtons*120 : 120;

         // all button events are wired (for this example) to the same channel ...

         final Any2OneChannel allEvents =
           Channel.any2one (new OverWriteOldestBuffer (10));

         final Any2OneChannel[] event = new Any2OneChannel[nButtons];

         for (int i = 0; i invalid input: '<' nButtons; i++) {
           event[i] = allEvents;
         }

         // each button is given its own configuration channel ...

         final One2OneChannel[] configure = Channel.one2oneArray (nButtons);

         // make the array of buttons ...

         final FramedButtonArray buttons =
           new FramedButtonArray (
             "FramedButtonArray Demo", nButtons,
             pixDown, pixAcross, horizontal,
             Channel.getInputArray (configure), Channel.getOutputArray (event)
           );

         // testrig ...

         new Parallel (

           new CSProcess[] {

             buttons,

             new CSProcess () {

               public void run () {

                 for (int i = 0; i invalid input: '<' nButtons; i++) {
                   configure[i].out ().write (label[i]);
                 }

                 boolean running = true;
                 while (running) {
                   final String s = (String) allEvents.in ().read ();
                   System.out.println ("Button `" + s + "' pressed ...");
                   running = (s != label[nButtons - 1]);
                 }

                 System.exit (0);

               }

             }

           }
         ).run ();

       }

     }

Author:
  P.H. Welch
See Also:
  - `ActiveButton`
  - `FramedButton`
  - `FramedButtonGrid`
  - `FramedScrollbar`

## Constructor Summary

Constructors
Constructor
Description
`FramedButtonArray(String title, int nButtons, int pixDown, int pixAcross, boolean horizontal, ChannelInput[] configure, ChannelOutput[] event)`
Construct a framed button array process.

## Method Summary

Modifier and Type
Method
Description
`void`
`run()`
This defines the actions of the process.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### FramedButtonArray

public FramedButtonArray(String title, int nButtons, int pixDown, int pixAcross, boolean horizontal, ChannelInput[] configure, ChannelOutput[] event)
Construct a framed button array process.

Parameters:
  `title` - the title for the frame (must not be null)
  `nButtons` - the number of buttons (must be at least 1)
  `pixDown` - the pixel hieght of the frame (must be at least 100)
  `pixAcross` - the pixel width of the frame (must be at least 100)
  `horizontal` - `true` for a horizontal array of buttons, `false` for a vertical one
  `configure` - the configure channels for the buttons (must not be null)
  `event` - the event channels from the buttons (must not be null)

## Method Details

### run

public void run()
Description copied from interface: `CSProcess`
This defines the actions of the process.

Specified by:
  `run` in interface `CSProcess`