//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, ulab3, ulab4;
    public TextField text1, text2, text4;
    public Scrollbar slider1, slider2, slider3;
    public Scrollbar slider10, slider30;
   
    public  Button b1, b2, b3;
    public  Button bupdate;
    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(" [ m ]",Label.LEFT);
	ulab2 = new Label(" [deg]",Label.LEFT);
        ulab4 = new Label(" \u03bb",Label.LEFT);
        ulab4.setFont(labelfont);
	
	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));
	
	//b1 = new Button("Range");
	b2 = new Button("Range"); b2.setBackground(Color.white);
	//b3 = new Button("Range");
	
	bupdate = new Button("Update");
	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 = 26+offset, vPos, dvPos = 7, vHeight = 18, vslider = 15; 
	vPos = vPosInit;
	
	lab1.setBounds(15,vPos-3,150,vHeight);    
	text1.setBounds(170,vPos-3,80,vHeight);
	ulab1.setBounds(250,vPos-3,40,vHeight);   
	
	vPos += vHeight + 2;   
	//slider for theta
	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;    
	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(15,vPos-3,150,vHeight);  8/8/14
    	lab2.setBounds(10,vPos-3,160,vHeight);
	text2.setBounds(170,vPos-3,80,vHeight);
	ulab2.setBounds(250,vPos-3,40,vHeight);   
	
	vPos += vHeight + 2;
	//slider for phi
	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;    
	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(15,vPos-3,150,vHeight);   8/8/14
    	lab4.setBounds(10,vPos-3,160,vHeight);
	text4.setBounds(170,vPos-3,80,vHeight);
	ulab4.setBounds(250,vPos-3,40,vHeight);   
	
	vPos += vHeight + 2;
	
	//slider for distance    
	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 + dvPos+11;
	// update
        int vupdate = 0;
	bupdate.setBounds(35,vPos+5+vupdate,225,vHeight+3);	
	Panel ps7 = new Panel();
	    ps7.setBackground(Color.cyan);
	    add(ps7);
	    ps7.setBounds(34,vPos+3+vupdate,227,vHeight+6);
	    
	Panel ps8 = new Panel();
	    ps8.setBackground(Color.black);
	    add(ps8);
	    ps8.setBounds(33,vPos+2+vupdate,229,vHeight+8);
    

	//Listeners
	bupdate.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.PLAIN,12));
	int vfloat = 60;
	g.setColor(Color.black);    
	g.setFont(new Font("Serif",Font.PLAIN,14));
	//g.drawString("\u03b8",140,vfloat);
	//g.drawString("\u03c6",140,vfloat + 68);
	//g.drawString("\u03bb",255,vfloat + 140);
	g.setFont(new Font("Serif",Font.PLAIN,14));
	//g.drawString("=",154,vfloat);
	//g.drawString("=",154,vfloat + 68);
        
        //g.setFont(new Font("SanSerif",Font.PLAIN,12));
        //g.drawString("Area of spherical front",20,247);
        //g.drawString("=",154,247);
        
        //MaestroG.superscripter(""+state.area_front,"","",g,11,160,247);
        //System.out.println("area = "+state.area_front);
       /*
        double test, testprint;
        test = state.area_front;
        if(test < 1.0e-12){
            testprint = test * 1.0E12;
            MaestroG.superscripter("  "+MaestroA.rounder(testprint,6)+" x 10","-12"," [m\u00b2]",g,11,160,247);
        }
        else if(test >= 1.0e-12 && test < 1.0e-9){
            testprint = test * 1.0E12;
            MaestroG.superscripter("  "+MaestroA.rounder(testprint,6)+" x 10","-12"," [m\u00b2]",g,11,160,247);
        }
        else if(test >= 1.0e-9 && test < 1.0e-6){
            testprint = test * 1.0E9;
            MaestroG.superscripter("  "+MaestroA.rounder(testprint,6)+" x 10","-9"," [m\u00b2]",g,11,160,247);
        }
        else if(test >= 1.0e-6 && test < 1.0e-3){
            testprint = test * 1.0E6;
            MaestroG.superscripter("  "+MaestroA.rounder(testprint,6)+" x 10","-6"," [m\u00b2]",g,11,160,247);
        }
        else if(test >= 1.0e-3 && test < 1.0){
            testprint = test * 1.0E3;
            MaestroG.superscripter("  "+MaestroA.rounder(testprint,6)+" x 10","-3"," [m\u00b2]",g,11,160,247);
        }
        else if(test >= 1.0 && test < 1.0e3){
            testprint = test;
            MaestroG.superscripter("  "+MaestroA.rounder(testprint,6)+"",""," [m\u00b2]",g,11,160,247);
        }
        else if(test >= 1.0e3 && test < 1.0e6){
            testprint = test*1.0e-3;
            MaestroG.superscripter("  "+MaestroA.rounder(testprint,6)+" x 10","3"," [m\u00b2]",g,11,160,247);
        }
        else if(test >= 1.0e6 && test < 1.0e9){
            testprint = test*1.0e-6;
            MaestroG.superscripter("  "+MaestroA.rounder(testprint,6)+" x 10","6"," [m\u00b2]",g,11,160,247);
        }
        else if(test >= 1.0e9 && test < 1.0e12){
            testprint = test*1.0e-9;
            MaestroG.superscripter("  "+MaestroA.rounder(testprint,6)+" x 10","9"," [m\u00b2]",g,11,160,247);
        }
        else if(test >= 1.0e12 && test < 1.0e15){
            testprint = test*1.0e-12;
            MaestroG.superscripter("  "+MaestroA.rounder(testprint,6)+" x 10","12"," [m\u00b2]",g,11,160,247);
        }
        else if(test >= 1.0e15 && test < 1.0e18){
            testprint = test*1.0e-15;
            MaestroG.superscripter("  "+MaestroA.rounder(testprint,6)+" x 10","15"," [m\u00b2]",g,11,160,247);
        }
        else if(test >= 1.0e18 && test < 1.0e21){
            testprint = test*1.0e-18;
            MaestroG.superscripter("  "+MaestroA.rounder(testprint,6)+" x 10","18"," [m\u00b2]",g,11,160,247);
        }
        else if(test >= 1.0e21 && test < 1.0e24){
            testprint = test*1.0e-21;
            MaestroG.superscripter("  "+MaestroA.rounder(testprint,6)+" x 10","21"," [m\u00b2]",g,11,160,247);
        }
        else if(test >= 1.0e24){
            testprint = test*1.0e-24;
            MaestroG.superscripter("  "+MaestroA.rounder(testprint,6)+" x 10","24"," [m\u00b2]",g,11,160,247);
        }
        */
    }
    
    public void actionPerformed(ActionEvent evt){
     //***********
     if(evt.getSource()==bupdate){
	//*********
	  
	    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();
    }//End of actionPerformed()
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
	if(evt.getSource()==slider2){
	    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();
	    
	}
	//-----------------------------------------
	else if(evt.getSource()==slider10){
	    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();
	}
	
	//------------------------------------------
	else if(evt.getSource()==slider30){
	    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();
	}
	//-----------------------------------------
	else if(evt.getSource()==slider1){
	  
	    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();
	}
	
	//------------------------------------------
	else if(evt.getSource()==slider3){
	  
	    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();
	}
	
    }
    public void itemStateChanged(ItemEvent evt){
	
	   
    }
    
}


