import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.Enumeration;

public class visual extends Applet implements Runnable {
        Panel containerPanel,listPanel,textPanel,ptext;
        Panel centerPanel,lcenterPanel,bottomPanel,lbottomPanel;
        Label lab;
        TextArea t;
        TextField Title;
        Button but_URL,but_next, but_news, but_guide,but_return;
        Component [] components;
        Button but_quick[];
        Thread newsBox=null;
        Clips Campus_Clips[];
        int Clips_index,N_Clips;
        String Clip_Font,Blanks;
        String chooserLink[];
        URL  URLLink[];
        boolean first_news,first_guide,news_alert;
        int istep;
        Color umsl_red;
        Color umsl_yellow;



    public void init() {

        istep=7;
      but_quick=new Button[11];
     chooserLink = new String[20];
      URLLink = new URL[20];
     first_news=true;
     first_guide=true;
     news_alert=false;
     Blanks = "                ";
     Clips_index=0;
     String par =getParameter("nclips");
     N_Clips=(par ==null)?2:Integer.parseInt(par);
     par = getParameter("news_alert");
     if(par.equals("yes")){news_alert=true;}

     par = getParameter("thefont");
      // System.out.println(par);
     Clip_Font=(par==null)?"TimeRoman":par;
    // System.out.println(par);
     Campus_Clips= new Clips[N_Clips];
     par = getParameter("news0");
     Campus_Clips[0]=new Clips(par,20," Campus Web Guide","Show Guide  ","Visit This Page" );
     par= getParameter("news1");
     Campus_Clips[1]=new Clips(par,20,"   Campus News ","Show News   "," More Details  " );

 // Now we build the Applet Layout begining with Fonts and Colors

        Font fnt24 =new Font(Clip_Font,Font.BOLD,24);
        Font fnt18 = new Font(Clip_Font,Font.BOLD,18);
        Font fnt14 = new Font(Clip_Font,Font.BOLD,14);
        Font fnt14p = new Font(Clip_Font,Font.PLAIN,14);
        umsl_red=new Color(200,61,61);
        umsl_yellow=new Color(240,180,60);

 // First create two panels, one for the quick-buttons, one for the text window.
        listPanel =new Panel();
        listPanel.setName("listPanel");
        lcenterPanel  = new Panel();
        lcenterPanel.setName("lcenterPanel");
        lbottomPanel   = new Panel();
        lbottomPanel.setName("lbottomPanel");

        textPanel = new Panel();
        textPanel.setName("textPanel");
        bottomPanel = new Panel();
        bottomPanel.setName("bottomPanel");
        centerPanel = new Panel();
        centerPanel.setName("centerPanel");

   //Where to put them
        containerPanel = new Panel();
        containerPanel.setName("containerPanel");
        //containerPanel.setBackground(Color.lightGray);
        //containerPanel.setForeground(Color.black);

 //Now the whole show, "The Applet panel"

        setLayout(new BorderLayout());
        setForeground(umsl_red);
        setBackground(umsl_yellow);

        add("South",containerPanel);

        lab = new Label("The Campus Information Kiosk",Label.CENTER) ;
        lab.setFont(fnt18);
        lab.setName("lab");
        add("North",lab );

        Title=new TextField(20);
        Title.setName("Title");
        Panel ptext =new Panel();
        ptext.setName("ptext");
  
      ptext.setLayout(new FlowLayout(FlowLayout.CENTER));
        Title.setEditable(false);
        Title.setForeground(Color.black);
        Title.setBackground(Color.lightGray);
        Title.setFont(fnt14);
        ptext.add(Title);
        add("Center",ptext);

        containerPanel.setLayout(new CardLayout());
        containerPanel.add("lists",listPanel);
        containerPanel.add("text", textPanel);



     // Lay out the textPanel with button panel on bottom and the rest for text.
        textPanel.setLayout(new BorderLayout());
        textPanel.add("South", bottomPanel);
        textPanel.add("Center", centerPanel);


     // put the buttons on the panels, with labels.
        bottomPanel.setFont(fnt14);
        but_next=new Button("Next Entry");
        but_next.setName("but_next");
        but_next.setBackground(Color.lightGray);
        //umsl_red still in Foreground
        but_URL= new Button(Campus_Clips[Clips_index].URLLink()) ;
        but_URL.setName("but_URL");
        but_URL.setBackground(Color.lightGray);
        but_return=new Button("Return to Main Menu");
        but_return.setBackground(Color.lightGray);
        but_return.setName("but_return");
        bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        bottomPanel.add(but_next);
        bottomPanel.add(but_URL);
        bottomPanel.add(but_return);


       // Build  the centerPanel
        centerPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
         t= new TextArea("TextArea", 5, 70);
         t.setName("t");
         t.setEditable(false);
        t.setForeground(Color.black);
        t.setBackground(Color.white);
        t.setFont(fnt14);
        centerPanel.add( t);



     // Lay out the listPanel.
        listPanel.setLayout(new BorderLayout());
        listPanel.add("South", lbottomPanel);
        listPanel.add("Center", lcenterPanel);

        lcenterPanel.setLayout(new GridLayout(2,5));
        lcenterPanel.setFont(fnt14);
        lcenterPanel.setForeground(Color.blue);

        but_news=new Button("Campus News");
        but_news.setName("but_news");
        but_news.setBackground(Color.lightGray);
        but_guide=new Button("Campus Web Guide");
        but_guide.setName("but_guide");
        but_guide.setBackground(Color.lightGray);

        lbottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        lbottomPanel.setFont(fnt14);
        lbottomPanel.add(but_news);
        lbottomPanel.add(but_guide);

        but_quick[0]= new Button("Directory");
         chooserLink[0]=new String("http://www.umsl.edu/depts/depts.html");
        but_quick[1]=new Button("Events");
         chooserLink[1]=new String("http://www.umsl.edu/studentlife/dsa/calcamev.html");
        but_quick[2]=new Button("Academic Areas");
         chooserLink[2]=new String("http://www.umsl.edu/curriculum/curriculum.html");
        but_quick[10]=new Button("Administration");
         chooserLink[10]=new String("http://www.umsl.edu/curriculum/curriculum.html");
        but_quick[3]=new Button("Computing");
         chooserLink[3]=new String("http://www.umsl.edu/services/computing/computing.html");
        but_quick[4]=new Button("Faculty");
         chooserLink[4]=new String("http://www.umsl.edu/faculty/faculty.html");
        but_quick[5]=new Button("Library");
         chooserLink[5]=new String("http://www.umsl.edu/services/library/library.html");
        but_quick[6]=new Button("Service");
         chooserLink[6]=new String("http://www.umsl.edu/services/services.html");
        but_quick[7]=new Button("Student Life");
         chooserLink[7]=new String("http://www.umsl.edu/studentlife/student.html");
        but_quick[8]=new Button("Web Help");
         chooserLink[8]=new String("http://www.umsl.edu/help/htmlhelp.html");
        but_quick[9]=new Button("Whats New");
         chooserLink[9]=new String("http://www.umsl.edu/misc/whatsnew.html");
         int j;
         for(j=0;j<  10;j++){
          try{URLLink[j]= new URL(chooserLink[j]);}
                     catch (MalformedURLException e)
                       {URLLink[j]=null;}
         but_quick[j].setBackground(Color.lightGray);

         lcenterPanel.add(but_quick[j]);
         }
          components=new Component[9];
        components[0]=but_news;
        components[1]=but_guide;
        components[2]=lbottomPanel;
        components[3]=lcenterPanel;
        components[4]=listPanel;
        components[5]=containerPanel;
        components[6]=ptext;
        components[7]=lab;



// is there a news alert?
        if(news_alert){
         boolean testit;
        ( (CardLayout)(containerPanel.getLayout())).next(containerPanel);
                  Clips_index=1;
                   testit=Campus_Clips[Clips_index].load_clip(t);
                  if(testit){but_URL.setLabel(Campus_Clips[Clips_index].URLLink());}
                  else      {but_URL.setLabel("No Link Available");}
                  first_news=false;
                  Title.setText(Campus_Clips[Clips_index].Clip_Title());

        }else{

    Title.setText("    Main Menu    "); }
    }

    public void start(){
    // run this as a new thread
        if(newsBox==null){
                newsBox= new Thread(this);
                newsBox.start();
        }
    }

    public void run(){
    // Nothing! everything is event driven
      while(true){
        try {Thread.currentThread().sleep(4000);} catch (InterruptedException e){}
      }
    }

   public void stop(){
                if ( newsBox != null) {
                        newsBox.stop();
                        newsBox = null;
                  }
         }

    public boolean action(Event evt, Object obj) {
               int   button_type=0;
               boolean testit;
               int choice_index;

        if (evt.target instanceof Button) {
          int k;
          for(k=0;k<  11;k++){
                if(evt.target==but_quick[k]){
                        this.getAppletContext().showDocument(URLLink[k]);}
                 }
           if(evt.target==but_next){button_type=0;};
           if(evt.target==but_URL){button_type=1;};
           if(evt.target==but_guide){button_type=2;};
           if(evt.target==but_news){button_type=3;}
           if(evt.target==but_return){button_type=4;}
           switch(button_type){
             case 0:
                  testit=Campus_Clips[Clips_index].load_clip(t);
                  if(testit){but_URL.setLabel(Campus_Clips[Clips_index].URLLink());}
                  else      {but_URL.setLabel("No Link Available");}
                  break;

             case 1:
                  this.getAppletContext().showDocument(Campus_Clips[Clips_index].clip_URL());
                  break;

             case 2:

               ( (CardLayout)(containerPanel.getLayout())).next(containerPanel);

                  if((first_guide==true)||(Clips_index==1)){
                         Clips_index=0;
                   testit=Campus_Clips[Clips_index].load_clip(t);
                  if(testit){but_URL.setLabel(Campus_Clips[Clips_index].URLLink());}
                  else      {but_URL.setLabel("No Link Available");}
                  first_guide=false;
                  }
                  Title.setText(Campus_Clips[Clips_index].Clip_Title());
                  break;

              case 3:
               ( (CardLayout)(containerPanel.getLayout())).next(containerPanel);
                  if((first_news==true)||(Clips_index==0)){
                    Clips_index=1;
                   testit=Campus_Clips[Clips_index].load_clip(t);
                  if(testit){but_URL.setLabel(Campus_Clips[Clips_index].URLLink());}
                  else      {but_URL.setLabel("No Link Available");}
                  first_news=false;
                  }
                  Title.setText(Campus_Clips[Clips_index].Clip_Title());
               break;

               case 4:
               ( (CardLayout)(containerPanel.getLayout())).next(containerPanel);
                Title.setText("    Main Menu    ");
               break;



              }
        }
        return true;
     }

public void clearit(){
            int ihide=0;
            for( ihide=0;ihide<  8;ihide++){
                components[ihide].hide();
            ((vbutton)(getAppletContext().getApplet("vbutton"))).which_component(components[ihide].getName());
              try {Thread.currentThread().sleep(2000);}
                   catch (InterruptedException e){}
     }
}
public void step(){
        if(istep>  =0){
          ((vbutton)(getAppletContext().getApplet("vbutton"))).which_component(components[istep].getName());
          components[istep--].show();
        }

}

  }//end of class

  /* The class Clips provides an interface to a file of clips to be displayed
  in the layout The Clips may or may not have a link attached.
   The idea is to create a compromise between the screen real estate limitation and
   the need for informative descriptions of what is being linked to.

   The expected format of a Clips-file is (<  clip>  ]<  URL possibly empty>  ])+ . As it
   stands ] cannot be used in a clip or URL.  (I have always escaped new-lines and tabs
   \n,\t  I am not sure I need to)

    Here is the public Interface.

    public Clips(String s,int count, String titl, String next_b)

        s               is the URL of the Clips-file
        count           is larger than the number of Clips entries in the file.
        titl, next_b,   are three strings probably to be used for labels. A Title,
        and link_l        a "to" label, and a  "from" lable.

    public boolean test_clip(){return goodRead;}
    public String Clip_Title(){return title;}
    public String Next_Label(){return next_button;}

    public String URLLink(){return link_label;}
    public boolean load_clip(TextArea t,int i){return testURL; }
                this loads a specific clip the i refers to the order in the
                clip file..starting with 1. This is not a well developed interface.
            the method returns true if associated URL false if not.

    public boolean load_clip(TextArea t){return testURL; }
                this loads the next clip in sequence. Return as above.

    public URL clip_URL(){return news_URL);}
                this returns the URL associated with a clip.
    */




 class Clips {
        int count;
        byte ascii[];
        boolean goodRead =true;
        boolean isURL;
        String news_file=null;
        int news_top;
        URL news_URL;
        InputStream iStream;
        DataInputStream  dStream;
        StringBuffer str ;
        StringBuffer ubuf ;
        String clips[];
        URL     URLs[];
        int   clip_size[];
        int next_clip;
        String title,next_button,link_label,Blanks20;


    public Clips(String s,int count, String titl, String next_b,String link_l){
        Blanks20="                    ";
        ascii = new byte[280];
        URLs = new URL[count];
        clips = new String[count];
        clip_size = new int[count];
       // str = new StringBuffer(200);
        //ubuf   = new StringBuffer(50);
        title=new String(titl);
        link_label= new String(link_l);
        next_button=new String(next_b);
//First we try to get a DataInputStream
           goodRead =true;
           news_file = s;
           try{news_URL= new URL(news_file);}
                     catch (MalformedURLException e)
                       {goodRead=false;}
//System.out.println("after URL"+news_file);
           if(goodRead){
                try{iStream=  news_URL.openStream();}
                          catch (IOException e)
                                {goodRead=false;}
                          catch(NullPointerException e)
                                {goodRead=false;}
             }
// System.out.println("After openStream");
         if(goodRead){
              dStream = new DataInputStream(iStream);
         }
   /* At this point it is not clear we can read from it( is file readable?)
   I think that all we have accomplished is made byte reading available*/

 //We now start to fill the string buffers from the input stream beginning
 //with the dummy headline

        clips[0]= new String("There are no additional clips in this file. \nPress \"Next\" to recycle clips.");
        clip_size[0]=76;
     //  try{URLs[0]= new URL("http://www.umsl.edu/index.html"); }
       //      catch (MalformedURLException e) {};
         URLs[0]=null;
        news_top=1; //where real news begins
        next_clip=1; //assume something there
        if(goodRead){

                   byte testbyte=0x20;
                   boolean more_bytes=true;
                try{testbyte= dStream.readByte();}
                                catch (IOException e){more_bytes = false;}

               while(more_bytes){

                         count = 0;
                         //str  = new StringBuffer();
                         //ubuf   = new StringBuffer();
                         if(testbyte==']'){break;}
                         //str.append((char)testbyte);
                         ascii[count++]=testbyte;
                          while(testbyte!=']'){
                                try{testbyte= dStream.readByte();}
                                  catch (IOException e){more_bytes = false;
                                                        testbyte=']';}
                                if(testbyte!=']'){
                                  //str.append((char)testbyte);
                                ascii[count++]=testbyte;
                                }
                           }
                           clips[news_top]=new String(ascii,0,0,count);
                           clip_size[news_top]=count;
                            count=0;
                           try{testbyte= dStream.readByte();}
                                catch (IOException e){more_bytes = false;
                                                        testbyte=']';}
                            if(testbyte==']'){
                                 URLs[news_top]=null;
                            }else{
                                // ubuf.append((char)testbyte);
                                  ascii[count++]=testbyte;
                                 while(testbyte!=']'){
                                        try{testbyte= dStream.readByte();}
                                          catch (IOException e){more_bytes = false;
                                                                testbyte=']'; }
                                    if(testbyte!=']'){
                                      //  ubuf.append((char)testbyte);
                                        ascii[count++]=testbyte;
                                    }
                                  }
                               }
                              //System.out.println(ubuf.toString());
                              //   try{URLs[news_top]=new URL(ubuf.toString());}
                                    try{URLs[news_top]=new URL(new String(ascii,0,0,count));}

                                        catch (MalformedURLException e)
                                                {URLs[news_top]=null;}

                             try{testbyte= dStream.readByte();}
                                catch (IOException e){more_bytes = false;};
       // System.out.println(clips[news_top]);
                             if(testbyte=='['){more_bytes=false;}

                             news_top++; //always one more

               }//clearing buffer with while


        }
    }
    public boolean test_clip(){return goodRead;}
    public String Clip_Title(){return title;}
    public String Next_Label(){return next_button;}
    public String URLLink(){return link_label;}


   public boolean load_clip(TextArea t,int i){
       boolean testURL;
       next_clip=(news_top<  =i)?0:i;
       for(int j=0;j<  14;j++){
         t.replaceText(Blanks20,j*20,(j*20)+19);}
       t.replaceText(clips[next_clip],0,clip_size[next_clip]);
      System.out.println(clip_size[next_clip]);
       if(URLs[next_clip]!=null){
                news_URL= URLs[next_clip];
                testURL=true;
       }else{
               testURL=false;
       }

         next_clip++;
         return (testURL);

    }



  public boolean load_clip(TextArea t){
        boolean testURL;
        if(next_clip==news_top){next_clip=0; }
        for(int j=0;j<  14;j++){
         t.replaceText(Blanks20,j*20,(j*20)+19);}
         t.replaceText(clips[next_clip],0,clip_size[next_clip]);
        //System.out.println(clip_size[next_clip]);
         if(URLs[next_clip]!=null){
                news_URL= URLs[next_clip];
                testURL=true;
          }else{
                testURL=false;
          }

         next_clip++;
         return (testURL);
        }
  public URL clip_URL(){
        return news_URL;
        }
  }//end of class