//TransSlidePanel.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class TransSlidePanel extends Panel implements ActionListener, ChangeListener, ItemListener{
    private Trans_State state;
    public JSlider slider3;
    private Label vlab1, vlab2, vlab3;
    private double lineLength;
    private Font labelfont=new Font("Sanserif",Font.PLAIN,12);
    private Font labelfont2=new Font("Sanserif",Font.PLAIN,12);
    private Font labelfont3=new Font("Sanserif",Font.PLAIN,11);
    public Panel slide1 = new Panel();
    public Panel slide2 = new Panel();
    public Panel slide3 = new Panel();
    public Panel pH, pV, pC;
    
    public Button buttonHorizontal;
    public Button buttonVertical;
    public Button buttonCurrent;
    
        
    //private static final Color bgcolor = new Color(216,216,191);
    private  static final Color bgcolor = new Color(200,255,255);
    public int shiftdown = 15;
    public int yplus = 0;//200;
    public Checkbox ch1, ch2, ch3, ch4, ch5, ch6, trace;
    public CheckboxGroup cbc1, cbc2, cbc3;

    private static final Color tinta = new Color(236,236,221);
    private boolean Is_Millimeter;
    public int SCROLLMIN=0, SCROLLMAX=351, SCROLLMAXW = 101, SCROLLMINW = 5, SCROLLMAX_TIME = 5001;
    public int SCROLLMIN_Lambda = 300, SCROLLMAX_Lambda = 751;


    public TransSlidePanel(Trans_State state){
        super();	
        setLayout(null);
        setBackground(Color.white);

        this.state = state;

        labelfont=new Font("Sanserif",Font.PLAIN,state.font12);
        labelfont2=new Font("Sanserif",Font.PLAIN,state.font12);
        labelfont3=new Font("Sanserif",Font.PLAIN,state.font11);

        CheckboxGroup cbc1 = new CheckboxGroup();
        ch1 = new Checkbox("Horizontal Plane",false,cbc1);
        ch1.setForeground(Color.red.darker());
        ch1.setFont(labelfont);
        ch2 = new Checkbox("Vertical Plane",true,cbc1);
        ch2.setForeground(Color.blue.darker());
        ch2.setFont(labelfont);
        add(ch1); add(ch2); 

        trace = new Checkbox("Current Animation",false);
        trace.setFont(labelfont);
        
        buttonHorizontal = new Button("Horizontal");
        buttonHorizontal.setBackground(new Color(255,255,255));
	buttonHorizontal.setForeground(Color.gray.brighter());
        buttonHorizontal.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
        
        buttonVertical = new Button("Vertical");
        buttonVertical.setBackground(new Color(255,255,255));
	buttonVertical.setForeground(Color.red.darker());
        buttonVertical.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
        
        buttonCurrent = new Button("Current Animation");
        buttonCurrent.setBackground(new Color(255,255,255));
	buttonCurrent.setForeground(Color.gray.brighter());
        buttonCurrent.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
        
        CheckboxGroup cbc2 = new CheckboxGroup();
        ch3 = new Checkbox("Smooth Palette",true,cbc2);
        ch3.setForeground(Color.blue);
        ch4 = new Checkbox("Coarse  Palette",false,cbc2);
        ch4.setForeground(Color.red);
        //add(ch3); add(ch4);

        CheckboxGroup cbc3 = new CheckboxGroup();
        ch5 = new Checkbox("32 levels",true,cbc3);
        ch5.setForeground(Color.red);
        ch6 = new Checkbox("16 levels",false,cbc3);
        ch6.setForeground(Color.red);
        //add(ch5); add(ch6); 
        ch5.setVisible(false);
        ch6.setVisible(false);

        vlab1 = new Label("100.0",Label.LEFT);
        vlab1.setFont(labelfont);

        vlab2 = new Label("mm",Label.LEFT);
        vlab3 = new Label("mils",Label.LEFT);

        vlab2.setFont(labelfont2);
        vlab3.setFont(labelfont2);

        vlab1.setBackground(Color.white);
        vlab2.setBackground(Color.white);

	slider3 = new JSlider(SCROLLMINW,SCROLLMAXW);// width of barrier
	slider3.setValue(20);
        //slider3 = new Scrollbar(Scrollbar.HORIZONTAL,20,1,SCROLLMINW,SCROLLMAXW);// width of barrier
        //slider3.setBlockIncrement(10);	
        slider3.addChangeListener(this);
        //slider3.setBackground(new Color(230,230,255));

        lineLength=100.0;
        Is_Millimeter = true;		 		

        add(slider3);
        //ch1.setBounds(state.s25,state.s50,state.s120,state.s18);
        //ch2.setBounds(state.s145,state.s50,state.s120,state.s18);

        //trace.setBounds(state.s60,state.s75,state.s150,state.s20); 
        trace.setForeground(Color.black);
        add(trace);
        
        //======================================================================
        buttonHorizontal.setBounds(state.s24,state.s48,state.s105,state.s16);
        buttonVertical.setBounds(state.s147,state.s48,state.s105,state.s16);
        buttonCurrent.setBounds(state.s65,state.s75,state.s150,state.s16); 
        add(buttonHorizontal); add(buttonVertical); add(buttonCurrent);  
        //======================================================================
        pH = new Panel();
	    pH.setBackground(Color.lightGray);
	    add(pH);
	    pH.setBounds(state.s24-3,state.s48-3,state.s105+6,state.s16+6);
            
        pV = new Panel();
	    pV.setBackground(Color.red);
	    add(pV);
	    pV.setBounds(state.s147-3,state.s48-3,state.s105+6,state.s16+6);
        
        pC = new Panel();
	    pC.setBackground(Color.lightGray);
	    add(pC);
	    pC.setBounds(state.s65-3,state.s75-3,state.s150+6,state.s16+6);
        
        
        slider3.setBounds(state.s28,state.s22,state.s200+state.s20,state.s15);
        slider3.setBackground(Color.white);
	/*
            slide3.setBackground(Color.cyan);
            add(slide3);
            slide3.setBounds(state.s28-1,state.s22-1,state.s200+state.s20+2,state.s15+2);
            slide2.setBackground(Color.black);
            add(slide2);
            slide2.setBounds(state.s28-2,state.s22-2,state.s200+state.s20+4,state.s15+4);
	*/
        slider3.addChangeListener(this);
        buttonHorizontal.addActionListener(this);
        buttonVertical.addActionListener(this);
        buttonCurrent.addActionListener(this);
    }
	
	
    public void paint(Graphics g){
        //g.setColor(bgcolor);
        //g.fill3DRect(0,0,getSize().width-1,getSize().height-1,true);
        g.clearRect(0,0,getSize().width-1,getSize().height-1);

        //Draw the background	
        g.setColor(Color.black);
        g.drawLine(0,getSize().height-1,getSize().width-1,getSize().height-1);
        g.drawLine(getSize().width-1,0,getSize().width-1,getSize().height-1);
        //g.setColor(Color.white);

        g.drawLine(0,0,getSize().width-1,0);
        g.drawLine(0,0,0,getSize().height-1);

        int plus = state.s44;

        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

        double range;
        range = state.delta_lambda * state.Nx/2;

        g.setColor(Color.black);
        MaestroG.Custom3("Plot Range",""," = "+MaestroA.rounder(range,2)," \u03bb",g,state.font12,state.s80,state.s15);

        g.setColor(Color.red);
	slider3.requestFocusInWindow();
    }
        
    public void stateChanged(ChangeEvent evt){
		     
        if (evt.getSource()==slider3){
                //System.out.println("slider = "+slider3.getValue());
               repaint();
        }	
    }
    
    public void actionPerformed(ActionEvent evt){
        if (evt.getSource() == buttonHorizontal){
            buttonHorizontal.setForeground(Color.red.darker());
            buttonVertical.setForeground(Color.gray.brighter());
            pH.setBackground(Color.red);
            pV.setBackground(Color.gray.brighter());
            repaint();
            state.ignition();
        }
        else if (evt.getSource() == buttonVertical){
            buttonHorizontal.setForeground(Color.gray.brighter());
            buttonVertical.setForeground(Color.red.darker());
            pV.setBackground(Color.red);
            pH.setBackground(Color.gray.brighter());
            repaint();
            state.ignition();
        }
        else if (evt.getSource() == buttonCurrent){
            if(state.ShowCurrent){
                buttonCurrent.setForeground(Color.gray.brighter());
                pC.setBackground(Color.gray.brighter());
            }
            else{
                buttonCurrent.setForeground(Color.red.darker());
                pC.setBackground(Color.red);
            }
            repaint();
            state.ignition();
        }
    }

    
        
    public void itemStateChanged(ItemEvent evt){
        ItemSelectable ie = evt.getItemSelectable();

        if(evt.getSource()== trace){
            repaint();
        }
    }
        
    public synchronized void reset(){

        //slider.setValue(slider.getMaximum());
        {
          // vlab1.setText(""+lineLength);
          // vlab2.setText("mm");
        }

    }
}//End of TransSlidePanel

