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

public class NewGuideInputPanel 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 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 = 10000, ScrollMax2 = 1001; 
    public int ScrollMax1 = 180; 
    public int ScrollMax3 = 1000;
    
    public int offset = 10;
    private Label titlelabel, titlelabel2;
    private Label lab1A, lab1, lab2, lab2_a, lab2_b, lab2_c, lab2A, lab3, 
                  lab3b, lab3c, lab4, lab4A, lab6a, lab6b, lab6c, labT;
    private Label ulab1, ulab3, ulab2, ulab4, ulabT, Click;
    public TextField text1, text2, text3, text4, text6, textT;
    public Scrollbar slider1, slider2, slider3, slider5;
    public Scrollbar sliderT, sliderT0;
    public  Button b1, b2, b3;
    public  Button bupdate;
    public Panel ps1, ps2;
    Choice ch1, ch2;
  
    public NewGuideInputPanel(NewGuide_State state){
	super();
	setLayout(null);
	setBackground(bgcolor);
	this.state = state;
	
	titlelabel = new Label("Set Antenna Parameters",Label.LEFT);
	titlelabel.setFont(new Font("SanSerif",Font.BOLD,12));    
	titlelabel2 = new Label("Elevation Angle",Label.LEFT);
	titlelabel2.setFont(new Font("SanSerif",Font.BOLD,12));    
        
	lab1A = new Label("Antenna Aperture",Label.LEFT);
	lab1A.setFont(labfont);
        lab1A.setForeground(Color.red);
	
        Click = new Label("Click Update",Label.LEFT);
	Click.setFont(labfontB);
        Click.setForeground(Color.red);
	
        lab1 = new Label("d  = ",Label.RIGHT);
	lab1.setFont(labelfont2);
	
        lab2A = new Label("Illumination factor",Label.LEFT);
	lab2A.setFont(labfont);
	lab2A.setForeground(Color.red);
        lab2 = new Label("\u03b1 ",Label.RIGHT);
        lab2.setFont(labelfont2);
        //lab2_a = new Label("E",Label.RIGHT);
	//lab2_a.setFont(labelfont);
	//lab2_b = new Label("0",Label.LEFT);
	//lab2_b.setFont(labelfont0);
        lab2_c = new Label(" = ",Label.RIGHT);
	lab2_c.setFont(labelfont2);
        
        
        // labels for epsilon - disabled
        lab3 = new Label("\u03b5",Label.RIGHT);
	lab3.setFont(labelfont);
	lab3b = new Label("r",Label.LEFT);
	lab3b.setFont(labelfont);
	lab3.setForeground(Color.black);
	lab3b.setForeground(Color.black);
	lab3c = new Label("=",Label.LEFT);
	lab3c.setFont(labelfont);
	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);
        
        // labels for wire - disabled
	lab6a = new Label("a",Label.RIGHT);
	lab6a.setFont(labelfontIT);
	lab6b = new Label("Radius",Label.CENTER);
	lab6b.setFont(labfont2);
	lab6c = new Label(" = ",Label.RIGHT);
	lab6c.setFont(labelfont);
			
	text1 = new TextField(""+state.a,8);
        text2 = new TextField(""+state.taper_parameter,8);
	text3 = new TextField(""+state.epsilon_r,8);
	text4 = new TextField(""+state.frequency,8);
	text6 = new TextField(""+state.wire_radius,8);
	
	ulab4 = new Label(" [Hz]",Label.LEFT);
	ulab2 = new Label(" [V/m]",Label.LEFT);
        ulab1 = new Label("\u03bb",Label.LEFT);
        ulab1.setFont(labfont2);
        ulab3 = new Label("\u03bb",Label.LEFT);
        ulab3.setFont(labfont2);
        
	slider1 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes a
	slider2 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes frequency
	slider3 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax2);//changes taper parameter
	slider1.setBackground(Color.white);
        slider2.setBackground(Color.white);
        slider3.setBackground(Color.white);
	slider1.setValue((int)((state.a-state.a_minimum)*ScrollMax/(state.a_maximum-state.a_minimum)));
	slider2.setValue((int)((state.frequency-state.frequency_minimum)*ScrollMax/(state.frequency_maximum-state.frequency_minimum)));
	slider3.setValue(500);
	
        sliderT = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax1+1);//changes theta
        sliderT0 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax3+1);//changes theta decimal
        sliderT.setValue((int)((state.theta_angle-state.theta_minimum)*ScrollMax1/(state.theta_maximum-state.theta_minimum)));
	sliderT0.setValue((int)((state.theta_angle-(int)state.theta_angle)*ScrollMax3));
        sliderT.setBackground(Color.white);
        sliderT0.setBackground(Color.white);
        
        labT = new Label(" \u03b8  = ",Label.RIGHT);
	labT.setFont(labelfont2);
	textT = new TextField(""+(state.theta_angle-90.0),8);
	ulabT = new Label(" [deg]",Label.LEFT);
	
        
	b1 = new Button("Range");
	b2 = new Button("Range");
	b3 = new Button("Range");
        b1.setBackground(Color.white);
        b2.setBackground(Color.white);
        b3.setBackground(Color.white);
	
	bupdate = new Button("Update");
        bupdate.setBackground(Color.white);
	add(titlelabel);
	add(titlelabel2);
        
	add(lab1);
        add(lab1A);
        add(Click);
        Click.setVisible(false);
	//add(lab2_a);
        //add(lab2_b);
        add(lab2_c);
        add(lab2A);
	add(lab2);
        add(labT);
        
        //add(lab3);
	//add(lab3b);
	//add(lab3c);
	
        add(lab4);
        add(lab4A);
        
	//add(lab6a);
	//add(lab6b);
	//add(lab6c);
	
	add(text1);
	add(text2);
	add(text3);
	add(text4);
	add(text6);
        add(textT);
	
	add(ulab4);
	add(ulab2);
        add(ulab1);
        add(ulab3);
        add(ulabT);
        
	add(slider1);
	add(slider2);
	add(slider3);
	add(sliderT);
        add(sliderT0);
        
	add(b1);
	add(b2);
	add(b3);
	add(bupdate);
	
	//titlelabel.setBounds(10,5,150,18);  8/8/14
	titlelabel.setBounds(10,5,170,18);  
	Click.setBounds(190,5,95,18);
        int vPosInit = 20+offset, vPos, dvPos = 7, vHeight = 18, vSlider = 15; 
	vPos = vPosInit;
	
	//lab2A.setBounds(15,vPos-3,120,vHeight);
	//lab2A.setBounds(15,vPos-3,110,vHeight);   // 8/8/14
	lab2A.setBounds(10,vPos-3,115,vHeight);
        //lab2.setBounds(135,vPos-3,15,vHeight);
        lab2.setBounds(125,vPos-3,20,vHeight);
        lab2_c.setBounds(145,vPos-3,20,vHeight);
	text2.setBounds(170,vPos-3,80,vHeight);
	//ulab2.setBounds(250,vPos-3,35,vHeight);   
	
	vPos += vHeight + 2;
	slider3.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);
	
	//b3.setBounds(15,vPos,55,20);
	
	vPos += vHeight + 2*dvPos;
    	//lab2_c.setBounds(150,vPos-3,20,vHeight);
        //lab2_a.setBounds(122,vPos-3,20,vHeight);
        //lab2_b.setBounds(140,vPos+2,10,vHeight);
        
        //lab1.setBounds(115,vPos-3,50,vHeight);    
        lab1.setBounds(120,vPos-3,45,vHeight);    
        //lab1A.setBounds(15,vPos-3,105,vHeight);    
        //lab1A.setBounds(15,vPos-3,110,vHeight);    8/8/14
        lab1A.setBounds(10,vPos-3,120,vHeight);    
	text1.setBounds(170,vPos-3,80,vHeight);
        ulab1.setBounds(255,vPos-3,35,vHeight); 
	
	vPos += vHeight + 2;   
	//slider for antenna length
	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,20);
	
            
	vPos += vHeight + 2*dvPos;
    	lab4.setBounds(115,vPos-3,50,vHeight);
        //lab4A.setBounds(15,vPos-3,90,vHeight);   8/8/14
        lab4A.setBounds(10,vPos-3,90,vHeight);
	text4.setBounds(170,vPos-3,80,vHeight);
	ulab4.setBounds(250,vPos-3,35,vHeight);   
	
	vPos += vHeight + 2;
	
	//slider for frequency    
	slider2.setBounds(80,vPos+2,196,vSlider);
	Panel ps5 = new Panel();
	    ps5.setBackground(Color.cyan);
	    add(ps5);
	    ps5.setBounds(79,vPos+1,198,vSlider+2);
	    
	Panel ps6 = new Panel();
	    ps6.setBackground(Color.black);
	    add(ps6);
	    ps6.setBounds(78,vPos,200,vSlider+4);
	
	b2.setBounds(15,vPos,55,20);
	
	vPos += vHeight + 15*dvPos/10;
	
	// update
	bupdate.setBounds(80,vPos+5,196,vHeight+3);	
	Panel ps7 = new Panel();
	    ps7.setBackground(Color.cyan);
	    add(ps7);
	    ps7.setBounds(79,vPos+3,198,vHeight+6);
	    
	Panel ps8 = new Panel();
	    ps8.setBackground(Color.black);
	    add(ps8);
	    ps8.setBounds(78,vPos+2,200,vHeight+8);
    
        vPos += vHeight + 40*dvPos/10 + 2;
        //titlelabel2.setBounds(10,vPos-3,100,18);  8/8/14
        titlelabel2.setBounds(10,vPos-3,110,18);
        labT.setBounds(115,vPos-3,50,vHeight);    
	textT.setBounds(170,vPos-3,80,vHeight);
	ulabT.setBounds(250,vPos-3,40,vHeight);   
	
	vPos += vHeight + 2;   
        sliderT.setBounds(80,vPos+2,196,vSlider);
	Panel pT = new Panel();
	    pT.setBackground(Color.cyan);
	    add(pT);
	    pT.setBounds(79,vPos+1,198,vSlider+2);
	    
	Panel pT2 = new Panel();
	    pT2.setBackground(Color.black);
	    add(pT2);
	    pT2.setBounds(78,vPos,200,vSlider+4);
            
        vPos += vSlider+4;
            
        sliderT0.setBounds(80,vPos+2,196,vSlider);
	Panel pT0 = new Panel();
	    pT0.setBackground(Color.cyan);
	    add(pT0);
	    pT0.setBounds(79,vPos+1,198,vSlider+2);
	    
	Panel pT02 = new Panel();
	    pT02.setBackground(Color.black);
	    add(pT02);
	    pT02.setBounds(78,vPos,200,vSlider+4);
        
	//Listeners
	bupdate.addActionListener(this);
	slider1.addAdjustmentListener(this);
	slider2.addAdjustmentListener(this);
	slider3.addAdjustmentListener(this);
        sliderT0.addAdjustmentListener(this);
	sliderT.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){
     //***********
     if(evt.getSource()==bupdate){
	//*********
	  
	    try{ if(Double.valueOf(text1.getText()).doubleValue()<=state.a_maximum &&
		    Double.valueOf(text1.getText()).doubleValue()>=state.a_minimum){
		    state.a=Double.valueOf(text1.getText()).doubleValue();
		    slider1.setValue((int)(ScrollMax*(state.a-state.a_minimum)
						    /(state.a_maximum-state.a_minimum)));
		 }
		 else if(Double.valueOf(text1.getText()).doubleValue()>state.a_maximum){
		    state.a = state.a_maximum;
		    text1.setText(""+state.a_maximum);
		    slider1.setValue(ScrollMax);
		 }
		 else if(Double.valueOf(text1.getText()).doubleValue()<state.a_minimum){
		    state.a = state.a_minimum;
		    text1.setText(""+state.a_minimum);
		    slider1.setValue(0);
		 }
		 
	    }
	     catch(NumberFormatException e){
		  state.a =  0.0;
	    } 	    
	    
	    try{if(Double.valueOf(text3.getText()).doubleValue()>=1.0){
		    state.epsilon_r=Double.valueOf(text3.getText()).doubleValue();
		}
		else{
		    state.epsilon_r=1.0;
		    text3.setText(""+state.epsilon_r);
		}
	    }
	    catch(NumberFormatException e){
		  state.epsilon_r = 1.0;
	    }
	    
	     /* 
	    try{if(Double.valueOf(text6.getText()).doubleValue()>=0.0){
		  if(Double.valueOf(text6.getText()).doubleValue() <= state.DipoleLength_lambda/10.){
		    state.wire_radius=Double.valueOf(text6.getText()).doubleValue();
		  }
		  if(Double.valueOf(text6.getText()).doubleValue() > state.DipoleLength_lambda/10.){
		    state.wire_radius=state.DipoleLength_lambda/10.;
		  }
		}
		else{
		    state.wire_radius=1.0e-5;
		    text6.setText(""+state.wire_radius);
		}
		
	    }
	    catch(NumberFormatException e){
		  state.wire_radius = 1.0e-5;
	    } 
	    */
	     
	    try{if((Double.valueOf(text6.getText()).doubleValue())>=0.0 
		   //&&(Double.valueOf(text6.getText()).doubleValue())<=(state.DipoleLength_lambda/10.)
		   ){
	    
		      state.wire_radius=Double.valueOf(text6.getText()).doubleValue();
		}
		//else if(Double.valueOf(text6.getText()).doubleValue() > state.DipoleLength_lambda/10.){
		//    state.wire_radius=state.DipoleLength_lambda/10.;
		//    text6.setText(""+state.wire_radius);
		//}
		else if((Double.valueOf(text6.getText()).doubleValue())<0.0){
		    state.wire_radius=1.0e-5;
		    text6.setText(""+state.wire_radius);
		}
		
	    }
	    catch(NumberFormatException e){
		  state.wire_radius = 1.0e-5;
	    }   
	      
	    
	    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();
		    slider2.setValue((int)(ScrollMax*(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);
		    slider2.setValue(ScrollMax);
		}
		else if(Double.valueOf(text4.getText()).doubleValue()<state.frequency_minimum){
		    state.frequency = state.frequency_minimum;
		    text4.setText(""+state.frequency_minimum);
		    slider2.setValue(0);
		}
	    }
	    catch(NumberFormatException e){
		  state.frequency = 0.0; 
	    }
	    
	    try{
                if(Double.valueOf(text2.getText()).doubleValue() > 1.0){
                    state.taper_parameter = 1.0;
		    slider3.setValue(ScrollMax2-1);
                    text2.setText("1.0");
                }
                else if(Double.valueOf(text2.getText()).doubleValue()<0.0){
                    state.taper_parameter = 0.0;
		    slider3.setValue(0);
                    text2.setText("0.0");
                }
                else{
                    state.taper_parameter = Double.valueOf(text2.getText()).doubleValue();
		    slider3.setValue((int)((ScrollMax2-1)*(state.taper_parameter)));
                }
	    }
	    catch(NumberFormatException e){
		  state.taper_parameter = 0.0; 
	    }
           
	  }
            Click.setVisible(false);
            repaint();
	    state.ignition();
            state.scanField();
            state.scan_coefficients();
            
    }//End of actionPerformed()
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
	if(evt.getSource()==slider2){
	    if(slider2.getValue()>=ScrollMax){
		state.frequency=state.frequency_maximum;
		slider2.setValue((int)(ScrollMax*(state.frequency-state.frequency_minimum)
						    /(state.frequency_maximum-state.frequency_minimum)));
	    }
	    else{
		state.frequency = state.frequency_minimum + 
		    (state.frequency_maximum-state.frequency_minimum)*slider2.getValue()/ScrollMax;
	    }
	    
	    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.a=state.a_maximum;
		    slider1.setValue((int)(ScrollMax*(state.a-state.a_minimum)
						    /(state.a_maximum-state.a_minimum)));
	    }
	    else{
	    state.a  = state.a_minimum + 
		(state.a_maximum-state.a_minimum)*slider1.getValue()/ScrollMax;
	    }
	    text1.setText(""+MaestroA.rounder(state.a,7));
            
            Click.setVisible(true);
            repaint();
            
	    //state.ignition();
            //state.scanField();
            //state.scan_coefficients();
	}
	
	//------------------------------------------
	
        else if(evt.getSource()==slider3){
	    state.taper_parameter  = (double)slider3.getValue()/(double)(ScrollMax2-1);
	    text2.setText(""+MaestroA.rounder(state.taper_parameter,7));
            
            Click.setVisible(true);
            repaint();
            
            state.ignition();
            //state.scanField();
            //state.scan_coefficients();
	}
        
        
        // Change angle theta
        else if(evt.getSource()==sliderT0){
	    if(sliderT.getValue()>=ScrollMax1){
		    state.theta_angle=state.theta_maximum;
		    sliderT.setValue((int)(ScrollMax1*(state.theta_angle-state.theta_minimum)
						    /(state.theta_maximum-state.theta_minimum)));
		    sliderT0.setValue(0);
	    }
	    else
	    {
	    state.theta_angle  = state.theta_minimum + 
		(state.theta_maximum-state.theta_minimum)*sliderT.getValue()/ScrollMax1 +
		(double)sliderT0.getValue()/(double)ScrollMax3;
	    }
	    textT.setText(""+MaestroA.rounder(state.theta_angle-90.0,7));
	    state.ignition();
	}
	
	//------------------------------------------
	else if(evt.getSource()==sliderT){
	  
	    if(sliderT.getValue()>=ScrollMax1){
		    state.theta_angle=state.theta_maximum;
		    sliderT.setValue((int)(ScrollMax1*(state.theta_angle-state.theta_minimum)
						    /(state.theta_maximum-state.theta_minimum)));
		    sliderT0.setValue(0);
	    }
	    else{
	    state.theta_angle  = state.theta_minimum + 
		(state.theta_maximum-state.theta_minimum)*sliderT.getValue()/ScrollMax1 +
		(double)sliderT0.getValue()/(double)ScrollMax3;
	    }
	    textT.setText(""+MaestroA.rounder(state.theta_angle-90.0,7));
	    state.ignition();
	}
	
	
	
    }
    public void itemStateChanged(ItemEvent evt){
	
	   
    }
    
}


