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


public class TransSlidePanel extends Panel implements AdjustmentListener, ActionListener{
	public Scrollbar slider, slider2;
        
        // CHANGE ==============================================================
        public Button b2a, b2b, b3a, b3b;
        //======================================================================
        
	private Label vlab1, vlab2, vlab3;
	private double lineLength = 1.0, lineLength_part1 = 1.0, lineLength_part2 = 0.0, linecounter1 = 1000, linecounter2 = 0;
        //private final Font labelfont=new Font("Sanserif",Font.PLAIN,12);
        public static Font labelfont;
	public static Font labelfont2;
        
	//private static final Color bgcolor = new Color(216,216,191);
	//private static final Color bgcolor = new Color(236,236,221);
	private static final Color bgcolor = new Color(255,255,255);
	private static final Color tinta = new Color(236,236,221);
	
	//private static final Color bgcolor = Color.lightGray;
	public int SCROLLMIN=0, SCROLLMAX=1001, SCROLLMAX2 = 1000;
	public TransSlidePanel(){
		super();	
		setLayout(null);
		setBackground(tinta);
				
		vlab1 = new Label(" 0.0",Label.LEFT);
		vlab1.setFont(labelfont);
		vlab2 = new Label(" \u03bb",Label.LEFT);
		vlab2.setFont(labelfont2);
		
		vlab3 = new Label(" d = ",Label.LEFT);
		vlab3.setFont(labelfont);
		//vlab1.setBackground(bgcolor);
		vlab1.setBackground(Color.white);
		vlab2.setBackground(Color.white);
		vlab3.setBackground(Color.white);	
		
                // 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);
                add(b2a); add(b2b); add(b3a); add(b3b);
                //==============================================================
                
                slider = new Scrollbar(Scrollbar.HORIZONTAL,1000,1,SCROLLMIN,SCROLLMAX);	
		slider2 = new Scrollbar(Scrollbar.HORIZONTAL,1000,1,SCROLLMIN,SCROLLMAX2);	
		slider.setBackground(new Color(230,250,230));
		slider2.setBackground(Color.white);
		//slider.setForeground(bgcolor.darker());
		slider.addAdjustmentListener(this);
		slider2.addAdjustmentListener(this);
		lineLength=1.0;		 		
	    
		add(slider);
		add(slider2);
		add(vlab1);
		add(vlab2);
		add(vlab3);
    
		//vlab1.setBounds(3,2,45,12);
		
                // CHANGE ======================================================
                //vlab3.setBounds(15,10,25,12);
                vlab3.setBounds(10,10,35,12);
                //==============================================================
		
                vlab2.setBounds(881,10,12,18);
                
		slider.setBounds(50,3,830,15);	
		slider2.setBounds(50,19,830,15);	
		Panel slide1 = new Panel();
		    slide1.setBackground(Color.black);
		    add(slide1);
		    slide1.setBounds(49,2,832,33);
		
                
                // CHANGE  =====================================================
                b2a.setBounds(50,3,14,15);
                b2b.setBounds(866,3,14,15);
                b3a.setBounds(50,19,14,15);
                b3b.setBounds(866,19,14,15);
                //==============================================================
                
                //Listeners
		//slider.addAdjustmentListener(this);	
                
                // CHANGE 10/1/2017 ============================================
                b2a.addActionListener(this);
                b2b.addActionListener(this);
                b3a.addActionListener(this);
                b3b.addActionListener(this);
                //==============================================================
	}
	
	
	public void paint(Graphics g){
		g.setColor(bgcolor);
		g.fill3DRect(0,0,getSize().width-1,getSize().height-1,true);
	}
	
    	public void adjustmentValueChanged(AdjustmentEvent evt){
	  if(evt.getSource()==slider){
		SCROLLMAX = slider.getMaximum();
		SCROLLMIN = slider.getMinimum();
		    vlab1.setText(" "+MaestroA.rounder((SCROLLMAX-SCROLLMIN-1-slider.getValue())*lineLength_part1/(SCROLLMAX-SCROLLMIN-1),5));    
	  }
	}
        
        public void actionPerformed(ActionEvent evt){
            if(evt.getSource()==b2a){
                slider.setValue(slider.getValue()-1);
                
            }
            else if(evt.getSource()==b2b){
                slider.setValue(slider.getValue()+1);
            }
            else if(evt.getSource()==b3a){
                slider2.setValue(slider2.getValue()-1);
            }
            else if(evt.getSource()==b3b){
                slider2.setValue(slider2.getValue()+1);
            }
        }
	
	public double getValue(){
		SCROLLMAX = slider.getMaximum();
		SCROLLMIN = slider.getMinimum();
		
		    return (MaestroA.rounder((SCROLLMAX-SCROLLMIN-1-slider.getValue())*lineLength_part1/(SCROLLMAX-SCROLLMIN-1),5));
	}
	
	public double getValue2(){
		SCROLLMAX2 = slider2.getMaximum();
		SCROLLMIN = slider2.getMinimum();
		if(lineLength < 1.0){
		    return (MaestroA.rounder((double)(SCROLLMAX2-SCROLLMIN-1-slider2.getValue())*0.0001/(double)(SCROLLMAX2-SCROLLMIN),7));
		}
		else if(lineLength >= 1.0 && lineLength < 10.0){
		    return (MaestroA.rounder((double)(SCROLLMAX2-SCROLLMIN-1-slider2.getValue())*0.001/(double)(SCROLLMAX2-SCROLLMIN),6));
		}
		else if(lineLength >= 10.0 && lineLength < 100.0){
		    return (MaestroA.rounder((double)(SCROLLMAX2-SCROLLMIN-1-slider2.getValue())*0.01/(double)(SCROLLMAX2-SCROLLMIN),5));
		}
		else if(lineLength >= 100.0 && lineLength < 1000.0){
		    return (MaestroA.rounder((double)(SCROLLMAX2-SCROLLMIN-1-slider2.getValue())*0.1/(double)(SCROLLMAX2-SCROLLMIN),4));
		}
		else if(lineLength >= 1000.0 && lineLength < 10000.0){
		    return (MaestroA.rounder((double)(SCROLLMAX2-SCROLLMIN-1-slider2.getValue())/(double)(SCROLLMAX2-SCROLLMIN),3));
		}
		else if(lineLength >= 10000.0 && lineLength < 100000.0){
		    return (MaestroA.rounder((double)(SCROLLMAX2-SCROLLMIN-1-slider2.getValue())*10.0/(double)(SCROLLMAX2-SCROLLMIN),2));
		}
		else{
		    return (MaestroA.rounder((double)(SCROLLMAX2-SCROLLMIN-1-slider2.getValue())*100.0/(double)(SCROLLMAX2-SCROLLMIN),1));
		}
		
	}
	
	public synchronized void setValue(double x){
	    slider.setValue((int)(x));
	}
	
	public synchronized void setValue2(double x){
	    slider2.setValue((int)(x));
	}
	
	public synchronized void setLineLength(double lineLength, double lineLength_part1, double lineLength_part2, int linecounter1, int linecounter2){
	    this.lineLength=lineLength;
	    this.lineLength_part1=lineLength_part1;
	    this.lineLength_part2=lineLength_part2;
	    this.linecounter1 = linecounter1;
	    this.linecounter2 = linecounter2;
	}
	
	public synchronized void reset(){
	    vlab1.setText(" 0.0");
	    slider.setValue(slider.getMaximum());
	    slider2.setValue(slider2.getMaximum());
	}
}//End of LossySlidePanel

