//RectWaveModeControlPanel.java
import java.awt.*;
import java.awt.event.*;
//import java.applet.*;
//import java.lang.*;  

public class RectWaveModeControlPanel extends Panel implements MouseListener,
    ItemListener, KeyListener{
    
    private static final Color bgcolor = new Color(255,255,255);
    
    public CheckboxGroup cgrp;
    public Checkbox c1, c2;
    public TextField tf1, tf2;
    public Label lab1;
    public Button b11, b21, b31, b41, b12, b22, b32, b42; 
    public RectWaveGuide_State state;
    
    
    public RectWaveModeControlPanel(RectWaveGuide_State state){
	super();
	setLayout(null);
        
        this.state = state;
	setBackground(bgcolor);
	
	cgrp = new CheckboxGroup();
	c1 = new Checkbox("TE",cgrp,true);
	c2 = new Checkbox("TM",cgrp,false);
	c1.setBackground(bgcolor);
	c2.setBackground(bgcolor);
        
        b11 = new Button("<<");
        b21 = new Button("<");
        b31 = new Button(">");
        b41 = new Button(">>");
        b11.setFont(new Font("SanSerif",Font.BOLD,14));
        b21.setFont(new Font("SanSerif",Font.BOLD,14));
        b31.setFont(new Font("SanSerif",Font.BOLD,14));
        b41.setFont(new Font("SanSerif",Font.BOLD,14));
        
        b12 = new Button("<<");
        b22 = new Button("<");
        b32 = new Button(">");
        b42 = new Button(">>");
        b12.setFont(new Font("SanSerif",Font.BOLD,14));
        b22.setFont(new Font("SanSerif",Font.BOLD,14));
        b32.setFont(new Font("SanSerif",Font.BOLD,14));
        b42.setFont(new Font("SanSerif",Font.BOLD,14));
        
        
        add(b11); add(b21); add(b31); add(b41);
        add(b12); add(b22); add(b32); add(b42);
        
	tf1 = new TextField("1",3);
	tf2 = new TextField("0",3);
	lab1 = new Label("Mode Selector");
	lab1.setFont(new Font("Helvetica",Font.BOLD,12));
	
	lab1.setBounds(30,10,90,15);
        // CHANGE ==============================================================
        //c1.setBounds(130,10,40,20);
        //c2.setBounds(180,10,40,20);
        c1.setBounds(130,10,60,20);
        c2.setBounds(190,10,60,20);
        //======================================================================
        
        b11.setBounds(30,40,40,20);
        b21.setBounds(80,40,40,20);
        b31.setBounds(130,40,40,20);
        b41.setBounds(180,40,40,20);
        b12.setBounds(30,70,40,20);
        b22.setBounds(80,70,40,20);
        b32.setBounds(130,70,40,20);
        b42.setBounds(180,70,40,20);
        
        tf1.setBounds(230,40,40,20);
        tf2.setBounds(230,70,40,20);
        
        add(lab1);
	add(c1);
	add(c2);
	
	add(tf1);
	add(tf2);
	
	
	//Listeners
	b11.addMouseListener(this);
	b21.addMouseListener(this);
	b31.addMouseListener(this);
	b41.addMouseListener(this);
	b12.addMouseListener(this);
	b22.addMouseListener(this);
	b32.addMouseListener(this);
	b42.addMouseListener(this);
	c1.addItemListener(this);
	c2.addItemListener(this);
	tf1.addKeyListener(this);
	tf2.addKeyListener(this);
	tf1.setBackground(Color.white);
	tf2.setBackground(Color.white);
    }
    
    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);
    }
    
    private void myEventHandler(MouseEvent evt){
	//Upper rows of buttons (the first index)
	// IF press first button on the left (lowest mode). Rewinding
	
        
        if(evt.getSource()==b11){
	    if(c1.getState()){ //TE mode is set
		state.mode_number1 = 0;
		if(state.mode_number2==0){state.mode_number1=1;} //There is no TE0,0
	    }
	    else{//TM mode is set
		 state.mode_number1 = 1;
		 if(state.mode_number2==0){state.mode_number2=1;}//There is no TMi,0
	    }
	}
	
	// IF press button on the right (highest mode). Fast Forward
	else if(evt.getSource()==b41){
	    if(c1.getState()){//TE mode is set
		//state.mode_number1 = state.max_modes_this_m;//ModeMax1;
		state.mode_number1 = state.max_modes_excited_m0;//ModeMax1;
	    }
	    else{//TM Mode
		//state.mode_number1 = state.max_modes_this_m;//ModeMax1;
		state.mode_number1 = state.max_modes_excited_m0;//ModeMax1;
		if(state.mode_number2==0){state.mode_number2=1;}
	    }
	}
	
	// IF press second button (go to previous lower mode)
	else if(evt.getSource()==b21){
	    int x;
	    try{
	        x=Integer.valueOf(tf1.getText()).intValue()-1;
	    }
	    catch(NumberFormatException e){x=1;}
		
	    if(c1.getState()){//TE Mode    
	        x=Math.max(0,x);
	        state.mode_number1 = x;
	        if(state.mode_number1==0 && state.mode_number2==0){state.mode_number1=1;}
	    }
	    else{ //TM Mode
	        x=Math.max(1,x);
	        state.mode_number1 = x;
	        if(state.mode_number2==0){
		    state.mode_number2=1;
		}
		if(state.mode_number1 ==0){
		    state.mode_number1=1;
		}
	    }
	}
	
	// IF press third button (go to next higher mode)
	else if(evt.getSource()==b31){
	    int x;
	    try{
	        x=Integer.valueOf(tf1.getText()).intValue()+1;
	    }
	    catch(NumberFormatException e){x=1;}
		
	    if(c1.getState()){//TE Mode    
	        //x=Math.min(state.max_modes_this_m,x);
		x=Math.min(state.max_modes_excited_m0,x);
	        state.mode_number1 = x;
	    }
	    else{ //TM Mode
	        //x=Math.min(state.max_modes_this_m,x);
		x=Math.min(state.max_modes_excited_m0,x);
	        state.mode_number1 = x;
	        if(state.mode_number2==0){state.mode_number2=1;}
	    }
	}
	
	//Lower rows of buttons (the first index)
	// IF press first button on the left (lowest mode). Rewinding
	if(evt.getSource()==b12){
	    if(c1.getState()){ //TE mode is set
		state.mode_number2 = 0;
		if(state.mode_number1==0){state.mode_number2=1;} //There is no TE0,0
	    }
	    else{//TM mode is set
		state.mode_number2 = 1;
		if(state.mode_number1==0){state.mode_number1=1;}//There is no TMi,0
	    }
	}
	
	// IF press button on the right (highest mode). Fast Forward
	else if(evt.getSource()==b42){
		//state.mode_number2 = state.max_modes_this_n;//ModeMax2;
		state.mode_number2 = state.max_modes_excited_0n;//ModeMax2;
	}
	
	// IF press second button (go to previous lower mode)
	else if(evt.getSource()==b22){
	    int x;
	    try{
	        x=Integer.valueOf(tf2.getText()).intValue()-1;
	    }
	    catch(NumberFormatException e){x=1;}
		
	    if(c1.getState()){//TE Mode    
	        x=Math.max(0,x);
	        state.mode_number2 = x;
	        if(state.mode_number1==0 && state.mode_number2==0){state.mode_number2=1;}
	    }
	    else{ //TM Mode
	        x=Math.max(1,x);
	        state.mode_number2 = x;
	        if(state.mode_number1==0) {state.mode_number1=1;}
		if(state.mode_number2==0) {state.mode_number2=1;}
	    }
	}
	
	// IF press third button (go to next higher mode)
	else if(evt.getSource()==b32){
	    int x;
	    try{
	        x=Integer.valueOf(tf2.getText()).intValue()+1;
	    }
	    catch(NumberFormatException e){x=1;}
	    
	    if(c1.getState()){//TE mode is on
		x=Math.min(x,state.max_modes_excited_0n);
		//System.out.println(x+"   "+state.max_modes_excited_0n);
		state.mode_number2 = x;
	    }
	    else{//TM mode is on
		x=Math.min(x,state.max_modes_excited_0n);
		state.mode_number2 = x;
		if(state.mode_number1==0) {state.mode_number1=1;}
	    }
	}
	tf1.setText(""+state.mode_number1);
	tf2.setText(""+state.mode_number2);
	state.ignition();
    }
    
    public void mouseClicked(MouseEvent evt){/*myEventHandler(evt);*/}
    public void mouseEntered(MouseEvent evt){ }
    public void mouseExited(MouseEvent evt) { }
    public void mousePressed(MouseEvent evt) {myEventHandler(evt);}
    public void mouseReleased(MouseEvent evt) { }
    
    public void itemStateChanged(ItemEvent evt){
	int x1, x2, ModeMax;
	
	try{
	    x1=Integer.valueOf(tf1.getText()).intValue();
	}
	catch(NumberFormatException e){x1=1;}
	
	try{
	    x2=Integer.valueOf(tf2.getText()).intValue();
	}
	catch(NumberFormatException e){x2=1;}
	
	
	if(evt.getSource()==c1){//TE mode was checked
	    state.mode_number1 = x1;
	    state.mode_number2 = x2;
	    if(state.mode_number1==0 && state.mode_number2==0) {state.mode_number2=1;}
	    state.IsTEmode = true;
	}
	else if(evt.getSource()==c2){//TM mode was checked
	    if(state.mode_number2==0){state.mode_number2=1;} 
	    if(state.mode_number1==0){state.mode_number1=1;}
	    state.IsTEmode = false;
	}
	tf1.setText(""+state.mode_number1);
	tf2.setText(""+state.mode_number2);
	state.ignition();
    }
    
    public void keyPressed(KeyEvent evt){}
    public void keyReleased(KeyEvent evt){}
    public void keyTyped(KeyEvent evt){}
    
}