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

public class NewGuideInputPanel2 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 normalfont = new Font("SanSerif",Font.PLAIN,14);
    private static final Font labelfont = new Font("Serif",Font.PLAIN,14);
    public int ScrollMax = 10000; 
    public int ScrollMax1 = 180; 
    public int ScrollMax2 = 360; 
    public int ScrollMax3 = 1000; 
    public int offset = 20;
    private Label titlelabel;
    private Label lab1, lab3, lab3b, lab3c, lab4, lab2;
    private Label ulab1, ulab2, ulab4;
    public TextField text1, text2, text4;
    public Scrollbar slider1, slider2, slider3;
    public Scrollbar slider10, slider30;
   
    public  Button b1, b2, b3;
    public  Button bupdate;
    // CHANGE ==============================================================
        public Button b2a, b2b, b3a, b3b, b4a, b4b, b5a, b5b, b6a, b6b;
    //======================================================================
    
    public Panel ps1, ps2;
  
    public NewGuideInputPanel2(NewGuide_State state){
	super();
	setLayout(null);
	setBackground(bgcolor);
	this.state = state;
	
	titlelabel = new Label("Scan Fields and Power",Label.CENTER);
	titlelabel.setFont(new Font("SanSerif",Font.BOLD,16));
	
	lab1 = new Label("Polar Angle  \u03b8 = ",Label.RIGHT);
	lab1.setFont(labelfont);
	lab2 = new Label("Azimuthal Angle  \u03c6 = ",Label.RIGHT);
	lab2.setFont(labelfont);
	
	lab4 = new Label("Radial Distance   R  =  ",Label.RIGHT);
	lab4.setFont(labelfont);		
	
	text1 = new TextField(""+state.theta_angle,8);
	text2 = new TextField(""+state.phi_angle,8);
	
	text4 = new TextField(""+state.point_distance,8);
	
	ulab1 = new Label(" deg",Label.LEFT);
	ulab4 = new Label(" \u03bb",Label.LEFT);
	ulab2 = new Label(" deg",Label.LEFT);
	
	slider1 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax1+1);//changes theta
	slider2 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes distance
	slider3 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax2+1);//changes phi
	slider10 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax3+1);//changes theta decimal
	slider30 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax3+1);//changes phi decimal
	slider1.setBackground(Color.white);
        slider2.setBackground(Color.white);
        slider3.setBackground(Color.white);
        slider10.setBackground(Color.white);
        slider30.setBackground(Color.white);
        
	slider1.setValue((int)((state.theta_angle-state.theta_minimum)*ScrollMax1/(state.theta_maximum-state.theta_minimum)));
	slider2.setValue((int)((state.point_distance-state.distance_minimum)*ScrollMax/(state.distance_maximum-state.distance_minimum)));
	slider3.setValue((int)((state.phi_angle-state.phi_minimum)*ScrollMax2/(state.phi_maximum-state.phi_minimum)));
	slider10.setValue((int)((state.theta_angle-(int)state.theta_angle)*ScrollMax3));
	slider30.setValue((int)((state.phi_angle-(int)state.phi_angle)*ScrollMax3));
	
        // 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);
                b6a = new Button("<");
                b6a.setBackground(bgcolor);
                b6b = new Button(">");
                b6b.setBackground(bgcolor);
                
                add(b2a); add(b2b); add(b3a); add(b3b); add(b4a); add(b4b); 
                add(b5a); add(b5b); add(b6a); add(b6b);
        //==============================================================
        
	//b1 = new Button("Range");
	b2 = new Button("Range");
        b2.setBackground(Color.white);
	//b3 = new Button("Range");
	
	bupdate = new Button("Update");
        bupdate.setBackground(Color.white);
        
	add(titlelabel);
	
	add(lab1);
	add(lab2);
	add(lab4);
	
	add(text1);
	add(text2);
	add(text4);
	
	add(ulab4);
	add(ulab2);
	add(ulab1);
	add(slider1);
	add(slider10);
	add(slider2);
	add(slider3);
	add(slider30);
		
	//add(b1);
	add(b2);
	//add(b3);
	add(bupdate);
	
	titlelabel.setBounds(10,10,280,18);
	
	int vPosInit = 40+offset, vPos, dvPos = 7, vHeight = 18, vSlider = 15; 
	vPos = vPosInit;
	
	//lab1.setBounds(30,vPos-3,100,vHeight);   
        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 theta
	// 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);
            
	vPos += vHeight;    
	// CHANGE  =====================================================
                b3a.setBounds(80,vPos+2,14,15);
                b3b.setBounds(262,vPos+2,14,15);
        //==============================================================
        slider10.setBounds(80,vPos+2,196,vSlider);
	Panel pb9 = new Panel();
	    pb9.setBackground(Color.cyan);
	    add(pb9);
	    pb9.setBounds(79,vPos+1,198,vSlider+2);
	    
	Panel pb10 = new Panel();
	    pb10.setBackground(Color.black);
	    add(pb10);
	    pb10.setBounds(78,vPos,200,vSlider+4);
	
	//b1.setBounds(20,vPos,45,vHeight);
	    
	vPos += vHeight + 2*dvPos - 4;
    	//lab2.setBounds(30,vPos-3,100,vHeight);
        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 phi
	// 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);
	
        vPos += vHeight;    
	// CHANGE  =====================================================
                b5a.setBounds(80,vPos+2,14,15);
                b5b.setBounds(262,vPos+2,14,15);
        //==============================================================
        slider30.setBounds(80,vPos+2,196,vSlider);
	Panel pb19 = new Panel();
	    pb19.setBackground(Color.cyan);
	    add(pb19);
	    pb19.setBounds(79,vPos+1,198,vSlider+2);
	    
	Panel pb110 = new Panel();
	    pb110.setBackground(Color.black);
	    add(pb110);
	    pb110.setBounds(78,vPos,200,vSlider+4);
	
	//b3.setBounds(20,vPos,45,vHeight);
	    
	vPos += vHeight + 2*dvPos-4;
    	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 distance    
	// CHANGE  =====================================================
                b6a.setBounds(80,vPos+2,14,15);
                b6b.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 + dvPos+11;
	// update
	bupdate.setBounds(35,vPos+4,225,vHeight+3);	
	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);
                b6a.addActionListener(this);
                b6b.addActionListener(this);
        //==============================================================
	slider1.addAdjustmentListener(this);
	slider2.addAdjustmentListener(this);
	slider3.addAdjustmentListener(this);
	slider10.addAdjustmentListener(this);
	slider30.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);
	
	g.setColor(Color.black);
	g.drawLine(10,35,getSize().width-10,35);
	
	g.setColor(Color.red);    
	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);
        }
        else{
            //g.setColor(bgcolor);    
            //g.drawString("Click Update to recalculate fields",10,40);
        }
        
	g.setColor(Color.black);    
	g.setFont(new Font("Serif",Font.PLAIN,14));
	//g.drawString("\u03b8",140,74);
	//g.drawString("\u03c6",140,142);
	//g.drawString("\u03bb",255,214);
	//g.setFont(new Font("Serif",Font.PLAIN,14));
	//g.drawString("=",154,74);
	//g.drawString("=",154,142);
    }
    
    public void actionPerformed(ActionEvent evt){
        if(evt.getSource()==b2a){
            slider1.setValue(slider1.getValue()-1);
            state.need_update = true;
	    state.theta_angle  = state.theta_minimum + 
		(state.theta_maximum-state.theta_minimum)*slider1.getValue()/ScrollMax1 +
		(double)slider10.getValue()/(double)ScrollMax3;
	    text1.setText(""+MaestroA.rounder(state.theta_angle,7));
	    repaint();
        }
        else if(evt.getSource()==b2b){
            slider1.setValue(slider1.getValue()+1);
            state.need_update = true;
	    state.theta_angle  = state.theta_minimum + 
		(state.theta_maximum-state.theta_minimum)*slider1.getValue()/ScrollMax1 +
		(double)slider10.getValue()/(double)ScrollMax3;
	    text1.setText(""+MaestroA.rounder(state.theta_angle,7));
	    repaint();
        }
        else if(evt.getSource()==b3a){
            slider10.setValue(slider10.getValue()-1);
            state.need_update = true;
	    state.theta_angle  = state.theta_minimum + 
		(state.theta_maximum-state.theta_minimum)*slider1.getValue()/ScrollMax1 +
		(double)slider10.getValue()/(double)ScrollMax3;
	    text1.setText(""+MaestroA.rounder(state.theta_angle,7));
	    repaint();
        }
        else if(evt.getSource()==b3b){
            slider10.setValue(slider10.getValue()+1);
            state.need_update = true;
	    state.theta_angle  = state.theta_minimum + 
		(state.theta_maximum-state.theta_minimum)*slider1.getValue()/ScrollMax1 +
		(double)slider10.getValue()/(double)ScrollMax3;
	    text1.setText(""+MaestroA.rounder(state.theta_angle,7));
	    repaint();
        }
        else if(evt.getSource()==b4a){
            slider3.setValue(slider3.getValue()-1);
            state.need_update = true;
	    state.phi_angle  = state.phi_minimum + 
		(state.phi_maximum-state.phi_minimum)*slider3.getValue()/ScrollMax2 +
		(double)slider30.getValue()/(double)ScrollMax3;
	    text2.setText(""+MaestroA.rounder(state.phi_angle,7));
	    repaint();
        }
        else if(evt.getSource()==b4b){
            slider3.setValue(slider3.getValue()+1);
            state.need_update = true;
	    state.phi_angle  = state.phi_minimum + 
		(state.phi_maximum-state.phi_minimum)*slider3.getValue()/ScrollMax2 +
		(double)slider30.getValue()/(double)ScrollMax3;
	    text2.setText(""+MaestroA.rounder(state.phi_angle,7));
	    repaint();
        }
        else if(evt.getSource()==b5a){
            slider30.setValue(slider30.getValue()-1);
            state.need_update = true;
	    state.phi_angle  = state.phi_minimum + 
		(state.phi_maximum-state.phi_minimum)*slider3.getValue()/ScrollMax2 +
		(double)slider30.getValue()/(double)ScrollMax3;
	    text2.setText(""+MaestroA.rounder(state.phi_angle,7));
	    repaint();
        }
        else if(evt.getSource()==b5b){
            slider30.setValue(slider30.getValue()+1);
            state.need_update = true;
	    state.phi_angle  = state.phi_minimum + 
		(state.phi_maximum-state.phi_minimum)*slider3.getValue()/ScrollMax2 +
		(double)slider30.getValue()/(double)ScrollMax3;
	    text2.setText(""+MaestroA.rounder(state.phi_angle,7));
	    repaint();
        }
        else if(evt.getSource()==b6a){
            slider2.setValue(slider2.getValue()-1);
            state.need_update = true;
	    state.point_distance = state.distance_minimum + 
		    (state.distance_maximum-state.distance_minimum)*slider2.getValue()/ScrollMax;
	    text4.setText(""+state.point_distance);
	    repaint();
        }
        else if(evt.getSource()==b6b){
            slider2.setValue(slider2.getValue()+1);
            state.need_update = true;
	    state.point_distance = state.distance_minimum + 
		    (state.distance_maximum-state.distance_minimum)*slider2.getValue()/ScrollMax;
	    text4.setText(""+state.point_distance);
	    repaint();
        }
        
        //***********
        if(evt.getSource()==bupdate){
	//*********
            state.need_update = false;
	    try{ if(Double.valueOf(text1.getText()).doubleValue()<=state.theta_maximum &&
		    Double.valueOf(text1.getText()).doubleValue()>=state.theta_minimum){
		    state.theta_angle=Double.valueOf(text1.getText()).doubleValue();
		    slider1.setValue((int)(ScrollMax1*(state.theta_angle-state.theta_minimum)
						    /(state.theta_maximum-state.theta_minimum)));
		    slider10.setValue((int)(ScrollMax3*(state.theta_angle - (int)state.theta_angle)));
		 }
		 else if(Double.valueOf(text1.getText()).doubleValue()>state.theta_maximum){
		    state.theta_angle = state.theta_maximum;
		    text1.setText(""+state.theta_maximum);
		    slider1.setValue(ScrollMax1);
		    slider10.setValue(0);
		 }
		 else if(Double.valueOf(text1.getText()).doubleValue()<state.theta_minimum){
		    state.theta_angle = state.theta_minimum;
		    text1.setText(""+state.theta_minimum);
		    slider1.setValue(0);
		    slider10.setValue(0);
		 }
		 
	    }
	     catch(NumberFormatException e){
		  state.theta_angle =  0.0;
	    } 	    
	    
	    
	    try{if(Double.valueOf(text4.getText()).doubleValue()<=state.distance_maximum &&
		    Double.valueOf(text4.getText()).doubleValue()>=state.distance_minimum){
		    state.point_distance=Double.valueOf(text4.getText()).doubleValue();
		    slider2.setValue((int)(ScrollMax*(state.point_distance-state.distance_minimum)/
					    (state.distance_maximum-state.distance_minimum)));
		}
		else if(Double.valueOf(text4.getText()).doubleValue()>state.distance_maximum){
		    state.point_distance = state.distance_maximum;
		    text4.setText(""+state.distance_maximum);
		    slider2.setValue(ScrollMax);
		}
		else if(Double.valueOf(text4.getText()).doubleValue()<state.distance_minimum){
		    state.point_distance = state.distance_minimum;
		    text4.setText(""+state.distance_minimum);
		    slider2.setValue(0);
		}
	    }
	    catch(NumberFormatException e){
		  state.point_distance = 0.0; 
	    }
	    
	    try{if(Double.valueOf(text2.getText()).doubleValue()<=state.phi_maximum &&
		    Double.valueOf(text2.getText()).doubleValue()>=state.phi_minimum){
		    state.phi_angle=Double.valueOf(text2.getText()).doubleValue();
		    slider3.setValue((int)(ScrollMax2*(state.phi_angle-state.phi_minimum)/
					    (state.phi_maximum-state.phi_minimum)));
		    slider30.setValue((int)(ScrollMax3*(state.phi_angle - (int)state.phi_angle)));
		}
		else if(Double.valueOf(text2.getText()).doubleValue()>state.phi_maximum){
		    state.phi_angle = state.phi_maximum;
		    text2.setText(""+state.phi_maximum);
		    slider3.setValue(ScrollMax2);
		    slider30.setValue(0);
		}
		else if(Double.valueOf(text2.getText()).doubleValue()<state.phi_minimum){
		    state.phi_angle = state.phi_minimum;
		    text2.setText(""+state.phi_minimum);
		    slider3.setValue(0);
		    slider30.setValue(0);
		}
	    }
	    catch(NumberFormatException e){
		  state.phi = 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.point_distance=state.distance_maximum;
		//slider2.setValue((int)(ScrollMax*(state.point_distance-state.distance_minimum)
		//				    /(state.distance_maximum-state.distance_minimum)));
	    //}
	    //else
            {
		state.point_distance = state.distance_minimum + 
		    (state.distance_maximum-state.distance_minimum)*slider2.getValue()/ScrollMax;
	    }
	    
	    text4.setText(""+state.point_distance);
	    //state.ignition();
	    repaint();
	}
	//-----------------------------------------
	else if(evt.getSource()==slider10){
            state.need_update = true;
	    //if(slider1.getValue()>=ScrollMax1){
		//    state.theta_angle=state.theta_maximum;
		//    slider1.setValue((int)(ScrollMax1*(state.theta_angle-state.theta_minimum)
		//				    /(state.theta_maximum-state.theta_minimum)));
		//    slider10.setValue(0);
	    //}
	    //else
	    {
	    state.theta_angle  = state.theta_minimum + 
		(state.theta_maximum-state.theta_minimum)*slider1.getValue()/ScrollMax1 +
		(double)slider10.getValue()/(double)ScrollMax3;
	    }
	    text1.setText(""+MaestroA.rounder(state.theta_angle,7));
	    //state.ignition();
            repaint();
	}
	
	//------------------------------------------
	else if(evt.getSource()==slider30){
            state.need_update = true;
	    //if(slider3.getValue()>=ScrollMax2){
		//    state.phi_angle=state.phi_maximum;
		//    slider3.setValue((int)(ScrollMax2*(state.phi_angle-state.phi_minimum)
		//				    /(state.phi_maximum-state.phi_minimum)));
		//    slider30.setValue(0);
	    //}
	    //else
	    {
	    state.phi_angle  = state.phi_minimum + 
		(state.phi_maximum-state.phi_minimum)*slider3.getValue()/ScrollMax2 +
		(double)slider30.getValue()/(double)ScrollMax3;
	    }
	    text2.setText(""+MaestroA.rounder(state.phi_angle,7));
	    //state.ignition();
            repaint();
	}
	//-----------------------------------------
	else if(evt.getSource()==slider1){
            state.need_update = true;
	    //if(slider1.getValue()>=ScrollMax1){
		//    state.theta_angle=state.theta_maximum;
		//    slider1.setValue((int)(ScrollMax1*(state.theta_angle-state.theta_minimum)
		//				    /(state.theta_maximum-state.theta_minimum)));
		//    slider10.setValue(0);
	    //}
	    //else
            {
	    state.theta_angle  = state.theta_minimum + 
		(state.theta_maximum-state.theta_minimum)*slider1.getValue()/ScrollMax1 +
		(double)slider10.getValue()/(double)ScrollMax3;
	    }
	    text1.setText(""+MaestroA.rounder(state.theta_angle,7));
	    //state.ignition();
            repaint();
	}
	
	//------------------------------------------
	else if(evt.getSource()==slider3){
            state.need_update = true;
	    //if(slider3.getValue()>=ScrollMax2){
		//    state.phi_angle=state.phi_maximum;
		//    slider3.setValue((int)(ScrollMax2*(state.phi_angle-state.phi_minimum)
		//				    /(state.phi_maximum-state.phi_minimum)));
		//    slider30.setValue(0);
	    //}
	    //else
            {
	    state.phi_angle  = state.phi_minimum + 
		(state.phi_maximum-state.phi_minimum)*slider3.getValue()/ScrollMax2 +
		(double)slider30.getValue()/(double)ScrollMax3;
	    }
	    text2.setText(""+MaestroA.rounder(state.phi_angle,7));
	    //state.ignition();
            repaint();
	}
	
    }
    public void itemStateChanged(ItemEvent evt){
	
	   
    }
    
}


