All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object | +----jcsp.awt.event.ItemEventHandler
__________________ | | event | ItemEventHandler |--->--- |__________________|
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.
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. |
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.
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(); } }
public ItemEventHandler(ChannelOutput event)
public void itemStateChanged(ItemEvent e)
public void run()
All Packages Class Hierarchy This Package Previous Next Index