//LossyLinePanel.java
/* A Java class for
 * LossyWide.java
 * Electromagnetic Transmission Line Applet
 * Applet without Smith Chart - Prepared by Umberto Ravaioli 
 * for 6th edition of Fundamentals of Applied Electromagnetics Book
 * May 2009 - All Rights Reserved
 */ 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;


public class LossyLinePanel extends Panel implements ActionListener,ItemListener{
    //protected static final Color bgcolor = Color.lightGray;
    protected static final Color labcolor = Color.red;
    //protected static final Font labfont=new Font("SanSerif",Font.PLAIN,12);
    //protected static final Font titlefont=new Font("SanSerif",Font.BOLD,16);
    public static  Font labfont;
    public static  Font titlefont;
    private static final Label titlelabel=new Label("Set Line ",Label.CENTER);
    private static final Label lengthlabel=new Label("Length units:",Label.RIGHT);
    public boolean Is_Wavelength;
    
    //private static final Color bgcolor = new Color(216,216,191);
    private static final Color bgcolor = new Color(236,236,236);
    private static final Color tinta = new Color(236,236,236);
	
    
    //Checkbox c1, c2;
    Checkbox c3, c4;
    //CheckboxGroup cgrp;
    CheckboxGroup cgrp2;
    
    //These are the panels with inputs
    // lossless and low-loss
    PR lowpanel; 
    // general lossy line
    PR2 highpanel;
    Trans_State state;
    TransSlidePanel slidepanel;
   
    //String arg1A[]={"Char. Imp. Zo  =  ","Attenuation \u03b1  =  ",
    //		"Frequency  f  =  ","Rel. Permittivity \u03b5  =  ","Total Length  L  =  "};
    //String arg2A[]={" \u03a9"," Ne/\u03bb"," Hz",""," \u03bb"};
    //
    String arg1A[]={"Char. Imp. Zo  =  ",
    		"Frequency  f  =  ","Rel. Permittivity \u03b5  =  ","Total Length  L  =  "};
    String arg2A[]={" \u03a9"," Hz",""," \u03bb"};
    
    String arg1B[]={"Resistance R  =  ","Inductance L  =  ","Conductance G  =  ","Capacitance C  =  ",
		"Frequency  f  =  ","Total Length  L  =  "};
    String arg2B[]={" \u03a9/m"," \u00b5H/m"," S/m"," pF/m"," Hz"," \u03bb"};
	    
    CircuitCanvas ccan;
    public LossyLinePanel(Trans_State state){
	setBackground(bgcolor);
	this.state = state;
	setLayout(null);
	add(titlelabel);
	add(lengthlabel);
	titlelabel.setBounds(20,6,85,20);
	titlelabel.setFont(titlefont);
	//lengthlabel.setBounds(35,26,80,20);
	lengthlabel.setBounds(35,26,90,20);
	lengthlabel.setFont(labfont);
	
	//cgrp = new CheckboxGroup();
	cgrp2 = new CheckboxGroup();
	
	//c1 = new Checkbox("Low Loss",cgrp,true);
	//c2 = new Checkbox("General",cgrp,false);
	
	c3 = new Checkbox("[ \u03bb ]",cgrp2,true);
	c4 = new Checkbox("[ m ]",cgrp2,false);
	Is_Wavelength = true;
	
	//c1.setFont(labfont); c2.setFont(labfont);
	c3.setFont(labfont); c4.setFont(labfont);
	
	//c1.setBackground(bgcolor);
	//c2.setBackground(bgcolor);
	c3.setBackground(bgcolor);
	c4.setBackground(bgcolor);
    
	//add(c1);
	//add(c2);
	
	add(c3);
	add(c4);
	
	//c1.setBounds(135,6,80,20);
	//c2.setBounds(220,6,70,20);
	c3.setBounds(135,26,60,20);
	c4.setBounds(220,26,60,20);
	
    	{	    
	    lowpanel = new PR("Lossless Approximation",arg1A,arg2A);
	    add(lowpanel);
            lowpanel.setBackground(new Color(240,240,230));
	   
	    lowpanel.setBounds(7,50,280,225);
            /*
	    lowpanel.setValue(state.Z0.Real(),0);//characteristic impedance
	    lowpanel.setValue(state.alpha,1);//alpha attenuation constant
	    lowpanel.setValue(state.frequency,2);//frequency
	    lowpanel.setValue(state.epsilon_r,3);//relative dielectric constant
	    lowpanel.setValue(state.lineLength,4);//total length
            */
            // JAN'S Changes:
	    lowpanel.setValue(state.Z0.Real(),0);//characteristic impedance
	    lowpanel.setValue(state.frequency,1);//frequency
	    lowpanel.setValue(state.epsilon_r,2);//relative dielectric constant
	    lowpanel.setValue(state.lineLength,3);//total length
            // END JAN'S changes
	}
	
	{
	    highpanel = new PR2("General Lossy Line",arg1B,arg2B);
	    add(highpanel);
	    highpanel.setBackground(new Color(240,240,230));
            
	    highpanel.setBounds(7,50,280,225);	    
	    highpanel.setVisible(false);
	    highpanel.setValue(state.R,0);//resistance
	    highpanel.setValue(state.L,1);//inductance
	    highpanel.setValue(state.G,2);//conductance
	    highpanel.setValue(state.C,3);//capacitance
	    highpanel.setValue(state.frequency,4);//frequency
	    highpanel.setValue(state.lineLength,5);//total length
	    
	}
	//Listeners
	//c1.addItemListener(this);
	//c2.addItemListener(this);
	c3.addItemListener(this);
	c4.addItemListener(this);
	
	lowpanel.b1.addActionListener(this);
	highpanel.b1.addActionListener(this);
        lowpanel.b1.setBackground(Color.white);
        highpanel.b1.setBackground(Color.white);
	
    }
    
    public void paint(Graphics g){
            FontMetrics fm;
				
		String alpha, Ohm, lambda, infinity, Gamma;
		alpha="\u03b1";
		lambda="\u03bb";
		Ohm="\u03a9";
		infinity="\u221e";
		Gamma="\u0393";
            
        
	    g.clearRect(0,0,getSize().width,getSize().height);
	    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);
    }
    
    public void itemStateChanged(ItemEvent evt){
	//if(evt.getSource() == c1){
	//    lowpanel.setVisible(true);
	//    highpanel.setVisible(false);
	//}
	//else if (evt.getSource() ==c2){
	//    lowpanel.setVisible(false);
	//    highpanel.setVisible(true);
	//}
	//else if (evt.getSource() ==c3){
        if (evt.getSource() ==c3){
	    Is_Wavelength = true;
	    //arg2A[4] = " [ \u03bb ] ";
	    arg2A[3] = " [ \u03bb ] ";
	    //arg2A[1] = " [ Ne / \u03bb ]";
	    lowpanel.setStrings(arg1A,arg2A);
	    	    
	    arg2B[5] = " [ \u03bb ] ";
	    highpanel.setStrings(arg1B,arg2B);
	}
	else if (evt.getSource() ==c4){
	    Is_Wavelength = false;
	    //arg2A[4] = " [ m ] ";
	    arg2A[3] = " [ m ] ";
	    //arg2A[1] = " [ Ne / m ]";
	    lowpanel.setStrings(arg1A,arg2A);
	    
	    arg2B[5] = " [ m ] ";
	    highpanel.setStrings(arg1B,arg2B);
	}   
	
    }
    
    public void actionPerformed(ActionEvent evt){
	double tempalpha;
	/*
	if(evt.getSource() == lowpanel.b1){
	    
	    state.Z0 = new Complex(lowpanel.getValue(0),0.0);
	    state.alpha = lowpanel.getValue(1);
	    state.frequency = lowpanel.getValue(2);
	    state.epsilon_r = lowpanel.getValue(3);
	    state.lineLength = lowpanel.getValue(4);
	}
	else if(evt.getSource() == highpanel.b1){
	    
	    if(highpanel.getValue(0) < 0.0){
		state.R = 0.0;
		highpanel.setValue(0.0,0);
	    }
	    else{
		state.R = highpanel.getValue(0);
	    }
	    
	    if(highpanel.getValue(1) < 0.0){
		state.L = 0.0;
		highpanel.setValue(0.0,1);
	    }
	    else{
		state.L = highpanel.getValue(1);
	    }
	    
	    if(highpanel.getValue(2) < 0.0){
		state.G = 0.0;
		highpanel.setValue(0.0,2);
	    }
	    else{
		state.G = highpanel.getValue(2);
	    }
	    
	    if(highpanel.getValue(3) < 0.0){
		state.C = 0.0;
		highpanel.setValue(0.0,3);
	    }
	    else{
		state.C = highpanel.getValue(3);
	    }
	    state.frequency = highpanel.getValue(4);
	    state.lineLength = highpanel.getValue(5);
	}*/
	
    }
}
