//TransLoadPanel
//author Umberto Ravaioli, 2005

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


public class TransLoadPanel extends PC implements ItemListener, AdjustmentListener, ActionListener{
    public Checkbox c1, c2, c3;
    protected CheckboxGroup cgrp;
    private Trans_State state;
    //private static final Color bgcolor = new Color(216,216,191);
    private static final Color tinta = new Color(236,236,236);
    public static final Color bgcolor = new Color(236,236,236);
    //private static final Color bgcolor = new Color(200,200,200);
    private static final Font labfont=TheFonts.sanSerif10;
    
    public Scrollbar slider1, slider2, slider3, slider4;
    //public int SCROLLMIN=0, SCROLLMAX=3000, SCROLLMAX2 = 5999;
    public int SCROLLMIN=0, SCROLLMAX=10482, SCROLLMAX2 = 10963, SCROLLMAXG1 = 1001, SCROLLMAXG2 = 3601;
    public static String Gamma;
    public boolean locked_open, locked_short;
    private Image im;
    private Graphics buf;
    private int shift=30;
    //private boolean isGammaInput = false; 
    
    // CHANGE ==============================================================
        //public Button b2a, b2b, b3a, b3b;
        //private final Font labelfont=TheFonts.sanSerif12;
    //======================================================================
	
    public TransLoadPanel(String titulo, String[] nome, String[] unidade, Trans_State state){
	super(titulo,nome,unidade);
	setLayout(null);

	setBackground(bgcolor);
	this.state = state;
	cgrp = new CheckboxGroup();
	c1 = new Checkbox("Z",cgrp,true);
	c2 = new Checkbox("Y",cgrp,false);
	c3 = new Checkbox("",cgrp,false);
	//c3 = new Checkbox("\u0393",cgrp,false);
	add(c1);
	add(c2);
	add(c3);
	//titlelabel.setBounds(5,15,280,30);
	slider1 = new Scrollbar(Scrollbar.HORIZONTAL,500,1,SCROLLMIN,SCROLLMAX);	
	slider2 = new Scrollbar(Scrollbar.HORIZONTAL,5981,1,SCROLLMIN,SCROLLMAX2);	
	slider1.addAdjustmentListener(this);
	slider2.addAdjustmentListener(this);
	slider1.setVisible(true);
	slider2.setVisible(true);
        slider1.setBackground(Color.white);
        slider2.setBackground(Color.white);
	//slider1.setValue(1001);
	
	slider3 = new Scrollbar(Scrollbar.HORIZONTAL,707,1,SCROLLMIN,SCROLLMAXG1);	
	slider4 = new Scrollbar(Scrollbar.HORIZONTAL,450,1,SCROLLMIN,SCROLLMAXG2);
        slider3.setBackground(Color.white);
        slider4.setBackground(Color.white);
	slider3.addAdjustmentListener(this);
	slider4.addAdjustmentListener(this);
	slider3.setVisible(false);
	slider4.setVisible(false);
        
        // CHANGE  =====================================================
                //b2a = new Button("<");
                //b2a.setBackground(bgcolor);        
                //b2b = new Button(">");
                //b2b.setBackground(bgcolor);
                //b3a = new Button("<");
                //b3a.setBackground(bgcolor);
                //b3b = new Button(">");
                //b3b.setBackground(bgcolor);
                //b2a.setFont(labelfont);
                //b2b.setFont(labelfont);
                //b3a.setFont(labelfont);
                //b3b.setFont(labelfont);
                //add(b2a); add(b2b); add(b3a); add(b3b);
        //==============================================================
		
	add(slider1);
	add(slider2);
	add(slider3);
	add(slider4);
	
	
	b1.setBounds(85,3,100,20);
	b1.setBackground(Color.white);
        
	//lab1[0].setBounds(6+shift,32,10,10);
	text1[0].setBounds(32+shift,25,160,18);
	//lab2[0].setBounds(6+shift,67,25,18);
	text2[0].setBounds(32+shift,67,160,18);
	
	text1[1].setBounds(32+shift,25,160,18);
	text2[1].setBounds(32+shift,67,160,18);
	text1[1].setVisible(false);
	text2[1].setVisible(false);
	
	lab3[0].setBounds(195+shift,70,35,18); // units
        
	
        // sliders for Z and Y
	slider1.setBounds(32+shift,46,160,15);
        Panel ps1 = new Panel();
	    ps1.setBackground(Color.black);
	    ps1.setBounds(31+shift,45,162,17);
	slider2.setBounds(32+shift,88,160,15);
	Panel ps2 = new Panel();
	    ps2.setBackground(Color.black);
	    ps2.setBounds(31+shift,87,162,17);
            
        // CHANGE  =====================================================
                //b2a.setBounds(32+shift,46,14,15);
                //b2b.setBounds(32+shift+146,46,14,15);
                //b3a.setBounds(32+shift,88,14,15);
                //b3b.setBounds(32+shift+146,88,14,15);
        //==============================================================
                
	// sliders for Gamma
        slider3.setBounds(32+shift,46,160,15);
	slider4.setBounds(32+shift,88,160,15);
	Panel ps3 = new Panel();
	    ps3.setBackground(Color.black);
	    ps3.setBounds(31+shift,45,162,17);
	Panel ps4 = new Panel();
	    ps4.setBackground(Color.black);
	    ps4.setBounds(31+shift,87,162,17);
        
        add(ps1); add(ps2); add(ps3); add(ps3);
        
	c1.setBounds(30+shift,105,40,20);
	c1.setBackground(bgcolor);
	
	c2.setBounds(90+shift,105,40,20);
	c2.setBackground(bgcolor);
	
	c3.setBounds(150+shift,105,25,20);
	c3.setBackground(bgcolor);
	
	
	//text1[0].setText(""+state.ZL.Real());
	//text2[0].setText(""+state.ZL.Imaginary());
	text1[0].setText(""+MaestroA.rounder(state.ZL.Real(),7));
	text2[0].setText(""+MaestroA.rounder(state.ZL.Imaginary(),7));
	
	locked_open = false; locked_short = false;
	
	
	//Listeners
	c1.addItemListener(this);
	c2.addItemListener(this);
	c3.addItemListener(this);
	slider1.addAdjustmentListener(this);
	slider2.addAdjustmentListener(this);
	slider3.addAdjustmentListener(this);
	slider4.addAdjustmentListener(this);
	b1.addActionListener(this);
        //b2a.addActionListener(this);
        //b2b.addActionListener(this);
        //b3a.addActionListener(this);
        //b3b.addActionListener(this);
    }
    
    public void draw(Graphics g){
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
	    
            g.clearRect(0,0,getSize().width,getSize().height);
	    
            String Gamma;
	    Gamma="\u0393";
	    
            g.setColor(Color.black);
	    MaestroG.subscripterB("Set Load","","",g,12,10,15); 
            
            MaestroG.subscripterSerif(""+Gamma,"","",g,12,178+shift,120);

	    if(!c3.getState()){
                g.setColor(Color.black);	
                
                if(state.is_Load_Ztype){
                    MaestroG.subscripterSerif("Z","L","=",g,12,shift,40);
                }
                else{
                    MaestroG.subscripterSerif("Y","L","=",g,12,shift,40);
                }
            }
            else{
                MaestroG.subscripterSerif(""+Gamma,"L","=",g,12,shift,40);
            }
            
	    if(c3.getState()){
		g.drawLine(25+shift,70,15+shift,80);
		g.drawLine(15+shift,80,25+shift,80);
	    }
            else{
                MaestroG.subscripter("+ j","","",g,14,5+shift,80);
            }
	    g.setColor(Color.gray);
	     //g.drawLine(51,80,51,110);
	    //g.drawLine(106,80,106,110);
	    //g.drawLine(158,80,158,110);
	    
	    // CHANGE - REMOVE THIS WARNING, NO SPACE  =========================
            /*
	    if(c1.getState()){
		if(locked_open){
		    g.setColor(Color.red);
		    MaestroG.subscripter("Click second slider or chart to unlock state","","",g,10,5+shift,28);
		}
		else{
		    g.setColor(bgcolor);
		    MaestroG.subscripter("Click second slider or chart to unlock state","","",g,10,5+shift,28);
		}
	    }
	    if(c2.getState()){
		if(locked_short){
		    g.setColor(Color.red);
		    MaestroG.subscripter("Click second slider or chart to unlock state","","",g,10,5+shift,28);
		}
		else{
		    g.setColor(bgcolor);
		    MaestroG.subscripter("Click second slider or chart to unlock state","","",g,10,5+shift,28);
		}
	    }
            */
            //==================================================================
    }
    
    public void update(Graphics g){
		paint(g);
    }

    public void paint(Graphics g){
		
		if(im == null){
			im = createImage(getSize().width,getSize().height);
			buf = im.getGraphics();
			draw(buf);
		}
		else{
			draw(buf);
		}
		g.drawImage(im,0,0,null);
    }
    
    
   
    
    public void itemStateChanged(ItemEvent evt){
	
	if(evt.getSource()==c1){
	    	
	    lab1[0].setText("Z");
	    lab2[0].setText("+ j");
	    lab2[0].setVisible(true);
	    lab3[0].setText("[ \u03a9 ]");
	    
	    state.is_Load_Ztype = true;
	    slider1.setVisible(true);
	    slider2.setVisible(true);
	    slider3.setVisible(false);
	    slider4.setVisible(false);
	    
	    text1[0].setVisible(true);
	    text2[0].setVisible(true);
	    text1[1].setVisible(false);
	    text2[1].setVisible(false);
	    
	    Complex znorm = Complex.Divide(state.ZL,state.Z0);
	    if(state.IsLoadRegular || state.IsLoadImaginary){
		//Real part slider
		if(znorm.Real() > 0.0 && znorm.Real() <= 1.0){
		    slider1.setValue((int)(znorm.Real()*500));
		}
		else if(znorm.Real() > 1.0 && znorm.Real() <= 20.0){
		    slider1.setValue(500+(int)((znorm.Real()-1)*500));
		}
		else if(znorm.Real() > 20.0 && znorm.Real() <= 500.0){
		    slider1.setValue((int)(znorm.Real()-20)+10000);
		}
		else if(znorm.Real() == 0.0){
		    if(znorm.Imaginary() > -1.0E135){
			slider1.setValue(0);
		    }
		    else{
			slider1.setValue(0);
			slider2.setValue(0);
		    }
		}
		
		//Imaginary part slider
		if(znorm.Imaginary() > 0.0 && znorm.Imaginary() <= 20.0){
		    slider2.setValue((int)(znorm.Imaginary()*250)+5481);
		}
		else if(znorm.Imaginary() > 20.0 && znorm.Imaginary() <= 500.0){
		    slider2.setValue((int)(znorm.Imaginary()-20)+10481);
		}
		else if(znorm.Imaginary() < 0.0 && znorm.Imaginary() >= -20.0){
		    slider2.setValue(5481+(int)(znorm.Imaginary()*250));
		}
		else if(znorm.Imaginary() < -20.0 && znorm.Imaginary() >= - 500.0){
		    slider2.setValue(481+(int)(znorm.Imaginary()+20));
		}
		else if(znorm.Imaginary() == 0.0){
		    slider2.setValue(5481);
		}
		else if(znorm.Imaginary() == 500.0){
		    slider2.setValue(10963);
		}
		else if(znorm.Imaginary() == -500.0){
		    slider2.setValue(0);
		}
                //NEW
		text1[0].setText(""+MaestroA.rounder(state.ZL.Real(),7));
		text2[0].setText(""+MaestroA.rounder(state.ZL.Imaginary(),7));
	    }
	    else if(state.IsLoadShort){
		slider1.setValue(0);
		slider2.setValue(5481);
		text1[0].setText("0.0");
		text2[0].setText("0.0");
	    }
	    else if(state.IsLoadOpen){
		slider1.setValue(0);
		slider2.setValue(0);
		text1[0].setText("0.0");
		text2[0].setText("-1.0E135");
	    }
	    
	    if(state.IsLoadOpen){
		b1.setEnabled(false);
		slider1.setEnabled(false);
		locked_open = true;
		locked_short = false;
	    }
	    else{
		b1.setEnabled(true);
		slider1.setEnabled(true);
		locked_open = false;
		locked_short = false;
	    }
            repaint();
	}
	
	else if(evt.getSource()==c2){
	    
	    lab1[0].setText("Y");
	    lab2[0].setText("+ j");
	    lab2[0].setVisible(true);
	    lab3[0].setText("[ S ]");
	    
	    state.is_Load_Ztype = false;
	    slider1.setVisible(true);
	    slider2.setVisible(true);
	    slider3.setVisible(false);
	    slider4.setVisible(false);
	    
	    text1[0].setVisible(true);
	    text2[0].setVisible(true);
	    text1[1].setVisible(false);
	    text2[1].setVisible(false);
	    
	    Complex znorm = Complex.Multiply(state.YL,state.Z0);
	    if(state.IsLoadRegular || state.IsLoadImaginary){
		//Real part slider
		if(znorm.Real() > 0.0 && znorm.Real() <= 1.0){
		    slider1.setValue((int)(znorm.Real()*500));
		}
		else if(znorm.Real() > 1.0 && znorm.Real() <= 20.0){
		    slider1.setValue(500+(int)((znorm.Real()-1)*500));
		}
		else if(znorm.Real() > 20.0 && znorm.Real() <= 500.0){
		    slider1.setValue((int)(znorm.Real()-20)+10000);
		}
		else if(znorm.Real() == 0.0){
		    if(znorm.Imaginary() > -1.0E135){
			slider1.setValue(0);
		    }
		    else{
			slider1.setValue(0);
			slider2.setValue(0);
		    }
		}
		
		//Imaginary part slider
		if(znorm.Imaginary() > 0.0 && znorm.Imaginary() <= 20.0){
		    slider2.setValue((int)(znorm.Imaginary()*250)+5481);
		}
		else if(znorm.Imaginary() > 20.0 && znorm.Imaginary() <= 500.0){
		    slider2.setValue((int)(znorm.Imaginary()-20)+10481);
		}
		else if(znorm.Imaginary() < 0.0 && znorm.Imaginary() >= -20.0){
		    slider2.setValue(5481+(int)(znorm.Imaginary()*250));
		}
		else if(znorm.Imaginary() < -20.0 && znorm.Imaginary() >= - 500.0){
		    slider2.setValue(481+(int)(znorm.Imaginary()+20));
		}
		else if(znorm.Imaginary() == 0.0){
		    slider2.setValue(5481);
		}
		else if(znorm.Imaginary() == 500.0){
		    slider2.setValue(10963);
		}
		else if(znorm.Imaginary() == -500.0){
		    slider2.setValue(0);
		}
		
                //NEW
                text1[0].setText(""+MaestroA.rounder(state.YL.Real(),7));
                text2[0].setText(""+MaestroA.rounder(state.YL.Imaginary(),7));
		
	    }
	    else if(state.IsLoadOpen){
		slider1.setValue(0);
		slider2.setValue(5481);
		text1[0].setText("0.0");
		text2[0].setText("0.0");
	    }
	    else if(state.IsLoadShort){
		slider1.setValue(0);
		slider2.setValue(0);
		text1[0].setText("0.0");
		text2[0].setText("-1.0E135");
	    }
	    
	    if(state.IsLoadShort){
		b1.setEnabled(false);
		slider1.setEnabled(false);
		locked_open = false;
		locked_short = true;
	    }
	    else{
		b1.setEnabled(true);
		slider1.setEnabled(true);
		locked_open = false;
		locked_short = false;
	    }
	    repaint();
	}
	else if(evt.getSource()==c3){
	    b1.setEnabled(true);
	    slider1.setEnabled(true);
	    
	    lab1[0].setText("\u0393");
	    lab2[0].setText("");
	    lab2[0].setVisible(false);
	    lab3[0].setText(" \u00ba");
	    text1[1].setText(""+MaestroA.rounder(Complex.Magnitude(state.GammaL),8));
	    text2[1].setText(""+MaestroA.rounder(Complex.Arg2(state.GammaL,1),6));
	    state.is_Load_Ztype = true;
	    slider1.setVisible(false);
	    slider2.setVisible(false);
	    slider3.setVisible(true);
	    slider4.setVisible(true);
	    slider3.setValue((int)(Complex.Magnitude(state.GammaL)*1000.0));
	    slider4.setValue((int)(MaestroA.rounder(Complex.Arg2(state.GammaL,1),6)*10)+1800);
	    text1[0].setVisible(false);
	    text2[0].setVisible(false);
	    text1[1].setVisible(true);
	    text2[1].setVisible(true);
	
            repaint();
        }
        
    }
    
    public void actionPerformed(ActionEvent evt){
	            
    }
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
	  double tempres;
	  boolean setopen = false;
	  if(evt.getSource()==slider1){
	  //------------------------------------------------------------------------------
	    if(c1.getState()){
		
		tempres = 0.0;
		//if(slider1.getValue() == (SCROLLMAX-1) || slider2.getValue() == 0 || slider2.getValue() == (SCROLLMAX2-1)){// Open
		//if(slider2.getValue() == 0 || slider2.getValue() == (SCROLLMAX2-1)){// Open
		  //  //tempres = 20.0+((double)(slider1.getValue()-10000-1));
		  //  //text1[0].setText(""+tempres*state.Z0.Real());
		  //  //state.ZL = new Complex(tempres*state.Z0.Real(),state.ZL.Imaginary());
		  //  state.ZL = new Complex(0.0,-1.0E135);
		  //  state.YL = new Complex(0.0,0.0);
		//}
		//else 
		if(slider1.getValue() == 0){
		    tempres = 0.0;
		    //text1[0].setText(""+tempres*state.Z0.Real());
		    if(slider2.getValue() == 5481){//Short
			state.ZL = new Complex(0.0,0.0);
			state.YL = new Complex(0.0,-1.0E135);
			
		    }
		    else{//Imaginary 
			state.ZL = new Complex(0.0,state.ZL.Imaginary());
			state.YL = EMF.Inv(state.ZL);
		    }
		}
		else if(slider1.getValue() >=1 && slider1.getValue()<= 10000){
		    tempres = ((double)(slider1.getValue()))/500.0;
		    //text1[0].setText(""+tempres*state.Z0.Real());
		    state.ZL = new Complex(tempres*state.Z0.Real(),state.ZL.Imaginary());
		    state.YL = EMF.Inv(state.ZL);
		}
		else if(slider1.getValue() >10000 && slider1.getValue()<= 10480){
		    tempres = 20.0+((double)(slider1.getValue()-10000));
		    //text1[0].setText(""+tempres*state.Z0.Real());
		    state.ZL = new Complex(tempres*state.Z0.Real(),state.ZL.Imaginary());
		    state.YL = EMF.Inv(state.ZL);
		}
		else if(slider1.getValue() == (SCROLLMAX-1)){
		    tempres = 20.0+((double)(slider1.getValue()-10000-1));
		    state.ZL = new Complex(tempres*state.Z0.Real(),state.ZL.Imaginary());
		    state.YL = EMF.Inv(state.ZL);
		}
		state.GammaL = new Complex(MaestroA.rounder(EMF.computeGamma(state.ZL, state.Z0).Real(),8),
					    MaestroA.rounder(EMF.computeGamma(state.ZL, state.Z0).Imaginary(),8));
		
		text1[0].setText(""+MaestroA.rounder(tempres*state.Z0.Real(),6));
		
		repaint();
	    }
	    
	    else if(c2.getState()){
		
				
		tempres = 0.0;
		//if(slider1.getValue() == (SCROLLMAX-1) || slider2.getValue() == 0 || slider2.getValue() == (SCROLLMAX2-1)){// Open
		//if(slider2.getValue() == 0 || slider2.getValue() == (SCROLLMAX2-1)){// Open
		  //  state.YL = new Complex(0.0,-1.0E135);
		  //  state.ZL = new Complex(0.0,0.0);
		//}
		//else 
		if(slider1.getValue() == 0){
		    tempres = 0.0;
		    if(slider2.getValue() == 5481){//Short
			state.YL = new Complex(0.0,0.0);
			state.ZL = new Complex(0.0,-1.0E135);
			
		    }
		    else{//Imaginary 
			state.YL = new Complex(0.0,state.YL.Imaginary());
			state.ZL = EMF.Inv(state.YL);
		    }
		}
		else if(slider1.getValue() >=1 && slider1.getValue()<= 10000){
		    tempres = ((double)(slider1.getValue()))/500.0;
		    state.YL = new Complex(tempres/state.Z0.Real(),state.YL.Imaginary());
		    state.ZL = EMF.Inv(state.YL);
		}
		else if(slider1.getValue() >10000 && slider1.getValue()<= 10480){
		    tempres = 20.0+((double)(slider1.getValue()-10000));
		    state.YL = new Complex(tempres/state.Z0.Real(),state.YL.Imaginary());
		    state.ZL = EMF.Inv(state.YL);
		}
		else if(slider1.getValue() == (SCROLLMAX-1)){
		    tempres = 20.0+((double)(slider1.getValue()-10000-1));
		    state.YL = new Complex(tempres/state.Z0.Real(),state.YL.Imaginary());
		    state.ZL = EMF.Inv(state.YL);
		}
		
		state.GammaL = new Complex(MaestroA.rounder(EMF.computeGamma(state.ZL, state.Z0).Real(),8),
					    MaestroA.rounder(EMF.computeGamma(state.ZL, state.Z0).Imaginary(),8));
		
		text1[0].setText(""+MaestroA.rounder(tempres/state.Z0.Real(),6));
	    }
            repaint();
	  }
	  
	  if(evt.getSource()==slider2){
	    if(c1.getState()){
		b1.setEnabled(true);
		slider1.setEnabled(true);
		locked_open =false;
		locked_short = false;
		
		tempres = 0.0;
		
		if(slider2.getValue() == (SCROLLMAX2-1)){
		    tempres = 20.0+((double)(slider2.getValue()-10481-1));
		    //text2[0].setText(""+tempres*state.Z0.Real());
		    state.ZL = new Complex(state.ZL.Real(),tempres*state.Z0.Real());
		}
		else if(slider2.getValue() == 5481){
		    tempres = 0.0;
		    //text2[0].setText(""+tempres*state.Z0.Real());
		    state.ZL = new Complex(state.ZL.Real(),tempres*state.Z0.Real());
		}
		else if(slider2.getValue() >5481 && slider2.getValue()<= 10481){
		    tempres = ((double)(slider2.getValue())-5481)/250.0;
		    //text2[0].setText(""+tempres*state.Z0.Real());
		    state.ZL = new Complex(state.ZL.Real(),tempres*state.Z0.Real());
		}
		
		else if(slider2.getValue() >10481 && slider2.getValue()<= 10961){
		    tempres = 20.0+((double)(slider2.getValue()-10481));
		    //text2[0].setText(""+tempres*state.Z0.Real());
		    state.ZL = new Complex(state.ZL.Real(),tempres*state.Z0.Real());
		}
		//negative values
		else if(slider2.getValue() >481 && slider2.getValue()< 5481){
		    tempres = -((double)(5481-slider2.getValue()))/250.0;
		    //text2[0].setText(""+tempres*state.Z0.Real());
		    state.ZL = new Complex(state.ZL.Real(),tempres*state.Z0.Real());
		}
		else if(slider2.getValue() >0 && slider2.getValue()<= 481){
		    tempres = -20.0-((double)(481 - slider2.getValue()));
		    //text2[0].setText(""+tempres*state.Z0.Real());
		    state.ZL = new Complex(state.ZL.Real(),tempres*state.Z0.Real());
		}
		else if(slider2.getValue() == 0){
		    tempres = -20.0-((double)(481 - slider2.getValue())-1);
		    //text2[0].setText(""+tempres*state.Z0.Real());
		    state.ZL = new Complex(state.ZL.Real(),tempres*state.Z0.Real());
		}
		//-----------------------------------------------------
		state.YL = EMF.Inv(state.ZL);
		state.GammaL = EMF.computeGamma(state.ZL, state.Z0);
		
		if(state.is_Load_Ztype){
		    text2[0].setText(""+MaestroA.rounder(tempres*state.Z0.Real(),6));
		}
		else{
		    text2[0].setText(""+MaestroA.rounder(state.YL.Imaginary(),9));
		}
		//reset sliders for Gamma
		//slider3.setValue((int)(Complex.Magnitude(state.GammaL)/100.0));
		//slider4.setValue((int)(MaestroA.rounder(Complex.Arg2(state.GammaL,1),6)*10));	  
	    }
	    else if(c2.getState()){
		b1.setEnabled(true);
		slider1.setEnabled(true);
		locked_open =false;
		locked_short = false;
	    
		tempres = 0.0;
		
		if(slider2.getValue() == (SCROLLMAX2-1)){
		    tempres = 20.0+((double)(slider2.getValue()-10481-1));
		    state.YL = new Complex(state.YL.Real(),tempres/state.Z0.Real());
		}
		else if(slider2.getValue() == 5481){
		    tempres = 0.0;
		    state.YL = new Complex(state.YL.Real(),tempres/state.Z0.Real());
		}
		else if(slider2.getValue() >5481 && slider2.getValue()<= 10481){
		    tempres = ((double)(slider2.getValue())-5481)/250.0;
		    state.YL = new Complex(state.YL.Real(),tempres/state.Z0.Real());
		}
		
		else if(slider2.getValue() >10481 && slider2.getValue()<= 10961){
		    tempres = 20.0+((double)(slider2.getValue()-10481));
		    state.YL = new Complex(state.YL.Real(),tempres/state.Z0.Real());
		}
		//negative values
		else if(slider2.getValue() >481 && slider2.getValue()< 5481){
		    tempres = -((double)(5481-slider2.getValue()))/250.0;
		    state.YL = new Complex(state.YL.Real(),tempres/state.Z0.Real());
		}
		else if(slider2.getValue() >0 && slider2.getValue()<= 481){
		    tempres = -20.0-((double)(481 - slider2.getValue()));
		    state.YL = new Complex(state.YL.Real(),tempres/state.Z0.Real());
		}
		else if(slider2.getValue() == 0){
		    tempres = -20.0-((double)(481 - slider2.getValue())-1);
		    state.YL = new Complex(state.YL.Real(),tempres/state.Z0.Real());
		}
		//-----------------------------------------------------
		state.ZL = EMF.Inv(state.YL);
		state.GammaL = EMF.computeGamma(state.ZL, state.Z0);
		
		text2[0].setText(""+MaestroA.rounder(tempres/state.Z0.Real(),6));
	    }
	    repaint();
	  }
	  
	  double phase = 0.0, mag = 1.0;
	  phase = MaestroA.rounder(Math.PI/180*0.1*(double)(slider4.getValue()-1800),6);
	  mag = MaestroA.rounder(0.001*(double)(slider3.getValue()),8);
	  
	  if(evt.getSource()==slider3){
		b1.setEnabled(true);
		slider1.setEnabled(true);
		locked_open =false;
		locked_short = false;
	  //------------------------------------------------------------------------------
		if(slider3.getValue() == (SCROLLMAXG1-1) && (slider4.getValue() == 0 || slider4.getValue() == (SCROLLMAXG2-1))){//short
		    state.GammaL = new Complex(-1.0,0.0);
		    state.YL = new Complex(0.0, -1.0E135);
		    state.ZL = new Complex(0.0, 0.0);
		    text1[1].setText("1.0");
		    state.IsLoadShort = true; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
		else if(slider3.getValue() == (SCROLLMAXG1-1) && slider4.getValue() == ((SCROLLMAXG2-1)/2)){//open
		    state.GammaL = new Complex(1.0,0.0);
		    state.ZL = new Complex(0.0, -1.0E135);
		    state.YL = new Complex(0.0, 0.0);
		    text1[1].setText("1.0");
		    text2[1].setText("0.0");
		    state.IsLoadShort = false; state.IsLoadOpen = true; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
		else if(slider3.getValue() == (SCROLLMAXG1-1) && (slider4.getValue() != 0 || slider4.getValue() != (SCROLLMAXG2-1))){//imaginary
		    state.GammaL = new Complex(mag*Math.cos(phase), mag*Math.sin(phase));
		    text1[1].setText("1.0");
		    state.ZL = new Complex(0.0, EMF.computeZinAt(state.GammaL,Complex.Real(state.Z0),0.0,true).Imaginary());
		    state.YL = new Complex(0.0, EMF.computeYinAt(state.GammaL,Complex.Real(state.Z0),0.0,true).Imaginary());
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = true; state.IsLoadRegular = false;
		}
                else if((slider4.getValue() == 0 || slider4.getValue() == SCROLLMAXG2-1) 
                        && (slider3.getValue() > 0 && slider3.getValue() < SCROLLMAXG1-1)){// Real - non short or open
                        
                            state.IsLoadShort = false; state.IsLoadOpen = true; state.IsLoadImaginary = false; state.IsLoadRegular = true;
                            phase = MaestroA.rounder(Math.PI/180*0.1*(double)(slider4.getValue()-1800),6);
                            double phasedeg = MaestroA.rounder(0.1*(double)(slider4.getValue()-1800),6);
                            mag = MaestroA.rounder(0.001*(double)(slider3.getValue()),8);
                            state.GammaL = new Complex(mag*Math.cos(phase), mag*Math.sin(phase));
                            //text2[1].setText(""+phasedeg);
                            text1[1].setText(""+MaestroA.rounder(mag,7));
                            state.ZL = new Complex(Complex.Real(EMF.computeZinAt(state.GammaL,Complex.Real(state.Z0),0.0,true)),0.0);
                            state.YL = new Complex(Complex.Real(EMF.computeYinAt(state.GammaL,Complex.Real(state.Z0),0.0,true)),0.0);
                }
                else if(slider3.getValue()== 0){
                    slider4.setValue(SCROLLMAXG2/2);
                    state.GammaL = new Complex(0.0,0.0);
                    text1[1].setText("0.0");
                    text2[1].setText("0.0");
                    state.ZL = EMF.computeZinAt(state.GammaL,Complex.Real(state.Z0),0.0,true);
		    state.YL = EMF.computeYinAt(state.GammaL,Complex.Real(state.Z0),0.0,true);
		    
                    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = true;
                }
		else{
		    state.GammaL = new Complex(mag*Math.cos(phase), mag*Math.sin(phase));
		    text1[1].setText(""+MaestroA.rounder(mag,7));
		    state.ZL = EMF.computeZinAt(state.GammaL,Complex.Real(state.Z0),0.0,true);
		    state.YL = EMF.computeYinAt(state.GammaL,Complex.Real(state.Z0),0.0,true);
		    
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = true;
		}
	  }
	  
	  
	  if(evt.getSource()==slider4){
		if(slider3.getValue() == (SCROLLMAXG1-1) && slider4.getValue() == (SCROLLMAXG2-1)){//short 180
			state.GammaL = new Complex(-1.0,0.0);
			state.ZL = new Complex(0.0, -1.0E135);
			state.YL = new Complex(0.0, 0.0);
			text1[1].setText("1.0");
			text2[1].setText("180.0");
			state.IsLoadShort = true; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
		else if(slider3.getValue() == (SCROLLMAXG1-1) && slider4.getValue() == 0){//short -180
			state.GammaL = new Complex(-1.0,0.0);
			state.ZL = new Complex(0.0, -1.0E135);
			state.YL = new Complex(0.0, 0.0);
			text1[1].setText("1.0");
			text2[1].setText("-180.0");
			state.IsLoadShort = true; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
		else if(slider3.getValue() == (SCROLLMAXG1-1) && slider4.getValue() == (SCROLLMAXG2-1)/2){//open
			state.GammaL = new Complex(1.0,0.0);
			state.ZL = new Complex(0.0, -1.0E135);
			state.YL = new Complex(0.0, 0.0);
			text1[1].setText("1.0");
			text2[1].setText("0.0");
			state.IsLoadShort = false; state.IsLoadOpen = true; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
                else if((slider4.getValue() == 0 || slider4.getValue() == SCROLLMAXG2-1) 
                        && (slider3.getValue() > 0 && slider3.getValue() < SCROLLMAXG1-1)){// Real - non short or open
                        
                            state.IsLoadShort = false; state.IsLoadOpen = true; state.IsLoadImaginary = false; state.IsLoadRegular = true;
                            phase = MaestroA.rounder(Math.PI/180*0.1*(double)(slider4.getValue()-1800),6);
                            double phasedeg = MaestroA.rounder(0.1*(double)(slider4.getValue()-1800),6);
                            mag = MaestroA.rounder(0.001*(double)(slider3.getValue()),8);
                            state.GammaL = new Complex(mag*Math.cos(phase), mag*Math.sin(phase));
                            text2[1].setText(""+MaestroA.rounder(phasedeg,7));
                            state.ZL = new Complex(Complex.Real(EMF.computeZinAt(state.GammaL,Complex.Real(state.Z0),0.0,true)),0.0);
                            state.YL = new Complex(Complex.Real(EMF.computeYinAt(state.GammaL,Complex.Real(state.Z0),0.0,true)),0.0);
                }
		else{// regular
		    phase = MaestroA.rounder(Math.PI/180*0.1*(double)(slider4.getValue()-1800),6);
		    double phasedeg = MaestroA.rounder(0.1*(double)(slider4.getValue()-1800),6);
                    
		    mag = MaestroA.rounder(0.001*(double)(slider3.getValue()),8);
                    
                    {
                        state.GammaL = new Complex(mag*Math.cos(phase), mag*Math.sin(phase));
                    }
                    
		    text2[1].setText(""+MaestroA.rounder(phasedeg,7));
		    state.ZL = EMF.computeZinAt(state.GammaL,Complex.Real(state.Z0),0.0,true);
		    state.YL = EMF.computeYinAt(state.GammaL,Complex.Real(state.Z0),0.0,true);
		    state.IsLoadShort = false; state.IsLoadOpen = false; 
		    
		    if(slider3.getValue() == (SCROLLMAXG1-1)){
			state.IsLoadImaginary = true;
			state.IsLoadRegular = false;
			Complex tz, ty;
			tz = EMF.computeZinAt(state.GammaL,Complex.Real(state.Z0),0.0,true);
			ty = EMF.computeYinAt(state.GammaL,Complex.Real(state.Z0),0.0,true);
			state.ZL = new Complex(0.0, tz.Imaginary());
			state.YL = new Complex(0.0, ty.Imaginary());
		    }
		    else{
			state.IsLoadImaginary = false;
			state.IsLoadRegular = true;
			state.ZL = EMF.computeZinAt(state.GammaL,Complex.Real(state.Z0),0.0,true);
			state.YL = EMF.computeYinAt(state.GammaL,Complex.Real(state.Z0),0.0,true);
		    }
		}
	  }
    }

}
