/*
A basic extension of the java.applet.Applet class
*/


import SimDesign;
import SKPop;
import Time;
import Household;
import DField;
import DoubleField;
import java.awt.*;
import java.applet.*;
import SimParam;

public class SimApp extends Applet {

public void init() {
super.init();

// Take out this line if you don't use symantec.itools.net.RelativeURL, or symantec.itools.awt.util.StatusScroller
symantec.itools.lang.Context.setApplet(this);

//{{INIT_CONTROLS
setLayout(new BorderLayout(0,0));
resize(515,338);
setFont(new Font("TimesRoman", Font.BOLD, 14));
setBackground(new Color(16762880));
nCycles = new DoubleField();
nCycles.setText("50");
nCycles.reshape(65,3,45,24);
nCycles.setBackground(new Color(16777215));
add(nCycles);
CycleB = new java.awt.Button("Run");
CycleB.reshape(2,3,48,24);
CycleB.setBackground(new Color(12632256));
add(CycleB);
logButton = new java.awt.Button("Start Log");
logButton.reshape(287,2,69,22);
logButton.setBackground(new Color(12632256));
add(logButton);
resetButton = new java.awt.Button("Reset");
resetButton.reshape(3,34,46,24);
resetButton.setBackground(new Color(12632256));
add(resetButton);
month = new DField();
month.setLayout(new GridLayout(1,2,5,5));
month.reshape(190,73,172,19);
month.setBackground(new Color(16762880));
add(month);
year = new DField();
year.setLayout(new GridLayout(1,2,5,5));
year.reshape(7,71,168,19);
year.setBackground(new Color(16762880));
add(year);
superCycle = new java.awt.Checkbox("SuperCycle");
superCycle.reshape(182,-1,97,31);
add(superCycle);
superText = new java.awt.TextField();
superText.setText("0");
superText.hide();
superText.reshape(135,3,42,24);
superText.setBackground(new Color(16777215));
add(superText);
stats = new java.awt.Checkbox("Stats");
stats.hide();
stats.reshape(368,3,62,23);
add(stats);
Interval = new java.awt.TextField();
Interval.setText("1");
Interval.hide();
Interval.reshape(442,3,44,23);
Interval.setBackground(new Color(16777215));
add(Interval);
cyclesLab = new java.awt.Label("Cycles",Label.CENTER);
cyclesLab.reshape(63,29,49,20);
cyclesLab.setFont(new Font("TimesRoman", Font.BOLD, 12));
add(cyclesLab);
logIntervalLab = new java.awt.Label("Log Interval",Label.CENTER);
logIntervalLab.hide();
logIntervalLab.reshape(430,30,70,18);
logIntervalLab.setFont(new Font("TimesRoman", Font.BOLD, 12));
add(logIntervalLab);
sCyclesLab = new java.awt.Label("S Cycles",Label.CENTER);
sCyclesLab.hide();
sCyclesLab.reshape(130,29,49,20);
sCyclesLab.setFont(new Font("TimesRoman", Font.BOLD, 12));
add(sCyclesLab);
population = new DField();
population.setLayout(new GridLayout(1,2,5,5));
population.reshape(5,162,168,20);
population.setBackground(new Color(16762880));
add(population);
birthrate = new DField();
birthrate.setLayout(new GridLayout(1,2,5,5));
birthrate.reshape(5,113,168,20);
birthrate.setBackground(new Color(16762880));
add(birthrate);
deathrate = new DField();
deathrate.setLayout(new GridLayout(1,2,5,5));
deathrate.reshape(191,111,172,19);
deathrate.setBackground(new Color(16762880));
add(deathrate);
households = new DField();
households.setLayout(new GridLayout(1,2,5,5));
households.reshape(5,209,168,20);
households.setBackground(new Color(16762880));
add(households);
migrants = new DField();
migrants.setLayout(new GridLayout(1,2,5,5));
migrants.reshape(190,160,172,19);
migrants.setBackground(new Color(16762880));
add(migrants);
hhpop = new DField();
hhpop.setLayout(new GridLayout(1,2,5,5));
hhpop.reshape(190,209,172,19);
hhpop.setBackground(new Color(16762880));
add(hhpop);
hhmax = new DField();
hhmax.setLayout(new GridLayout(1,2,5,5));
hhmax.reshape(5,278,175,18);
hhmax.setBackground(new Color(16762880));
add(hhmax);
land = new DField();
land.setLayout(new GridLayout(1,2,5,5));
land.reshape(5,318,176,19);
land.setBackground(new Color(16762880));
add(land);
minland = new DField();
minland.setLayout(new GridLayout(1,2,5,5));
minland.reshape(190,317,177,19);
minland.setBackground(new Color(16762880));
add(minland);
hhnorm = new DField();
hhnorm.setLayout(new GridLayout(1,2,5,5));
hhnorm.reshape(190,278,175,18);
hhnorm.setBackground(new Color(16762880));
add(hhnorm);
label1 = new java.awt.Label("Sim. Turkish Village");
label1.reshape(204,34,195,26);
label1.setFont(new Font("Helvetica", Font.ITALIC, 20));
label1.setBackground(new Color(16762880));
add(label1);
//}}

// new Labour(LabourF,"Labour").registerGraph();
// Names are defined in classes for moment
new Year(year); // should be first defined
new Month(month);
new BirthRate(birthrate).registerGraph();
new DeathRate(deathrate);
new Population(population).registerGraph();
new Land(land);
new MinLandHH(minland);
new HHNorm(hhnorm);
new HHMax(hhmax);
new HouseHolds(households).registerGraph();
new HHPop(hhpop).registerGraph();
new Migrants(migrants).registerGraph();
SimParam.initAll();
SimParam.setGraphTitle("Simulation");

}

public boolean handleEvent(Event event) {
if (event.target == CycleB && event.id == Event.ACTION_EVENT) {
CycleB_Clicked(event);
return true;
}
if (event.target == logButton && event.id == Event.ACTION_EVENT) {
logButton_Clicked(event);
return true;
}
if (event.target == resetButton && event.id == Event.ACTION_EVENT) {
resetButton_Clicked(event);
return true;
}
if (event.target == superCycle && event.id == Event.ACTION_EVENT) {
superCycle_Action(event);
return true;
}
return superCycle.handleEvent(event);
}

//{{DECLARE_CONTROLS
DoubleField nCycles;
java.awt.Button CycleB;
java.awt.Button logButton;
java.awt.Button resetButton;
DField month;
DField year;
java.awt.Checkbox superCycle;
java.awt.TextField superText;
java.awt.Checkbox stats;
java.awt.TextField Interval;
java.awt.Label cyclesLab;
java.awt.Label logIntervalLab;
java.awt.Label sCyclesLab;
DField population;
DField birthrate;
DField deathrate;
DField households;
DField migrants;
DField hhpop;
DField hhmax;
DField land;
DField minland;
DField hhnorm;
java.awt.Label label1;
//}}
int superduper=0;

void CycleB_Clicked(Event event) {


//{{CONNECTION
if (superText.isShowing()) superduper = new Integer(superText.getText()).intValue();
SimParam.logInterval = new Integer(Interval.getText()).intValue();
SimParam.dumpMeans = stats.getState();
//CycleB.setLabel("Wait");
if (superduper == 0) {
int n = (int) nCycles.getValue();
SimParam.multiCycle(n);
} else {
int n = (int) nCycles.getValue();
SimParam.superCycle(superduper,n);
}
//}}
//CycleB.setLabel("Run");

}

void logButton_Clicked(Event event) {
//{{CONNECTION
if (logButton.getLabel().equals("Start Log")) {
logButton.setLabel("Stop Log");
SimParam.startLog();
logIntervalLab.show();
Interval.show();
stats.show();
} else {
logButton.setLabel("Start Log");
SimParam.stopLog();
logIntervalLab.hide();
Interval.hide();
stats.hide();
}
//}}
}

void resetButton_Clicked(Event event) {
//{{CONNECTION
if (logButton.getLabel().equals("Stop Log")) {
logButton_Clicked(null);
}
SimParam.sReset();
//}}
}

void superCycle_Action(Event event) {


//{{CONNECTION
if (superText.isShowing()) {
superText.show(false);
sCyclesLab.show(false);
superText.setText(1+"");
superduper = 0;
} else {
superText.show(true);
sCyclesLab.show(true);
}
//}}
}

}