//StubPanel.java
/* A Java class for
 * LineImpedance.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.applet.*;
import java.awt.event.*;


public class StubPanel extends Panel implements AdjustmentListener, ActionListener, ItemListener{
    //private static final Color bgcolor = new Color(180,147,112);
    private static final Color bgcolor = new Color(200,200,200);
    private Label lab1, lab2, lab3, lab4, lab5, lab6, lab7, titlelabel;
    private Label vlab4, vlab5, vlab6, vlab7;
    private String titulo = "Unknown Title";
    public CheckboxGroup cgrid1, cgrid2;
    public Checkbox c1, c2, c3, c4;
    private TextField text1;
    public Scrollbar s1, s2;
    GridBagLayout gb = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    private static final Font labfont = TheFonts.sanSerif12;
    private static final Font titlefont = TheFonts.bold16;
    private static final int SCROLLMIN=0, SCROLLMAX=10001;
    public Button b1;
    private Stub stub;
    private Trans_State state;
    public StubPanel(String titulo,Stub stub,Trans_State state){
	super();
	this.stub = stub;
	this.state = state;
	setBackground(bgcolor);
	this.titulo = titulo;
	setLayout(gb);
	titlelabel  = new Label(titulo,Label.CENTER); 
	titlelabel.setFont(titlefont);
	lab1 = new Label("Stub Status        ",Label.RIGHT);
	lab2 = new Label("Stub Type        ",Label.RIGHT);
	lab3 = new Label("Stub Zo [ \u03a9 ]  =      ",Label.RIGHT);
	lab4 = new Label("Stub Position  =      ",Label.RIGHT);
	lab5 = new Label("Stub Length  =      ",Label.RIGHT);
	lab6 = new Label("Ystub  [ S ]   =       ",Label.RIGHT);
	lab7 = new Label("(normalized)   =       ",Label.RIGHT);
	lab1.setFont(labfont);
	lab2.setFont(labfont);
	lab3.setFont(labfont);
	lab4.setFont(labfont);
	lab5.setFont(labfont);
	lab6.setFont(labfont);
	lab7.setFont(labfont);
	lab1.setForeground(Color.blue);
	lab2.setForeground(Color.blue);
	lab3.setForeground(Color.blue);
	lab4.setForeground(Color.magenta);
	lab5.setForeground(Color.red);
	lab6.setForeground(Color.red);
	lab7.setForeground(Color.red);
	vlab4 = new Label(""+MaestroA.rounder(stub.getPosition(),5)+" \u03bb",Label.LEFT);
	vlab5 = new Label(""+MaestroA.rounder(stub.getLength(),5)+" \u03bb",Label.LEFT);
	vlab6 = new Label("   0.0  ( open )",Label.LEFT);
	vlab7 = new Label("   0.0 ",Label.LEFT);
	vlab4.setFont(labfont);
	vlab5.setFont(labfont);
	vlab6.setFont(labfont);
	vlab7.setFont(labfont);
	cgrid1 = new CheckboxGroup();
	cgrid2 = new CheckboxGroup();
	c1 = new Checkbox("Enabled",cgrid1,false);
	c2 = new Checkbox("Disabled",cgrid1,true);
	c3 = new Checkbox("Open",cgrid2,false);
	c4 = new Checkbox("Short",cgrid2,true);
	text1 = new TextField("50.0",8);
	s1 = new Scrollbar(Scrollbar.HORIZONTAL,(int)((SCROLLMAX-SCROLLMIN)*(1.0-stub.getPosition()/state.lineLength)),
			    1,SCROLLMIN,SCROLLMAX);
	s2 = new Scrollbar(Scrollbar.HORIZONTAL,(int)((SCROLLMAX-SCROLLMIN)*(1.0-stub.getLength()/0.5)),
			    1,SCROLLMIN,SCROLLMAX);
	
	s1.setBackground(Color.white);
	s2.setBackground(Color.white);
	s1.setForeground(bgcolor);
	s2.setForeground(bgcolor);
	b1 = new Button("Update");
	b1.setBackground(bgcolor);
	try{
	    MaestroG.addComponent(this,titlelabel,0,0,3,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	
	try{
	    MaestroG.addComponent(this,lab1,0,1,1,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,lab2,0,2,1,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,lab3,0,3,1,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,lab4,0,4,1,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,lab5,0,6,1,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,lab6,0,8,1,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,lab7,0,9,1,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,c1,1,1,1,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,c2,2,1,1,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,c3,1,2,1,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,c4,2,2,1,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,text1,1,3,1,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,b1,2,3,1,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,s1,0,5,3,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,s2,0,7,3,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,vlab4,1,4,1,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,vlab5,1,6,1,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,vlab6,1,8,2,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	try{
	    MaestroG.addComponent(this,vlab7,1,9,2,1,
		    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
	    } catch(Exception e){e.printStackTrace();}
	
	
	//Listeners
	s1.addAdjustmentListener(this);
	s2.addAdjustmentListener(this);
	b1.addActionListener(this);
	c1.addItemListener(this);
	c2.addItemListener(this);
	c3.addItemListener(this);
	c4.addItemListener(this);
    }
    
    public void paint(Graphics g){
	
    }
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
	double temp;
	
	if(evt.getSource()==s1){
	    stub.setPosition((SCROLLMAX-SCROLLMIN-1-s1.getValue())*state.lineLength/(SCROLLMAX-SCROLLMIN-1));
	    vlab4.setText(""+stub.getPosition()+" \u03bb");
	}
	else if(evt.getSource()==s2){
	    stub.setLength((SCROLLMAX-SCROLLMIN-1-s2.getValue())*0.5/(SCROLLMAX-SCROLLMIN-1));
	    vlab5.setText(""+stub.getLength()+" \u03bb");
	    if(c3.getState()==true){
		
		stub.Open();
		temp = Complex.Imaginary(Complex.Multiply(stub.getYAsIfOn(),stub.getZchar()));
		if(stub.getLength() == 0.25){
		    vlab6.setText("   \u221e  ( short )");
		    vlab7.setText("   \u221e ");
		}
		else if(stub.getLength() == 0.0 || stub.getLength() == 0.5){
		    vlab6.setText("   0.0   ( open )");
		    vlab7.setText("   0.0 ");
		}
		else{
		    if(temp < 0.0){
		    vlab6.setText("- j "+MaestroA.rounder(Complex.Imaginary(stub.getYAsIfOn())*(-1.0),8));
		    vlab7.setText("- j "+MaestroA.rounder(temp*(-1.0),8));
		  }
		  else{
		    vlab6.setText(" j "+MaestroA.rounder(Complex.Imaginary(stub.getYAsIfOn()),8));
		    vlab7.setText(" j "+MaestroA.rounder(temp,8));
		  }
		}
		
	    }
	    else if(c4.getState()==true){
		
		stub.Short();
		temp = Complex.Imaginary(Complex.Multiply(stub.getYAsIfOn(),stub.getZchar()));
		
		if(stub.getLength() == 0.0 || stub.getLength() == 0.5){
		    vlab6.setText("   \u221e   ( short )");
		    vlab7.setText("   \u221e ");
		}
		else if(stub.getLength() == 0.25){
		    vlab6.setText("   0.0   ( open )");
		    vlab7.setText("   0.0 ");
		}
		else{
		  if(temp < 0.0){
		    vlab6.setText("- j "+MaestroA.rounder(Complex.Imaginary(stub.getYAsIfOn())*(-1.0),8));
		    vlab7.setText("- j "+MaestroA.rounder(temp*(-1.0),8));
		  }
		  else{
		    vlab6.setText(" j "+MaestroA.rounder(Complex.Imaginary(stub.getYAsIfOn()),8));
		    vlab7.setText(" j "+MaestroA.rounder(temp,8));
		  }
		}
		
	    }
	}
    }
    
    public void actionPerformed(ActionEvent evt){
	double temp;
	if(evt.getSource()==b1){
	    stub.setZchar(Double.valueOf(text1.getText()).doubleValue());
	}
	if(c3.getState()==true){
		
		stub.Open();
		temp = Complex.Imaginary(Complex.Multiply(stub.getYAsIfOn(),stub.getZchar()));
		if(stub.getLength() == 0.25){
		    vlab6.setText("   \u221e   ( short )");
		    vlab7.setText("   \u221e ");
		}
		else if(stub.getLength() == 0.0 || stub.getLength() == 0.5){
		    vlab6.setText("   0.0   ( open )");
		    vlab7.setText("   0.0 ");
		}
		else{
		  if(temp < 0.0){
		    vlab6.setText("- j "+MaestroA.rounder(Complex.Imaginary(stub.getYAsIfOn())*(-1.0),8));
		    vlab7.setText("- j "+MaestroA.rounder(temp*(-1.0),8));
		  }
		  else{
		    vlab6.setText(" j "+MaestroA.rounder(Complex.Imaginary(stub.getYAsIfOn()),8));
		    vlab7.setText(" j "+MaestroA.rounder(temp,8));
		  }
		}
		
	}
	else if(c4.getState()==true){
		
		stub.Short();
		temp = Complex.Imaginary(Complex.Multiply(stub.getYAsIfOn(),stub.getZchar()));
		if(stub.getLength() == 0.0 || stub.getLength() == 0.5){
		    vlab6.setText("   \u221e   ( short )");
		    vlab7.setText("   \u221e ");
		}
		else if(stub.getLength() == 0.25){
			vlab6.setText("   0.0   ( open )");
			vlab7.setText("   0.0 ");
		}
		else{
		  if(temp < 0.0){
		    vlab6.setText("- j "+MaestroA.rounder(Complex.Imaginary(stub.getYAsIfOn())*(-1.0),8));
		    vlab7.setText("- j "+MaestroA.rounder(temp*(-1.0),8));
		  }
		  else{
		    vlab6.setText(" j "+MaestroA.rounder(Complex.Imaginary(stub.getYAsIfOn()),8));
		    vlab7.setText(" j "+MaestroA.rounder(temp,8));
		  }
		}
		
	}	       
    }
    
    public void itemStateChanged(ItemEvent evt){
	double temp;
	ItemSelectable ie = evt.getItemSelectable();
	if(evt.getSource()==c3 || evt.getSource()==c4){
		if(c3.getState()==true){
		    
		    stub.Open();
		    temp = Complex.Imaginary(Complex.Multiply(stub.getYAsIfOn(),stub.getZchar()));
		    if(stub.getLength() == 0.25){
			vlab6.setText("   \u221e   ( short )");
			vlab7.setText("   \u221e ");
		    }
		    else if(stub.getLength() == 0.0 || stub.getLength() == 0.5){
			vlab6.setText("   0.0   ( open )");
			vlab7.setText("   0.0 ");
		    }
		    else{
			if(temp < 0.0){
			    vlab6.setText("- j "+MaestroA.rounder(Complex.Imaginary(stub.getYAsIfOn())*(-1.0),8));
			    vlab7.setText("- j "+MaestroA.rounder(temp*(-1.0),8));
			}
			else{
			    vlab6.setText(" j "+MaestroA.rounder(Complex.Imaginary(stub.getYAsIfOn()),8));
			    vlab7.setText(" j "+MaestroA.rounder(temp,8));
			}
		    }
		    
		}
		else if(c4.getState()==true){
		    
		    stub.Short();
		    temp = Complex.Imaginary(Complex.Multiply(stub.getYAsIfOn(),stub.getZchar()));
		    if(stub.getLength() == 0.0 || stub.getLength() == 0.5){
			vlab6.setText("   \u221e   ( short )");
			vlab7.setText("   \u221e ");
		    }
		    else if(stub.getLength() == 0.25){
			vlab6.setText("   0.0   ( open )");
			vlab7.setText("   0.0 ");
		    }
		    else{
			if(temp < 0.0){
			    vlab6.setText("- j "+MaestroA.rounder(Complex.Imaginary(stub.getYAsIfOn())*(-1.0),8));
			    vlab7.setText("- j "+MaestroA.rounder(temp*(-1.0),8));
			}
			else{
			    vlab6.setText(" j "+MaestroA.rounder(Complex.Imaginary(stub.getYAsIfOn()),8));
			    vlab7.setText(" j "+MaestroA.rounder(temp,8));
			}
		    }
		    
		}	 
	}
	if(evt.getSource()==c1 || evt.getSource()==c2){
		if(c1.getState()==true){
		    stub.isEnable(true);
		}
		else if(c2.getState()==true){
		    stub.isEnable(false);
		}	 
	}
    } 
}//StubPanel
