//InputPanel.java

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;

//public class InputPanel extends Panel implements ActionListener{
public class InputPanel extends Panel implements ActionListener, ItemListener, AdjustmentListener{
    //public static final Color bgcolor = new Color(190,220,220);
    private static final Color bgcolor = new Color(236,236,236);
    private Color colortext = Color.gray;
    
    //private static final Color bgcolor = Color.lightGray;
    Label tlab1, ulab1, ulab3;
    Label tlab2, ulab2_1, ulab2_2;
    Label tlab3, tlab4, clab, tracelab, flab, eps1lab, eps2lab;
    TextField tf1, tf2_1, tf2_2, tf4, tf_freq, tf_eps1, tf_eps2;
    Checkbox ch1, ch2, ch3, ch4;
    CheckboxGroup checkbox, checktrace;
        
    Button b1;

    StateVars state;
    private Image im;
    private Graphics buf;
    public Scrollbar slider;
    public int SCROLLMIN=0, SCROLLMAX=1001;
    public int SleepTime = 101;
    final Font labfont = TheFonts.sanSerif12;
    
    InputPanel(StateVars state){
	super();
	this.state = state;
	setBackground(bgcolor);
	setLayout(null);
        setFont(TheFonts.sanSerif14);

	tlab1 = new Label(" Characteristic Impedance = ",Label.LEFT);
	tlab2 = new Label(" Load Impedance = ",Label.LEFT);	
	tlab4 = new Label(" Sleep timestep = ",Label.LEFT);	
	clab = new Label(" Display Line Admittance?",Label.LEFT);
	tracelab = new Label(" Persistent Trace on Chart?", Label.LEFT);	
	flab = new Label(" Frequency = ",Label.LEFT);
	eps1lab = new Label("  \u03b5   =", Label.LEFT);
	eps2lab = new Label("  \u03b5   =", Label.LEFT);
	
	tlab1.setFont(labfont);
	tlab1.setBackground(bgcolor.brighter());
	tlab2.setFont(labfont);
	tlab2.setBackground(bgcolor.brighter());
	
	tlab4.setFont(labfont);
	tlab4.setBackground(bgcolor.brighter());
	tlab4.setForeground(Color.blue);
	
	flab.setFont(labfont);
	flab.setBackground(bgcolor.brighter());
	eps1lab.setFont(labfont);
	eps1lab.setBackground(bgcolor.brighter());
	eps2lab.setFont(labfont);
	eps2lab.setBackground(bgcolor.brighter());
	
	clab.setFont(labfont);
	clab.setBackground(bgcolor.brighter());
	clab.setForeground(Color.blue);
	
	tracelab.setFont(labfont);
	tracelab.setBackground(bgcolor.brighter());
	tracelab.setForeground(Color.red);
	
	ulab1 = new Label("\u03a9",Label.LEFT);
	ulab1.setFont(labfont);
	
	tf1 = new TextField(""+MaestroA.rounder(state.getZ0(),2));
	tf2_1 = new TextField(""+MaestroA.rounder(state.getZL().Real(),2));
	tf2_2 = new TextField(""+MaestroA.rounder(state.getZL().Imaginary(),2));
	//tf4 = new TextField(""+state.getSleep());
	
	tf_freq = new TextField(""+MaestroA.rounder(state.frequency,3));
	tf_eps1 = new TextField(""+MaestroA.rounder(state.epsilon_r1,3));
	tf_eps2 = new TextField(""+MaestroA.rounder(state.epsilon_r2,3));

        tf1.setFont(labfont);
        tf2_1.setFont(labfont);
        tf2_2.setFont(labfont);
        tf_freq.setFont(labfont);
        tf_eps1.setFont(labfont);
        tf_eps2.setFont(labfont);

	ulab2_1 = new Label(" + j ",Label.LEFT);
	ulab2_1.setFont(labfont);
	
	ulab2_2 = new Label("\u03a9",Label.LEFT);
	ulab2_2.setFont(labfont);
	
	ulab3 = new Label("Hz",Label.LEFT);
	ulab3.setFont(labfont);
	
	checkbox = new CheckboxGroup();
	ch1 = new Checkbox("Yes",false,checkbox);
	ch2 = new Checkbox("No",true,checkbox);
	ch1.setFont(labfont);
	ch2.setFont(labfont);
	ch1.setBackground(bgcolor);
	ch2.setBackground(bgcolor);
	ch1.setForeground(Color.blue);
	ch2.setForeground(Color.blue);
	
	checktrace = new CheckboxGroup();
	
	ch3 = new Checkbox("Yes",true,checktrace);
	ch4 = new Checkbox("No",false,checktrace);
	ch3.setFont(labfont);
	ch4.setFont(labfont);
	ch3.setBackground(bgcolor);
	ch4.setBackground(bgcolor);
	ch3.setForeground(Color.red);
	ch4.setForeground(Color.red);
				
	b1 = new Button("Update");
	b1.setFont(labfont);
        slider = new Scrollbar(Scrollbar.HORIZONTAL,SCROLLMAX-SleepTime,1,SCROLLMIN,SCROLLMAX);
        slider.setBackground(Color.white);
        //slider.setForeground(Color.lightGray);
        add(slider);
	
	add(ulab2_1);
	add(ulab3);
	
	add(tf1);
	add(tf2_1);
	add(tf2_2);
	add(tf_freq);
	add(tf_eps1);
	add(tf_eps2);

	add(ch1);
	add(ch2);
	add(ch3);
	add(ch4);	
	
	add(b1);
	int vstart = 30;
    	
        tf1.setBounds(125,22,60,18);
        Panel ptf1 = new Panel();
	    ptf1.setBackground(colortext);
	    add(ptf1);
	    ptf1.setBounds(124,21,62,20);
	ulab1.setBounds(190,22,25,18);
	
	tf2_1.setBounds(125,49,60,18);
        Panel ptf21 = new Panel();
	    ptf21.setBackground(colortext);
	    add(ptf21);
	    ptf21.setBounds(124,48,62,20);
	ulab2_1.setBounds(190,49,35,18);
        
	tf2_2.setBounds(230,49,60,18);
        Panel ptf22 = new Panel();
	    ptf22.setBackground(colortext);
	    add(ptf22);
	    ptf22.setBounds(229,48,62,20);
	ulab2_2.setBounds(277,49,20,18);
	
	tf_freq.setBounds(125,76,60,18);
        Panel ptfreq = new Panel();
	    ptfreq.setBackground(colortext);
	    add(ptfreq);
	    ptfreq.setBounds(124,75,62,20);
	ulab3.setBounds(190,76,25,18);
	
        tf_eps1.setBounds(125,103,60,18);
        Panel pteps1 = new Panel();
	    pteps1.setBackground(colortext);
	    add(pteps1);
	    pteps1.setBounds(124,102,62,20);

        tf_eps2.setBounds(230,103,60,18);
	Panel pteps2 = new Panel();
	    pteps2.setBackground(colortext);
	    add(pteps2);
	    pteps2.setBounds(229,102,62,20);

	//clab.setBounds(10,135,160,15);
        
	// CHANGE===============================================================
        // ch1.setBounds(180,154,50,18);
	// ch2.setBounds(230,154,50,18);
            ch1.setBounds(205,154,60,18);
            ch2.setBounds(285,154,60,18);
	
	//tracelab.setBounds(10,155,160,15);
	// ch3.setBounds(180,174,50,18);
	// ch4.setBounds(230,174,50,18);
            ch3.setBounds(205,174,60,18);
            ch4.setBounds(285,174,60,18);
	//======================================================================
        
	b1.setBounds(220,215,100,30);
        b1.setBackground(Color.white);
        Panel pb1 = new Panel();
	    pb1.setBackground(Color.black);
	    add(pb1);
	    pb1.setBounds(219,214,102,32);
        
        slider.setBounds(20,225,150,15);
        Panel pslider = new Panel();
	    pslider.setBackground(Color.black);
	    add(pslider);
	    pslider.setBounds(19,224,152,17);

	
	//Listeners
	b1.addActionListener(this);
	ch1.addItemListener(this);
	ch2.addItemListener(this);
	ch3.addItemListener(this);
	ch4.addItemListener(this);
        slider.addAdjustmentListener(this);

    }
    
    @Override
        public void paint(Graphics g){
            if(im == null){
                im = createImage(getSize().width,getSize().height);
                buf = im.getGraphics();
                drawCanvas(buf);
            }
            else{
                drawCanvas(buf);
            }
            g.drawImage(im,0,0,null);
        }
        
        //Addition to reduce flicker new routine
	public void update(Graphics g){		// added to avoid clearing
	    paint(g);
	}
    
    public void drawCanvas(Graphics g){
	Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        g.clearRect(0,0,getSize().width-2,getSize().height);
        Font symbolfont=TheFonts.symbol12;
        Font normalfont=TheFonts.sanSerif12;
        Font normalfontB=TheFonts.bold11;
        Font normalfont2=TheFonts.sanSerif11;
        Font smallfont=TheFonts.sanSerif10;
        FontMetrics fm;
              
        
        String alpha, Ohm, lambda, infinity, Gamma, epsilon;
            g.setFont(symbolfont);
		alpha="\u03b1";
		lambda="\u03bb";
		Ohm="\u03a9";
		infinity="\u221e";
		Gamma="\u0393";
                epsilon ="\u03b5";
            g.setFont(normalfont);
	
	g.setColor(bgcolor.darker());
	g.fillRect(0,getSize().height-2,getSize().width,2);
	g.fillRect(getSize().width-2,0,2,getSize().height);
	g.setColor(bgcolor.brighter());
	g.fillRect(0,0,2,getSize().height-1);
	g.fillRect(0,0,getSize().width-2,2);
	
	String titulo = "Input Panel";
	g.setColor(Color.red.darker());
	g.setFont(TheFonts.bold12);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,270,fmetrics.getHeight());
        
        g.setColor(Color.black);
        MaestroG.subscripter("Z","01","",g,12,90,35);
        MaestroG.subscripter("Z","L","",g,12,95,62);
        MaestroG.subscripterSerIT("f","","",g,14,97,89);
        MaestroG.subscripterSymFirst2(""+epsilon,"r1","",g,14,92,116);
        MaestroG.subscripterSymFirst2(""+epsilon,"r2","",g,14,197,116);
        
        MaestroG.subscripterSymFirst(""+Ohm,"","",g,12,192,35);
        MaestroG.subscripterSymFirst(""+Ohm,"","",g,12,279,62);
        
           
            g.setFont(normalfont);
            g.drawString("= ",113,35);
            g.drawString("= ",113,62);
            g.drawString("= ",113,89);
            g.drawString("= ",113,116);
            g.drawString("= ",218,116);
            
            g.setFont(smallfont);
            g.setColor(Color.blue.darker());
            g.drawString("Characteristic",15,28);
            g.drawString("Impedance",15,39);
            
            g.setColor(Color.red.darker());
            g.drawString("Load",15,55);
            g.drawString("Impedance",15,66);
            
            g.setColor(Color.blue.darker());
            g.drawString("Frequency",15,89);
            
            g.setColor(Color.red.darker());
            g.drawString("Relative",15,109);
            g.drawString("Permittivity",15,120);

            g.setFont(normalfontB);
            g.setColor(Color.black);
            g.drawString("Display Line Admittance?",15,163);
            g.drawString("Persistent Trace on Chart?",15,188);
            
            g.setColor(Color.red.darker());
            //g.drawRect(122,9,64,22);
            //g.drawRect(122,36,64,22);
            //g.drawRect(209,36,64,22);
            //g.drawRect(122,63,64,22);
            //g.drawRect(122,90,64,22);
            //g.drawRect(227,90,64,22);
            
            g.drawLine(10,143,getSize().width-10,143);
            g.drawLine(10,200,getSize().width-10,200);
            
            g.setFont(TheFonts.sanSerif11);
            g.drawString("Main Line",134,135);
            g.drawString("Transformer",230,135);
            
            g.setColor(Color.blue.darker());
	    MaestroG.drawArrowtip(29,246,4,g);
	    MaestroG.drawArrowtip(37,246,4,g);
            
            g.setColor(Color.black);
            g.setFont(normalfont2);
            g.drawString("Animation Speed",60,220);
            
	    g.setColor(Color.red.darker());
	    MaestroG.drawArrowtip(152,246,3,g);
	    MaestroG.drawArrowtip(160,246,3,g);
            
    }
    
    public void actionPerformed(ActionEvent evt){
	if(evt.getSource()==b1){
	
	    if(Double.valueOf(tf1.getText()).doubleValue() < 0){
		state.setZ0(50.0);
		state.setZ0old(50.0);
		tf1.setText("50.0");
	    }
	    else{
		state.setZ0(Double.valueOf(tf1.getText()).doubleValue());
		state.setZ0old(Double.valueOf(tf1.getText()).doubleValue());
	    }
	    
	    if(Double.valueOf(tf2_1.getText()).doubleValue() < 0.0){
		state.setZL(100.0,Double.valueOf(tf2_2.getText()).doubleValue());
		tf2_1.setText("100.0");
	    }
	    else{
		state.setZL(Double.valueOf(tf2_1.getText()).doubleValue(),
			    Double.valueOf(tf2_2.getText()).doubleValue());
	    }
	    
	    if(Double.valueOf(tf_freq.getText()).doubleValue() < 0.0){
		state.frequency=0.0;
		tf_freq.setText("0.0");
	    }
	    else{
		state.frequency=(Double.valueOf(tf_freq.getText()).doubleValue());
	    }
	    
	    if(Double.valueOf(tf_eps1.getText()).doubleValue() < 1.0){
		state.epsilon_r1= 1.0;
		tf_eps1.setText("1.0");
	    }
	    else{
		state.epsilon_r1=(Double.valueOf(tf_eps1.getText()).doubleValue());
	    }
	    
	    if(Double.valueOf(tf_eps2.getText()).doubleValue() < 1.0){
		state.epsilon_r2= 1.0;
		tf_eps2.setText("1.0");
	    }
	    else{
		state.epsilon_r2=(Double.valueOf(tf_eps2.getText()).doubleValue());
	    }
	    
	    //if(Integer.valueOf(tf4.getText()).intValue() < 0){
		//state.setSleep(100);
		//tf4.setText("100");
	    //}
	    //else{
		//state.setSleep(Integer.valueOf(tf4.getText()).intValue());
	    //}
            
            SleepTime = (SCROLLMAX-SCROLLMIN)-slider.getValue()-1; 
                state.sleeptime = SleepTime;

	}
    }
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
	  if(evt.getSource()==slider){
		SCROLLMAX = slider.getMaximum();
		SCROLLMIN = slider.getMinimum();
                SleepTime = (SCROLLMAX-SCROLLMIN)-slider.getValue()-1; 
                state.sleeptime = SleepTime;
	  }
    }
    
    public void itemStateChanged(ItemEvent evt){
	if(evt.getSource()==ch1){//Display Admittance
	    state.setPaintY(true);
	}
	else if(evt.getSource()==ch2){//No Display of Admittance
	    state.setPaintY(false);
	}
	if(evt.getSource()==ch3){//Display Trace
	    state.setTrace(true);
	}
	else if(evt.getSource()==ch4){//No Display of Trace
	    state.setTrace(false);
	}
	
    }
}
