//FrequencyInputPanel.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 maestro.lib.math.*;

public class FrequencyInputPanel extends Panel implements AdjustmentListener, ActionListener{
    private Trans_State state;
    //private static final Color bgcolor = new Color(160,200,200);
    //private static final Color bgcolor = new Color(236,236,236);
    private static final Color bgcolor = Color.white;
    
    private static final Font labfont=TheFonts.sanSerif12;
    private static final Font normalfont = TheFonts.sanSerif14;
    public int ScrollMax = 4990;//3999; 
    public int offset = 20;
    private Label titlelabel;
    private Label lab1, lab2, lab3, lab4, lab5, lab6, lab7;
    private Label ulab1, ulab2, ulab3, ulab4, ulab5, ulab6;
    public TextField text1, text2, text3, text4, text5, text6, text7, text8;
    public Scrollbar slider1, slider2, slider3, slider5;
    public Checkbox ch1, ch2, ch3, cg1, cg2, cg3, cg4, cg5, cg6;
    public CheckboxGroup cgrp, cgauge;
    public  Button b1, b2, b3, b5;
    // CHANGE ==============================================================
        public Button b2a, b2b, b3a, b3b;
    //======================================================================
    public  Button bupdate;
    public Panel ps1, ps2;
  
    public FrequencyInputPanel(Trans_State state){
	super();
	setLayout(null);
	setBackground(bgcolor);
	this.state = state;
        
        setFont(labfont);
        
	slider2 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes frequency
	slider2.setValue(3000);
	
        slider1 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,1000);
	slider1.setValue(0);
	
        // 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(slider2);
        add(slider1);
	
	lab1 = new Label("f = 1.0 GHz",Label.LEFT);
	add(lab1);
	lab1.setBounds(10,10,105,16);
	
	lab2 = new Label("frequency",Label.LEFT);
	//add(lab2);
	//lab2.setBounds(520,10,80,15);
	lab2.setBounds(520,7,80,20);   // JAN
	
        lab3 = new Label("[ range: 1.0 Hz to 200.0 GHz ]",Label.LEFT);
	//add(lab3);
	lab3.setBounds(450,18,200,15);
	
	//slider for frequency    
	slider2.setBounds(120,3,383,15);
        slider1.setBounds(120,19,383,15);
        slider2.setBlockIncrement(10);
	slider2.setBackground(new Color(245,255,235));
        slider1.setBackground(Color.white);
        
        // CHANGE  =====================================================
                b2a.setBounds(120,3,14,15);
                b2b.setBounds(489,3,14,15);
                b3a.setBounds(120,19,14,15);
                b3b.setBounds(489,19,14,15);
        //==============================================================
        
	slider2.addAdjustmentListener(this);
        slider1.addAdjustmentListener(this);
        // CHANGE ======================================================
                b2a.addActionListener(this);
                b2b.addActionListener(this);
                b3a.addActionListener(this);
                b3b.addActionListener(this);
        //==============================================================
        
	Panel pslide = new Panel();
		pslide.setBackground(Color.black);
		pslide.setBounds(119,2,385,33);
		add(pslide);
                
                
        // update button
	bupdate = new Button("Instructions");
        bupdate.setBackground(new Color(240,240,255));
	add(bupdate);
        int buttonx = 520;
        int buttony = 7;
        int buttonwide = 90;
        int buttonheight = 22;
        
        bupdate.setBounds(buttonx,buttony,buttonwide,buttonheight);	
        Panel ps7 = new Panel();
	    ps7.setBackground(Color.lightGray);
	    add(ps7);
	    ps7.setBounds(buttonx-1,buttony-1,buttonwide+2,buttonheight+2);
	    
	Panel ps8 = new Panel();
	    ps8.setBackground(Color.black);
	    add(ps8);
	    ps8.setBounds(buttonx-2,buttony-2,buttonwide+4,buttonheight+4);
                	
    }
    
    public void paint(Graphics g){
	
    }
    
    // CHANGE - ADD actionPerformed ============================================
    public void actionPerformed(ActionEvent evt){
        if(evt.getSource()==b3a){
            slider1.setValue(slider1.getValue()-1);
            if(slider2.getValue()==0){
		    state.frequency = slider2.getValue()+1.0+slider1.getValue()*0.001;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency,6)+" Hz");
		} 
		if(slider2.getValue() < 1000 && slider2.getValue()>0){
		    state.frequency = slider2.getValue()+slider1.getValue()*0.001;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency,6)+" Hz");
		}   
		if(slider2.getValue()==1000){
		    state.frequency = (slider2.getValue()-1000)*1.0e3 +1.0e3+slider1.getValue();
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e3,6)+" kHz");
		} 
		    
                if(slider2.getValue() < 2000 && slider2.getValue()>1000){
		    state.frequency = (slider2.getValue()-1000)*1.0e3 +slider1.getValue();
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e3,6)+" kHz");
		} 
		
		if(slider2.getValue() == 2000 ){
		    state.frequency = (slider2.getValue() - 2000) * 1.0e6 + 1.0e6+ slider1.getValue()*1000.0;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e6,6)+" MHz");
		}
		if(slider2.getValue() > 2000 && slider2.getValue() < 3000 ){
		    state.frequency = (slider2.getValue() - 2000) * 1.0e6 +slider1.getValue()*1000.0;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e6,6)+" MHz");
		}
                if(slider2.getValue() == 3000){
		    state.frequency = (slider2.getValue() - 3000) * 1.0e9 +1.0e9+slider1.getValue()*1.0e5;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e9,6)+" GHz");
		}
		if(slider2.getValue() > 3000 && slider2.getValue() <= 4990 ){
		    state.frequency = (slider2.getValue() - 3000) * 1.0e8 +1.0e9+slider1.getValue()*1.0e5;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e9,6)+" GHz");
		}
	    
	    state.ignition();
	        
        }
        else if(evt.getSource()==b3b){
            slider1.setValue(slider1.getValue()+1);
            if(slider2.getValue()==0){
		    state.frequency = slider2.getValue()+1.0+slider1.getValue()*0.001;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency,6)+" Hz");
		} 
		if(slider2.getValue() < 1000 && slider2.getValue()>0){
		    state.frequency = slider2.getValue()+slider1.getValue()*0.001;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency,6)+" Hz");
		}   
		if(slider2.getValue()==1000){
		    state.frequency = (slider2.getValue()-1000)*1.0e3 +1.0e3+slider1.getValue();
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e3,6)+" kHz");
		} 
		    
                if(slider2.getValue() < 2000 && slider2.getValue()>1000){
		    state.frequency = (slider2.getValue()-1000)*1.0e3 +slider1.getValue();
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e3,6)+" kHz");
		} 
		
		if(slider2.getValue() == 2000 ){
		    state.frequency = (slider2.getValue() - 2000) * 1.0e6 + 1.0e6+ slider1.getValue()*1000.0;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e6,6)+" MHz");
		}
		if(slider2.getValue() > 2000 && slider2.getValue() < 3000 ){
		    state.frequency = (slider2.getValue() - 2000) * 1.0e6 +slider1.getValue()*1000.0;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e6,6)+" MHz");
		}
                if(slider2.getValue() == 3000){
		    state.frequency = (slider2.getValue() - 3000) * 1.0e9 +1.0e9+slider1.getValue()*1.0e5;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e9,6)+" GHz");
		}
		if(slider2.getValue() > 3000 && slider2.getValue() <= 4990 ){
		    state.frequency = (slider2.getValue() - 3000) * 1.0e8 +1.0e9+slider1.getValue()*1.0e5;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e9,6)+" GHz");
		}
	    
	    state.ignition();
	    
        }
        else if(evt.getSource()==b2a){
            slider2.setValue(slider2.getValue()-1);
            slider1.setValue(0);
            
            if(state.frequency >= 1.0E9){
                slider2.setBlockIncrement(10);
            }
            else{
                slider2.setBlockIncrement(10);
            }
            
	    if(slider2.getValue()>=ScrollMax){
		state.frequency=state.frequency_maximum;
		lab1.setText("f = 200.0 GHz");
                slider1.setEnabled(false);
	    }
	    else{
		slider1.setEnabled(true);    
		if(slider2.getValue() == 0){
		    state.frequency = 1.0;
		    lab1.setText("f = 1.0 Hz");
		} 
		if(slider2.getValue() < 1000 && slider2.getValue()>0){
		    state.frequency = slider2.getValue();
		    lab1.setText("f = "+MaestroA.rounder(state.frequency,3)+" Hz");
		}   
		    
		if(slider2.getValue() == 1000){
		    state.frequency = 1.0e3;
		    lab1.setText("f = 1.0 kHz");
		}
                
                if(slider2.getValue() < 2000 && slider2.getValue()>1000){
		    state.frequency = (slider2.getValue()-1000)*1.0e3;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e3,3)+" kHz");
		} 
                
                if(slider2.getValue() == 2000){
		    state.frequency = 1.0e6;
		    lab1.setText("f = 1.0 MHz");
		}
		
		if(slider2.getValue() > 2000 && slider2.getValue() < 3000 ){
		    state.frequency = (slider2.getValue() - 2000) * 1.0e6;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e6,3)+" MHz");
		}
		
		if(slider2.getValue() == 3000){
		    state.frequency = 1.0e9;
		    lab1.setText("f = 1.0 GHz");
		}
		
		if(slider2.getValue() > 3000 && slider2.getValue() <= 4990 ){
		    state.frequency = (slider2.getValue() - 3000) * 1.0e8 + 1.0e9;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e9,3)+" GHz");
                    //System.out.println(slider2.getValue());
		}
	    }
	    
	    state.ignition();
	    
        }
        else if(evt.getSource()==b2b){
            slider2.setValue(slider2.getValue()+1);
            slider1.setValue(0);
            
            if(state.frequency >= 1.0E9){
                slider2.setBlockIncrement(10);
            }
            else{
                slider2.setBlockIncrement(10);
            }
            
	    if(slider2.getValue()>=ScrollMax){
		state.frequency=state.frequency_maximum;
		lab1.setText("f = 200.0 GHz");
                slider1.setEnabled(false);
	    }
	    else{
		slider1.setEnabled(true);    
		if(slider2.getValue() == 0){
		    state.frequency = 1.0;
		    lab1.setText("f = 1.0 Hz");
		} 
		if(slider2.getValue() < 1000 && slider2.getValue()>0){
		    state.frequency = slider2.getValue();
		    lab1.setText("f = "+MaestroA.rounder(state.frequency,3)+" Hz");
		}   
		    
		if(slider2.getValue() == 1000){
		    state.frequency = 1.0e3;
		    lab1.setText("f = 1.0 kHz");
		}
                
                if(slider2.getValue() < 2000 && slider2.getValue()>1000){
		    state.frequency = (slider2.getValue()-1000)*1.0e3;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e3,3)+" kHz");
		} 
                
                if(slider2.getValue() == 2000){
		    state.frequency = 1.0e6;
		    lab1.setText("f = 1.0 MHz");
		}
		
		if(slider2.getValue() > 2000 && slider2.getValue() < 3000 ){
		    state.frequency = (slider2.getValue() - 2000) * 1.0e6;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e6,3)+" MHz");
		}
		
		if(slider2.getValue() == 3000){
		    state.frequency = 1.0e9;
		    lab1.setText("f = 1.0 GHz");
		}
		
		if(slider2.getValue() > 3000 && slider2.getValue() <= 4990 ){
		    state.frequency = (slider2.getValue() - 3000) * 1.0e8 + 1.0e9;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e9,3)+" GHz");
                    //System.out.println(slider2.getValue());
		}
	    }
	    
	    state.ignition();
	    
        }
    }  
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
	if(evt.getSource()==slider2){
            slider1.setValue(0);
            if(state.frequency >= 1.0E9){
                slider2.setBlockIncrement(10);
            }
            else{
                slider2.setBlockIncrement(10);
            }
            
	    if(slider2.getValue()>=ScrollMax){
		state.frequency=state.frequency_maximum;
		lab1.setText("f = 200.0 GHz");
                slider1.setEnabled(false);
	    }
	    else{
		slider1.setEnabled(true);    
		if(slider2.getValue() == 0){
		    state.frequency = 1.0;
		    lab1.setText("f = 1.0 Hz");
		} 
		if(slider2.getValue() < 1000 && slider2.getValue()>0){
		    state.frequency = slider2.getValue();
		    lab1.setText("f = "+MaestroA.rounder(state.frequency,3)+" Hz");
		}   
		    
		if(slider2.getValue() == 1000){
		    state.frequency = 1.0e3;
		    lab1.setText("f = 1.0 kHz");
		}
                
                if(slider2.getValue() < 2000 && slider2.getValue()>1000){
		    state.frequency = (slider2.getValue()-1000)*1.0e3;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e3,3)+" kHz");
		} 
                
                if(slider2.getValue() == 2000){
		    state.frequency = 1.0e6;
		    lab1.setText("f = 1.0 MHz");
		}
		
		if(slider2.getValue() > 2000 && slider2.getValue() < 3000 ){
		    state.frequency = (slider2.getValue() - 2000) * 1.0e6;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e6,3)+" MHz");
		}
		
		if(slider2.getValue() == 3000){
		    state.frequency = 1.0e9;
		    lab1.setText("f = 1.0 GHz");
		}
		
		if(slider2.getValue() > 3000 && slider2.getValue() <= 4990 ){
		    state.frequency = (slider2.getValue() - 3000) * 1.0e8 + 1.0e9;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e9,3)+" GHz");
                    //System.out.println(slider2.getValue());
		}
	    }
	    
	    state.ignition();
	    
	}

        if(evt.getSource()==slider1){
                if(slider2.getValue()==0){
		    state.frequency = slider2.getValue()+1.0+slider1.getValue()*0.001;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency,6)+" Hz");
		} 
		if(slider2.getValue() < 1000 && slider2.getValue()>0){
		    state.frequency = slider2.getValue()+slider1.getValue()*0.001;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency,6)+" Hz");
		}   
		if(slider2.getValue()==1000){
		    state.frequency = (slider2.getValue()-1000)*1.0e3 +1.0e3+slider1.getValue();
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e3,6)+" kHz");
		} 
		    
                if(slider2.getValue() < 2000 && slider2.getValue()>1000){
		    state.frequency = (slider2.getValue()-1000)*1.0e3 +slider1.getValue();
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e3,6)+" kHz");
		} 
		
		if(slider2.getValue() == 2000 ){
		    state.frequency = (slider2.getValue() - 2000) * 1.0e6 + 1.0e6+ slider1.getValue()*1000.0;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e6,6)+" MHz");
		}
		if(slider2.getValue() > 2000 && slider2.getValue() < 3000 ){
		    state.frequency = (slider2.getValue() - 2000) * 1.0e6 +slider1.getValue()*1000.0;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e6,6)+" MHz");
		}
                if(slider2.getValue() == 3000){
		    state.frequency = (slider2.getValue() - 3000) * 1.0e9 +1.0e9+slider1.getValue()*1.0e5;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e9,6)+" GHz");
		}
		if(slider2.getValue() > 3000 && slider2.getValue() <= 4990 ){
		    state.frequency = (slider2.getValue() - 3000) * 1.0e8 +1.0e9+slider1.getValue()*1.0e5;
		    lab1.setText("f = "+MaestroA.rounder(state.frequency/1.0e9,6)+" GHz");
		}
	    
	    state.ignition();
	    
	}
	
    }    
    
}


