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

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 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 offset = 20;
    private Label titlelabel;
    private Label lab0, lab1, lab3, lab3b, lab3c, lab4, lab6a, lab6b, lab6c, lab2;
    private Label ulab2, ulab4, ulab3;
    public TextField text1, text2, text3, text4, text5, text6;
    public Scrollbar slider1, slider2, slider3, slider5;
   
    public  Button b1, b2, b3;
    public  Button bupdate;
    // CHANGE ==============================================================
        public Button b2a, b2b, b3a, b3b, b4a, b4b;
    //======================================================================
    
    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.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("Dipole A - Length =  ",Label.RIGHT);
	lab1.setFont(labelfont);
	lab2 = new Label("Dipole B - Length =  ",Label.RIGHT);
	lab2.setFont(labelfont);
	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);
	lab4 = new Label("Frequency =  ",Label.RIGHT);
	lab4.setFont(labelfont);	
	lab6a = new Label("Wire",Label.CENTER);
	lab6a.setFont(labfont2);
	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.a2,8);
	text3 = new TextField(""+state.epsilon_r,8);
	text4 = new TextField(""+state.frequency,8);
	text5 = new TextField(""+state.distance,8);
	text6 = new TextField(""+state.wire_radius,8);
	
	ulab4 = new Label(" [Hz]",Label.LEFT);
	ulab2 = new Label(" \u03bb",Label.LEFT);
        ulab3 = new Label(" \u03bb",Label.LEFT);
	
        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,ScrollMax+1);//changes current
	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((int)((state.currentin-state.currentin_minimum)*ScrollMax/(state.currentin_maximum-state.currentin_minimum)));
	slider3.setValue((int)((state.a2-state.a2_minimum)*ScrollMax/(state.a2_maximum-state.a2_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);
                
                add(b2a); add(b2b); add(b3a); add(b3b); add(b4a); add(b4b);
        //==============================================================
        
	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(lab1);
	add(lab2);
	add(lab3);
	add(lab3b);
	add(lab3c);
	add(lab4);
	add(lab6a);
	add(lab6b);
	add(lab6c);
	
	add(text1);
	add(text2);
	add(text3);
	add(text4);
	add(text6);
	
	add(ulab4);
	add(ulab2);
	add(ulab3);
        
        add(slider1);
	add(slider2);
	add(slider3);
		
	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);
	ulab3.setBounds(250,vPos-3,35,vHeight); 
        
	vPos += vHeight + 2;   
	//slider for antenna length
	// 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 + 2*dvPos;
    	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
	// CHANGE  =====================================================
                b4a.setBounds(80,vPos+2,14,15);
                b4b.setBounds(262,vPos+2,14,15);
        //==============================================================
        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,vHeight+2);
	    
	vPos += vHeight + 2*dvPos;
    	lab4.setBounds(20,vPos-3,150,vHeight);
	text4.setBounds(170,vPos-3,80,vHeight);
	ulab4.setBounds(250,vPos-3,40,vHeight);   
	
	vPos += vHeight + 2;
	
	//slider for frequency    
	// CHANGE  =====================================================
                b3a.setBounds(80,vPos+2,14,15);
                b3b.setBounds(262,vPos+2,14,15);
        //==============================================================
        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,vHeight+2);
	
	vPos += vHeight + 2*dvPos;
	
	text3.setBounds(80,vPos+5,80,vHeight);
	lab3.setBounds(25,vPos+5,25,vHeight);
	lab3b.setBounds(52,vPos+11,10,vHeight);
	lab3c.setBounds(65,vPos+5,10,vHeight);
	
	//lab6a.setBounds(140,vPos,40,vHeight-2);
	//lab6b.setBounds(140,vPos+15,40,vHeight-2);
	//lab6c.setBounds(153,vPos+5,15,vHeight);
	//text6.setBounds(170,vPos+5,80,vHeight);
		
	vPos += vHeight + 2*dvPos-2;
	// 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);
        //==============================================================
	
	slider1.addAdjustmentListener(this);
	slider2.addAdjustmentListener(this);
	slider3.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.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.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;
	    //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));
	    state.ignition();
            repaint();
        }
        else if(evt.getSource()==b2b){
            slider1.setValue(slider1.getValue()+1);
            state.need_update = true;
	    //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));
	    state.ignition();
            repaint();
        }
        else if(evt.getSource()==b3a){
            slider2.setValue(slider2.getValue()-1);
            state.need_update = true;
	    //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);
	    state.ignition();
	    repaint();
        }
        else if(evt.getSource()==b3b){
            slider2.setValue(slider2.getValue()+1);
            state.need_update = true;
            state.frequency = state.frequency_minimum + 
		    (state.frequency_maximum-state.frequency_minimum)*slider2.getValue()/ScrollMax;
	    
	    text4.setText(""+state.frequency);
	    state.ignition();
	    repaint();
        }
        else if(evt.getSource()==b4a){
            slider3.setValue(slider3.getValue()-1);
            state.need_update = true;
	    state.a2  = state.a2_minimum + 
		(state.a2_maximum-state.a2_minimum)*slider3.getValue()/ScrollMax;
	    text2.setText(""+MaestroA.rounder(state.a2,7));
	    state.ignition();
            repaint();
        }
        else if(evt.getSource()==b4b){
            slider3.setValue(slider3.getValue()+1);
            state.need_update = true;
	    state.a2  = state.a2_minimum + 
		(state.a2_maximum-state.a2_minimum)*slider3.getValue()/ScrollMax;
	    text2.setText(""+MaestroA.rounder(state.a2,7));
	    state.ignition();
            repaint();
        }
        
        //***********
        if(evt.getSource()==bupdate){
	//*********
	  state.need_update = false;
	    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()<=state.currentin_maximum &&
		    Double.valueOf(text2.getText()).doubleValue()>=state.currentin_minimum){
		    state.currentin=Double.valueOf(text2.getText()).doubleValue();
		    slider3.setValue((int)(ScrollMax*(state.currentin-state.currentin_minimum)/
					    (state.currentin_maximum-state.currentin_minimum)));
		}
		else if(Double.valueOf(text2.getText()).doubleValue()>state.currentin_maximum){
		    state.currentin = state.currentin_maximum;
		    text2.setText(""+state.currentin_maximum);
		    slider3.setValue(ScrollMax);
		}
		else if(Double.valueOf(text2.getText()).doubleValue()<state.currentin_minimum){
		    state.currentin = state.currentin_minimum;
		    text2.setText(""+state.currentin_minimum);
		    slider3.setValue(0);
		}
	    }
	    catch(NumberFormatException e){
		  state.currentin = 0.0; 
	    }
	    */
	    
	    try{if(Double.valueOf(text2.getText()).doubleValue()<=state.a2_maximum &&
		    Double.valueOf(text2.getText()).doubleValue()>=state.a2_minimum){
		    state.a2=Double.valueOf(text2.getText()).doubleValue();
		    slider3.setValue((int)(ScrollMax*(state.a2-state.a2_minimum)/
					    (state.a2_maximum-state.a2_minimum)));
		}
		else if(Double.valueOf(text2.getText()).doubleValue()>state.a2_maximum){
		    state.a2 = state.a2_maximum;
		    text2.setText(""+state.a2_maximum);
		    slider3.setValue(ScrollMax);
		}
		else if(Double.valueOf(text2.getText()).doubleValue()<state.a2_minimum){
		    state.a2 = state.a2_minimum;
		    text2.setText(""+state.a2_minimum);
		    slider3.setValue(0);
		}
	    }
	    catch(NumberFormatException e){
		  state.a2 = 0.0; 
	    }
	    
	    
	  }
	    state.ignition();
            repaint();
    }//End of actionPerformed()
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
	if(evt.getSource()==slider2){
            state.need_update = true;
	    //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);
	    state.ignition();
	    repaint();
	}
	//-----------------------------------------
	else if(evt.getSource()==slider1){
            state.need_update = true;
	    //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));
	    state.ignition();
            repaint();
	}
	
	//------------------------------------------
	else if(evt.getSource()==slider3){
            state.need_update = true;
	    //if(slider3.getValue()>=ScrollMax){
		//    state.a2=state.a2_maximum;
		//    slider3.setValue((int)(ScrollMax*(state.a2-state.a2_minimum)
		//				    /(state.a2_maximum-state.a2_minimum)));
	    //}
	    //else
            {
	    state.a2  = state.a2_minimum + 
		(state.a2_maximum-state.a2_minimum)*slider3.getValue()/ScrollMax;
	    }
	    text2.setText(""+MaestroA.rounder(state.a2,7));
	    state.ignition();
            repaint();
	}
	
    }
    public void itemStateChanged(ItemEvent evt){
	
	   
    }
    
}


