//TransSlidePanel.java
/* A Java class for
 * LineImpedance.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;
	public Label vlab1, vlab2;
        
        // CHANGE ==============================================================
        public Button b2a, b2b, b3a, b3b;
        //======================================================================
	
        //private double lineLength;
        private double lineLength = 1.66782, lineLength_part1 = 1.667, 
             lineLength_part2 = 8.2e-4, linecounter1 = 3335, linecounter2 = 641;
      
        private final Font labelfont=TheFonts.sanSerif12;
	
	private static final Color bgcolor = Color.white;
	private static final Color tinta = new Color(236,236,236);
	//private static final Color bgcolor = Color.lightGray;
	//private int SCROLLMIN=0, SCROLLMAX=10001;
        public int SCROLLMIN=0, SCROLLMAX=3336, SCROLLMAX2 = 1000;
	
        public TransSlidePanel(){
		super();	
		setLayout(null);
		setBackground(bgcolor);
	
		lineLength=1.66782;	

		//vlab1 = new Label(" "+lineLength,Label.LEFT);
		vlab1 = new Label ("   d = ", Label.LEFT);
                vlab1.setFont(labelfont);
		vlab2 = new Label(" \u03bb",Label.LEFT);
		vlab2.setFont(labelfont);
		//vlab1.setBackground(bgcolor);
		vlab1.setBackground(Color.white);
		vlab2.setBackground(Color.white);	
		slider = new Scrollbar(Scrollbar.HORIZONTAL,3336,1,SCROLLMIN,SCROLLMAX);
                slider2 = new Scrollbar(Scrollbar.HORIZONTAL,1000,1,SCROLLMIN,SCROLLMAX2);	

		slider.addAdjustmentListener(this);
                slider2.addAdjustmentListener(this);
                
		//vlab1.setText(" "+lineLength);
		slider.setValue(slider.getMaximum());
                slider2.setValue(slider2.getMaximum());
                
		//slider.setBackground(bgcolor);	
		//slider.setBackground(bgcolor.brighter());
		//slider.setForeground(bgcolor.darker());	 		
                
                // 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);
                //==============================================================
		
		add(slider);
                add(slider2);
		add(vlab1);
		add(vlab2);
		
                vlab1.setBounds(4,8,40,15);
		vlab2.setBounds(604,8,20,15);
		slider.setBounds(50,3,553,15);	
                slider2.setBounds(50,19,553,15);	
                slider.setBackground(new Color(245,255,235));
                slider2.setBackground(Color.white);
                
                // CHANGE  =====================================================
                b2a.setBounds(50,3,14,15);
                b2b.setBounds(589,3,14,15);
                b3a.setBounds(50,19,14,15);
                b3b.setBounds(589,19,14,15);
                //==============================================================
                
                
                Panel slide1 = new Panel();
		    slide1.setBackground(Color.black);
		    add(slide1);
		    slide1.setBounds(49,2,555,33);	
                    
                // CHANGE ======================================================
                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("    d = ");    
	  }
	}
        
        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 < 0.0001){
		    return (MaestroA.rounder((double)(SCROLLMAX2-SCROLLMIN-1-slider2.getValue())*0.00000001/(double)(SCROLLMAX2-SCROLLMIN),11));
		}
                else if(lineLength >=0.0001 && lineLength < 0.001){
		    return (MaestroA.rounder((double)(SCROLLMAX2-SCROLLMIN-1-slider2.getValue())*0.0000001/(double)(SCROLLMAX2-SCROLLMIN),10));
		}
                else if(lineLength >=0.001 && lineLength < 0.01){
		    return (MaestroA.rounder((double)(SCROLLMAX2-SCROLLMIN-1-slider2.getValue())*0.000001/(double)(SCROLLMAX2-SCROLLMIN),9));
		}
                else if(lineLength >=0.01 && lineLength < 0.1){
		    return (MaestroA.rounder((double)(SCROLLMAX2-SCROLLMIN-1-slider2.getValue())*0.00001/(double)(SCROLLMAX2-SCROLLMIN),8));
		}
                else if(lineLength >= 0.1 && 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){
	  //  this.lineLength=lineLength;
	//}
        
        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(" "+MaestroA.rounder(lineLength,5));
	    //vlab1.setText("    d = ");
            slider.setValue(slider.getMaximum());
            slider2.setValue(slider.getMaximum());
        }

}//End of TransSlidePanel

