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

public class RectWaveGuideInputPanel extends Panel implements ActionListener, AdjustmentListener{
    private RectWaveGuide_State state;
    //private RectWaveModeControlPanel pwmc;
    //private static final Color bgcolor = new Color(160,200,200);
    private static final Color bgcolor = new Color(236,236,236);
    private static final Font labfont=new Font("SanSerif",Font.PLAIN,12);
    public int ScrollMax = 1000;
    private Label titlelabel;
    private Label lab1, lab2, lab3, lab4, lab5;
    private Label ulab1, ulab4;
    public TextField text1, text2, text3, text4, text5;
    public Scrollbar slider1, slider2, slider5;
    public  Button b1, b2, b5;
    public  Button bupdate;
    
    // CHANGE ==============================================================
        public Button b2a, b2b, b3a, b3b, b4a, b4b;
    //======================================================================
    
    private Image im;
    private Graphics buf;
    
    private int shift = 0;
    public RectWaveGuideInputPanel(RectWaveGuide_State state){
	super();
	setLayout(null);
	setBackground(bgcolor);
	this.state = state;
	
        titlelabel = new Label("Input",Label.LEFT);
	titlelabel.setFont(new Font("SanSerif",Font.BOLD,16));
	
        lab1 = new Label("Width   a  = ",Label.RIGHT);
	lab1.setFont(labfont);
        
        lab2 = new Label("Relative \u03b5  = ",Label.RIGHT);
	lab2.setFont(labfont);
	
        lab3 = new Label("Relative \u03bc  = ",Label.RIGHT);
	lab3.setFont(labfont);
	
        lab4 = new Label("Frequency  = ",Label.RIGHT);
	lab4.setFont(labfont);
        
        lab5 = new Label("Ratio a / b  = ",Label.RIGHT);
	lab5.setFont(labfont);
        
        text1 = new TextField(""+state.a,8);
	text2 = new TextField(""+state.epsilon_r,8);
	text3 = new TextField(""+state.mu_r,8);
	text4 = new TextField(""+state.frequency,8);
	text5 = new TextField(""+state.a_to_b_ratio,8);
	
        ulab1 = new Label(" m",Label.LEFT);
	ulab4 = new Label(" Hz",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
	slider5 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes a/b ratio
	slider1.setValue((int)(state.a*ScrollMax/(state.a_maximum-state.a_minimum)));
	slider2.setValue((int)(state.frequency*ScrollMax/(state.frequency_maximum-state.frequency_minimum)));
	slider5.setValue((int)((state.a_to_b_ratio-1)*ScrollMax/(state.a_to_b_ratio_maximum-state.a_to_b_ratio_minimum)));
	
        slider1.setBackground(Color.white);
        slider2.setBackground(Color.white);
        slider5.setBackground(Color.white);
        
	b1 = new Button("Range");
	b2 = new Button("Range");
	b5 = new Button("Range");
        b1.setBackground(new Color(245,245,245));
        b2.setBackground(new Color(245,245,245));
        b5.setBackground(new Color(245,245,245));
        
	bupdate = new Button("Update");
        bupdate.setBackground(new Color(245,245,245));
        bupdate.setBackground(bgcolor);
        
        // 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);
        //==============================================================
	    
	add(titlelabel); 
        add(lab1); //add(lab2); add(lab3); 
        add(lab4); add(lab5);
	add(text1); add(text2); add(text3); add(text4); add(text5);
	add(ulab1); add(ulab4); 
        add(slider1); add(slider2); add(slider5);
	add(b1); add(b2); add(b5);
	add(bupdate);
        
	titlelabel.setBounds(20,5,80,25);
	int vPosInit = 25, vPos, dvPos = 2, vHeight = 20, vHeight2 = 15, vHeight3 = 20;
	vPos = vPosInit;
	
        // Side "a" of waveguide
	lab1.setBounds(50,vPos,90,vHeight);
	text1.setBounds(150,vPos-2,100,vHeight);
	ulab1.setBounds(250,vPos,35,vHeight);
	
	    vPos += vHeight + dvPos; 
	    
	slider1.setBounds(80,vPos+2,196,vHeight2);
	// CHANGE  =====================================================
                b2a.setBounds(80,vPos+2,14,15);
                b2b.setBounds(262,vPos+2,14,15);
        //==============================================================
              
        Panel ps1 = new Panel();
	    ps1.setBackground(Color.cyan);
	    add(ps1);
	    ps1.setBounds(79,vPos+1,198,vHeight2+2);
	    
	Panel ps2 = new Panel();
	    ps2.setBackground(Color.black);
	    add(ps2);
	    ps2.setBounds(78,vPos,200,vHeight2+4);
	
	b1.setBounds(10,vPos,55,vHeight);
	
	    vPos += vHeight + 4*dvPos; 
	
        // Ratio "a/b" of waveguide
	lab5.setBounds(50,vPos,90,vHeight);    
	text5.setBounds(150,vPos-2,100,vHeight);  
	  
	     vPos += vHeight + dvPos;
	     
	slider5.setBounds(80,vPos+2,196,vHeight2);
	// CHANGE  =====================================================
                b3a.setBounds(80,vPos+2,14,15);
                b3b.setBounds(262,vPos+2,14,15);
        //==============================================================
        Panel ps3 = new Panel();
	    ps3.setBackground(Color.cyan);
	    add(ps3);
	    ps3.setBounds(79,vPos+1,198,vHeight2+2);
	    
	Panel ps4 = new Panel();
	    ps4.setBackground(Color.black);
	    add(ps4);
	    ps4.setBounds(78,vPos,200,vHeight2+4);
	
	b5.setBounds(10,vPos,55,vHeight);
	
	    vPos += vHeight + 4*dvPos;
	    
        // frequency
	lab4.setBounds(40,vPos,100,vHeight);
	text4.setBounds(150,vPos-2,100,vHeight);
	ulab4.setBounds(250,vPos,35,vHeight);   
	
	    vPos += vHeight + dvPos;
	    
	slider2.setBounds(80,vPos+2,196,vHeight2);
	// CHANGE  =====================================================
                b4a.setBounds(80,vPos+2,14,15);
                b4b.setBounds(262,vPos+2,14,15);
        //==============================================================
        Panel ps5 = new Panel();
	    ps5.setBackground(Color.cyan);
	    add(ps5);
	    ps5.setBounds(79,vPos+1,198,vHeight2+2);
	    
	Panel ps6 = new Panel();
	    ps6.setBackground(Color.black);
	    add(ps6);
	    ps6.setBounds(78,vPos,200,vHeight2+4);
	
	b2.setBounds(10,vPos,55,vHeight);
	
	    vPos += vHeight + 4*dvPos; 
	    
        // Relative epsilon
	//lab2.setBounds(50,vPos,90,vHeight);
	//text2.setBounds(130,vPos,50,vHeight);
        text2.setBounds(45,vPos,50,vHeight);
	shift = vPos;  
        //vPos += vHeight + dvPos;
	
        // Relative mu
	//lab3.setBounds(50,vPos,90,vHeight);
	//text3.setBounds(150,vPos,100,vHeight);
	text3.setBounds(230,vPos,50,vHeight);
        
        //vPos += vHeight + 8*dvPos;
	
	// UPDATE BUTTON
        // CHANGE ==============================================================
        //bupdate.setBounds(120,vPos,60,vHeight+5);
        bupdate.setBounds(105,vPos,90,vHeight+5);
        //======================================================================
	Panel ps7 = new Panel();
	    ps7.setBackground(Color.cyan);
	    //add(ps7);
	    ps7.setBounds(19,vPos-1,262,vHeight3+2);
	    
	Panel ps8 = new Panel();
	    ps8.setBackground(Color.black);
	    //add(ps8);
	    ps8.setBounds(18,vPos-2,264,vHeight3+4);
	
        //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);
	slider5.addAdjustmentListener(this);
    }
    
    @Override
    public void paint(Graphics g){
	    if(im == null){
		im = createImage(getSize().width,getSize().height);
		buf = im.getGraphics();
		drawCanvas(buf);
	    }
	    else{
		drawCanvas(buf);
	    }
	    g.drawImage(im,0,0,null);
    }
    
    
    public void update(Graphics g){
	paint(g);
    }
   
    public void drawCanvas(Graphics g){
        g.clearRect(0,0,getSize().width,getSize().height);
	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);
        
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

        g.setColor(Color.black);
        //MaestroG.subscripterSer2("\u03b5","r"," = ",g,12,100,getSize().height-25);
        MaestroG.subscripterSer2("\u03b5","r"," = ",g,12,15,shift+15);
        MaestroG.subscripterSer2("\u03bc","r"," = ",g,12,200,shift+15);
    }
    
    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(text2.getText()).doubleValue()>=1.0){
		    state.epsilon_r=Double.valueOf(text2.getText()).doubleValue();
		}
		else{
		    state.epsilon_r=1.0;
		    text2.setText(""+state.epsilon_r);
		}
	    }
	    catch(NumberFormatException e){
		  state.epsilon_r = 1.0;
	    }
	    try{if(Double.valueOf(text3.getText()).doubleValue()>=1.0){
		  state.mu_r=Double.valueOf(text3.getText()).doubleValue();
		}
		else{
		    state.mu_r=1.0;
		    text3.setText(""+state.mu_r);
		}
		
	    }
	    catch(NumberFormatException e){
		  state.mu_r = 1.0;
	    }
	    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(text5.getText()).doubleValue()<=state.a_to_b_ratio_maximum &&
		    Double.valueOf(text5.getText()).doubleValue()>=state.a_to_b_ratio_minimum){
		    state.a_to_b_ratio=Double.valueOf(text5.getText()).doubleValue();
		     slider5.setValue((int)(ScrollMax*(state.a_to_b_ratio-state.a_to_b_ratio_minimum)/
					(state.a_to_b_ratio_maximum-state.a_to_b_ratio_minimum)));
		}
		else if(Double.valueOf(text5.getText()).doubleValue()>state.a_to_b_ratio_maximum){
		    state.a_to_b_ratio = state.a_to_b_ratio_maximum;
		    text5.setText(""+state.a_to_b_ratio_maximum);
		    slider5.setValue(ScrollMax);
		}
		else if(Double.valueOf(text5.getText()).doubleValue()<state.a_to_b_ratio_minimum){
		    state.a_to_b_ratio = state.a_to_b_ratio_minimum;
		    text5.setText(""+state.a_to_b_ratio_minimum);
		    slider5.setValue(0);
		}
		
	    }
	    catch(NumberFormatException e){
		state.a_to_b_ratio = 1.0;
	    }
	    state.ignition();
	}
        
        if(evt.getSource()==b2a){
            slider1.setValue(slider1.getValue()-1);
            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();
        }
        if(evt.getSource()==b2b){
            slider1.setValue(slider1.getValue()+1);
            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();
        }
        if(evt.getSource()==b3a){
            slider5.setValue(slider5.getValue()-1);
            if(slider5.getValue()>=ScrollMax){
		    state.a_to_b_ratio=state.a_to_b_ratio_maximum;
		    slider5.setValue((int)(ScrollMax*(state.a_to_b_ratio-state.a_to_b_ratio_minimum)
						    /(state.a_to_b_ratio_maximum-state.a_to_b_ratio_minimum)));
	    }
	    else{
		state.a_to_b_ratio = state.a_to_b_ratio_minimum + 
		    (state.a_to_b_ratio_maximum-state.a_to_b_ratio_minimum)*slider5.getValue()/ScrollMax;
	    }
	
	    text5.setText(""+MaestroA.rounder(state.a_to_b_ratio,4));
	    state.ignition();
        }
        if(evt.getSource()==b3b){
            slider5.setValue(slider5.getValue()+1);
            if(slider5.getValue()>=ScrollMax){
		    state.a_to_b_ratio=state.a_to_b_ratio_maximum;
		    slider5.setValue((int)(ScrollMax*(state.a_to_b_ratio-state.a_to_b_ratio_minimum)
						    /(state.a_to_b_ratio_maximum-state.a_to_b_ratio_minimum)));
	    }
	    else{
		state.a_to_b_ratio = state.a_to_b_ratio_minimum + 
		    (state.a_to_b_ratio_maximum-state.a_to_b_ratio_minimum)*slider5.getValue()/ScrollMax;
	    }
	
	    text5.setText(""+MaestroA.rounder(state.a_to_b_ratio,4));
	    state.ignition();
        }
        if(evt.getSource()==b4a){
            slider2.setValue(slider2.getValue()-1);
            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();
        }
        if(evt.getSource()==b4b){
            slider2.setValue(slider2.getValue()+1);
            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();
        }
    }//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);
	    state.ignition();
	}
	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));
	    state.ignition();
	}
	else if(evt.getSource()==slider5){
	    if(slider5.getValue()>=ScrollMax){
		    state.a_to_b_ratio=state.a_to_b_ratio_maximum;
		    slider5.setValue((int)(ScrollMax*(state.a_to_b_ratio-state.a_to_b_ratio_minimum)
						    /(state.a_to_b_ratio_maximum-state.a_to_b_ratio_minimum)));
	    }
	    else{
		state.a_to_b_ratio = state.a_to_b_ratio_minimum + 
		    (state.a_to_b_ratio_maximum-state.a_to_b_ratio_minimum)*slider5.getValue()/ScrollMax;
	    }
	
	    text5.setText(""+MaestroA.rounder(state.a_to_b_ratio,4));
	    state.ignition();
	}
    }
    
}