/*Marcus Daniels, swarm-support Dec20, 1999
if you have a non-batch simulation and want an programmed
way to stop the simulation, here's an example.  (See the
swarm.simtoolsgui.ControlPanel reference for the other messages.)
*/

import swarm.Globals;
import swarm.simtoolsgui.GUISwarmImpl;
import swarm.simtoolsgui.GUISwarm;
import swarm.activity.Activity;
import swarm.activity.Schedule;
import swarm.activity.ScheduleImpl;
import swarm.activity.ActionGroup;
import swarm.activity.ActionGroupImpl;
import swarm.objectbase.Swarm;
import swarm.defobj.Zone;
import swarm.Selector;

public class TestControlPanel extends GUISwarmImpl {
  class Agent {
    Agent () {
      super ();
    }
    public Object step () {
        System.out.println (this + " step " + Globals.env.getCurrentTime ());
        return this;
    }
  }

  Agent agent;
  Schedule schedule;
  ActionGroup group;
  Schedule stopSchedule;
  Selector sel;

  TestControlPanel (Zone aZone) {
    super (aZone);
  }

  public Object buildObjects () {
    super.buildObjects ();

    agent = new Agent ();

    return this;
  }

  public Object buildActions () {
    super.buildActions ();

    schedule = new ScheduleImpl (this, 1);
    group = new ActionGroupImpl (this);

    stopSchedule = new ScheduleImpl (this);

    try {
      group.createActionTo$message
        (agent, 
         new Selector (agent.getClass (), "step", false));
      group.createActionTo$message
        (getActionCache (),
         new Selector (getActionCache ().getClass (), "doTkEvents", true));
      stopSchedule.at$createActionTo$message
        (10, getControlPanel (),
         new Selector (getControlPanel ().getClass (), 
                       "setStateStopped",
                       true));
      
    } catch (Exception e) {
      e.printStackTrace (System.err);
    }
    schedule.at$createAction (0, group);
    return this;
  }
  
  public Activity activateIn (Swarm swarmContext) {
    super.activateIn (swarmContext);
    
    schedule.activateIn (this);
    stopSchedule.activateIn (this);
    return getActivity ();
  }

  static void main (String []args) {
    Globals.env.initSwarm ("TestControlPanel", "0.0", "mgd@swarm.org", args);

    GUISwarm guiSwarm = new TestControlPanel (Globals.env.globalZone);

    guiSwarm.buildObjects ();
    guiSwarm.buildActions ();
    guiSwarm.activateIn (null);
    guiSwarm.go ();
  }
}

