//NewGuideInputPanel.java
/*
 * Panel with all input operations for microstrip
 * Using null layout - positions of elelments hard coded
 * authors: Umberto Ravaioli
 * version 1.0 - Copyright: Amanogawa.com - All Rights Reserved
 */

import java.awt.*;
import java.awt.event.*;
import java.text.*;

public class NewGuideInputPanel extends Panel implements ActionListener, AdjustmentListener{
    private NewGuide_State state;
    private static final Color bgcolor = new Color(236,236,236);
    //private static final Font labfont=new Font("SanSerif",Font.PLAIN,12);
    //private static final Font symbolfont = new Font("Serif",Font.PLAIN,12);
    //private static final Font normalfont = new Font("SanSerif",Font.PLAIN,14);
    //private static final Font smallfont = new Font("SanSerif",Font.PLAIN,12);
    private static Font labfont;
    private static Font symbolfont;
    private static Font normalfont;
    private static Font smallfont;

    public int ScrollMax = 10000;
    private Label titlelabel;
    private Label lab1, lab4, lab6;
    private Label ulab4;
    public TextField text1, text2, text3, text4, text6;
    public Scrollbar slider1, slider2, slider3;
    public  Button b1, b2, b3, b5;
    public  Button bupdate, binstr;
    DecimalFormat dec1 = new DecimalFormat("0.######");
    DecimalFormat dec2 = new DecimalFormat("0.######E0");
  
    public NewGuideInputPanel(NewGuide_State state){
	super();
	setLayout(null);
	setBackground(bgcolor);
	this.state = state;

        if (MaestroG.useBiggerFont) {
            labfont = state.sanSerifFont.deriveFont(14F);
            symbolfont = state.serifFont.deriveFont(14F);
            normalfont = state.sanSerifFont.deriveFont(16F);
            smallfont = state.sanSerifFont.deriveFont(14F);
        } else {
            labfont = state.sanSerifFont.deriveFont(12F);
            symbolfont = state.serifFont.deriveFont(12F);
            normalfont = state.sanSerifFont.deriveFont(14F);
            smallfont = state.sanSerifFont.deriveFont(12F);
        }

        setFont(normalfont);

	titlelabel = new Label("Input",Label.CENTER);
	//titlelabel.setFont(new Font("SanSerif",Font.BOLD,16));
        if (MaestroG.useBiggerFont) {
            titlelabel.setFont(state.sanSerifFont.deriveFont(Font.BOLD,18F));
        } else {
            titlelabel.setFont(state.sanSerifFont.deriveFont(Font.BOLD,16F));
        }
		
	lab1 = new Label("Substrate thickness  h  =  ",Label.RIGHT);
        lab1.setFont(smallfont);
	
	
        lab4 = new Label("Frequency   f  =  ",Label.RIGHT);
	lab4.setFont(labfont);
        
	lab6 = new Label("Strip width w  =  ",Label.RIGHT);
        lab6.setFont(labfont);
	
	//text1 = new TextField(""+state.a,8);
	//text2 = new TextField(""+state.epsilon_r0,8);
	//text3 = new TextField(""+state.epsilon_r,8);
	//text4 = new TextField(""+state.frequency,8);
	//text6 = new TextField(""+state.w,8);
	text1 = new TextField(""+dec1.format(state.a),8);
	text2 = new TextField(""+dec1.format(state.epsilon_r0),8);
	text3 = new TextField(""+dec1.format(state.epsilon_r),8);
	text4 = new TextField(""+dec2.format(state.frequency),8);
        text6 = new TextField(""+dec1.format(state.w),8);
        text1.setFont(labfont);
        text2.setFont(labfont);
        text3.setFont(labfont);
        text4.setFont(labfont);
        text6.setFont(labfont);
	
	
        // unit labels
        ulab4 = new Label(" [ Hz ]",Label.LEFT);
        ulab4.setFont(labfont);
	
        	
	slider1 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes a
	slider2 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes frequency
	slider3 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes w
	
	slider1.setValue((int)(state.a*ScrollMax/(state.a_maximum-state.a_minimum)));
	slider2.setValue((int)(state.frequency*ScrollMax/(state.frequency_maximum-state.frequency_minimum)));
	slider3.setValue((int)(state.w*ScrollMax/(state.w_maximum-state.w_minimum)));
	
        FontMetrics fmSmall = getFontMetrics(smallfont);
        int buttonWidth = fmSmall.stringWidth(" Range ");
	b1 = new Button("Range");
        b1.setFont(smallfont);
	b2 = new Button("Range");
        b2.setFont(smallfont);
	b3 = new Button("Range");
        b3.setFont(smallfont);

	bupdate = new Button("Update");
	bupdate.setFont(smallfont);
        bupdate.setBackground(Color.white);
        
        // update button
	binstr = new Button("Instructions");
        binstr.setFont(smallfont);
        binstr.setBackground(Color.white);
	add(binstr);
        int buttonx = 175;
        int buttony = 10;
        int buttonwide = 100;
        int buttonheight = 22;
	 
        binstr.setBounds(buttonx,buttony,buttonwide,buttonheight);	
        Panel pin = new Panel();
	    pin.setBackground(Color.black);
	    add(pin);
	    pin.setBounds(buttonx-1,buttony-1,buttonwide+2,buttonheight+2);
        
        add(titlelabel);
	add(lab1);
	add(lab4);
	add(lab6);
	add(text1);
	add(text3);
	add(text4);
	add(text6);
	add(ulab4);
	add(slider1);
	add(slider2);
	add(slider3);
	add(b1);
	add(b2);
	add(b3);
	//add(b5);
	add(bupdate);
	titlelabel.setBounds(20,5,50,18);
	int vPosInit = 25, vPos, dvPos = 2, vHeight = 15; 
	vPos = vPosInit;
	

	// band II  ....... was band I
	int y_thick = 98; 
      
	int y_wide = 98; 
	lab6.setBounds(20,y_wide-23,150,vHeight);
	text6.setBounds(170,y_wide-26,80,vHeight+3);
	    
	slider3.setBounds(80,y_wide,196,vHeight);
        slider3.setBackground(Color.white);
	Panel ps1 = new Panel();
	    ps1.setBackground(Color.cyan);
	    add(ps1);
	    ps1.setBounds(79,y_wide-1,198,vHeight+2);
	    
	Panel ps2 = new Panel();
	    ps2.setBackground(Color.black);
	    add(ps2);
	    ps2.setBounds(78,y_wide-2,200,vHeight+4);
	
        //b3.setBounds(20,y_wide-2,45,20);
        b3.setBounds(20,y_wide-2,buttonWidth,20);

	    
	// band III
	int y_sub = 144;   
	//lab1.setBounds(20,y_sub-23,150,vHeight);    
	//lab1.setBounds(12,y_sub-23,158,vHeight);    
	lab1.setBounds(6,y_sub-23,164,vHeight);    
	text1.setBounds(170,y_sub-26,80,vHeight+3);
	  
	slider1.setBounds(80,y_sub,196,vHeight);
	slider1.setBackground(Color.white);
        Panel ps9 = new Panel();
	    ps9.setBackground(Color.cyan);
	    add(ps9);
	    ps9.setBounds(79,y_sub-1,198,vHeight+2);
	    
	Panel ps10 = new Panel();
	    ps10.setBackground(Color.black);
	    add(ps10);
	    ps10.setBounds(78,y_sub-2,200,vHeight+4);
	
        //b1.setBounds(20,y_sub-2,45,20);
	b1.setBounds(20,y_sub-2,buttonWidth,20);
	
	// band IV
	int y_frequency = 190;
	lab4.setBounds(20,y_frequency-23,150,vHeight);
	text4.setBounds(170,y_frequency-26,80,vHeight+3);
	ulab4.setBounds(247,y_frequency-23,40,vHeight);   
	    
	slider2.setBounds(80,y_frequency,196,vHeight);
	slider2.setBackground(Color.white);
        Panel ps5 = new Panel();
	    ps5.setBackground(Color.cyan);
	    add(ps5);
	    ps5.setBounds(79,y_frequency-1,198,vHeight+2);
	    
	Panel ps6 = new Panel();
	    ps6.setBackground(Color.black);
	    add(ps6);
	    ps6.setBounds(78,y_frequency-2,200,vHeight+4);
	
        //b2.setBounds(20,y_frequency-2,45,20); // range button
	b2.setBounds(20,y_frequency-2,buttonWidth,20); // range button
	
	    
        // band V
	int y_material = 227;
        text3.setBounds(77,y_material,70,vHeight+5);
	
		
        //  band VI
	int y_update =260;
	bupdate.setBounds(20,y_update,260,20);	
	Panel ps7 = new Panel();
	    ps7.setBackground(Color.cyan);
	    add(ps7);
	    ps7.setBounds(19,y_update-1,262,22);
	    
	Panel ps8 = new Panel();
	    ps8.setBackground(Color.black);
	    add(ps8);
	    ps8.setBounds(18,y_update-2,264,24);
	


        //Listeners
	bupdate.addActionListener(this);
	slider1.addAdjustmentListener(this);
	slider2.addAdjustmentListener(this);
	slider3.addAdjustmentListener(this);
    }
    
    public void paint(Graphics g){
	Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        g.clearRect(0,0,getSize().width-1,getSize().height-1);

	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);
	int vPosInit = 30, vPos, dvPos = 2, vHeight = 20; 
	vPos = vPosInit+9*vHeight+8*dvPos;
	g.setColor(Color.red);    
	
	//MaestroG.subscripter("  ","r2","",g,14,30,vPos-5);	
	//MaestroG.subscripter("  ","s","",g,14,200,vPos-5);
	//MaestroG.subscripter("  ","2","",g,14,115,vPos-5);
	MaestroG.subscripter("  ","r"," = ",g,14,30,vPos+15);	
	
	g.setFont(new Font("Serif",Font.PLAIN,14));
	g.drawString("\u03b5",30,vPos+15);	
        
        //g.drawString("\u03c1",202,vPos-5);    //rho for metal
	//g.drawString("\u03c3",115,vPos-5);  //sigma for metal
	g.setFont(new Font("SanSerif",Font.PLAIN,12));
	
	g.setColor(Color.black);    
        //g.drawString("[mm]",252,41);
        //g.drawString("[mm]",252,87);
        //g.drawString("[mm]",252,133);
        g.drawString("[mm]",252,87);
        g.drawString("[mm]",252,133);
	
    }
    

    public void actionPerformed(ActionEvent evt){// NEED HERE
     
	if(evt.getSource()==bupdate){
	
            if(!state.IsMilsOn){
                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(""+dec1.format(state.a_maximum));
                        slider1.setValue(ScrollMax);
                    }
                    else if(Double.valueOf(text1.getText()).doubleValue()<state.a_minimum){
                        state.a = state.a_minimum;
                        text1.setText(""+dec1.format(state.a_minimum));
                        slider1.setValue(0);
                    }
                }
                catch(NumberFormatException e){
                    state.a =  0.0;
                }
            }
	    
	    try{if(Double.valueOf(text3.getText()).doubleValue()>=1.0){
		    state.epsilon_r=Double.valueOf(text3.getText()).doubleValue();
		}
		else{
		    state.epsilon_r=1.0;
		    text3.setText(""+dec1.format(state.epsilon_r));
		}
	    }
	    catch(NumberFormatException e){
                state.epsilon_r = 1.0;
	    }  
	    try{if(Double.valueOf(text2.getText()).doubleValue()>=1.0){
		    state.epsilon_r0=Double.valueOf(text2.getText()).doubleValue();
		}
		else{
		    state.epsilon_r0=1.0;
		    text2.setText(""+dec1.format(state.epsilon_r0));
		}
	    }
	    catch(NumberFormatException e){
                state.epsilon_r0 = 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(""+dec2.format(state.frequency_maximum));
		    slider2.setValue(ScrollMax);
		}
		else if(Double.valueOf(text4.getText()).doubleValue()<state.frequency_minimum){
		    state.frequency = state.frequency_minimum;
		    text4.setText(""+dec2.format(state.frequency_minimum));
		    slider2.setValue(0);
		}
	    }
	    catch(NumberFormatException e){
                state.frequency = 0.0; 
	    }
            if(!state.IsMilsOn){
                try{if(Double.valueOf(text6.getText()).doubleValue()<=state.w_maximum &&
                       Double.valueOf(text6.getText()).doubleValue()>=state.w_minimum){
                        state.w=Double.valueOf(text6.getText()).doubleValue();
                        slider3.setValue((int)(ScrollMax*(state.w-state.w_minimum)/
                                               (state.w_maximum-state.w_minimum)));
                    }
                    else if(Double.valueOf(text6.getText()).doubleValue()>state.w_maximum){
                        state.w = state.w_maximum;
                        text6.setText(""+dec1.format(state.w_maximum));
                        slider3.setValue(ScrollMax);
                    }
                    else if(Double.valueOf(text6.getText()).doubleValue()<state.w_minimum){
                        state.w = state.w_minimum;
                        text6.setText(""+dec1.format(state.w_minimum));
                        slider3.setValue(0);
                    }
                    
                }
                catch(NumberFormatException e){
                    state.w = 0.0;
                }
            }
	    state.ignition();
	}
    }//End of actionPerformed()
    

    public void adjustmentValueChanged(AdjustmentEvent evt){//NEED HERE
	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(""+dec2.format(state.frequency));
	    state.ignition();
	}
	else if(evt.getSource()==slider1){
            if(!state.IsMilsOn){
                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(""+dec1.format(state.a));
                state.ignition();
            }
	}
	else if(evt.getSource()==slider3){
            if(!state.IsMilsOn){
                if(slider3.getValue()>=ScrollMax){
		    state.w=state.w_maximum;
		    slider3.setValue((int)(ScrollMax*(state.w-state.w_minimum)
                                           /(state.w_maximum-state.w_minimum)));
                }
                else{
                    state.w = state.w_minimum + 
                        (state.w_maximum-state.w_minimum)*slider3.getValue()/ScrollMax;
                }
                
                text6.setText(""+dec1.format(state.w));
                state.ignition();
            }
	}
    }
    
}
