//StubPanel.java
// THIS CODE SETS UP LOWER PART OF SET LINE/DISCRETE ELEMENT PANEL
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class StubPanelNew extends Panel implements ChangeListener, ActionListener, ItemListener{
    //private static final Color bgcolor = new Color(250,250,250);
    private static final Color bgcolor = new Color(236,236,236);
    //private static final Color bgcolor = new Color(216,216,191); // kaki
    private Label lab1, lab2, lab3, lab3b, lab3c, lab3d, lab4, lab5, lab5b, lab5c, lab5d, lab6, lab7, titlelabel, labeps, labeps2, labeps3;
    public Label vlab3, vlab4, vlab5, vlab6, vlab7;
    private String titulo = "Unknown Title";
    public CheckboxGroup cgrid1, cgrid2;
    public Checkbox c1, c2, c3, c4;
    public Checkbox lock;
    public TextField text1, text2;
    public JSlider s1,s1b, s2, s2b;
    private Font labfont, labfont2, labfontsmall;
    private Font labfontI;
    private Font labfontS;
    private Font titlefont;
    public int SCROLLMIN=0, SCROLLMAX=10001, SCROLLMAXB=1000, SCROLLMAXC=2001;
    // CHANGE 9/27/2017  =======================================================
    public Button b1;
    //==========================================================================
    private Stub stub;
    public Color colorstub;
    private Trans_State state;
   
    public StubPanelNew(String titulo,Stub stub,Trans_State state, Color colorstub){
	super();
	this.stub = stub;
	this.state = state;
	setBackground(bgcolor);
	this.titulo = titulo;
        this.colorstub = colorstub;
        
        labfont = new Font("SanSerif",Font.PLAIN,state.s12);
        labfont2 = new Font("SanSerif",Font.PLAIN,state.s11);
        labfontsmall = new Font("SanSerif",Font.PLAIN,state.s10);
        labfontI = new Font("Serif",Font.ITALIC,state.s14);
        labfontS = new Font("Serif",Font.PLAIN,state.s14);
        titlefont = new Font("SanSerif",Font.PLAIN,state.s14);
        
	setLayout(null);
        
        titlelabel  = new Label(titulo,Label.CENTER); 
	titlelabel.setFont(titlefont);
	titlelabel.setForeground(bgcolor);
	//lab1 = new Label("Status ",Label.LEFT);
	//lab2 = new Label("Type ",Label.LEFT);
	lab3 = new Label("y",Label.RIGHT);
        lab3c = new Label("s",Label.LEFT);
        lab3d = new Label(" = ",Label.CENTER);
        lab3b = new Label("[ S ]",Label.LEFT);
	//CHANGE # 1  9/26/2017  ===============================================
        //lab4 = new Label("Ys =",Label.RIGHT);
        lab4 = new Label("Ys",Label.LEFT);
        //======================================================================
	lab5 = new Label("Discrete Element - Admittance",Label.LEFT);
        lab5b= new Label("[ @         ]",Label.LEFT);
        lab5c= new Label("f",Label.RIGHT);
	lab5d= new Label("o",Label.LEFT);
        
        labeps3 = new Label(" = ",Label.CENTER);
        labeps = new Label("\u03b5",Label.RIGHT);
        labeps2 = new Label("r",Label.LEFT);
        
	lab3.setFont(labfont);
        lab3c.setFont(labfont);
        lab3b.setFont(titlefont);
	lab4.setFont(labfont);
	lab5.setFont(labfont);
        lab5b.setFont(labfont);
        lab5c.setFont(labfontI);
        lab5d.setFont(labfontI);
        
        labeps.setFont(titlefont);
        labeps2.setFont(labfont);
        labeps3.setFont(labfont);
        
	lab3.setForeground(colorstub);
        lab3c.setForeground(colorstub);
        lab3d.setForeground(colorstub);
        labeps.setForeground(colorstub);
        labeps2.setForeground(colorstub);
        labeps3.setForeground(colorstub);
        
        lab3b.setForeground(colorstub);
	lab4.setForeground(colorstub);
	lab5.setForeground(colorstub);
	
	//CHANGE 9/27/2017 =====================================================
        vlab3 = new Label("= j "+MaestroA.rounder(state.ZIMP[0]*state.YS.Imaginary(),6)+" (norm.)",Label.LEFT);
        vlab4 = new Label("= j "+MaestroA.rounder(state.YS.Imaginary(),6)+" [ S ]",Label.LEFT);
        //======================================================================
        
        vlab4.setForeground(colorstub);
        
	vlab5 = new Label(""+MaestroA.rounder(stub.getLength(),5)+" \u03bb",Label.LEFT);
	vlab5.setForeground(colorstub);
        
        vlab3.setFont(labfont);
        vlab4.setFont(labfont);
	vlab5.setFont(labfont);
	
        lock = new Checkbox(" Lock Cursor",false);
        lock.setForeground(Color.black);
        lock.setBackground(bgcolor);
        lock.setFont(labfont);
        
	text1 = new TextField("100.0",8);
        text1.setForeground(colorstub);
        text2 = new TextField("1.0",8);
        text2.setForeground(colorstub);

	
	s1 = new JSlider(SCROLLMIN,SCROLLMAX -1);
        s1b = new JSlider(SCROLLMIN,SCROLLMAXB -1);        
        s2 = new JSlider(SCROLLMIN,SCROLLMAXC -1);	
	s2b = new JSlider(SCROLLMIN,SCROLLMAXB -1);
	s1.setValue(SCROLLMAX -1);
	s1b.setValue(SCROLLMAXB -1);
	s2b.setValue(SCROLLMIN -1);
        s2.setValue(1000);
        /*
	s1 = new Scrollbar(Scrollbar.HORIZONTAL,(int)((SCROLLMAX-SCROLLMIN)*(1.0-stub.getPosition()/state.lineLength_part1)),
			    1,SCROLLMIN,SCROLLMAX);
        s1b = new Scrollbar(Scrollbar.HORIZONTAL,(SCROLLMAXB-SCROLLMIN),1,SCROLLMIN,SCROLLMAXB);
        
        s2 = new Scrollbar(Scrollbar.HORIZONTAL,(int)((SCROLLMAXC-SCROLLMIN)),
			    1,SCROLLMIN,SCROLLMAXC);
	
	s2b = new Scrollbar(Scrollbar.HORIZONTAL,(int)(SCROLLMAXB-SCROLLMIN),1,SCROLLMIN,SCROLLMAXB);
	s2b.setValue(SCROLLMIN);
        s2.setValue(1000);
	*/
        
        
        b1 = new Button("Update"); // not used here
	b1.setBackground(bgcolor);
        b1.setFont(labfontsmall);
        
        add(lock);
        
        // CHANGE #1 6/14/2017 =================================================
        // MODIFY LINE BELOW
        //lock.setBounds(180,15,120,15);
        lock.setBounds(state.s180,state.s15,state.s120,state.s15);
        //======================================================================
        lab3.setBounds(state.s15,0,state.s15,state.s15);
        lab3c.setBounds(state.s30,state.s4,state.s25,state.s15);
        lab3d.setBounds(state.s43,state.s10,state.s20,state.s20);
        lab3b.setBounds(state.s155,state.s10,state.s20,state.s20);
        //======================================================================
        vlab3.setBounds(state.s51,state.s2,state.s135,state.s15);
        add(vlab3);
        
        text1.setBounds(state.s55,state.s10,state.s75,state.s20);
        text2.setBounds(state.s241,state.s10,state.s45,state.s20);

        b1.setBounds(state.s225,state.s60,state.s60,state.s20);
        b1.setForeground(colorstub);
        b1.setFont(labfontsmall);
        add(lab3);
        
        //  CHANGE #7  9/26/2017  COMMENT LINE OUT =====================================
        //        add(lab3b);
        //==============================================================================
        
        add(lab3c);
        
        //  CHANGE #7  9/26/2017  COMMENT LINE OUT =====================================
        //        add(lab3d);
        //==============================================================================
        // CHANGE #2   9/26/2017  USE LINE BELOW AS CHANGED  ===================
        lab4.setBounds(state.s25,state.s20,state.s20,state.s15);
        //======================================================================
        
        add(lab4);
        
        vlab4.setBounds(state.s51,state.s20,state.s135,state.s15);
        add(vlab4);
        lab5b.setForeground(colorstub);
        lab5c.setForeground(colorstub);
        lab5d.setForeground(colorstub);
        
        lab5b.setBounds(state.s10,state.s40,state.s50,state.s15);
        lab5c.setBounds(state.s31,state.s40,state.s12,state.s17);
        lab5d.setBounds(state.s41,state.s46,state.s12,state.s11);
        
        lab5.setBounds(state.s15,state.s40,state.s200,state.s15);
        add(lab5);
        vlab5.setBounds(state.s160,state.s40,state.s120,state.s15);
        
        add(lab5);
        //add(vlab5);
        if(state.IsMac){
            s2.setBounds(state.s24,state.s60,state.s172,state.s15);
        }
        else{
            s2.setBounds(state.s10,state.s60,state.s200,state.s15);
        }
        add(s2);
        
        if(state.IsMac){
            s2b.setBounds(state.s10+state.s14,state.s78,state.s172,state.s15);
        }
        else{
            s2b.setBounds(state.s10,state.s78,state.s200,state.s15);
        }
        add(s2b);
	/*        
        Panel pslidA = new Panel();
	    pslidA.setBackground(Color.black);
	    add(pslidA);
	    pslidA.setBounds(state.s10-1,state.s60-1,state.s200+2,state.s15+2);
        Panel pslidB = new Panel();
	    pslidB.setBackground(Color.black);
	    add(pslidB);
	    pslidB.setBounds(state.s10-1,state.s78-1,state.s200+2,state.s15+2);
	*/
	s1.setBackground(bgcolor);     //new Color(230,230,230));
        s1b.setBackground(bgcolor);
        s2.setBackground(bgcolor);
	s2b.setBackground(bgcolor);
	
        s1.setForeground(colorstub);
        s1b.setForeground(colorstub);
        s2.setForeground(colorstub);
	
	//Listeners
	s1.addChangeListener(this);
        s1b.addChangeListener(this);
        s2.addChangeListener(this);
        s2b.addChangeListener(this);
	b1.addActionListener(this);
    }
    
    public void paint(Graphics g){
                Font normalfont = new Font("SanSerif",Font.PLAIN,state.font11);
                Font symbolfont = new Font("Serif",Font.PLAIN,state.font12);
                Graphics2D g2d = (Graphics2D)g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

		int x, y, dx, dxx, dy, dyy, stepx;
            	boolean Gamma_plusone, Gamma_minusone;
            	double tempR = 0.0; double tempX = 0.0;

            	String alpha, Ohm, lambda, infinity, Gamma, plusj, minusj, 
                       sign, plusr, minusr, signr;
		g.setFont(symbolfont);
		alpha="\u03b1";
		lambda="\u03bb";
		Ohm="\u03a9";
		infinity="\u221e";
		Gamma="\u0393";
                g.setFont(normalfont);
		plusj =" + j ";
		minusj=" - j ";
                plusr ="";
                minusr =" - ";
                
                Complex tempc;
	    FontMetrics fm;
	    x = state.s20;
	    y = state.s195;
	    dx = state.s80;
	    dxx = state.s15;
	    g.setFont(normalfont);
	    fm = g.getFontMetrics();
	    dy = fm.getHeight();
	    dyy = dy + state.s5;

            int xinit2, xinit3;
            xinit2 = x+dxx+state.s95;

	//g.setColor(Color.red.darker());
        g.setColor(colorstub);

        // frame of length slider
        g.drawRect(state.s37,state.s527,state.s245,state.s25);
	
	s1.requestFocusInWindow();
	s1b.requestFocusInWindow();
	s2.requestFocusInWindow();
	s2b.requestFocusInWindow();
    }
    
    public synchronized void reset(){
            //s1 = new Scrollbar(Scrollbar.HORIZONTAL,(SCROLLMAX-SCROLLMIN),1,SCROLLMIN,SCROLLMAX);
            //s1b = new Scrollbar(Scrollbar.HORIZONTAL,(SCROLLMAXB-SCROLLMIN),1,SCROLLMIN,SCROLLMAXB);
            int Zreset;
            
            Zreset = (int)((int)(state.ZBoundHigh/100.0)*100+100);
            SCROLLMAXC = Zreset+1;
            
            s2.setMaximum(SCROLLMAXC);
	    vlab4.setText(" 0.0");
	    s1.setValue(s1.getMaximum());
	    s1b.setValue(s1b.getMaximum());
            stub.setPosition(getValue()+getValue2());
            
            // CHANGE # 4  9/26/2017  MODIFY LINES BELOW AS WRITTEN  ===========
            if(state.YS.Imaginary() >= 0.0){
                if(state.YS.Imaginary() >= 1.0){
                    vlab4.setText("= j "+MaestroA.rounder(state.YS.Imaginary(),3)+" [ S ]");
                }
                else{
                    vlab4.setText("= j "+MaestroA.rounder(state.YS.Imaginary(),6)+" [ S ]");
                }
            }
            else{
                if(Math.abs(state.YS.Imaginary()) >= 1.0){
                    vlab4.setText("= - j "+MaestroA.rounder(Math.abs(state.YS.Imaginary()),3)+" [ S ]");
                }
                else{
                    vlab4.setText("= - j "+MaestroA.rounder(Math.abs(state.YS.Imaginary()),6)+" [ S ]");
                }
            }
            if((state.YS.Imaginary()) >= 0.0){
                if((state.YS.Imaginary()*state.ZIMP[0]) >= 1.0){
                    vlab3.setText("= j "+MaestroA.rounder(state.YS.Imaginary()*state.ZIMP[0],3)+" (norm.)");
                }
                else{
                    vlab3.setText("= j "+MaestroA.rounder(state.YS.Imaginary()*state.ZIMP[0],6)+" (norm.)");
                }
            }
            else{
                if(Math.abs(state.YS.Imaginary()*state.ZIMP[0]) >= 1.0){
                    vlab3.setText("= - j "+MaestroA.rounder(Math.abs(state.YS.Imaginary()*state.ZIMP[0]),3)+" (norm.)");
                }
                else{
                    vlab3.setText("= - j "+MaestroA.rounder(Math.abs(state.YS.Imaginary()*state.ZIMP[0]),6)+" (norm.)");
                }
            }
            //==================================================================
            
            s2.setValue((int)((SCROLLMAXC-SCROLLMIN)*0.5));
	    //s2b.setValue(s2b.getMaximum());
            s2b.setValue(SCROLLMIN);
            text1.setText(""+s2.getValue());
            
            stub.setLength(0.25);
            vlab5.setText(""+stub.getLength()+" \u03bb");
            
            
            repaint();
    }
    
    public void stateChanged(ChangeEvent evt){
	double temp;
	
	if(evt.getSource()==s2b){
	    //state.ZIMP[1] = s2.getValue()+s2b.getValue()*0.001;
            //state.YS = new Complex(0.0, 0.001*(s2.getValue()+s2b.getValue()*0.001));
            if(s2.getValue() < (SCROLLMAXC-1)/2){
                state.YS = new Complex(0.0, MaestroA.rounder(0.001*(s2.getValue()-(SCROLLMAXC-1)/2+s2b.getValue()*0.001),7));
            }
            else{
                state.YS = new Complex(0.0, MaestroA.rounder(0.001*(s2.getValue()-(SCROLLMAXC-1)/2+s2b.getValue()*0.001),7));
            }
                    // CHANGE # 5  9/26/2017  MODIFY LINES BELOW AS WRITTEN  ===
                    if(state.YS.Imaginary() >= 0.0){
                        if(state.YS.Imaginary() >= 1.0){
                            vlab4.setText("= j "+MaestroA.rounder(state.YS.Imaginary(),3)+" [ S ]");
                        }
                        else{
                            vlab4.setText("= j "+MaestroA.rounder(state.YS.Imaginary(),6)+" [ S ]");
                        }
                    }
                    else{
                        if(Math.abs(state.YS.Imaginary()) >= 1.0){
                            vlab4.setText("= - j "+MaestroA.rounder(Math.abs(state.YS.Imaginary()),3)+" [ S ]");
                        }
                        else{
                            vlab4.setText("= - j "+MaestroA.rounder(Math.abs(state.YS.Imaginary()),6)+" [ S ]");
                        }
                    }
                    if((state.YS.Imaginary()) >= 0.0){
                        if((state.YS.Imaginary()*state.ZIMP[0]) >= 1.0){
                            vlab3.setText("= j "+MaestroA.rounder(state.YS.Imaginary()*state.ZIMP[0],3)+" (norm.)");
                        }
                        else{
                            vlab3.setText("= j "+MaestroA.rounder(state.YS.Imaginary()*state.ZIMP[0],6)+" (norm.)");
                        }
                    }
                    else{
                        if(Math.abs(state.YS.Imaginary()*state.ZIMP[0]) >= 1.0){
                            vlab3.setText("= - j "+MaestroA.rounder(Math.abs(state.YS.Imaginary()*state.ZIMP[0]),3)+" (norm.)");
                        }
                        else{
                            vlab3.setText("= - j "+MaestroA.rounder(Math.abs(state.YS.Imaginary()*state.ZIMP[0]),6)+" (norm.)");
                        }
                    }
                    
            //==================================================================
            //stub.setZchar(state.ZIMP[1]);
            //vlab5.setText(""+state.ZIMP[1]+" \u03bb");
            //text1.setText(""+state.ZIMP[1]);
            
            vlab5.setText(""+state.YS.Imaginary()+" \u03bb");
	    text1.setText(""+state.YS.Imaginary());
            repaint();
	}
        else if(evt.getSource()==s2){
	    s2b.setValue(s2b.getMinimum());
            //state.ZIMP[1] = s2.getValue()+s2b.getValue()*0.001;
            //state.YS = new Complex(0.0, 0.001*(s2.getValue()+s2b.getValue()*0.001));
            state.YS = new Complex(0.0, MaestroA.rounder(0.001*(s2.getValue()-(SCROLLMAXC-1)/2+s2b.getValue()*0.001),6));
            
            // CHANGE # 6  9/26/2017  MODIFY LINES BELOW AS WRITTEN  ===========
            
            if(state.YS.Imaginary() >= 0.0){
                        if(state.YS.Imaginary() >= 1.0){
                            vlab4.setText("= j "+MaestroA.rounder(state.YS.Imaginary(),3)+" [ S ]");
                        }
                        else{
                            vlab4.setText("= j "+MaestroA.rounder(state.YS.Imaginary(),6)+" [ S ]");
                        }
                    }
                    else{
                        if(Math.abs(state.YS.Imaginary()) >= 1.0){
                            vlab4.setText("= - j "+MaestroA.rounder(Math.abs(state.YS.Imaginary()),3)+" [ S ]");
                        }
                        else{
                            vlab4.setText("= - j "+MaestroA.rounder(Math.abs(state.YS.Imaginary()),6)+" [ S ]");
                        }
                    }
                    if((state.YS.Imaginary()) >= 0.0){
                        if((state.YS.Imaginary()*state.ZIMP[0]) >= 1.0){
                            vlab3.setText("= j "+MaestroA.rounder(state.YS.Imaginary()*state.ZIMP[0],3)+" (norm.)");
                        }
                        else{
                            vlab3.setText("= j "+MaestroA.rounder(state.YS.Imaginary()*state.ZIMP[0],6)+" (norm.)");
                        }
                    }
                    else{
                        if(Math.abs(state.YS.Imaginary()*state.ZIMP[0]) >= 1.0){
                            vlab3.setText("= - j "+MaestroA.rounder(Math.abs(state.YS.Imaginary()*state.ZIMP[0]),3)+" (norm.)");
                        }
                        else{
                            vlab3.setText("= - j "+MaestroA.rounder(Math.abs(state.YS.Imaginary()*state.ZIMP[0]),6)+" (norm.)");
                        }
                    }
            
            
            
            //stub.setZchar(state.ZIMP[1]);
            
            //System.out.println(state.ZIMP[1]);
	    
            //vlab5.setText(""+state.ZIMP[1]+" \u03bb");
	    //text1.setText(""+state.ZIMP[1]);
            
            vlab5.setText(""+state.YS.Imaginary()+" \u03bb");
	    text1.setText(""+state.YS.Imaginary());
            
            //if(s2.getValue() == 0 || s2.getValue() == (SCROLLMAXC-1)){
            if(s2.getValue() == (SCROLLMAXC-1)){
                s2b.setEnabled(false);
            }
            else{
                s2b.setEnabled(true);                
            }
	}
    }
    
    public void actionPerformed(ActionEvent evt){
	double temp;
	
	if(evt.getSource()==b1){
	    //double tempstub = Double.valueOf(text1.getText()).doubleValue();
	    //if( tempstub < 0.0){
		//stub.setZchar(0.0);
		//text1.setText("0.0");
		
	    //}
	    //else{
		//stub.setZchar(Double.valueOf(text1.getText()).doubleValue());
	    //}
            
            //double tempeps = Double.valueOf(text2.getText()).doubleValue();
	    //if( tempeps < 0.0){
		//text2.setText("1.0");
	    //}
            int temp1, temp2;
            temp1 = (int)(Double.valueOf(text1.getText()).doubleValue()*1000);
            if(temp1 > 1000.0){temp1 = 1000; temp2 = 0; text1.setText("1.0");}
            if(temp1 < -1000.0){temp1 = -1000; temp2 = 0; text1.setText("-1.0");}
            s2.setValue(temp1+(SCROLLMAXC - 1)/2);
            //System.out.println(temp1);
            //System.out.println((SCROLLMAXC - 1)/2);
            
            temp2 = (int)(1000*(1000*Double.valueOf(text1.getText()).doubleValue()-temp1));
            if(temp2 > 999){temp2 = 999;}
            s2b.setValue(temp2);
            //System.out.println(temp2);
            
	}
    }
    
    public double getValue(){
		SCROLLMAX = s1.getMaximum();
		SCROLLMIN = s1.getMinimum();
		
		    return (MaestroA.rounder((SCROLLMAX-SCROLLMIN-1-s1.getValue())*state.lineLength_part1/(SCROLLMAX-SCROLLMIN-1),5));
	}
	
	public double getValue2(){
		SCROLLMAXB = s1b.getMaximum();
		SCROLLMIN = s1b.getMinimum();
		if(state.lineLength < 1.0){
		    return (MaestroA.rounder((double)(SCROLLMAXB-SCROLLMIN-1-s1b.getValue())*0.0001/(double)(SCROLLMAXB-SCROLLMIN),7));
		}
		else if(state.lineLength >= 1.0 && state.lineLength < 10.0){
		    return (MaestroA.rounder((double)(SCROLLMAXB-SCROLLMIN-1-s1b.getValue())*0.001/(double)(SCROLLMAXB-SCROLLMIN),6));
		}
		else if(state.lineLength >= 10.0 && state.lineLength < 100.0){
		    return (MaestroA.rounder((double)(SCROLLMAXB-SCROLLMIN-1-s1b.getValue())*0.01/(double)(SCROLLMAXB-SCROLLMIN),5));
		}
		else if(state.lineLength >= 100.0 && state.lineLength < 1000.0){
		    return (MaestroA.rounder((double)(SCROLLMAXB-SCROLLMIN-1-s1b.getValue())*0.1/(double)(SCROLLMAXB-SCROLLMIN),4));
		}
		else if(state.lineLength >= 1000.0 && state.lineLength < 10000.0){
		    return (MaestroA.rounder((double)(SCROLLMAXB-SCROLLMIN-1-s1b.getValue())/(double)(SCROLLMAXB-SCROLLMIN),3));
		}
		else if(state.lineLength >= 10000.0 && state.lineLength < 100000.0){
		    return (MaestroA.rounder((double)(SCROLLMAXB-SCROLLMIN-1-s1b.getValue())*10.0/(double)(SCROLLMAXB-SCROLLMIN),2));
		}
		else{
		    return (MaestroA.rounder((double)(SCROLLMAXB-SCROLLMIN-1-s1b.getValue())*100.0/(double)(SCROLLMAXB-SCROLLMIN),1));
		}
		
	}
	
	public synchronized void setValue(double x){
	    s1.setValue((int)(x));
	}
	
	public synchronized void setValue2(double x){
	    s1.setValue((int)(x));
	}
	
    
    public void itemStateChanged(ItemEvent evt){
	double temp;
	ItemSelectable ie = evt.getItemSelectable();
	
    } 
}//StubPanel
