//SlidePanel.java

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


public class SlidePanel extends Panel implements ItemListener, ActionListener, AdjustmentListener{
	Antenna ant;
	Scrollbar slider1, slider2, slider3, slider4;
	TextField text1, text2;
	TextField text3, text4;
	TextField text5, text6;
	Checkbox c1, c2;
	CheckboxGroup cgrid1;
	private static final Color bgcolor = new Color(236,236,221);
	
	Button button1;
        // CHANGE ==============================================================
        public Button b2a, b2b, b3a, b3b, b4a, b4b, b5a, b5b;
        //======================================================================
	Rectangle r;
	final int ScrollMin=0;
	final int ScrollMax=3001;
	final int ScrollFaseMax=361;
	final int ScrollFaseMin=0;
	final int ScrollMax2 = 1001;
	final int ScrollDipoleMin = 1;
	final int ScrollDipoleMax = 51;
	double fase = 0.0;
	
        SlidePanel(Antenna ant){
		super();
		this.ant=ant;
		setBackground(Color.white);
		cgrid1=new CheckboxGroup();
		c1 = new Checkbox("deg",cgrid1,true);
		c2 = new Checkbox("rad",cgrid1,false);

		setLayout(null);
		setBackground(bgcolor);
		c1.addItemListener(this);
		c2.addItemListener(this);
		add(c1);
		add(c2);
                // CHANGE ======================================================
		//c1.setBounds(165,97,50,17);
		//c2.setBounds(220,97,50,17);
                c1.setBounds(165,94,55,17);
		c2.setBounds(220,94,55,17);
                //==============================================================
		c1.setBackground(bgcolor);
		c2.setBackground(bgcolor);

		text1=new TextField("0.5",8);	
		text2=new TextField("5",8);
		text3=new TextField("0",8);
		text4=new TextField("0.5",8);
		text5=new TextField("1.0",8);
		text6=new TextField("1.0",8);
                
                // 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);
                b5a = new Button("<");
                b5a.setBackground(bgcolor);
                b5b = new Button(">");
                b5b.setBackground(bgcolor);
                
                add(b2a); add(b2b); add(b3a); add(b3b); add(b4a); add(b4b); add(b5a); add(b5b);
                //==============================================================
		slider1=new Scrollbar(Scrollbar.HORIZONTAL,500,1,ScrollMin,ScrollMax);
		slider2=new Scrollbar(Scrollbar.HORIZONTAL,5,1,ScrollDipoleMin,ScrollDipoleMax);
		slider3=new Scrollbar(Scrollbar.HORIZONTAL,180,1,ScrollFaseMin,ScrollFaseMax);
		slider4=new Scrollbar(Scrollbar.HORIZONTAL,500,1,ScrollMin,ScrollMax2);
                slider1.setBackground(new Color(255,255,235));
                slider2.setBackground(new Color(255,255,235));
                slider3.setBackground(new Color(255,255,235));
                slider4.setBackground(new Color(255,255,235));
                
		button1=new Button("Update");
		add(text1);
		add(text2);
		add(text3);
		add(text4);
		add(text5);
		
		slider1.addAdjustmentListener(this);
		slider2.addAdjustmentListener(this);
		slider3.addAdjustmentListener(this);
		slider4.addAdjustmentListener(this);
                // CHANGE ======================================================
                b2a.addActionListener(this);
                b2b.addActionListener(this);
                b3a.addActionListener(this);
                b3b.addActionListener(this);
                b4a.addActionListener(this);
                b4b.addActionListener(this);
                b5a.addActionListener(this);
                b5b.addActionListener(this);
                //==============================================================
		add(slider1);
		add(slider2);
		add(slider3);
		add(slider4);
		
		button1.addActionListener(this);
                button1.setBackground(Color.white);
		
		text1.setBounds(220,35,60,20);
                Panel t1 = new Panel();
                t1.setBackground(Color.black);
                add(t1);
                t1.setBounds(219,34,62,22);
		text2.setBounds(220,70,60,20);
                Panel t2 = new Panel();
                t2.setBackground(Color.black);
                add(t2);
                t2.setBounds(219,69,62,22);
		text3.setBounds(220,115,60,20);
                Panel t3 = new Panel();
                t3.setBackground(Color.black);
                add(t3);
                t3.setBounds(219,114,62,22);
		text4.setBounds(220,150,60,20);
                Panel t4 = new Panel();
                t4.setBackground(Color.black);
                add(t4);
                t4.setBounds(219,149,62,22);
		text5.setBounds(18,190,80,20);
                Panel t5 = new Panel();
                t5.setBackground(Color.black);
                add(t5);
                t5.setBounds(17,189,82,22);

		slider1.setBounds(23,36,185,15);
		// CHANGE  =====================================================
                b2a.setBounds(23,36,14,15);
                b2b.setBounds(194,36,14,15);
                //==============================================================
                
                slider2.setBounds(23,71,185,15);
		// CHANGE  =====================================================
                b5a.setBounds(23,71,14,15);
                b5b.setBounds(194,71,14,15);
                //==============================================================
                
                slider3.setBounds(23,116,185,15);
		// CHANGE  =====================================================
                b3a.setBounds(23,116,14,15);
                b3b.setBounds(194,116,14,15);
                //==============================================================
                
                slider4.setBounds(23,151,185,15);
		// CHANGE  =====================================================
                b4a.setBounds(23,151,14,15);
                b4b.setBounds(194,151,14,15);
                //==============================================================
                
	    
	    Panel slider1 = new Panel();
		slider1.setBackground(Color.black);
		add(slider1);
		slider1.setBounds(22,35,187,17);
	    
	    Panel slider2 = new Panel();
		slider2.setBackground(Color.black);
		add(slider2);
		slider2.setBounds(22,70,187,17);
	    
	    Panel slider3 = new Panel();
		slider3.setBackground(Color.black);
		add(slider3);
		slider3.setBounds(22,115,187,17);
	    
	    Panel slider4 = new Panel();
		slider4.setBackground(Color.black);
		add(slider4);
		slider4.setBounds(22,150,187,17);
	    
		button1.setBounds(195,190,90,30);
		add(button1);
                Panel pb = new Panel();
                pb.setBackground(Color.black);
                add(pb);
                pb.setBounds(194,189,92,32);	
	}
	
	public void paint(Graphics g){
		r=getBounds();
                
                g.setColor(bgcolor);
                g.fillRect(0,0,r.width-1,r.height-1);
                
                Graphics2D g2d = (Graphics2D)g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		
		g.setFont(new Font("SanSerif",Font.PLAIN,11));
		g.setColor(Color.blue);
		g.drawString("Dipole Length",10,30);
		g.drawString("Number of dipoles N",10,65);
		MaestroG.AttachSymbol2("Incremental Phase Delay  ","\u03b4" ,g,11,13,10,110);
		//g.drawString("Phase Difference ",10,90);
		g.drawString("Dipole Spacing d",10,145);
		g.drawString("Maximum Dipole Current",10,185);
		g.setColor(Color.black);
		g.drawString("[ A ]",105,205);
		
		g.setFont(new Font("Serif",Font.PLAIN,14));
		g.drawString(" \u03bb ",282,50);
		
		g.drawString(" \u03bb ",282,165);
		
		g.draw3DRect(0,0,r.width-1,r.height-1,false);
		g.setColor(Color.black);
		g.setFont(new Font("Sanserif",Font.BOLD,12));	
		
	}


    	public void adjustmentValueChanged(AdjustmentEvent evt){
	 	
	if(evt.getSource()==slider1 || evt.getSource()==slider2 || evt.getSource()==slider3 || evt.getSource()==slider4){
	
	    double dipolelengthA, dipolelengthB;
	    double fase=ant.fase, distancia;
	    
	    if((double)ant.dipolelengthmaxA*slider1.getValue()/(double)(ScrollMax-1)>ant.dipolelengthmaxA){
		dipolelengthA=ant.dipolelengthmaxA;
		slider1.setValue(ScrollMax-1);
	    }
	    else{
		dipolelengthA=(double)ant.dipolelengthmaxA*slider1.getValue()/(double)(ScrollMax-1);
	    }
		
	    
	    {
		ant.number_dipoles=slider2.getValue();
	    }
		
	    	    
	    {
		distancia=(double)ant.distanciamax*slider4.getValue()/(double)(ScrollMax2-1);
	    }
	     text4.setText(String.valueOf(MaestroA.rounder(distancia,4)));
	    
	    if(dipolelengthA==0.0) dipolelengthA=1.0e-10;
		
	    text1.setText(String.valueOf(MaestroA.rounder(dipolelengthA,4)));
	    text2.setText(String.valueOf(ant.number_dipoles));
		
	    if(c2.getState()){
		
		
		    fase=(double)((slider3.getValue()-180)*2*Math.PI/(ScrollFaseMax-1));
		    text3.setText(String.valueOf(MaestroA.rounder(fase,4)));
		    ant.fase=(double)(Double.valueOf(text3.getText()).doubleValue());
	    }
	    else if(c1.getState()){
		//degree
		
		    fase=(double)(2*Math.PI*(slider3.getValue()-180)/(ScrollFaseMax-1));
		    text3.setText(String.valueOf(MaestroA.rounder(fase*180/Math.PI,2)));
		    ant.fase=(double)(Double.valueOf(text3.getText()).doubleValue()*Math.PI/180);
	    }
		
	   
	    
	   
		ant.dipolelengthA=Double.valueOf(text1.getText()).doubleValue();
		ant.dipolecurrentA=Double.valueOf(text5.getText()).doubleValue();		
		ant.distancia=Double.valueOf(text4.getText()).doubleValue();
		
		if(ant.dipolelengthA < 0.0){
		    ant.dipolelengthA=0.0;
		    text1.setText(String.valueOf(0.0));
		}
		if(ant.dipolelengthA > ant.dipolelengthmaxA){
		    ant.dipolelengthA=ant.dipolelengthmaxA;
		    text1.setText(String.valueOf(ant.dipolelengthmaxA));
		}
		
		if(ant.distancia < 0.0){
		    ant.distancia=0.0;
		    text4.setText(String.valueOf(ant.distancia));
		}
		if(ant.distancia>1.0){
		    ant.distancia=1.0;
		    text4.setText(String.valueOf(ant.distancia));
		}

		//slider1.setValue((int)(ScrollMax*ant.dipolelengthA/ant.dipolelengthmaxA));
		//slider2.setValue(ant.number_dipoles);
		
		//slider3.setValue((int)(ScrollFaseMax*ant.fase*0.5/Math.PI)+180);
		//slider4.setValue((int)(ScrollMax2*ant.distancia/ant.distanciamax));
		//ant.dcan.redraw(); //draw the dipole
		//ant.setThetaPlaneMaxima();
		//ant.UpdateRadiation();
		//ant.rcan1.redraw();//draw the radar canvas
		//ant.rcan2.redraw();
		
		//ant.outpanel.repaint();
		if(ant.dipolelengthA==0.0) ant.dipolelengthA=1.0e-10;   
	    
	    //=====================================	   
    	  }
	}
	

	public void itemStateChanged(ItemEvent evt){
	    fase=(double)(2*Math.PI*slider3.getValue()/(ScrollFaseMax-1)-Math.PI);
	    if(evt.getSource()==c2){
		text3.setText(String.valueOf(MaestroA.rounder(fase,4)));
	    }	
	    else if(evt.getSource()==c1){
		text3.setText(String.valueOf(MaestroA.rounder(fase*180/Math.PI,2)));
	    }
	}


        public void actionPerformed(ActionEvent evt){
            
            if(evt.getSource()==b2a || evt.getSource()==b2b || evt.getSource()==b3a ||
               evt.getSource()==b3b || evt.getSource()==b4a || evt.getSource()==b4b ||
               evt.getSource()==b5a || evt.getSource()==b5b)
            {
                if(evt.getSource()==b2a){
                    slider1.setValue(slider1.getValue()-1);
                }
                else if(evt.getSource()==b2b){
                    slider1.setValue(slider1.getValue()+1);
                }
                else if(evt.getSource()==b5a){
                    slider2.setValue(slider2.getValue()-1);
                }
                else if(evt.getSource()==b5b){
                    slider2.setValue(slider2.getValue()+1);
                }
                else if(evt.getSource()==b3a){
                    slider3.setValue(slider3.getValue()-1);
                }
                else if(evt.getSource()==b3b){
                    slider3.setValue(slider3.getValue()+1);
                }
                else if(evt.getSource()==b4a){
                    slider4.setValue(slider4.getValue()-1);
                }
                else if(evt.getSource()==b4b){
                    slider4.setValue(slider4.getValue()+1);
                }
                
                double dipolelengthA, dipolelengthB;
                double fase=ant.fase, distancia;
	    
                if((double)ant.dipolelengthmaxA*slider1.getValue()/(double)(ScrollMax-1)>ant.dipolelengthmaxA){
                    dipolelengthA=ant.dipolelengthmaxA;
                    slider1.setValue(ScrollMax-1);
                }
                else{
                    dipolelengthA=(double)ant.dipolelengthmaxA*slider1.getValue()/(double)(ScrollMax-1);
                }
                
		ant.number_dipoles=slider2.getValue();
		distancia=(double)ant.distanciamax*slider4.getValue()/(double)(ScrollMax2-1);
                text4.setText(String.valueOf(MaestroA.rounder(distancia,4)));
	    
                if(dipolelengthA==0.0) dipolelengthA=1.0e-10;
		
                text1.setText(String.valueOf(MaestroA.rounder(dipolelengthA,4)));
                text2.setText(String.valueOf(ant.number_dipoles));
		
                if(c2.getState()){
		    fase=(double)((slider3.getValue()-180)*2*Math.PI/(ScrollFaseMax-1));
		    text3.setText(String.valueOf(MaestroA.rounder(fase,4)));
		    ant.fase=(double)(Double.valueOf(text3.getText()).doubleValue());
                }
                else if(c1.getState()){
		//degree
		    fase=(double)(2*Math.PI*(slider3.getValue()-180)/(ScrollFaseMax-1));
		    text3.setText(String.valueOf(MaestroA.rounder(fase*180/Math.PI,2)));
		    ant.fase=(double)(Double.valueOf(text3.getText()).doubleValue()*Math.PI/180);
                }
		
		ant.dipolelengthA=Double.valueOf(text1.getText()).doubleValue();
		ant.dipolecurrentA=Double.valueOf(text5.getText()).doubleValue();		
		ant.distancia=Double.valueOf(text4.getText()).doubleValue();
		
		if(ant.dipolelengthA < 0.0){
		    ant.dipolelengthA=0.0;
		    text1.setText(String.valueOf(0.0));
		}
		if(ant.dipolelengthA > ant.dipolelengthmaxA){
		    ant.dipolelengthA=ant.dipolelengthmaxA;
		    text1.setText(String.valueOf(ant.dipolelengthmaxA));
		}
		
		if(ant.distancia < 0.0){
		    ant.distancia=0.0;
		    text4.setText(String.valueOf(ant.distancia));
		}
		if(ant.distancia>1.0){
		    ant.distancia=1.0;
		    text4.setText(String.valueOf(ant.distancia));
		}
                
                if(ant.dipolelengthA==0.0) ant.dipolelengthA=1.0e-10;   
	    
	    }
            
            if(evt.getSource()==button1){
	   
		//update dipole lengths
		ant.dipolelengthA=Double.valueOf(text1.getText()).doubleValue();
		ant.dipolecurrentA=Double.valueOf(text5.getText()).doubleValue();
		
		// update number of dipoles
		if((int)(Double.valueOf(text2.getText()).doubleValue()) < 1){
		    ant.number_dipoles=1;
		}
		else{
		    ant.number_dipoles=(int)(Double.valueOf(text2.getText()).doubleValue());
		}
		
		text2.setText(""+ant.number_dipoles);
		
		if(c2.getState()){
		    //rad
		    ant.fase=(double)(Double.valueOf(text3.getText()).doubleValue());
		}
		else{ 
		    //degree
		    ant.fase=(double)(Double.valueOf(text3.getText()).doubleValue()*Math.PI/180);
		}
		
		ant.distancia=Double.valueOf(text4.getText()).doubleValue();
				
		if(ant.dipolelengthA < 0.0){
		    ant.dipolelengthA=0.0;
		    text1.setText(String.valueOf(0.0));
		}
		if(ant.dipolelengthA > ant.dipolelengthmaxA){
		    ant.dipolelengthA=ant.dipolelengthmaxA;
		    text1.setText(String.valueOf(ant.dipolelengthmaxA));
		}
		
		if(ant.distancia < 0.0){
		    ant.distancia=0.0;
		    text4.setText(String.valueOf(ant.distancia));
		}
		if(ant.distancia>1.0){
		    ant.distancia=1.0;
		    text4.setText(String.valueOf(ant.distancia));
		}

		slider1.setValue((int)(ScrollMax*ant.dipolelengthA/ant.dipolelengthmaxA));
		slider2.setValue(ant.number_dipoles);
		
		slider3.setValue((int)(ScrollFaseMax*ant.fase*0.5/Math.PI)+ 180);
		slider4.setValue((int)(ScrollMax2*ant.distancia/ant.distanciamax));
		ant.dcan.redraw(); //draw the dipole
		ant.setThetaPlaneMaxima();
		ant.UpdateRadiation();
		ant.rcan1.redraw();//draw the radar canvas
		ant.rcan2.redraw();
		
		ant.outpanel.repaint();
		if(ant.dipolelengthA==0.0) ant.dipolelengthA=1.0e-10;
		
	    }
	}

}//End of SlidePanel
