//package coco;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

/*type of all the coco pages; javacode of this class should be there at
  the client and the page designer as well*/
public final class CocoPage extends Container{
  /*vector containing all the client-programmable components; these are
    command-driven objects of classes implementing the interface
    CocoComponent*/
  private Vector ccv=new Vector();

  /*coco submitter vector; only the elements of this vector will be able
    to submit requests on coco components; they implement CocoSubmitter*/
  private Vector csv=new Vector();

  /*override addImpl() to modify effects of all the add() methods*/
  protected void addImpl(Component comp,Object constraints,int index){
    /*check whether component is a CocoComponent*/
    if(comp instanceof CocoComponent)
      ccv.add(comp); //in that case it should also be added to ccv

    /*check whether component is a CocoSubmitter*/
    if(comp instanceof CocoSubmitter)
      csv.add(comp); //in that case it should also be added to csv

    /*after that, add it the same way as if this was a simple Container*/
    super.addImpl(comp,constraints,index);
  }

  /*fake coco components have no appearance, needn't be derived from
    java.awt.Component but they are CocoComponents and communicate
    the way CocoComponents do; to add such a component, use this method*/
  public void addFake(CocoComponent comp){
    ccv.add(comp);
  }

  /*override remove(int) to remove CocoComponents from ccv/csv if necessary*/
  public void remove(int index){
    Component comp=getComponent(index);
    if(comp instanceof CocoComponent)ccv.remove(comp);
    if(comp instanceof CocoSubmitter)csv.remove(comp);
    super.remove(index);
  }

  /*should be called by the page designer after all the components added;
    outputs a file containing this page; output should then be loaded up
    to the coco page server*/
  public void dump(String fileName)throws IOException{
    FileOutputStream fos=new FileOutputStream(fileName);
    ObjectOutputStream oos=new ObjectOutputStream(fos);
	
    oos.writeObject(this);
    oos.flush();
    fos.close();
  }

  /*search for a coco component and return its state string*/
  public String getStateOf(String id){
    int size=ccv.size();
    for(int i=0;i<size;i++){
      CocoComponent c=(CocoComponent)ccv.elementAt(i);
      if(id.equals(c.getCocoId()))return c.getState();
    }
    return null;
  }

  /*search for a coco component and set its state by string*/
  public void setStateOf(String id,String state){
    int size=ccv.size();
    loop:for(int i=0;i<size;i++){
      CocoComponent c=(CocoComponent)ccv.elementAt(i);
      if(id.equals(c.getCocoId())){
	c.setState(state);
	break loop;
      }
    }
  }

  /*search for a coco component and send a request to it (locally)*/
  public void requestOn(String id,String request){
    int size=ccv.size();
    loop:for(int i=0;i<size;i++){
      CocoComponent c=(CocoComponent)ccv.elementAt(i);
      if(id.equals(c.getCocoId())){
	c.request(request);
	break loop;
      }
    }
  }

  /*walk all the submitters added and set their listeners to the local one*/
  public void setCocoSubmitListener(ActionListener l){
    int size=csv.size();
    for(int i=0;i<size;i++){
      ((CocoSubmitter)csv.elementAt(i)).setCocoSubmitListener(l);
    }
  }
}

