/* Author: Jeff Dalton <J.Dalton@ed.ac.uk>
 * Updated: Fri Nov 24 16:28:11 2000 by Jeff Dalton
 * Copyright: (c) 2000, AIAI, University of Edinburgh
 */

package ix.examples;

import java.util.*;

import ix.util.*;
import ix.util.lisp.*;


/**
 * A simple example using an I-X framework.  It shows how to define
 * issue handlers, create an IX_System that uses those handlers,
 * and tell the system of "events" that become Issues.
 *
 * It also shows how to access PicoIX in a class that isn't a
 * subclass of PicoIX.
 */

public class PicoIXTest1 {

    static class PIX extends PicoIX { 	// just to have a shorter name
    }

    /**
     * Main program.
     */
    public static void main(String[] argv) {

	PIX.IX_System example = new PIX.IX_System();

	example.addIssueHandlers(makeTestIssueHandlers());
	example.setListener(new PIX.IX_SystemListener());
	example.start();

	example.newEvent
	    (new PIX.Issue("twice", new PIX.Issue("return", "hello")));

	example.newEvent
	    (new PIX.Issue("stop when finished"));

    }

    /**
     * Create issue handlers as "instant" subclasses of IssueHandler.
     */
    static Object[] makeTestIssueHandlers() {
	return new Object[] {

	    // Post an issue twice.
	    new PIX.IssueHandler("twice") {
		void handleIssue(PIX.Issue i) {
		    postIssue((PIX.Issue)i.object);
		    postIssue((PIX.Issue)i.object);
		}
	    },

            // Send something to the listener.
	    new PIX.IssueHandler("return") {
		void handleIssue(PIX.Issue i) {
		    system.notifyListener(i.object);
		}
	    },

	    // Stop the system.
	    new PIX.IssueHandler("stop when finished") {
		void handleIssue(PIX.Issue i) {
		    if (system.controller.issues.isEmpty())
			system.stop();
		    else
			postIssue(i); 	// go to the end of the queue
		}
	    }
	};
    }

}
