//TransLinePanel.java
// This routine sets Frequencies
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class TransLinePanel extends Panel implements ActionListener{
	private static final Color bgcolor = new Color(236,236,236);
	private static final Color tinta = new Color(236,236,236);
	protected TextField text1[];
	protected Label lab1[], lab2[];
	public Label titlelabel;
	public Button b1, bfa, bfb;
        public Button info;
        public JSlider slider;
        public Panel slide1;
        public boolean infoyes;
        
        // CHANGE ==============================================================
        private Image im;
	private Graphics buf;
        //======================================================================
        
        int lifty = 0;
        
	protected String titulo="Unknown Title";
	protected String[] nome;
	protected String[] unidade;
	protected Font labfont=new Font("SanSerif",Font.PLAIN,12);
	protected Font titlefont=new Font("SanSerif",Font.BOLD,16);
        protected Font titfont = new Font("SanSerif",Font.PLAIN,14);
        protected Font buttonfont = new Font("SanSerif",Font.PLAIN,10);
        
        protected double[] DValue;
	protected int indexsize;
	GridBagLayout gb = new GridBagLayout();
	GridBagConstraints gbc = new GridBagConstraints();
	// Note:  lab1[i] are not used, since cannot support subscripts
        Trans_State state;
        
	public TransLinePanel(String titulo, String[] nome, String[] unidade, Trans_State state){
            super();
            this.state = state;

            Font labfont=new Font("SanSerif",Font.PLAIN,state.font12);
            Font titlefont=new Font("SanSerif",Font.BOLD,state.font16);
            Font titfont = new Font("SanSerif",Font.PLAIN,state.font14);
            Font buttonfont = new Font("SanSerif",Font.PLAIN,state.font10);

            setLayout(null);
            this.indexsize=nome.length;
            this.titulo=titulo;
            this.nome=new String[indexsize];
            this.unidade=new String[indexsize];
            this.DValue=new double[indexsize];
            this.text1=new TextField[indexsize];
            this.lab1=new Label[indexsize];
            this.lab2=new Label[indexsize];
            int i;

            // CHANGE 10/1/2017  ===========================================
            bfa = new Button("<");
            bfa.setBackground(bgcolor);
            bfb = new Button(">");
            bfb.setBackground(bgcolor);
            
            bfa.setBounds(state.s40,state.s70-lifty,state.s14,state.s15);
            bfb.setBounds(state.s256,state.s70-lifty,state.s14,state.s15);
            
            if(state.IsMac){  // No auxiliary buttons if in Windows
                    add(bfa); add(bfb);
            }
            //add(bfa); add(bfb);                
            //==============================================================

            slider = new JSlider(0,10000);   //10001);
	    slider.setValue(5000);
            if(state.IsMac){
                slider.setBounds(state.s54,state.s70-lifty,state.s230-state.s28,state.s15);
            }
            else{
                slider.setBounds(state.s40,state.s70-lifty,state.s230,state.s15);
            }
            slider.setBackground(bgcolor);
            add(slider);
	    /*
            slide1 = new Panel();
                slide1.setBackground(Color.black);
                add(slide1);
                slide1.setBounds(state.s40-1,state.s70-lifty-1,state.s230+2,state.s15+2);
	    */
            infoyes = false;

            titlelabel = new Label(titulo,Label.LEFT);

            titlelabel.setFont(titfont);
            titlelabel.setForeground(Color.red.darker());
            add(titlelabel);

            for(i=0;i<indexsize;i++){
                    this.nome[i]=nome[i];
                    this.unidade[i]=unidade[i];
                    this.DValue[i]=0.0;

                    this.text1[i]=new TextField("0.0",8);
                    add(text1[i]);

                    this.lab1[i]=new Label(nome[i],Label.RIGHT);
                    this.lab2[i]=new Label(unidade[i],Label.LEFT);
                    this.lab1[i].setFont(labfont);
                    this.lab2[i].setFont(labfont);
                    add(lab1[i]);
                    add(lab2[i]);

            }

            setBackground(bgcolor);

            b1 = new Button("Update");
            b1.setBackground(Color.white);
            b1.setFont(labfont);
            add(b1);
            //Listeners
            b1.addActionListener(this);

            info = new Button("Assumptions");
            info.setBackground(Color.white);
            info.setFont(buttonfont);
            add(info);

            //Listeners
            info.addActionListener(this);

            // CHANGE
            bfa.addActionListener(this);
            bfb.addActionListener(this);

            titlelabel.setBounds(state.s15,state.s5,state.s200,state.s20);  
            text1[1].setBounds(state.s110,state.s35-lifty,state.s95,state.s20);
            text1[1].setFont(labfont);    
            lab2[1].setBounds(state.s210,state.s35-lifty,state.s50,state.s20);
            b1.setBounds(state.s120,state.s140,state.s70,state.s20);
            
            info.setBounds(state.s220,state.s5,state.s85,state.s20);
	}
        
        // CHANGE ==============================================================
        public void update(Graphics g){
	    paint(g);
	    //DrawMe(g);
	    
	}

	public void paint(Graphics g){
		if(im == null){
			im = createImage(getSize().width,getSize().height);
			buf = im.getGraphics();
			DrawMe(buf);
			g.drawImage(im,0,0,null);
		}
		else{
		    //DrawMe(buf);
		    UpdateStuff(g);
		}		
		
		slider.requestFocusInWindow();
	}

    

    // Need this, because otherwise lightweight Swing slider disappears
    //  after 1st call to repaint():
    public void UpdateStuff(Graphics g) {
	Font normalfont = new Font("SanSerif",Font.PLAIN,state.font12);
	Font smallfont = new Font("SanSerif",Font.PLAIN,state.s10);
	    
	int liftynew;
	liftynew = lifty-state.s30;
        
	g.setFont(normalfont);

	// First, clear out the "f = ...." line:
	int xf = state.s15;
	int yf = state.s100;
	g.setColor(bgcolor);
	g.fillRect(xf,yf-liftynew-state.s15,state.s120,state.s20);

	
	g.setFont(smallfont);
	g.setColor(Color.blue.darker());
	double frequency;
	frequency = Double.valueOf(text1[1].getText()).doubleValue() * 
	    ( 1.0 + (double)(slider.getValue() - 5000)/5000.0);
	
	if(frequency < 1.0E3){
	    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency,6)+"  Hz",g,state.font14,xf,yf-liftynew);
	}
	else if(frequency >= 1.0E3 && frequency < 1.0E6){
	    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency/1.0E3,6)+"  kHz",g,state.font14,xf,yf-liftynew);    
	}
	else if(frequency >= 1.0E6 && frequency < 1.0E9){
	    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency/1.0E6,6)+"  MHz",g,state.font14,xf,yf-liftynew);    
	}
	else if(frequency >= 1.0E9 && frequency < 1.0E12){
	    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency/1.0E9,6)+"  GHz",g,state.font14,xf,yf-liftynew);    
	}
	else if(frequency >= 1.0E12 && frequency < 1.0E15){
	    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency/1.0E12,6)+"  THz",g,state.font14,xf,yf-liftynew);    
	}
	else if(frequency >= 1.0E15){
	    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency/1.0E15,6)+"  PHz",g,state.font14,xf,yf-liftynew);    
	}
    }	
	
	public void DrawMe(Graphics g){
        //======================================================================
        //public void paint(Graphics g){
            Font symbolfont = new Font("Symbol",Font.PLAIN,state.font12);
            Font normalfont = new Font("SanSerif",Font.PLAIN,state.font12);
            Font smallfont = new Font("SanSerif",Font.PLAIN,state.s10);
            FontMetrics fm;
	
            String alpha, Ohm, lambda, infinity, Gamma, epsilon;
            
		alpha="\u03b1";
		lambda="\u03bb";
		Ohm="\u03a9";
		infinity="\u221e";
		Gamma="\u0393";
                epsilon ="\u03b5";
            g.setFont(normalfont);
	    
            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);
            
            if(infoyes){
                // CHANGE #1  9/25/2017  =======================================
                //int xstart = 30;
                int xstart = state.s15;
                int ystart = state.s60;
                //==============================================================
                g.setColor(Color.blue.darker());
                MaestroG.subsub("The  input  reactances  of  Z","L","  and  Z","g","  are set",g,state.font12,xstart,ystart);
                MaestroG.subIsub("for   the  reference   frequency   ","f","o","   assuming:","","",g,state.font12,xstart,ystart + state.s18);
                
                g.setColor(Color.red.darker());
                //CHANGE # 2  9/25/2017=========================================
                
                MaestroG.subsubsubsubsup("Im { Z } = j \u03c9","o","L","o","  or  Im { Z } = - j ( \u03c9","o","C", "o"," )","-1",g,state.font12,xstart,ystart + state.s40);
                //==============================================================
                
                g.setColor(Color.blue.darker());
                MaestroG.subsub("When  the frequency is changed,  Z","L","  and   Z","g","",g,state.font12,xstart,ystart + state.s62);
                MaestroG.subsub("are  recalculated  assuming  fixed  L","o","  or  C","o"," .",g,state.font12,xstart,ystart + state.s80);
                
                g.setColor(Color.black);
                g.drawRect(state.s220-1,state.s5-1,state.s85+2,state.s20+2);
            }
            else{
                if(state.IsMac){
                    g.setColor(Color.red.darker());
                    g.drawLine(state.s155,state.s57-lifty,state.s155,state.s70-lifty);
                    g.drawLine(state.s50,state.s85-lifty,state.s50,state.s91-lifty);
                    g.drawLine(state.s103,state.s85-lifty,state.s103,state.s91-lifty);
                    g.drawLine(state.s40+state.s216,state.s85-lifty,state.s256,state.s91-lifty);

                    g.setColor(Color.blue.darker());
                    MaestroG.subscripterSymFirst2I("f","o"," =",g,state.font14,state.s75,state.s50-lifty);
                    MaestroG.subscripterSymFirst2I("0.0","","",g,state.font12,state.s40,state.s104-lifty);
                    MaestroG.subscripterSymFirst2I("2 f","o","",g,state.font12,state.s250,state.s104-lifty);
                    MaestroG.subscripterSymFirst2I("f","o","/2",g,state.font12,state.s90,state.s104-lifty);
                    MaestroG.subscripterSymFirst2I("f",""," =",g,state.font14,state.s15,state.s83-lifty);
                }
                else{
                    g.setColor(Color.red.darker());
                    g.drawLine(state.s155,state.s57-lifty,state.s155,state.s70-lifty);
                    g.drawLine(state.s52,state.s85-lifty,state.s52,state.s91-lifty);
                    g.drawLine(state.s106,state.s85-lifty,state.s106,state.s91-lifty);
                    g.drawLine(state.s200+state.s58,state.s85-lifty,state.s258,state.s91-lifty);

                    g.setColor(Color.blue.darker());
                    MaestroG.subscripterSymFirst2I("f","o"," =",g,state.font14,state.s75,state.s50-lifty);
                    MaestroG.subscripterSymFirst2I("0.0","","",g,state.font12,state.s43,state.s104-lifty);
                    MaestroG.subscripterSymFirst2I("2 f","o","",g,state.font12,state.s248,state.s104-lifty);
                    MaestroG.subscripterSymFirst2I("f","o","/2",g,state.font12,state.s95,state.s104-lifty);
                    MaestroG.subscripterSymFirst2I("f",""," =",g,state.font14,state.s15,state.s83-lifty);
                }

                int liftynew;
                liftynew = lifty-state.s30;
                
                g.setFont(normalfont);
                //g.drawString("= ",state.s95,state.s50-liftynew);

                g.setFont(smallfont);
                g.setColor(Color.blue.darker());
                    int xf = state.s15;
                    int yf = state.s100;
                    double frequency;
                    frequency = Double.valueOf(text1[1].getText()).doubleValue() * 
                               ( 1.0 + (double)(slider.getValue() - 5000)/5000.0);

                    if(frequency < 1.0E3){
                        MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency,6)+"  Hz",g,state.font14,xf,yf-liftynew);
                    }
                    else if(frequency >= 1.0E3 && frequency < 1.0E6){
                        MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency/1.0E3,6)+"  kHz",g,state.font14,xf,yf-liftynew);    
                    }
                    else if(frequency >= 1.0E6 && frequency < 1.0E9){
                        MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency/1.0E6,6)+"  MHz",g,state.font14,xf,yf-liftynew);    
                    }
                    else if(frequency >= 1.0E9 && frequency < 1.0E12){
                        MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency/1.0E9,6)+"  GHz",g,state.font14,xf,yf-liftynew);    
                    }
                    else if(frequency >= 1.0E12 && frequency < 1.0E15){
                        MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency/1.0E12,6)+"  THz",g,state.font14,xf,yf-liftynew);    
                    }
                    else if(frequency >= 1.0E15){
                        MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency/1.0E15,6)+"  PHz",g,state.font14,xf,yf-liftynew);    
                    }

                g.setColor(Color.red.darker());
                g.drawRect(state.s110-1,state.s35-lifty-1,state.s95+2,state.s20+2);
                g.drawRect(state.s120-1,state.s140-1,state.s70+2,state.s20+2);
                
                g.setColor(Color.black);
                g.drawRect(state.s220-1,state.s5-1,state.s85+2,state.s20+2);
                                        
                int xstart = state.s15;
                int ystart = state.s180;
                //==============================================================
                g.setColor(Color.blue.darker());
                MaestroG.subscripter("Enter new value in text box and click Update to","","",g,state.font11,xstart,ystart);
                MaestroG.subscripter("change operating frequency f","o",".",g,state.font11,xstart,ystart+state.s15);
                
                g.setColor(Color.red.darker());
                MaestroG.subscripter("Explore frequency behavior about f","o"," with the slider",g,state.font11,xstart,ystart+state.s35);
                
            }
        }
	
	public void actionPerformed(ActionEvent evt){
		if(evt.getSource() == b1){
		  for(int i=0;i<indexsize;i++){
		  	DValue[i]=Double.valueOf(text1[i].getText()).doubleValue();
                  }
		}
                
                if(evt.getSource() == info){
                    if(infoyes){
                        infoyes = false;
                        text1[1].setVisible(true);
                        lab2[1].setVisible(true);
                        slider.setVisible(true);
                        b1.setVisible(true);
                        // CHANGE ==============================================
                        bfa.setVisible(true);
                        bfb.setVisible(true);
                        //======================================================
                        slide1.setVisible(true);
                        info.setLabel("Assumptions");
                        titlelabel.setText("Set Frequency");
                    }
                    else{
                        infoyes = true;
                        text1[1].setVisible(false);
                        lab2[1].setVisible(false);
                        slider.setVisible(false);
                        slide1.setVisible(false);
                        b1.setVisible(false);
                        // CHANGE ==============================================
                        bfa.setVisible(false);
                        bfb.setVisible(false);
                        //======================================================
                        info.setLabel("Set Frequency");
                        titlelabel.setText("Assumptions");
                    }
                    repaint();
                }
	}
	
        public double getValue(int i){
		return DValue[i];	
        }

        public synchronized void setValue(double D,int i){
		text1[i].setText(String.valueOf(D));
	}
}
