// file: $ISIP_IFC/class/java/ConnectionManager/DemoEventProcessor.java
// version: $Id: DemoEventProcessor.java 10231 2005-09-13 14:29:38Z stanley $

// import necessary java libraries
//
import java.awt.*;
import java.util.*;
    
/**
 * This class allows <code> AWTEvent </code>s to be added to its event
 * list and then processes the events by calling the <code>
 * handleEvent </code> method of the passed <code>
 * DemoEventProcessable </code> object.
 */
public class DemoEventProcessor {

    //-------------------------------------------------------------------------
    //
    // public constants
    //
    //-------------------------------------------------------------------------


    //------------------------------------------------------------------------
    //
    // protected data
    //
    //-----------------------------------------------------------------------
 
    /**
     * Linked list for events needed to handle
     */
    protected LinkedList<AWTEvent> event_list_d;

    /**
     * Event handler
     */
    protected DemoEventProcessable handler_d;

    
    //-------------------------------------------------------------------------
    //
    // public methods
    //
    //-------------------------------------------------------------------------
    /**
     * Creates a new <code> LinkedList </code> to hold events.
     *
     * @param handler_a the <code> DemoEventProcessable </code> that will
     * handle events.  
     *
     */
    public DemoEventProcessor(DemoEventProcessable handler_a) {

	// create a linked list to hold the events
	//
        event_list_d = new LinkedList<AWTEvent>();

	// holder the handler
	//
        this.handler_d = handler_a;
    }
    
    /**
     * Add an AWT event to the event list.
     *
     * @param event_a an event to be added to the event list.
     *
     * @return a boolean value indicating status
     */
    public boolean addEvent(AWTEvent event_a) { 
  
        synchronized(event_list_d) {
            event_list_d.add(event_a);
        }

	// exit gracefully
	//
	return true;
    }

    /**
     * Removes the first event from the event list and sends the event
     * to the <code> DemoEventProcessable </code> object through its
     * <code> handleEvent() </code> method.
     *
     * @return a boolean value indicating status.
     */  
    public boolean processEventList() {

	// local variables
	//
        AWTEvent event;
   
	// loop all event list
	//
        while (event_list_d.size() > 0) {

            synchronized(event_list_d) {
                event = (AWTEvent) event_list_d.removeFirst();
            }
   
	    // handle the event
	    //
            handler_d.handleEvent(event);
        }

	// exit gracefully
	//
	return true;

    }
}
