//NewGuideInputPanel.java
import java.awt.*;
import java.awt.event.*;

public class NewGuideInputPanel extends Panel implements ActionListener, AdjustmentListener, ItemListener{
    private NewGuide_State state;
    
    private static final Color bgcolor = new Color(236,236,221);
    private static final Font labfont=new Font("SanSerif",Font.PLAIN,12);
    private static final Font labfontB=new Font("SanSerif",Font.BOLD,12);
    private static final Font labfont0=new Font("SanSerif",Font.PLAIN,10);
    private static final Font labfont2=new Font("Serif",Font.PLAIN,14);
    private static final Font normalfont = new Font("SanSerif",Font.PLAIN,14);
    private static final Font labelfont = new Font("Serif",Font.ITALIC,14);
    private static final Font labelfont2 = new Font("Serif",Font.ITALIC,16);
    private static final Font labelfont0 = new Font("Serif",Font.ITALIC,12);
    private static final Font labelfontIT = new Font("Serif",Font.ITALIC,18);
    
    public int ScrollMax = 9500; // For Slider1 - Distance between antennas
    public int ScrollMax2 = 10001; // For Sliders 2 and 3 - Antenna Heights
    public int ScrollMaxF = 2970;//590; // For Slider 4 - Frequency
    
    public int offset = 10;
    
    private Label titlelabel;
    private Label lab1A, lab2Title, lab2A, lab3A, lab1, lab1c, lab2, lab2b, lab2c,
            lab3, lab3b, lab3c, lab4, lab4c, lab4A;
    private Label ulab1, ulab3, ulab2, ulab4;//, Click;
    
    public TextField text1, text2, text3, text4;
    public Scrollbar slider1, slider2, slider3, slider4;
    
    public Button bupdate;
    // CHANGE ==============================================================
        public Button b2a, b2b, b3a, b3b, b4a, b4b, b5a, b5b;
    //======================================================================
    
    public Panel ps1, ps2;
  
    public NewGuideInputPanel(NewGuide_State state){
	super();
	setLayout(null);
	setBackground(bgcolor);
	this.state = state;
	
	titlelabel = new Label("Input",Label.LEFT);
	titlelabel.setFont(new Font("SanSerif",Font.BOLD,15));    
	       
	//Click = new Label("Click Update",Label.LEFT);
	//Click.setFont(labfontB);
        //Click.setForeground(Color.red);
	
        lab1A = new Label("Ground Distance",Label.LEFT);
	lab1A.setFont(labfont);
        lab1A.setForeground(Color.red);
	
        lab1 = new Label("d",Label.RIGHT);
        lab1c = new Label("=",Label.LEFT);
        lab1.setForeground(Color.black);
        lab1c.setForeground(Color.black);
	lab1.setFont(labelfont2);
	lab1c.setFont(labfont2);
	
        lab2Title = new Label("Antennas Elevation",Label.LEFT);
        lab2Title.setFont(labfont);
        lab2Title.setForeground(Color.red);
	
        lab2A = new Label("   Transmitting",Label.LEFT);
        lab2A.setFont(labfont);
        lab2A.setForeground(Color.blue);
	lab2 = new Label("h",Label.RIGHT);
	lab2.setFont(labelfont2);
	lab2b = new Label("1",Label.LEFT);
	lab2b.setFont(labfont);
	lab2c = new Label("=",Label.LEFT);
	lab2c.setFont(labfont2);
	lab2.setForeground(Color.black);
	lab2b.setForeground(Color.black);
	lab2c.setForeground(Color.black);
        
        // labels for receiving antenna
        lab3A = new Label("    Receiving",Label.LEFT);
        lab3A.setFont(labfont);
        lab3A.setForeground(Color.blue);
        lab3 = new Label("h",Label.RIGHT);
	lab3.setFont(labelfont2);
	lab3b = new Label("2",Label.LEFT);
	lab3b.setFont(labfont);
	lab3c = new Label("=",Label.LEFT);
	lab3c.setFont(labfont2);
	lab3.setForeground(Color.black);
	lab3b.setForeground(Color.black);
	lab3c.setForeground(Color.black);
        
	lab4A = new Label("Frequency",Label.LEFT);
	lab4A.setFont(labfont);
        lab4A.setForeground(Color.red);
        lab4 = new Label("f",Label.RIGHT);
        lab4.setFont(labelfont2);
        lab4c = new Label("=",Label.LEFT);
	lab4c.setFont(labfont2);
        		
	text1 = new TextField(""+state.dist,8);
        text2 = new TextField(""+state.Tower1_height,8);
	text3 = new TextField(""+state.Tower2_height,8);
	text4 = new TextField(""+state.frequency,8);
	
	ulab1 = new Label("m",Label.LEFT);
        ulab1.setFont(labfont);
        ulab2 = new Label("m",Label.LEFT);
        ulab2.setFont(labfont);
        ulab3 = new Label("m",Label.LEFT);
        ulab3.setFont(labfont);
        ulab4 = new Label("Hz",Label.LEFT);
	ulab4.setFont(labfont);
        
        // 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);
                b4a = new Button("<");
                b4a.setBackground(bgcolor);
                b4b = new Button(">");
                b4b.setBackground(bgcolor);
                b5a = new Button("<");
                b5a.setBackground(bgcolor);
                b5b = new Button(">");
                b5b.setBackground(bgcolor);
                
                add(b2a); add(b2b); add(b3a); add(b3b); add(b4a); add(b4b); 
                add(b5a); add(b5b);
        //==============================================================
	        
	slider1 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes distance between antennas
	slider2 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax2);//changes transmitting antenna height 
	slider3 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax2);//changes receiving antenna height 
	slider4 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMaxF+1);//changes frequency
        
        slider1.setBackground(Color.white);
        slider2.setBackground(Color.white);
        slider3.setBackground(Color.white);
	slider4.setBackground(Color.white);
        
        slider1.setValue((int)((state.dist-state.dist_min)*ScrollMax/(state.dist_max-state.dist_min)));
	slider2.setValue((int)((state.Tower1_height-state.Tower_min)*ScrollMax2/(state.Tower_max-state.Tower_min)));
        slider3.setValue((int)((state.Tower2_height-state.Tower_min)*ScrollMax2/(state.Tower_max-state.Tower_min)));
	slider4.setValue((int)((state.frequency-state.frequency_minimum)*ScrollMaxF/(state.frequency_maximum-state.frequency_minimum)));
        
        bupdate = new Button("Update");
        bupdate.setBackground(Color.white);
	add(titlelabel);
	
	add(lab1A);
        add(lab1);
        add(lab1c);
        //add(Click);
        //Click.setVisible(false);
	
        add(lab2Title);
        add(lab2A);
	add(lab2);
        add(lab2b);
        add(lab2c);
        
        add(lab3A);
        add(lab3);
	add(lab3b);
	add(lab3c);
	
        add(lab4A);
        add(lab4);
        add(lab4c);
        
	add(text1);
	add(text2);
	add(text3);
	add(text4);
	
	add(ulab4);
	add(ulab2);
        add(ulab1);
        add(ulab3);
        
	add(slider1);
	add(slider2);
	add(slider3);
        add(slider4);
	add(bupdate);
	
	titlelabel.setBounds(10,5,150,18);
	//Click.setBounds(190,5,95,18);
        int vPosInit = 35+offset, vPos, dvPos = 6, vHeight_small = 9, vHeight = 18, vSlider = 15; 
	vPos = vPosInit;
	
	lab1A.setBounds(15,vPos-3,100,vHeight);    
        lab1.setBounds(130,vPos-3,15,vHeight);    
        lab1c.setBounds(150,vPos-3,15,vHeight);
        text1.setBounds(170,vPos-3,80,vHeight);
        ulab1.setBounds(255,vPos-3,35,vHeight); 
	
	vPos += vHeight + 2;   
	slider1.setBounds(20,vPos+2,256,vSlider);
	// CHANGE  =====================================================
                b2a.setBounds(20,vPos+2,14,15);
                b2b.setBounds(262,vPos+2,14,15);
        //==============================================================
        
        Panel ps9 = new Panel();
	    ps9.setBackground(Color.cyan);
	    add(ps9);
	    ps9.setBounds(19,vPos+1,258,vSlider+2);
	    
	Panel ps10 = new Panel();
	    ps10.setBackground(Color.black);
	    add(ps10);
	    ps10.setBounds(18,vPos,260,vSlider+4);
        
        vPos += vHeight + 2*dvPos;
    	            
        lab2Title.setBounds(15,vPos-3,150,vHeight);
        vPos += vHeight;
        lab2A.setBounds(15,vPos-3,100,vHeight);
        lab2.setBounds(120,vPos-3,15,vHeight);
        lab2b.setBounds(135,vPos+2,10,vHeight);
        lab2c.setBounds(150,vPos-3,15,vHeight);
	text2.setBounds(170,vPos-3,80,vHeight);
	ulab2.setBounds(255,vPos-3,35,vHeight);
        
	vPos += vHeight + 2;
	slider2.setBounds(20,vPos+2,256,vSlider);
	// CHANGE  =====================================================
                b3a.setBounds(20,vPos+2,14,15);
                b3b.setBounds(262,vPos+2,14,15);
        //==============================================================
        Panel ps19 = new Panel();
	    ps19.setBackground(Color.cyan);
	    add(ps19);
	    ps19.setBounds(19,vPos+1,258,vSlider+2);
	    
	Panel ps110 = new Panel();
	    ps110.setBackground(Color.black);
	    add(ps110);
	    ps110.setBounds(18,vPos,260,vSlider+4);
	
            vPos += vHeight + 2*dvPos;
    	            
        lab3A.setBounds(15,vPos-3,100,vHeight);
        lab3.setBounds(120,vPos-3,15,vHeight);
        lab3b.setBounds(135,vPos+2,10,vHeight);
        lab3c.setBounds(150,vPos-3,15,vHeight);
	text3.setBounds(170,vPos-3,80,vHeight);
	ulab3.setBounds(255,vPos-3,35,vHeight);
        
	vPos += vHeight + 2;
	slider3.setBounds(20,vPos+2,256,vSlider);
	// CHANGE  =====================================================
                b4a.setBounds(20,vPos+2,14,15);
                b4b.setBounds(262,vPos+2,14,15);
        //==============================================================
        Panel ps39 = new Panel();
	    ps39.setBackground(Color.cyan);
	    add(ps39);
	    ps39.setBounds(19,vPos+1,258,vSlider+2);
	    
	Panel ps310 = new Panel();
	    ps310.setBackground(Color.black);
	    add(ps310);
	    ps310.setBounds(18,vPos,260,vSlider+4);
	
	vPos += vHeight + 2*dvPos +5;
        
    	lab4A.setBounds(15,vPos-3,100,vHeight);
        lab4.setBounds(130,vPos-3,15,vHeight);
        lab4c.setBounds(150,vPos-3,15,vHeight);    
        text4.setBounds(170,vPos-3,80,vHeight);
	ulab4.setBounds(255,vPos-3,35,vHeight);   
	
	vPos += vHeight + 2;
	
	//slider for frequency    
	slider4.setBounds(20,vPos+2,256,vSlider);
	// CHANGE  =====================================================
                b5a.setBounds(20,vPos+2,14,15);
                b5b.setBounds(262,vPos+2,14,15);
        //==============================================================
        Panel ps5 = new Panel();
	    ps5.setBackground(Color.cyan);
	    add(ps5);
	    ps5.setBounds(19,vPos+1,258,vSlider+2);
	    
	Panel ps6 = new Panel();
	    ps6.setBackground(Color.black);
	    add(ps6);
	    ps6.setBounds(18,vPos,260,vSlider+4);
	
	vPos += vHeight +8;
	
	// update
	bupdate.setBounds(40,vPos+5,216,vHeight+3);	
	Panel ps7 = new Panel();
	    ps7.setBackground(Color.cyan);
	    add(ps7);
	    ps7.setBounds(39,vPos+3,218,vHeight+6);
	    
	Panel ps8 = new Panel();
	    ps8.setBackground(Color.black);
	    add(ps8);
	    ps8.setBounds(38,vPos+2,220,vHeight+8);
        
	//Listeners
	bupdate.addActionListener(this);
        // CHANGE ======================================================
                b2a.addActionListener(this);
                b2b.addActionListener(this);
                b3a.addActionListener(this);
                b3b.addActionListener(this);
                b4a.addActionListener(this);
                b4b.addActionListener(this);
                b5a.addActionListener(this);
                b5b.addActionListener(this);
        //==============================================================
        
	slider1.addAdjustmentListener(this);
	slider2.addAdjustmentListener(this);
	slider3.addAdjustmentListener(this);
        slider4.addAdjustmentListener(this);
    }
    
    @Override
    public void paint(Graphics g){
	
	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);
	int vPosInit = 30, vPos, dvPos = 2, vHeight = 20; 
	vPos = vPosInit;
	g.setColor(Color.black);
	//g.drawLine(10,218,getSize().width-10,218);
	
	g.setColor(Color.red);    
	g.setFont(new Font("SanSerif",Font.PLAIN,12));
	
	g.setColor(Color.black);    
	g.setFont(new Font("Serif",Font.PLAIN,14));
	//g.drawString("\u03bb",255,vPos+53);
	//g.drawString("\u03bb",255,getSize().height-52);
	//g.drawString("\u03c1",142,getSize().height-52);
	g.setFont(new Font("SanSerif",Font.PLAIN,12));
    }
    
    public void actionPerformed(ActionEvent evt){
     // CHANGE - ADD ALL BELOW =================================================
        if(evt.getSource()==b2a){
            slider1.setValue(slider1.getValue()-1);
            if(slider1.getValue()>=ScrollMax){
		    state.dist=state.dist_max;
		    slider1.setValue((int)(ScrollMax*(state.dist-state.dist_min)
						    /(state.dist_max-state.dist_min)));
	    }
	    else{
	    state.dist  = state.dist_min + 
		(state.dist_max-state.dist_min)*slider1.getValue()/ScrollMax;
	    }
	    text1.setText(""+MaestroA.rounder(state.dist,7));
            
            //Click.setVisible(true);
            repaint();
            
        }
        else if(evt.getSource()==b2b){
            slider1.setValue(slider1.getValue()+1);
            if(slider1.getValue()>=ScrollMax){
		    state.dist=state.dist_max;
		    slider1.setValue((int)(ScrollMax*(state.dist-state.dist_min)
						    /(state.dist_max-state.dist_min)));
	    }
	    else{
	    state.dist  = state.dist_min + 
		(state.dist_max-state.dist_min)*slider1.getValue()/ScrollMax;
	    }
	    text1.setText(""+MaestroA.rounder(state.dist,7));
            
            repaint();
            
        }
        else if(evt.getSource()==b3a){
            slider2.setValue(slider2.getValue()-1);
            state.Tower1_height  = state.Tower_min + 
		(state.Tower_max-state.Tower_min)*(double)slider2.getValue()/(ScrollMax2-1);
	    text2.setText(""+MaestroA.rounder(state.Tower1_height,7));
            
            repaint();
            
        }
        else if(evt.getSource()==b3b){
            slider2.setValue(slider2.getValue()+1);
            state.Tower1_height  = state.Tower_min + 
		(state.Tower_max-state.Tower_min)*(double)slider2.getValue()/(ScrollMax2-1);
	    text2.setText(""+MaestroA.rounder(state.Tower1_height,7));
            
            repaint();
            
        }
        else if(evt.getSource()==b4a){
            slider3.setValue(slider3.getValue()-1);
            state.Tower2_height  = state.Tower_min + 
		(state.Tower_max-state.Tower_min)*(double)slider3.getValue()/(ScrollMax2-1);
	    text3.setText(""+MaestroA.rounder(state.Tower2_height,7));
            
            repaint();
            
        }
        else if(evt.getSource()==b4b){
            slider3.setValue(slider3.getValue()+1);
            state.Tower2_height  = state.Tower_min + 
		(state.Tower_max-state.Tower_min)*(double)slider3.getValue()/(ScrollMax2-1);
	    text3.setText(""+MaestroA.rounder(state.Tower2_height,7));
            
            repaint();
            
        }
        else if(evt.getSource()==b5a){
            slider4.setValue(slider4.getValue()-1);
            if(slider4.getValue()>=ScrollMax){
		state.frequency=state.frequency_maximum;
		slider4.setValue((int)(ScrollMaxF*(state.frequency-state.frequency_minimum)
						    /(state.frequency_maximum-state.frequency_minimum)));
	    }
	    else{
		state.frequency = state.frequency_minimum + 
		    (state.frequency_maximum-state.frequency_minimum)*slider4.getValue()/ScrollMaxF;
	    }
	    
	    text4.setText(""+state.frequency);
	    
            repaint();
            
        }
        else if(evt.getSource()==b5b){
            slider4.setValue(slider4.getValue()+1);
            if(slider4.getValue()>=ScrollMax){
		state.frequency=state.frequency_maximum;
		slider4.setValue((int)(ScrollMaxF*(state.frequency-state.frequency_minimum)
						    /(state.frequency_maximum-state.frequency_minimum)));
	    }
	    else{
		state.frequency = state.frequency_minimum + 
		    (state.frequency_maximum-state.frequency_minimum)*slider4.getValue()/ScrollMaxF;
	    }
	    
	    text4.setText(""+state.frequency);
	    
            repaint();
            
        }
    //==========================================================================
        if(evt.getSource()==bupdate){
	 
	    try{ if(Double.valueOf(text1.getText()).doubleValue()<=state.dist_max &&
		    Double.valueOf(text1.getText()).doubleValue()>=state.dist_min){
		    state.dist=Double.valueOf(text1.getText()).doubleValue();
		    slider1.setValue((int)(ScrollMax*(state.dist-state.dist_min)
						    /(state.dist_max-state.dist_min)));
		 }
		 else if(Double.valueOf(text1.getText()).doubleValue()>state.dist_max){
		    state.dist = state.dist_max;
		    text1.setText(""+state.dist_max);
		    slider1.setValue(ScrollMax);
		 }
		 else if(Double.valueOf(text1.getText()).doubleValue()<state.dist_min){
		    state.dist = state.dist_min;
		    text1.setText(""+state.dist_min);
		    slider1.setValue(0);
		 }	 
	    }
	     catch(NumberFormatException e){
		  state.dist =  0.0;
	    } 	    
	     
	    try{if(Double.valueOf(text4.getText()).doubleValue()<=state.frequency_maximum &&
		    Double.valueOf(text4.getText()).doubleValue()>=state.frequency_minimum){
		    state.frequency=Double.valueOf(text4.getText()).doubleValue();
		    slider4.setValue((int)(ScrollMaxF*(state.frequency-state.frequency_minimum)/
					    (state.frequency_maximum-state.frequency_minimum)));
		}
		else if(Double.valueOf(text4.getText()).doubleValue()>state.frequency_maximum){
		    state.frequency = state.frequency_maximum;
		    text4.setText(""+state.frequency_maximum);
		    slider4.setValue(ScrollMaxF);
		}
		else if(Double.valueOf(text4.getText()).doubleValue()<state.frequency_minimum){
		    state.frequency = state.frequency_minimum;
		    text4.setText(""+state.frequency_minimum);
		    slider4.setValue(0);
		}
	    }
	    catch(NumberFormatException e){
		  state.frequency = 0.0; 
	    }
	    
            //----------------------------------------------- Transmitting antenna
	    try{
                if(Double.valueOf(text2.getText()).doubleValue() > state.Tower_max){
                    state.Tower1_height = 1000.0;
		    slider2.setValue(ScrollMax2-1);
                    text2.setText(""+state.Tower_max);
                }
                else if(Double.valueOf(text2.getText()).doubleValue() < state.Tower_min){
                    state.Tower1_height = 0.0;
		    slider2.setValue(0);
                    text2.setText(""+state.Tower_min);
                }
                else{
                    state.Tower1_height = Double.valueOf(text2.getText()).doubleValue();
                    slider2.setValue((int)((state.Tower1_height-state.Tower_min)*ScrollMax2/(state.Tower_max-state.Tower_min)));
                }
	    }
	    catch(NumberFormatException e){
		  state.Tower1_height = 0.0; 
	    }
            
            //----------------------------------------------- Receiving antenna
            try{
                if(Double.valueOf(text3.getText()).doubleValue() > state.Tower_max){
                    state.Tower2_height = 1000.0;
		    slider3.setValue(ScrollMax2-1);
                    text3.setText(""+state.Tower_max);
                }
                else if(Double.valueOf(text3.getText()).doubleValue() < state.Tower_min){
                    state.Tower2_height = 0.0;
		    slider3.setValue(0);
                    text3.setText(""+state.Tower_min);
                }
                else{
                    state.Tower2_height = Double.valueOf(text3.getText()).doubleValue();
                    slider3.setValue((int)((state.Tower2_height-state.Tower_min)*ScrollMax2/(state.Tower_max-state.Tower_min)));
                }
	    }
	    catch(NumberFormatException e){
		  state.Tower2_height = 0.0; 
	    }
           
            //============================================================================================
	  
        }
            //Click.setVisible(false);
            repaint();
	    state.ignition();
            state.scanField();
            state.scan_coefficients();
            
    }//End of actionPerformed()
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
	if(evt.getSource()==slider4){
	    if(slider4.getValue()>=ScrollMax){
		state.frequency=state.frequency_maximum;
		slider4.setValue((int)(ScrollMaxF*(state.frequency-state.frequency_minimum)
						    /(state.frequency_maximum-state.frequency_minimum)));
	    }
	    else{
		state.frequency = state.frequency_minimum + 
		    (state.frequency_maximum-state.frequency_minimum)*slider4.getValue()/ScrollMaxF;
	    }
	    
	    text4.setText(""+state.frequency);
	    
            //Click.setVisible(true);
            repaint();
            
            //state.ignition();
            //state.scanField();
            //state.scan_coefficients();
	    
	}
	//-----------------------------------------
	else if(evt.getSource()==slider1){
	  
	    if(slider1.getValue()>=ScrollMax){
		    state.dist=state.dist_max;
		    slider1.setValue((int)(ScrollMax*(state.dist-state.dist_min)
						    /(state.dist_max-state.dist_min)));
	    }
	    else{
	    state.dist  = state.dist_min + 
		(state.dist_max-state.dist_min)*slider1.getValue()/ScrollMax;
	    }
	    text1.setText(""+MaestroA.rounder(state.dist,7));
            
            //Click.setVisible(true);
            repaint();
            
	    //state.ignition();
            //state.scanField();
            //state.scan_coefficients();
	}
	
	//------------------------------------------
	
        else if(evt.getSource()==slider2){
            state.Tower1_height  = state.Tower_min + 
		(state.Tower_max-state.Tower_min)*(double)slider2.getValue()/(ScrollMax2-1);
	    text2.setText(""+MaestroA.rounder(state.Tower1_height,7));
            
            //Click.setVisible(true);
            repaint();
            
            //state.ignition();
            //state.scanField();
            //state.scan_coefficients();
	}
        else if(evt.getSource()==slider3){
            state.Tower2_height  = state.Tower_min + 
		(state.Tower_max-state.Tower_min)*(double)slider3.getValue()/(ScrollMax2-1);
	    text3.setText(""+MaestroA.rounder(state.Tower2_height,7));
            
            //Click.setVisible(true);
            repaint();
            
            //state.ignition();
            //state.scanField();
            //state.scan_coefficients();
	}
    }
    public void itemStateChanged(ItemEvent evt){
	
	   
    }
    
}


