/*                                                                           *\
        file: CocomoTool.java
       class: CocomoTool
        date: 06/05/1996
      author: Arthur Jeroen
 description: Main class to spawn other windows and calculate all stuff
\*                                                                           */

import java.awt.*;
import java.applet.*;

public class CocomoTool extends Applet
{
  /* private variabels for windows */
  private Language lang;
  private TCF tcf;
  private UFC ufc;
  private EAF eaf;
  /* public data */
  public boolean inAnApplet=true;
  /* private variables for calculation */
  private double fp=0.0;
  private double loc=53.0;
  private int projecttype=0;
  private final double cocomotable[]={2.4,1.05,2.5,0.38,3.2,1.05,
                                      3.0,1.12,2.5,0.35,3.0,1.12,
                                      3.6,1.20,2.5,0.32,2.8,1.20};
  private final double timetable1[]={0.08333,1.0,19.0,152.0};
  private final double timetable2[]={0.08333,1.0,30.0,365.0};

  /* textfileds for main window */
  private Label labFP,labLang,labLOC,labEffort,labDevTime,labPeople;
  private Checkbox cb1,cb2,cb3,cbbasis,cbinter;
  private Choice choice1,choice2;
  private Button costdriverbutton;
  
  /* method called when applet inializes */
  public void init()
  {
    /* making of objects for windows */
    lang=new Language(this);
    tcf=new TCF(this);
    ufc=new UFC(this);
    eaf=new EAF(this);

    /* setting of variables for changeble objects */
    CheckboxGroup grp1=new CheckboxGroup();
    cb1=new Checkbox("organic",grp1,true);
    cb2=new Checkbox("semi-detached",grp1,false);
    cb3=new Checkbox("embedded",grp1,false);

    CheckboxGroup grp2=new CheckboxGroup();
    cbbasis=new Checkbox("basic COCOMO",grp2,true);
    cbinter=new Checkbox("intermediate COCOMO",grp2,false);

    labFP=new Label("");
    labLang=new Label("");
    labLOC=new Label("");
    labEffort=new Label("");
    labDevTime=new Label("");
    labPeople=new Label("");

    choice1=new Choice();
    choice1.addItem("manyears");
    choice1.addItem("manmonths");
    choice1.addItem("mandays");
    choice1.addItem("manhours");
    choice1.select("manmonths");

    choice2=new Choice();
    choice2.addItem("years");
    choice2.addItem("months");
    choice2.addItem("days");
    choice2.addItem("hours");
    choice2.select("months");

    /* setting up of main window */
    Panel center=new Panel();
    center.setLayout(new GridLayout(1,2));
    Panel left=new Panel();
    Panel right=new Panel();

    left.setLayout(new GridLayout(9,1));
    left.add(cb1);
    left.add(cb2);
    left.add(cb3);
    left.add(new Button("Unadjusted Function Count"));
    left.add(new Button("Technical Complexity Factor"));
    left.add(new Button("Select Language"));
    left.add(cbbasis);
    left.add(cbinter);
    left.add(costdriverbutton=new Button("Set cost drivers"));

    right.setLayout(new GridLayout(9,2));
    right.add(new Label("Function Points :",Label.RIGHT));
    right.add(labFP);
    right.add(new Label("Language :",Label.RIGHT));
    right.add(labLang);
    right.add(new Label("Lines of code : ",Label.RIGHT));
    right.add(labLOC);
    right.add(new Label("Effort :",Label.RIGHT));
    right.add(labEffort);
    right.add(new Label(""));
    right.add(choice1);
    right.add(new Label("Development time :",Label.RIGHT));
    right.add(labDevTime);
    right.add(new Label(""));
    right.add(choice2);
    right.add(new Label("People required :",Label.RIGHT));
    right.add(labPeople);
    right.add(new Label(""));
    right.add(new Label(""));

    center.add(left);
    center.add(right);
    setLayout(new BorderLayout());
    add("Center",center);
    if(!inAnApplet)
      add("South",new Button("Quit"));
    costdriverbutton.enable(false);
  }

  /* method for specifying border around window */
  public Insets insets()
  {
     return new Insets(5,5,5,5);
  }   

  /* event handler */
  public boolean handleEvent(Event evt)
  {
    if(evt.id==Event.WINDOW_DESTROY)
    {
      if(inAnApplet)
      {
        hide();
        return true;
      }
      else
        System.exit(0);
    }
    else if((evt.target instanceof Checkbox)&&((evt.id==Event.ACTION_EVENT)||
            (evt.id==Event.GOT_FOCUS)))
    {
      if(((Checkbox)evt.target).getLabel().equals("basic COCOMO"))
      {
        costdriverbutton.enable(false);
      }
      else if(((Checkbox)evt.target).getLabel().equals("intermediate COCOMO"))
      {
        costdriverbutton.enable(true);
      }      /* value could have changed */
      update();
      return true;
    }
    else if((evt.target instanceof Button)&&(evt.id==Event.ACTION_EVENT))
    {
      String s=((Button)evt.target).getLabel();
      if(s.equals("Quit"))
      {
        if(inAnApplet)
        {
          hide();
          return true;
        }
        else
          System.exit(0);
      }
      else if(s.equals("Unadjusted Function Count"))
      {
        ufc.show();
        return true;
      }
      else if(s.equals("Technical Complexity Factor"))
      {
        tcf.show();
        return true;
      }
      else if(s.equals("Select Language"))
      {
        lang.show();
        return true;
      }
      else if(s.equals("Set cost drivers"))
      {
        eaf.show();
        return true;
      }
    }
    else if((evt.target instanceof Choice)&&(evt.id==Event.ACTION_EVENT))
    {
      update();
      return true;
    }
    return false;
  }

  /* method called to update values on screen */
  public void update()
  {
    labFP.setText(String.valueOf((int)fp));
    labLang.setText(lang.getLanguage());
    labLOC.setText(String.valueOf((int)(fp*loc)));
    if(cb1.getState()) projecttype=0;
    else if(cb2.getState()) projecttype=1;
    else if(cb3.getState()) projecttype=2;

    double d,e,f;
    if(fp<1.0e-100) fp=1.0e-100;
    if(loc<1.0e-100) loc=1.0e-100;

    /* effort */
    if(cbinter.getState())
      e=eaf.getEAF()*cocomotable[projecttype*6+4]*Math.exp(cocomotable[projecttype*6+5]*Math.log((fp*loc)/1000.0));
    else
      e=cocomotable[projecttype*6]*Math.exp(cocomotable[projecttype*6+1]*Math.log((fp*loc)/1000.0));

    /* development time */
    d=cocomotable[projecttype*6+2]*Math.exp(cocomotable[projecttype*6+3]*Math.log(e));

    /* set labels effort and development time */
    labEffort.setText(String.valueOf(Math.round(100.0*e*timetable1[choice1.getSelectedIndex()])/100.0));
    labDevTime.setText(String.valueOf(Math.round(100.0*d*timetable2[choice2.getSelectedIndex()])/100.0));

    /* set label people */
    labPeople.setText(String.valueOf(Math.round(10.0*e/d)/10.0));
  }

  /* method called when number of function points changess */
  public void updateFP()
  {
    fp=((double)ufc.getUFC())*tcf.getTCF();
    update();
  }

  /* method called when language changes */
  public void updateLOC()
  {
    loc=lang.getLOC();
    update();
  }

  /* method that will be run on haltin of a applet */
  public void stop()
  {
    lang.hide();
    tcf.hide();
    ufc.hide();
    eaf.hide();
  }

  /* method that will be run on startup of an applet */
  public void start()
  {
    update();
  }

  /* method that will be run for an applecation */
  public static void main(String args[])
  {
    Frame f=new Frame("CocomoTool");
    CocomoTool tool=new CocomoTool();
    tool.inAnApplet=false;
    tool.init();
    tool.start();
    f.add("Center",tool);
    f.pack();
    f.resize(512,330); 
/*    f.resize(1024,768);
*/
    f.show();
  }

}

