//InputPanel.java
/* A Java class for
 * SingleStub.java
 * Electromagnetic Transmission Line Applet
 * Applet without Smith Chart - Prepared by Umberto Ravaioli 
 * for 6th edition of Fundamentals of Applied Electromagnetics Book
 * June 2009 - All Rights Reserved
 */   

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

public class InputPanel extends Panel implements ActionListener, ItemListener, AdjustmentListener{
    //public static final Color bgcolor = new Color(190,220,220);
    //private static final Color bgcolor = Color.lightGray;
    
    private static final Color bgcolor = new Color(236,236,236);
    private Color colortext = Color.gray;
    
    Label tlab1, ulab1, ulab3;
    Label tlab2, ulab2_1, ulab2_2;
    Label tlab3, tlab4, clab, tracelab, flab, epslab;
    TextField tf1, tf2_1, tf2_2, tf4, tf_freq, tf_eps;
    Checkbox ch1, ch2, ch3, ch4, ch5, ch6;
    CheckboxGroup chgrpa, checkbox, checktrace;
    public 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);
        
	tlab1 = new Label(" Characteristic Impedance = ",Label.LEFT);
	tlab2 = new Label(" Load Impedance = ",Label.LEFT);
	tlab3 = new Label(" Stub Configuration : ",Label.LEFT);
	flab = new Label(" Frequency = ",Label.LEFT);
	tlab4 = new Label(" Sleep Timestep = ",Label.LEFT);
	clab = new Label(" Display Line Impedance?",Label.LEFT);
	tracelab = new Label(" Persistent Trace on Chart?", Label.LEFT);
	epslab = new Label("  \u03b5   =", Label.LEFT);
	
	tlab1.setFont(labfont);
	tlab1.setBackground(bgcolor.brighter());
	tlab2.setFont(labfont);
	tlab2.setBackground(bgcolor.brighter());
	tlab3.setFont(labfont);
	tlab3.setBackground(bgcolor.brighter());
	
	flab.setFont(labfont);
	flab.setBackground(bgcolor.brighter());
	epslab.setFont(labfont);
	epslab.setBackground(bgcolor.brighter());
	
	tlab4.setFont(labfont);
	tlab4.setBackground(bgcolor.brighter());
	tlab4.setForeground(Color.blue);
	
	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);
	ulab3 = new Label("Hz",Label.LEFT);
	ulab3.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_eps = new TextField(""+MaestroA.rounder(state.epsilon_r,3));
	
	ulab2_1 = new Label(" + j ",Label.LEFT);
	ulab2_1.setFont(labfont);
	
	ulab2_2 = new Label("\u03a9",Label.LEFT);
	ulab2_2.setFont(labfont);
	
	chgrpa = new CheckboxGroup();
	
	ch1 = new Checkbox("Open",false,chgrpa);
	ch2 = new Checkbox("Short",true,chgrpa);
	ch1.setFont(labfont);
	ch2.setFont(labfont);
	ch1.setBackground(bgcolor);
	ch2.setBackground(bgcolor);
	ch1.setForeground(Color.black);
	ch2.setForeground(Color.black);
	
	checkbox = new CheckboxGroup();
	ch3 = new Checkbox("Yes",false,checkbox);
	ch4 = new Checkbox("No",true,checkbox);
	ch3.setFont(labfont);
	ch4.setFont(labfont);
	ch3.setBackground(bgcolor);
	ch4.setBackground(bgcolor);
	ch3.setForeground(Color.blue);
	ch4.setForeground(Color.blue);
	
	checktrace = new CheckboxGroup();
	
	ch5 = new Checkbox("Yes",true,checktrace);
	ch6 = new Checkbox("No",false,checktrace);
	ch5.setFont(labfont);
	ch6.setFont(labfont);
	ch5.setBackground(bgcolor);
	ch6.setBackground(bgcolor);
	ch5.setForeground(Color.red);
	ch6.setForeground(Color.red);
	
	b1 = new Button("Update");
	b1.setFont(labfont);
        b1.setBackground(Color.white);
        slider = new Scrollbar(Scrollbar.HORIZONTAL,SCROLLMAX-SleepTime,1,SCROLLMIN,SCROLLMAX);
        add(slider);
        slider.setBackground(Color.white);
        slider.setForeground(Color.lightGray);
        
	
	//add(tlab1);
	//add(tlab2);
	//add(tlab3);
	//add(tlab4);
	//add(clab);
	//add(tracelab);
	//add(flab);
	//add(epslab);
		
	//add(ulab1);
	add(ulab2_1);
	//add(ulab2_2);
	add(ulab3);
	
	add(tf1);
	add(tf2_1);
	add(tf2_2);
	add(tf4);
	add(tf_freq);
	add(tf_eps);
	
	//add(ch1);
	//add(ch2);
	add(ch3);
	add(ch4);
	add(ch5);
	add(ch6);

	add(b1);
	
	//tlab1.setBounds(10,32,165,18);
	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);
	
	//tlab2.setBounds(10,55,110,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(297,49,20,18);
	
	//flab.setBounds(10,78,75,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);
	
	//epslab.setBounds(125,91,30,18);
	tf_eps.setBounds(125,103,60,18);
	Panel pteps = new Panel();
	    pteps.setBackground(colortext);
	    add(pteps);
	    pteps.setBounds(124,102,62,20);
            
	//tlab3.setBounds(10,101,125,18);
	ch1.setBounds(195,127,60,20); // not used
	ch2.setBounds(275,127,60,20); // not used
	
	//clab.setBounds(10,167,160,18);
	// ch3.setBounds(195,142,60,20);
	// ch4.setBounds(275,142,60,20);
        ch3.setBounds(205,142,60,20);
        ch4.setBounds(285,142,60,20);
	
	//tracelab.setBounds(10,147,160,18);
	// ch5.setBounds(195,167,60,20);
	// ch6.setBounds(275,167,60,20);
        ch5.setBounds(205,167,60,20);
        ch6.setBounds(285,167,60,20);
		
	//tlab4.setBounds(10,170,110,18);
	//tf4.setBounds(125,170,60,18);
	
	//b1.setBounds(120,190,60,30);
	b1.setBounds(220,215,100,30);
        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);
	ch5.addItemListener(this);
	ch6.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","0","",g,12,95,35);
        MaestroG.subscripter("Z","L","",g,12,95,62);
        MaestroG.subscripterSerIT("f","","",g,14,97,89);
        //MaestroG.subscripterSymFirst(""+epsilon,"r","",g,14,97,106);
        MaestroG.subscripterSymFirst2(""+epsilon,"r","",g,14,97,116);
        
        MaestroG.subscripterSymFirst(""+Ohm,"","",g,12,192,35);
        MaestroG.subscripterSymFirst(""+Ohm,"","",g,12,299,62);
        
           
            g.setFont(normalfont);
            g.drawString("= ",113,35);
            g.drawString("= ",113,62);
            g.drawString("= ",113,89);
            g.drawString("= ",113,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("Stub Configuration",15,138);           
            g.drawString("Display Line Impedance?",15,153);
            g.drawString("Persistent Trace on Chart?",15,178);
            
            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(10,123,278,64);
            
            g.drawLine(10,133,getSize().width-10,133);
            g.drawLine(10,200,getSize().width-10,200);
            
            //g.drawRect(18,203,153,11);
            //g.drawRect(184,195,104,24);
            
            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 ("+(slider.getValue())+")",35,230);
            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.0){
		state.setZ0(50.0);
		tf1.setText("50.0");
	    }
	    else{
		state.setZ0(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_eps.getText()).doubleValue() < 1.0){
		state.epsilon_r= 1.0;
		tf_eps.setText("1.0");
	    }
	    else{
		state.epsilon_r=(Double.valueOf(tf_eps.getText()).doubleValue());
	    }
	    
	    if(Integer.valueOf(tf4.getText()).intValue() < 0.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){//Open
	    state.setOpen(true);
	}
	else if(evt.getSource()==ch2){//Short
	    state.setOpen(false);
	}
	if(evt.getSource()==ch3){//Display Impedance
	    state.setPaintZ(true);
	}
	else if(evt.getSource()==ch4){//No Display of Impedance
	    state.setPaintZ(false);
	}
	if(evt.getSource()==ch5){//Display Trace
	    state.setTrace(true);
	}
	else if(evt.getSource()==ch6){//No Display of Trace
	    state.setTrace(false);
	}
	
    }
}
