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

public class RectWaveChooser extends Panel implements MouseListener,
    ItemListener, KeyListener{
    
    //private static final Color bgcolor = new Color(255,255,255); // white
    private static final Color bgcolor = new Color(236,236,236);
    //private static final Color bgcolor = new Color(216,230,230);
    Applet applet;
    private Label titlelabel;
    public CheckboxGroup cgrp, field;
    public Checkbox c1, c2, c3, c4, f1, f2, f3;
    public RectWaveGuide_State state;
    public boolean IsTE;
    
    //public RectWaveChooser(Applet applet,RectWaveGuide_State state){
    public RectWaveChooser(RectWaveGuide_State state){
	super();
	setLayout(null);
        
        this.state = state;
	setBackground(bgcolor);
	
        titlelabel = new Label("Plot",Label.LEFT);
	titlelabel.setFont(new Font("SanSerif",Font.BOLD,16));
        titlelabel.setForeground(Color.black);
        titlelabel.setBounds(10,0,50,18);
        add(titlelabel);
        
        IsTE = true;
        
	cgrp = new CheckboxGroup();
        field = new CheckboxGroup();
	//c1 = new Checkbox("Ex",cgrp,false);
	//c2 = new Checkbox("Ey",cgrp,false);
	//c3 = new Checkbox("| Etot |",cgrp,true);
	//c4 = new Checkbox("S",cgrp,false);
	
        c1 = new Checkbox("",cgrp,false); // Ex
	c2 = new Checkbox("",cgrp,false); // Ey
	c3 = new Checkbox("",cgrp,true); // Etot
	c4 = new Checkbox("",cgrp,false); // S
        
        c1.setBackground(bgcolor);
	c2.setBackground(bgcolor);
        c3.setBackground(bgcolor);
	c4.setBackground(bgcolor);
        
        f1 = new Checkbox("mode 1",field,true); // color map
	f2 = new Checkbox("mode 2",field,false); // colored arrows
	f3 = new Checkbox("mode 3",field,false); // only black arrows
	
        f1.setBackground(bgcolor);
	f2.setBackground(bgcolor);
        f3.setBackground(bgcolor);
	
	//add(c1);
	//add(c2);
	add(c3);
        add(c4);
        add(f1);
        add(f2);
        add(f3);
        
        //c1.setBounds(5,3,15,20);
        //c2.setBounds(50,3,15,20);
        //c3.setBounds(95,3,15,20);
        // CHANGE ==============================================================
        //c3.setBounds(90,30,15,20);
        //c4.setBounds(25,30,15,20);
        c3.setBounds(80,30,25,20);
        c4.setBounds(15,30,25,20);
        //======================================================================
        
        // CHANGE ==============================================================
        //f1.setBounds(160,10,75,20);
        //f2.setBounds(160,30,75,20);
        //f3.setBounds(160,50,75,20);
        
        f1.setBounds(160,10,100,20);
        f2.setBounds(160,30,100,20);
        f3.setBounds(160,50,100,20);
        
        //======================================================================
        
	//Listeners
	//c1.addItemListener(this);
	//c2.addItemListener(this);
	c3.addItemListener(this);
        c4.addItemListener(this);
        
        f1.addItemListener(this);
	f2.addItemListener(this);
	f3.addItemListener(this);
    }
    
    public void paint(Graphics g){
	g.clearRect(0,0,getSize().width,getSize().height);
        
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        g.setColor(Color.magenta.darker());
        //MaestroG.subscripterSerB("S","z","",g,12,178,16);
        MaestroG.subscripterSerB("S","z","",g,14,44,43);
        
        if(IsTE){
            g.setColor(Color.red.darker());
            //MaestroG.subscripterSerB("E","x,y","",g,14,109,43);
            MaestroG.subscripterSerB("E(x,y)","","",g,14,109,43);
            //MaestroG.subscripterSerB2("|"," E","tot","|",g,12,114,16);
            //MaestroG.subscripterSerB("E","y","",g,12,69,16);
            //MaestroG.subscripterSerB("E","x","",g,12,24,16);
        }
        else{
            g.setColor(Color.blue.darker());
            //MaestroG.subscripterSerB("H","x,y","",g,14,109,43);
            MaestroG.subscripterSerB("H(x,y)","","",g,14,109,43);
            //MaestroG.subscripterSerB2("|"," H","tot","|",g,12,114,16);
            //MaestroG.subscripterSerB("H","y","",g,12,69,16);
            //MaestroG.subscripterSerB("H","x","",g,12,24,16);
        }
    }
    
    private void myEventHandler(MouseEvent evt){
	
    }
    
    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 redoitE(){
        c1.setLabel("Ex");
        c2.setLabel("Ey");
        c3.setLabel("| Etot |");
    }
    
    public void redoitH(){
        c1.setLabel("Hx");
        c2.setLabel("Hy");
        c3.setLabel("| Htot |");
    }
    
    public void itemStateChanged(ItemEvent evt){
	
	if(evt.getSource()==c3){
	    f1.setEnabled(true);
            f2.setEnabled(true);
            f3.setEnabled(true);
	}
	else if(evt.getSource()==c4){
	    f1.setEnabled(false);
            f2.setEnabled(false);
            f3.setEnabled(false);
	}
	state.ignition();
    }
    
    public void keyPressed(KeyEvent evt){}
    public void keyReleased(KeyEvent evt){}
    public void keyTyped(KeyEvent evt){}
    
}