/*****************************************************************
                        Algorithm Animation
PURPOSE: This is the main applet driver. It coordinates the animation 
view, statistics view, and the input view. 
*****************************************************************/
import java.awt.*;
import java.applet.*;
import java.util.Vector;
import java.util.Observer;
import java.util.Observable;

/* <applet code="CPU" width=10 height=10></applet> virtually not embedded in web page*/

public class AlgAnime extends Applet implements Observer {
   animeFrame anime;
   statsFrame stats;
   inputFrame input;

   Scheduler lesson;
   Vector Q;

   /*--------------------------------------------------------
                        Constructor
   PURPOSE:  To set up all views, scheduling algorithm, and input queue
   --------------------------------------------------------*/
   public void init() {
      anime = new animeFrame();
      anime.show();
      anime.resize(300,300);

      stats = new statsFrame();
      stats.pack();
      stats.show();
      stats.resize(300,300);

      input = new inputFrame(this);
      input.show();
      input.resize(300,300);

      lesson=null;
      Q = new Vector(1,1);
   } // set up lecture

   /*--------------------------------------------------------
                        Update 
   PURPOSE:  To respond to user input via GUIs; overriden version of system event handler
   PARAMTERS: references argument specified by user, and associates GUI handler
   --------------------------------------------------------*/
   public void update(Observable obj, Object arg) {
      if (arg instanceof String) {
         if (((String)arg).equals("pause") && lesson!=null)
            lesson.thread.suspend();
         else if (((String)arg).equals("resume") && lesson!=null)
            lesson.thread.resume();
         else if (((String)arg).equals("clear")) {
            input.resetGUI();
            Q.setSize(0);
            lesson.resetQ();
            lesson.thread.stop();
            lesson = null;               
         } // reset all vectors
         else if (((String)arg).equals("quit")) {
            input.hide();   input.dispose();
            anime.hide();   anime.dispose();
            stats.hide();   stats.dispose();
         } // quit applet
      } // string event
      else if (arg instanceof Packet) {
         getParms((Packet)arg);
         anime.cleargrid();
         int click = anime.setgrid(Q);
         if (((Packet)arg).getAlg().equals("FCFS")) {            
            anime.setTitle("First Come First Serve Scheduling");
            lesson = new FCFS (Q, stats, anime,input,click);
         } // do FCFS
         else if (((Packet)arg).getAlg().equals("RR1")) {
            anime.setTitle("Round Robin, q = 1");
            lesson = new RR1 (Q, stats, anime,input,click);
         } // do RR1
         else if (((Packet)arg).getAlg().equals("RR4")) {
            anime.setTitle("Round Robin, q = 4");
            lesson = new RR4 (Q, stats, anime, input,click);
         } // do RR4
         else if (((Packet)arg).getAlg().equals("SPN")) {
            anime.setTitle("Shortest Process Next");
            lesson = new SPN(Q, stats, anime, input,click);
         } // do SPN
         else if (((Packet)arg).getAlg().equals("SRT")) {
            anime.setTitle("Shortest Remaining Time");
            lesson = new SRT(Q, stats, anime, input,click);
         } // do SRT
         else if (((Packet)arg).getAlg().equals("HRRN")) {
            anime.setTitle("Highest Response Ratio Next");
            lesson = new HRRN(Q, stats, anime, input,click);
         } // do HRRN
         else if (((Packet)arg).getAlg().equals("FB1")) {
            anime.setTitle("Feedback, q = 1");
            lesson = new FB1(Q, stats, anime, input,click);
         } // do FB1
      } // hefty data packet->run
   } // handle event

   /*--------------------------------------------------------
                        Get Parms utility
   PURPOSE:  To sort out input given by user and set up input queue
   PARAMETERS: information packet GUI given by user
   --------------------------------------------------------*/
   private void getParms(Packet in) {  
      String name, t1, t2, t3, mark ="\n";
      Integer I = new Integer(0);
      int pos, a, s;
      boolean empty=false;
      t1 = in.getProc();
      t2 = in.getArriv();
      t3 = in.getServ();
 
      do {
         pos = t1.indexOf(mark);
         if (pos<0) {
            name = t1;
            empty = true;
         }
         else
            name = t1.substring(0,pos);
         t1 = t1.substring(pos+1);

         pos = t2.indexOf(mark);
         if (pos<0) {
            a = I.parseInt(t2);
            empty = true;
         }
         else
            a = I.parseInt(t2.substring(0,pos));
         t2 = t2.substring(pos+1);

         pos = t3.indexOf(mark);
         if (pos<0) {
            s = I.parseInt(t3);
            empty = true;
         }
         else
            s = I.parseInt(t3.substring(0,pos));
         t3 = t3.substring(pos+1);

         process temp = new process(name, a, s);
         Q.addElement(temp);         
      } while (!empty);      
   } // set up Queue

} // algorithm animation main driver
