//NewGuideInputPanel.java
import java.awt.*;
import java.awt.event.*;
//import maestro.lib.math.*;

public class NewGuideInputPanel3 extends Panel implements ActionListener, AdjustmentListener, ItemListener{
    private NewGuide_State state;
    //private NewModeControlPanel pwmc;
    //private static final Color bgcolor = new Color(160,200,200);
    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 labfont2=new Font("Serif",Font.PLAIN,12);
    private static final Font normalfont = new Font("SanSerif",Font.PLAIN,14);
    private static final Font labelfont = new Font("Serif",Font.PLAIN,14);
    public int ScrollMax = 10000; 
    public int ScrollMax2 = 3600; 
    public int offset = 20;
    private Label titlelabel;
    private Label lab0, lab1, lab2, lab3, lab4;
    private Label ulab1, ulab2, ulab3, ulab4;
    public TextField text1, text2, text3, text4, text5, text6;
    public Scrollbar slider1, slider2, slider3, slider4;
   
    public  Button b1, b2, b3;
    public  Button bupdate;
    // CHANGE ==============================================================
        public Button b2a, b2b, b3a, b3b, b4a, b4b, b5a, b5b;
    //======================================================================
    public Panel ps1, ps2;
    Choice ch2;
  
    public NewGuideInputPanel3(NewGuide_State state){
	super();
	setLayout(null);
	setBackground(bgcolor);
	this.state = state;
	
	titlelabel = new Label("Set Array Parameters",Label.CENTER);
	titlelabel.setFont(new Font("SanSerif",Font.BOLD,16));    
	
	lab0 = new Label("Plot:",Label.RIGHT);
	lab0.setFont(new Font("SanSerif",Font.BOLD,12));
	ch2 = new Choice();
	ch2.addItem(" Directivity");
	ch2.addItem(" Radiation Resistance");
	ch2.addItem(" Self Reactance");
	ch2.addItem(" Input Resistance");
	ch2.addItem(" Input Reactance");
	//add(lab0);
	//add(ch2);
	
	lab1 = new Label("Dipoles Distance  d =  ",Label.RIGHT);
	lab1.setFont(labelfont);
	lab2 = new Label("Phase Difference  \u03b4 =  ",Label.RIGHT);
	lab2.setFont(labelfont);
	lab3 = new Label("A - Current Max =  ",Label.RIGHT);
	lab3.setFont(labelfont);
	lab4 = new Label("B - Current Max =  ",Label.RIGHT);
	lab4.setFont(labelfont);
			
	text1 = new TextField(""+state.antenna_distance_lambda,8);
	text2 = new TextField(""+state.antenna_phase_degree,8);
	text3 = new TextField(""+state.currentin,8);
	text4 = new TextField(""+state.currentin2,8);
	
        ulab1 = new Label(" \u03bb",Label.LEFT);
	ulab2 = new Label(" deg",Label.LEFT);
	ulab3 = new Label(" [ A ]",Label.LEFT);
	ulab4 = new Label(" [ A ]",Label.LEFT);
	slider1 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes distance
	slider2 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax2+1);//changes phase
	slider3 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes phase
	slider4 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes phase
        slider1.setBackground(Color.white);
        slider2.setBackground(Color.white);
        slider3.setBackground(Color.white);
        slider4.setBackground(Color.white);
        
	slider1.setValue((int)((state.antenna_distance_lambda-state.antenna_distance_minimum)*ScrollMax/(state.antenna_distance_maximum-state.antenna_distance_minimum)));
	slider2.setValue((int)((state.antenna_phase_degree-state.antenna_phase_minimum)*ScrollMax2/(state.antenna_phase_maximum-state.antenna_phase_minimum)));
	slider3.setValue((int)((state.currentin-state.currentin_minimum)*ScrollMax/(state.currentin_maximum-state.currentin_minimum)));
	slider4.setValue((int)((state.currentin2-state.currentin2_minimum)*ScrollMax/(state.currentin2_maximum-state.currentin2_minimum)));
	
        // 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);
        //==============================================================
        
	b1 = new Button("Range");
	b2 = new Button("Range");
	b3 = new Button("Range");
	bupdate = new Button("Update");
	b1.setBackground(Color.white);
        b2.setBackground(Color.white);
        b3.setBackground(Color.white);
        bupdate.setBackground(Color.white);
        
	add(titlelabel);
	
	add(lab1);
	add(lab2);
	add(lab3);
	add(lab4);
		
	add(text1);
	add(text2);
	add(text3);
	add(text4);
	
	add(ulab1);
        add(ulab2);
	add(ulab3);
	add(ulab4);
	add(slider1);
	add(slider2);
	add(slider3);
	add(slider4);
			
	add(b1);
	add(b2);
	add(b3);
	
	add(bupdate);
	
	titlelabel.setBounds(10,10,280,18);
	lab0.setBounds(58,42,30,20);
	ch2.setBounds(93,42,150,20);
	int vPosInit = 50+offset, vPos, dvPos = 7, vHeight = 18, vSlider = 15; 
	vPos = vPosInit;
	
	lab1.setBounds(20,vPos-3,150,vHeight);    
	text1.setBounds(170,vPos-3,80,vHeight);
	ulab1.setBounds(250,vPos-3,35,vHeight);
        
	vPos += vHeight + 2;   
	
	//slider for antenna distance
	// CHANGE  =====================================================
                b2a.setBounds(80,vPos+2,14,15);
                b2b.setBounds(262,vPos+2,14,15);
        //==============================================================
        slider1.setBounds(80,vPos+2,196,vSlider);
	Panel ps9 = new Panel();
	    ps9.setBackground(Color.cyan);
	    add(ps9);
	    ps9.setBounds(79,vPos+1,198,vSlider+2);
	    
	Panel ps10 = new Panel();
	    ps10.setBackground(Color.black);
	    add(ps10);
	    ps10.setBounds(78,vPos,200,vSlider+4);
	
	b1.setBounds(15,vPos,55,vHeight+2);
	    
	vPos += vHeight + 15*dvPos/10;
    	lab3.setBounds(20,vPos-3,150,vHeight);
	text3.setBounds(170,vPos-3,80,vHeight);
	ulab3.setBounds(250,vPos-3,35,vHeight);   
	
	vPos += vHeight + 2;
	
	//slider for current max A
	// CHANGE  =====================================================
                b4a.setBounds(80,vPos+2,14,15);
                b4b.setBounds(262,vPos+2,14,15);
        //==============================================================
        slider3.setBounds(80,vPos+2,196,vSlider);
	Panel ps11 = new Panel();
	    ps11.setBackground(Color.cyan);
	    add(ps11);
	    ps11.setBounds(79,vPos+1,198,vSlider+2);
	    
	Panel ps12 = new Panel();
	    ps12.setBackground(Color.black);
	    add(ps12);
	    ps12.setBounds(78,vPos,200,vSlider+4);
	
	b2.setBounds(15,vPos,55,vHeight+2);
	    
	vPos += vHeight + 15*dvPos/10;
    	lab4.setBounds(20,vPos-3,150,vHeight);
	text4.setBounds(170,vPos-3,80,vHeight);
	ulab4.setBounds(250,vPos-3,35,vHeight);   
	
	vPos += vHeight + 2;
	
	//slider for current max B
	// CHANGE  =====================================================
                b5a.setBounds(80,vPos+2,14,15);
                b5b.setBounds(262,vPos+2,14,15);
        //==============================================================
        slider4.setBounds(80,vPos+2,196,vSlider);
	Panel ps13 = new Panel();
	    ps13.setBackground(Color.cyan);
	    add(ps13);
	    ps13.setBounds(79,vPos+1,198,vSlider+2);
	    
	Panel ps14 = new Panel();
	    ps14.setBackground(Color.black);
	    add(ps14);
	    ps14.setBounds(78,vPos,200,vSlider+4);
	
	b3.setBounds(15,vPos,55,vHeight+2);
	    
	vPos += vHeight + 15*dvPos/10;
    	lab2.setBounds(20,vPos-3,150,vHeight);
	text2.setBounds(170,vPos-3,80,vHeight);
	ulab2.setBounds(250,vPos-3,35,vHeight);   
	
	vPos += vHeight + 2;
	
	//slider for current phase
        // CHANGE  =====================================================
                b3a.setBounds(80,vPos+2,14,15);
                b3b.setBounds(262,vPos+2,14,15);
        //==============================================================
        
	slider2.setBounds(80,vPos+2,196,vSlider);
	Panel ps19 = new Panel();
	    ps19.setBackground(Color.cyan);
	    add(ps19);
	    ps19.setBounds(79,vPos+1,198,vSlider+2);
	    
	Panel ps110 = new Panel();
	    ps110.setBackground(Color.black);
	    add(ps110);
	    ps110.setBounds(78,vPos,200,vSlider+4);

	vPos += vHeight + 15*dvPos/10;
	
	// update
	// CHANGE ==============================================================
	bupdate.setBounds(35,vPos+4,225,vHeight+4);
        //======================================================================
	Panel ps7 = new Panel();
	    ps7.setBackground(Color.cyan);
	    add(ps7);
	    ps7.setBounds(34,vPos+3,227,vHeight+6);
	    
	Panel ps8 = new Panel();
	    ps8.setBackground(Color.black);
	    add(ps8);
	    ps8.setBounds(33,vPos+2,229,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);
    }
    
    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,35,getSize().width-10,35);
	
	g.setColor(Color.red);    
	g.setFont(new Font("SanSerif",Font.BOLD,12));
	
	//g.setColor(Color.black);    
	//g.setFont(new Font("Serif",Font.PLAIN,14));
	//g.drawString("\u03bb",255,vPos+53);
	
	//g.setColor(Color.black);    
	//g.setFont(new Font("Serif",Font.PLAIN,14));
	//g.drawString("\u03c8",140,228);
	//g.setFont(new Font("Serif",Font.PLAIN,14));
	//g.drawString("=",154,228);
	//g.setFont(new Font("SanSerif",Font.BOLD,12));
	
        if(state.need_update){
            g.setColor(Color.red);    
            g.drawString("Click Update to recalculate values",50,50);
        }
    }
    
    public void actionPerformed(ActionEvent evt){
        if(evt.getSource()==b2a){
            slider1.setValue(slider1.getValue()-1);
            state.need_update = true;
	    state.antenna_distance_lambda  = state.antenna_distance_minimum + 
		(state.antenna_distance_maximum-state.antenna_distance_minimum)*slider1.getValue()/ScrollMax;
	    text1.setText(""+MaestroA.rounder(state.antenna_distance_lambda,7));
	    state.ignition();
            repaint();
        }
        else if(evt.getSource()==b2b){
            slider1.setValue(slider1.getValue()+1);
            state.need_update = true;
	    state.antenna_distance_lambda  = state.antenna_distance_minimum + 
		(state.antenna_distance_maximum-state.antenna_distance_minimum)*slider1.getValue()/ScrollMax;
	    text1.setText(""+MaestroA.rounder(state.antenna_distance_lambda,7));
	    state.ignition();
            repaint();
        }
        if(evt.getSource()==b3a){
            slider2.setValue(slider2.getValue()-1);
            state.need_update = true;
	    state.antenna_phase_degree  = state.antenna_phase_minimum + 
		(state.antenna_phase_maximum-state.antenna_phase_minimum)*slider2.getValue()/ScrollMax2;
	    text2.setText(""+MaestroA.rounder(state.antenna_phase_degree,7));
	    state.ignition();
            repaint();
        }
        else if(evt.getSource()==b3b){
            slider2.setValue(slider2.getValue()+1);
            state.need_update = true;
	    state.antenna_phase_degree  = state.antenna_phase_minimum + 
		(state.antenna_phase_maximum-state.antenna_phase_minimum)*slider2.getValue()/ScrollMax2;
	    text2.setText(""+MaestroA.rounder(state.antenna_phase_degree,7));
	    state.ignition();
            repaint();
        }
        if(evt.getSource()==b4a){
            slider3.setValue(slider3.getValue()-1);
            state.need_update = true;
	    state.currentin  = state.currentin_minimum + 
		(state.currentin_maximum-state.currentin_minimum)*slider3.getValue()/ScrollMax;
	    text3.setText(""+MaestroA.rounder(state.currentin,7));
	    state.ignition();
            repaint();
        }
        else if(evt.getSource()==b4b){
            slider3.setValue(slider3.getValue()+1);
            state.need_update = true;
	    state.currentin  = state.currentin_minimum + 
		(state.currentin_maximum-state.currentin_minimum)*slider3.getValue()/ScrollMax;
	    text3.setText(""+MaestroA.rounder(state.currentin,7));
	    state.ignition();
            repaint();
        }
        if(evt.getSource()==b5a){
            slider4.setValue(slider4.getValue()-1);
            state.need_update = true;
	    state.currentin2  = state.currentin2_minimum + 
		(state.currentin2_maximum-state.currentin2_minimum)*slider4.getValue()/ScrollMax;
	    text4.setText(""+MaestroA.rounder(state.currentin2,7));
	    state.ignition();
            repaint();
        }
        else if(evt.getSource()==b5b){
            slider4.setValue(slider4.getValue()+1);
            state.need_update = true;
	    state.currentin2  = state.currentin2_minimum + 
		(state.currentin2_maximum-state.currentin2_minimum)*slider4.getValue()/ScrollMax;
	    text4.setText(""+MaestroA.rounder(state.currentin2,7));
	    state.ignition();
            repaint();
        }
     //***********
     if(evt.getSource()==bupdate){
     //*********
	  state.need_update = false;
	    try{ if(Double.valueOf(text1.getText()).doubleValue()<=state.antenna_distance_maximum &&
		    Double.valueOf(text1.getText()).doubleValue()>=state.antenna_distance_minimum){
		    state.antenna_distance_lambda=Double.valueOf(text1.getText()).doubleValue();
		    slider1.setValue((int)(ScrollMax*(state.antenna_distance_lambda-state.antenna_distance_minimum)
						    /(state.antenna_distance_maximum-state.antenna_distance_minimum)));
		 }
		 else if(Double.valueOf(text1.getText()).doubleValue()>state.antenna_distance_maximum){
		    state.antenna_distance_lambda = state.antenna_distance_maximum;
		    text1.setText(""+state.antenna_distance_maximum);
		    slider1.setValue(ScrollMax);
		 }
		 else if(Double.valueOf(text1.getText()).doubleValue()<state.antenna_distance_minimum){
		    state.antenna_distance_lambda = state.antenna_distance_minimum;
		    text1.setText(""+state.antenna_distance_minimum);
		    slider1.setValue(0);
		 }
		 
	    }
	     catch(NumberFormatException e){
		  state.antenna_distance_lambda =  0.0;
	    } 	    
	     
	    try{if(Double.valueOf(text2.getText()).doubleValue()<=state.antenna_phase_maximum &&
		    Double.valueOf(text2.getText()).doubleValue()>=state.antenna_phase_minimum){
		    state.antenna_phase_degree=Double.valueOf(text2.getText()).doubleValue();
		    slider2.setValue((int)(ScrollMax2*(state.antenna_phase_degree-state.antenna_phase_minimum)/
					    (state.antenna_phase_maximum-state.antenna_phase_minimum)));
		}
		else if(Double.valueOf(text2.getText()).doubleValue()>state.antenna_phase_maximum){
		    state.antenna_phase_degree = state.antenna_phase_maximum;
		    text2.setText(""+state.antenna_phase_maximum);
		    slider2.setValue(ScrollMax2);
		}
		else if(Double.valueOf(text2.getText()).doubleValue()<state.antenna_phase_minimum){
		    state.currentin = state.antenna_phase_minimum;
		    text2.setText(""+state.antenna_phase_minimum);
		    slider2.setValue(0);
		}
	    }
	    catch(NumberFormatException e){
		  state.antenna_phase_degree = 0.0; 
	    }
	    
	    try{if(Double.valueOf(text3.getText()).doubleValue()<=state.currentin_maximum &&
		    Double.valueOf(text3.getText()).doubleValue()>=state.currentin_minimum){
		    state.currentin=Double.valueOf(text3.getText()).doubleValue();
		    slider3.setValue((int)(ScrollMax*(state.currentin-state.currentin_minimum)/
					    (state.currentin_maximum-state.currentin_minimum)));
		}
		else if(Double.valueOf(text3.getText()).doubleValue()>state.currentin_maximum){
		    state.currentin = state.currentin_maximum;
		    text3.setText(""+state.currentin_maximum);
		    slider3.setValue(ScrollMax);
		}
		else if(Double.valueOf(text3.getText()).doubleValue()<state.currentin_minimum){
		    state.currentin = state.currentin_minimum;
		    text3.setText(""+state.currentin_minimum);
		    slider3.setValue(0);
		}
	    }
	    catch(NumberFormatException e){
		  state.currentin = 0.0; 
	    }
	    
	    try{if(Double.valueOf(text4.getText()).doubleValue()<=state.currentin2_maximum &&
		    Double.valueOf(text4.getText()).doubleValue()>=state.currentin2_minimum){
		    state.currentin2=Double.valueOf(text4.getText()).doubleValue();
		    slider4.setValue((int)(ScrollMax*(state.currentin2-state.currentin2_minimum)/
					    (state.currentin2_maximum-state.currentin2_minimum)));
		}
		else if(Double.valueOf(text4.getText()).doubleValue()>state.currentin2_maximum){
		    state.currentin2 = state.currentin2_maximum;
		    text4.setText(""+state.currentin2_maximum);
		    slider4.setValue(ScrollMax);
		}
		else if(Double.valueOf(text4.getText()).doubleValue()<state.currentin2_minimum){
		    state.currentin2 = state.currentin2_minimum;
		    text4.setText(""+state.currentin2_minimum);
		    slider4.setValue(0);
		}
	    }
	    catch(NumberFormatException e){
		  state.currentin2 = 0.0; 
	    }
	  }
	    state.ignition();
            repaint();
    }//End of actionPerformed()
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
	
	//-----------------------------------------
	if(evt.getSource()==slider1){
            state.need_update = true;
	    //if(slider1.getValue()>=ScrollMax){
		//    state.antenna_distance_lambda=state.antenna_distance_maximum;
		//    slider1.setValue((int)(ScrollMax*(state.antenna_distance_lambda-state.antenna_distance_minimum)
		//				    /(state.antenna_distance_maximum-state.antenna_distance_minimum)));
	    //}
	    //else
            {
	    state.antenna_distance_lambda  = state.antenna_distance_minimum + 
		(state.antenna_distance_maximum-state.antenna_distance_minimum)*slider1.getValue()/ScrollMax;
	    }
	    text1.setText(""+MaestroA.rounder(state.antenna_distance_lambda,7));
	    state.ignition();
            repaint();
	}
	
	//------------------------------------------
	else if(evt.getSource()==slider2){
            state.need_update = true;
	    //if(slider2.getValue()>=ScrollMax2){
		//    state.antenna_phase_degree=state.antenna_phase_maximum;
		//    slider2.setValue((int)(ScrollMax2*(state.antenna_phase_degree-state.antenna_phase_minimum)
		//				    /(state.antenna_phase_maximum-state.antenna_phase_minimum)));
	    //}
	    //else
            {
	    state.antenna_phase_degree  = state.antenna_phase_minimum + 
		(state.antenna_phase_maximum-state.antenna_phase_minimum)*slider2.getValue()/ScrollMax2;
	    }
	    text2.setText(""+MaestroA.rounder(state.antenna_phase_degree,7));
	    state.ignition();
            repaint();
	}
	//-------------------------------------------
	else if(evt.getSource()==slider3){
            state.need_update = true;
	    //if(slider3.getValue()>=ScrollMax){
		//    state.currentin=state.currentin_maximum;
		//    slider3.setValue((int)(ScrollMax*(state.currentin-state.currentin_minimum)
		//				    /(state.currentin_maximum-state.currentin_minimum)));
	    //}
	    //else
            {
	    state.currentin  = state.currentin_minimum + 
		(state.currentin_maximum-state.currentin_minimum)*slider3.getValue()/ScrollMax;
	    }
	    text3.setText(""+MaestroA.rounder(state.currentin,7));
	    state.ignition();
            repaint();
	}
	//---------------------------------------------
	else if(evt.getSource()==slider4){
            state.need_update = true;
	    //if(slider4.getValue()>=ScrollMax){
		//    state.currentin2=state.currentin2_maximum;
		//    slider4.setValue((int)(ScrollMax*(state.currentin2-state.currentin2_minimum)
		//				    /(state.currentin2_maximum-state.currentin2_minimum)));
	    //}
	    //else
            {
	    state.currentin2  = state.currentin2_minimum + 
		(state.currentin2_maximum-state.currentin2_minimum)*slider4.getValue()/ScrollMax;
	    }
	    text4.setText(""+MaestroA.rounder(state.currentin2,7));
	    state.ignition();
            repaint();
	}
	
    }
    public void itemStateChanged(ItemEvent evt){
	
	   
    }
    
}


