//TransLinePanel.java
// This routine sets Frequencies
import java.awt.*;
import java.awt.event.*;
//import java.applet.*;
import java.lang.*;
public class TransLinePanel extends Panel implements ActionListener{
	//protected static final Color bgcolor = new Color(180,147,112);
	//protected static final Color bgcolor = new Color(200,200,200);
	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 Scrollbar 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 static final Font labfont=new Font("SanSerif",Font.PLAIN,12);
	protected static final Font titlefont=new Font("SanSerif",Font.BOLD,16);
        protected static final Font titfont = new Font("SanSerif",Font.PLAIN,14);
        protected static final 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
        
	public TransLinePanel(String titulo, String[] nome, String[] unidade){
		super();
		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);
                add(bfa);
                bfb = new Button(">");
                bfb.setBackground(bgcolor);
                add(bfb);
                
                bfa.setBounds(40,70-lifty,14,15);
                bfb.setBounds(256,70-lifty,14,15);
                //==============================================================
                
                slider = new Scrollbar(Scrollbar.HORIZONTAL,5000,1,0,10001);	
                slider.setBounds(40,70-lifty,230,15);
                slider.setBackground(Color.white);
                add(slider);
                slide1 = new Panel();
		    slide1.setBackground(Color.black);
		    add(slide1);
		    slide1.setBounds(39,69-lifty,232,17);
                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(bgcolor);
		add(b1);
                
                
                //Listeners
		b1.addActionListener(this);
                
                info = new Button("Assumptions");
		info.setBackground(bgcolor);
                info.setFont(buttonfont);
		add(info);
                
                //Listeners
		info.addActionListener(this);
                
                // CHANGE
                bfa.addActionListener(this);
                bfb.addActionListener(this);
                
                titlelabel.setBounds(15,5,200,20);  
                
                //text1[0].setBounds(130,60,100,20);
                //text1[1].setBounds(130,100,100,20);
                text1[1].setBounds(110,35-lifty,95,20);
                
                //text1[2].setBounds(130,140,100,20);
                //text1[3].setBounds(130,180,100,20);
                
                //lab2[0].setBounds(235,60,50,20);
                //lab2[1].setBounds(235,100,50,20);
                lab2[1].setBounds(210,35-lifty,50,20);
                //lab2[2].setBounds(235,140,50,20);
                //lab2[3].setBounds(235,180,50,20);
                
                b1.setBounds(120,160,70,20);
                info.setBounds(220,5,85,20);
                
	}
        // CHANGE ==============================================================
        public void update(Graphics g){
		paint(g);
	}

	public void paint(Graphics g){
		if(im == null){
			im = createImage(getSize().width,getSize().height);
			buf = im.getGraphics();
			DrawMe(buf);
		}
		else{
			DrawMe(buf);
		}
		g.drawImage(im,0,0,null);
	}
	
	
	public void DrawMe(Graphics g){
        //======================================================================
        //public void paint(Graphics g){
            Font symbolfont=new Font("Symbol",Font.PLAIN,12);
            Font normalfont=new Font("SanSerif",Font.PLAIN,12);
            Font smallfont=new Font("SanSerif",Font.PLAIN,10);
            FontMetrics fm;
	
            String alpha, Ohm, lambda, infinity, Gamma, epsilon;
            g.setFont(symbolfont);
		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 = 15;
                //==============================================================
                g.setColor(Color.blue.darker());
                MaestroG.subsub("The  input  reactances  of  Z","L","  and  Z","g","  are set",g,12,xstart,45);
                MaestroG.subIsub("for   the  reference   frequency   ","f","o","   assuming:","","",g,12,xstart,63);
                
                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,12,xstart,85);
                MaestroG.subsubsubsubsup("Im { Z } = j \u03c9","o","L","o","  or  Im { Z } = - j ( \u03c9","o","C", "o"," )","-1",g,12,xstart,85);
                //==============================================================
                
                g.setColor(Color.blue.darker());
                MaestroG.subsub("When  the frequency is changed,  Z","L","  and   Z","g","",g,12,xstart,107);
                MaestroG.subsub("are  recalculated  assuming  fixed  L","o","  or  C","o"," .",g,12,xstart,125);
                //MaestroG.subsub("are recalculated by using a factor  \u03c9","","/ \u03c9","o","   .",g,12,xstart,125);
            }
            else{
                g.setColor(Color.red.darker());
                g.drawLine(155,57-lifty,155,70-lifty);
                g.drawLine(50,85-lifty,50,91-lifty);
                g.drawLine(103,85-lifty,103,91-lifty);
                g.drawLine(260,85-lifty,260,91-lifty);

                g.setColor(Color.blue.darker());
                MaestroG.subscripterSymFirst2I("f","o","",g,14,75,50-lifty);
                MaestroG.subscripterSymFirst2I("0.0","","",g,12,40,104-lifty);
                MaestroG.subscripterSymFirst2I("2 f","o","",g,12,250,104-lifty);
                MaestroG.subscripterSymFirst2I("f","o","/2",g,12,90,104-lifty);
                MaestroG.subscripterSymFirst2I("f",""," =",g,14,15,83-lifty);

                int liftynew;
                liftynew = lifty-30;
                
                g.setFont(normalfont);
                g.drawString("= ",95,50-liftynew);

                g.setFont(smallfont);
                g.setColor(Color.blue.darker());
                    int xf = 120;
                    int yf = 100;
                    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,14,xf,yf-liftynew);
                    }
                    else if(frequency >= 1.0E3 && frequency < 1.0E6){
                        MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency/1.0E3,6)+"  kHz",g,14,xf,yf-liftynew);    
                    }
                    else if(frequency >= 1.0E6 && frequency < 1.0E9){
                        MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency/1.0E6,6)+"  MHz",g,14,xf,yf-liftynew);    
                    }
                    else if(frequency >= 1.0E9 && frequency < 1.0E12){
                        MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency/1.0E9,6)+"  GHz",g,14,xf,yf-liftynew);    
                    }
                    else if(frequency >= 1.0E12 && frequency < 1.0E15){
                        MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency/1.0E12,6)+"  THz",g,14,xf,yf-liftynew);    
                    }
                    else if(frequency >= 1.0E15){
                        MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(frequency/1.0E15,6)+"  PHz",g,14,xf,yf-liftynew);    
                    }

                g.setColor(Color.red.darker());
                g.drawRect(107,32-lifty,99,24);
                g.drawRect(117,157,74,24);
            }
        }
	
	
	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));
	}
}