jcsp-master/src/main/java/jcsp/awt/ActiveFileDialog.java

//////////////////////////////////////////////////////////////////////
//                                                                  //
//  JCSP ("CSP for Java") Libraries                                 //
//  Copyright (C) 1996-2018 Peter Welch, Paul Austin and Neil Brown //
//                2001-2004 Quickstone Technologies Limited         //
//                2005-2018 Kevin Chalmers                          //
//                                                                  //
//  You may use this work under the terms of either                 //
//  1. The Apache License, Version 2.0                              //
//  2. or (at your option), the GNU Lesser General Public License,  //
//       version 2.1 or greater.                                    //
//                                                                  //
//  Full licence texts are included in the LICENCE file with        //
//  this library.                                                   //
//                                                                  //
//  Author contacts: P.H.Welch@kent.ac.uk K.Chalmers@napier.ac.uk   //
//                                                                  //
//////////////////////////////////////////////////////////////////////

package jcsp.awt;

import java.awt.*;
import java.util.Vector;

import jcsp.lang.*;

/**
 * {@link FileDialog <TT>java.awt.FileDialog</TT>}
 * with a channel interface.
 * <H2>Process Diagram</H2>
 * <p><img src="doc-files/ActiveFileDialog1.gif"></p>
 * <H2>Description</H2>
 * <TT>ActiveFileDialog</TT> is a process extension of <TT>java.awt.FileDialog</TT>
 * 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 <I>Listener</I> to this component).
 * <P>
 * All channels are optional.  The <TT>configure</TT> and <TT>event</TT> channels are
 * settable from a constructor.
 * The <TT>event</TT> channel delivers directory and file names (see next paragraph).
 * Other event channels can be added to notify the occurrence of any other events
 * the component generates (by calling the appropriate
 * <TT>add</TT><I>XXX</I><TT>EventChannel</TT> method <I>before</I> the process is run).
 * Messages can be sent down the <TT>configure</TT> channel at any time to configure
 * the component.  See the <A HREF="#Protocols">table below</A> for details.
 * <P>
 * The <TT>ActiveFileDialog</TT> is made visible by sending a <TT>Boolean.TRUE</TT> down
 * its <TT>configure</TT> channel.  A <I>modal</I> dialogue takes place to select a file
 * and the selected directory and file names are output down its <TT>event</TT> channel.
 * <P>
 * 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 &ndash; no deadlock can occur.
 * <P>
 * <I>IMPORTANT: it is essential that event channels from this process are
 * always serviced &ndash; 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:</I>
 * <PRE>
 *   final One2OneChannel myFileDialogConfigure = Channel.one2one ();
 *   final One2OneChannel myFileDialogEvent = Channel.one2one (new OverWriteOldestBuffer (n));
 * 
 *   final ActiveFileDialog myFileDialog =
 *     new ActiveFileDialog (myFileDialogConfigure.in (), myFileDialogEvent.out ());
 * </PRE>
 * <I>This will ensure that the Java Event Thread will never be blocked.
 * Slow or inattentive readers may miss rapidly generated events, but
 * the </I><TT>n</TT><I> most recent events will always be available.</I>
 * </P>
 * <H2><A NAME="Protocols">Channel Protocols</A></H2>
 * <CENTER>
 * <TABLE BORDER="2">
 *   <TR>
 *     <TH COLSPAN="3">Input Channels</TH>
 *   </TR>
 *   <TR>
 *     <TH ROWSPAN="3">configure</TH>
 *     <TD>String</TD>
 *     <TD>
 *       The start directory for the fileDialog
 *     </TD>
 *   </TR>
 *   <TR>
 *     <TD>Boolean</TD>
 *     <TD>
 *       <OL>
 *         <LI>If this is the <TT>Boolean.TRUE</TT> object,
 *           the fileDialog is made visible</LI>
 *         <LI>If this is the <TT>Boolean.FALSE</TT> object,
 *            the fileDialog is made invisible</LI>
 *         <LI>Other <TT>Boolean</TT> objects are ignored</LI>
 *       </OL>
 *     </TD>
 *   </TR>
 *   <TR>
 *     <TD>ActiveFileDialog.Configure</TD>
 *     <TD>Invoke the user-defined <TT>Configure.configure</TT> method on the activeFileDialog.</TD>
 *   </TR>
 *   <TR>
 *     <TH COLSPAN="3">Output Channels</TH>
 *   </TR>
 *   <TR>
 *     <TH>event</TH>
 *     <TD>String, String</TD>
 *     <TD>The directory and file names generated by the fileDialog &ndash; these may be null</TD>
 *   </TR>
 *   <TR>
 *     <TH>windowEvent</TH>
 *     <TD>WindowEvent</TD>
 *     <TD>See the {@link #addWindowEventChannel
 *         <TT>addWindowEventChannel</TT>} method.</TD>
 *   </TR>
 *   <TR>
 *     <TH>containerEvent</TH>
 *     <TD>ContainerEvent</TD>
 *     <TD>See the {@link #addContainerEventChannel
 *         <TT>addContainerEventChannel</TT>} method.</TD>
 *   </TR>
 *   <TR>
 *     <TH>componentEvent</TH>
 *     <TD>ComponentEvent</TD>
 *     <TD>See the {@link #addComponentEventChannel
 *         <TT>addComponentEventChannel</TT>} method.</TD>
 *   </TR>
 *   <TR>
 *     <TH>focusEvent</TH>
 *     <TD>FocusEvent</TD>
 *     <TD>See the {@link #addFocusEventChannel
 *         <TT>addFocusEventChannel</TT>} method.</TD>
 *   </TR>
 *   <TR>
 *     <TH>keyEvent</TH>
 *     <TD>KeyEvent</TD>
 *     <TD>See the {@link #addKeyEventChannel
 *         <TT>addKeyEventChannel</TT>} method.</TD>
 *   </TR>
 *   <TR>
 *     <TH>mouseEvent</TH>
 *     <TD>MouseEvent</TD>
 *     <TD>See the {@link #addMouseEventChannel
 *         <TT>addMouseEventChannel</TT>} method.</TD>
 *   </TR>
 *   <TR>
 *     <TH>mouseMotionEvent</TH>
 *     <TD>MouseEvent</TD>
 *     <TD>See the {@link #addMouseMotionEventChannel
 *         <TT>addMouseMotionEventChannel</TT>} method.</TD>
 *   </TR>
 * </TABLE>
 * </CENTER>
 * <H2>Example</H2>
 * <PRE>
 * import java.awt.*;
 * import jcsp.lang.*;
 * import jcsp.util.*;
 * import jcsp.awt.*;
 * 
 * public class ActiveFileDialogExample {
 * 
 *   public static void main (String argv[]) {
 * 
 *     final Frame root = new Frame ();
 * 
 *     final One2OneChannel configure = Channel.one2one ();
 * 
 *     final One2OneChannel event = Channel.one2one (new OverWriteOldestBuffer (10));
 * 
 *     final ActiveFileDialog fileDialog =
 *       new ActiveFileDialog (configure.in (), event.out (), root, "ActiveFileDialog Example");
 * 
 *     new Parallel (
 *       new CSProcess[] {
 *         fileDialog,
 *         new CSProcess () {
 *           public void run () {
 *             String dir = ".";           // start directory for the file dialogue
 *             String file = "";
 *             while (file != null) {
 *               configure.out ().write (dir);
 *               configure.out ().write (Boolean.TRUE);
 *               dir = (String) event.in ().read ();
 *               file = (String) event.in ().read ();
 *               if (file != null)
 *                 System.out.println ("Chosen file = `" + dir + file + "'");
 *             }
 *           System.exit (0);
 *           }
 *         }
 *       }
 *     ).run ();
 *   }
 * 
 * }
 * </PRE>
 *
 * @see FileDialog
 * @see java.awt.event.WindowEvent
 * @see java.awt.event.ContainerEvent
 * @see java.awt.event.ComponentEvent
 * @see java.awt.event.FocusEvent
 * @see java.awt.event.KeyEvent
 * @see java.awt.event.MouseEvent
 * @see jcsp.util.OverWriteOldestBuffer
 *
 * @author P.D. Austin and P.H. Welch
 */

public class ActiveFileDialog extends FileDialog implements CSProcess
{
   /**
    * The Vector construct containing the handlers.
    */
   private Vector vec = new Vector();
   
   /**
    * The channel from which configuration messages arrive.
    */
   private ChannelInput configure;
   
   /**
    * The channel to which the selected directory and file names are sent.
    */
   private ChannelOutput event;
   
   /**
    * Constructs a new <TT>ActiveDialog</TT> with a blank title for loading a file
    * and with no configuration or event channels.
    * Constructs a new <TT>ActiveFileDialog</TT>.
    *
    * @param parent the parent frame for the fileDialog.
    */
   public ActiveFileDialog(Frame parent)
   {
      this(null, null, parent, "", LOAD);
   }
   
   /**
    * Constructs a new <TT>ActiveFileDialog</TT> with a blank title
    * and no configuration or event channels.
    *
    * @param parent the parent frame for the fileDialog.
    * @param mode java.awt.FileDialog.LOAD or java.awt.FileDialog.SAVE.
    */
   public ActiveFileDialog(Frame parent, int mode)
   {
      this(null, null, parent, "", mode);
   }
   
   /**
    * Constructs a new <TT>ActiveFileDialog</TT> for loading a file
    * and no configuration or event channels.
    *
    * @param parent the parent frame for the fileDialog.
    * @param title the title of the fileDialog.
    */
   public ActiveFileDialog(Frame parent, String title)
   {
      this(null, null, parent, title, LOAD);
   }
   
   /**
    * Constructs a new <TT>ActiveFileDialog</TT>
    * with no configuration or event channels.
    *
    * @param parent the parent frame for the fileDialog.
    * @param title the title of the fileDialog.
    * @param mode java.awt.FileDialog.LOAD or java.awt.FileDialog.SAVE.
    */
   public ActiveFileDialog(Frame parent, String title, int mode)
   {
      this(null, null, parent, title, mode);
   }
   
   /**
    * Constructs a new <TT>ActiveFileDialog</TT> with a blank title for loading a file.
    *
    * @param configure the channel for configuration events
    * &ndash; can be null if no configuration is required.
    * @param event the directory and file names will be output whenever selected
    * &ndash; can be null if no notification is required.
    * @param parent the parent frame for the fileDialog.
    */
   public ActiveFileDialog(ChannelInput configure, ChannelOutput event, Frame parent)
   {
      this(configure, event, parent, "", LOAD);
   }
   
   /**
    * Constructs a new <TT>ActiveFileDialog</TT> with a blank title.
    *
    * @param configure the channel for configuration events
    * &ndash; can be null if no configuration is required.
    * @param event the directory and file names will be output whenever selected
    * &ndash; can be null if no notification is required.
    * @param parent the parent frame for the fileDialog.
    * @param mode java.awt.FileDialog.LOAD or java.awt.FileDialog.SAVE.
    */
   public ActiveFileDialog(ChannelInput configure, ChannelOutput event, Frame parent, int mode)
   {
      this(configure, event, parent, "", mode);
   }
   
   /**
    * Constructs a new <TT>ActiveFileDialog</TT> for loading a file.
    *
    * @param configure the channel for configuration events
    * &ndash; can be null if no configuration is required.
    * @param event the directory and file names will be output whenever selected
    * &ndash; can be null if no notification is required.
    * @param parent the parent frame for the fileDialog.
    * @param title the title of the fileDialog.
    */
   public ActiveFileDialog(ChannelInput configure, ChannelOutput event, Frame parent, String title)
   {
      this(configure, event, parent, title, LOAD);
   }
   
   /**
    * Constructs a new <TT>ActiveFileDialog</TT>.
    *
    * @param configure the channel for configuration events
    * &ndash; can be null if no configuration is required.
    * @param event the directory and file names will be output whenever selected
    * &ndash; can be null if no notification is required.
    * @param parent the parent frame for the fileDialog.
    * @param title the title of the fileDialog.
    * @param mode java.awt.FileDialog.LOAD or java.awt.FileDialog.SAVE.
    * <!-- @param modal if true, fileDialog blocks input to the parent window when shown. -->
    */
   public ActiveFileDialog(ChannelInput configure, ChannelOutput event, Frame parent, String title, int mode)
   {
      super(parent, title, mode);
      this.configure = configure;
      this.event = event;
   }
   
   /**
    * Sets the configuration channel for this <TT>ActiveFileDialog</TT>.
    * This method overwrites any configuration channel set in the constructor.
    *
    * @param configure the channel for configuration events
    * &ndash; can be null if no configuration is required.
    */
   public void setConfigureChannel(ChannelInput configure)
   {
      this.configure = configure;
   }
   
   /**
    * Add a new channel to this component that will be used to notify that
    * a <TT>WindowEvent</TT> has occurred. <I>This should be used
    * instead of registering a WindowListener with the component.</I> It is
    * possible to add more than one channel by calling this method multiple times
    * If the channel passed is <TT>null</TT>, no action will be taken.
    * <P>
    * <I>NOTE: This method must be called before this process is run.</I>
    *
    * @param windowEvent the channel down which to send ContainerEvents.
    */
   public void addWindowEventChannel(ChannelOutput windowEvent)
   {
      if (windowEvent != null)
      {
         WindowEventHandler handler = new WindowEventHandler(windowEvent);
         addWindowListener(handler);
         vec.addElement(handler);
      }
   }
   
   /**
    * Add a new channel to this component that will be used to notify that
    * a <TT>ContainerEvent</TT> has occurred. <I>This should be used
    * instead of registering a ContainerListener with the component.</I> It is
    * possible to add more than one Channel by calling this method multiple times
    * If the channel passed is <TT>null</TT>, no action will be taken.
    * <P>
    * <I>NOTE: This method must be called before this process is run.</I>
    *
    * @param containerEvent the channel down which to send ContainerEvents.
    */
   public void addContainerEventChannel(ChannelOutput containerEvent)
   {
      if (containerEvent != null)
      {
         ContainerEventHandler handler = new ContainerEventHandler(containerEvent);
         addContainerListener(handler);
         vec.addElement(handler);
      }
   }
   
   /**
    * Add a new channel to this component that will be used to notify that
    * a <TT>ComponentEvent</TT> has occurred. <I>This should be used
    * instead of registering a ComponentListener with the component.</I> It is
    * possible to add more than one Channel by calling this method multiple times
    * If the channel passed is <TT>null</TT>, no action will be taken.
    * <P>
    * <I>NOTE: This method must be called before this process is run.</I>
    *
    * @param componentEvent the channel down which to send ComponentEvents.
    */
   public void addComponentEventChannel(ChannelOutput componentEvent)
   {
      if (componentEvent != null)
      {
         ComponentEventHandler handler = new ComponentEventHandler(componentEvent);
         addComponentListener(handler);
         vec.addElement(handler);
      }
   }
   
   /**
    * Add a new channel to this component that will be used to notify that
    * a <TT>FocusEvent</TT> has occurred. <I>This should be used
    * instead of registering a FocusListener with the component.</I> It is
    * possible to add more than one Channel by calling this method multiple times
    * If the channel passed is <TT>null</TT>, no action will be taken.
    * <P>
    * <I>NOTE: This method must be called before this process is run.</I>
    *
    * @param focusEvent the channel down which to send FocusEvents.
    */
   public void addFocusEventChannel(ChannelOutput focusEvent)
   {
      if (focusEvent != null)
      {
         FocusEventHandler handler = new FocusEventHandler(focusEvent);
         addFocusListener(handler);
         vec.addElement(handler);
      }
   }
   
   /**
    * Add a new channel to this component that will be used to notify that
    * a <TT>KeyEvent</TT> has occurred. <I>This should be used
    * instead of registering a KeyListener with the component.</I> It is
    * possible to add more than one Channel by calling this method multiple times
    * If the channel passed is <TT>null</TT>, no action will be taken.
    * <P>
    * <I>NOTE: This method must be called before this process is run.</I>
    *
    * @param keyEvent the channel down which to send KeyEvents.
    */
   public void addKeyEventChannel(ChannelOutput keyEvent)
   {
      if (keyEvent != null)
      {
         KeyEventHandler handler = new KeyEventHandler(keyEvent);
         addKeyListener(handler);
         vec.addElement(handler);
      }
   }
   
   /**
    * Add a new channel to this component that will be used to notify that
    * a <TT>MouseEvent</TT> has occurred. <I>This should be used
    * instead of registering a MouseListener with the component.</I> It is
    * possible to add more than one Channel by calling this method multiple times
    * If the channel passed is <TT>null</TT>, no action will be taken.
    * <P>
    * <I>NOTE: This method must be called before this process is run.</I>
    *
    * @param mouseEvent the channel down which to send MouseEvents.
    */
   public void addMouseEventChannel(ChannelOutput mouseEvent)
   {
      if (mouseEvent != null)
      {
         MouseEventHandler handler = new MouseEventHandler(mouseEvent);
         addMouseListener(handler);
         vec.addElement(handler);
      }
   }
   
   /**
    * Add a new channel to this component that will be used to notify that
    * a <TT>MouseMotionEvent</TT> has occurred. <I>This should be used
    * instead of registering a MouseMotionListener with the component.</I> It is
    * possible to add more than one Channel by calling this method multiple times
    * If the channel passed is <TT>null</TT>, no action will be taken.
    * <P>
    * <I>NOTE: This method must be called before this process is run.</I>
    *
    * @param mouseMotionEvent the channel down which to send MouseMotionEvents.
    */
   public void addMouseMotionEventChannel(ChannelOutput mouseMotionEvent)
   {
      if (mouseMotionEvent != null)
      {
         MouseMotionEventHandler handler = new MouseMotionEventHandler(mouseMotionEvent);
         addMouseMotionListener(handler);
         vec.addElement(handler);
      }
   }
   
   /**
    * This enables general configuration of this component.  Any object implementing
    * this interface and sent down the <TT>configure</TT> channel to this component will have its
    * <TT>configure</TT> method invoked on this component.
    * <P>
    * For an example, see {@link ActiveApplet.Configure}.
    */
   static public interface Configure
   {
      /**
       * @param fileDialog the FileDialog being configured.
       */
      public void configure(final FileDialog fileDialog);
   }
   
   /**
    * The main body of this process.
    */
   public void run()
   {
      if (configure != null)
      {
         while (true)
         {
            Object message = configure.read();
            if (message instanceof Boolean)
            {
               if (message == Boolean.TRUE)
               {
                  setVisible(true);
                  if (event != null)
                  {
                     event.write(getDirectory());
                     event.write(getFile());
                  }
               }
               else if (message == Boolean.FALSE)
                  setVisible(false);
            }
            else if (message instanceof String)
               setDirectory((String) message);
            else if (message instanceof Configure)
               ((Configure) message).configure(this);
         }
      }
   }  
}
