//TransSlidePanel.java

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


public class TransSlidePanel extends Panel implements AdjustmentListener, ActionListener{
	public Scrollbar slider;
	private Label vlab1, vlab2, vlab3;
	private double lineLength;
        private final Font labelfont=TheFonts.sanSerif12;
	private final Font labelfont2=TheFonts.sanSerif12;
	private static final Font buttonfont = TheFonts.sanSerif10;
        private static final Color bgcolor = new Color(236,236,236);
	private static final Color tinta = new Color(236,236,236);
	private boolean Is_Millimeter;
        public Button details;
	private int SCROLLMIN=0, SCROLLMAX=10001;
	// CHANGE ==============================================================
        public Button b2a, b2b;
        //======================================================================
	
        
        public TransSlidePanel(){
		super();	
		setLayout(null);
		setBackground(tinta);

		//vlab1 = new Label("0.0 \u03bb ",Label.RIGHT);
		
		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);
		
		//slider = new Scrollbar(Scrollbar.HORIZONTAL,10000,1,SCROLLMIN,SCROLLMAX);
                slider = new Scrollbar(Scrollbar.HORIZONTAL,SCROLLMIN,1,SCROLLMIN,SCROLLMAX);
		slider.setBlockIncrement(10);	
		slider.addAdjustmentListener(this);
                slider.setBackground(Color.white);
                
                details = new Button("Details");
                add(details);
                details.setBackground(Color.white);
                details.setBounds(30,3,60,20);
                details.setFont(buttonfont);
                		
		lineLength=100.0;
		Is_Millimeter = true;		 		
                
                // CHANGE  =====================================================
                b2a = new Button("<");
                b2a.setBackground(bgcolor);        
                b2b = new Button(">");
                b2b.setBackground(bgcolor);
                add(b2a); add(b2b);
                //==============================================================
		add(slider);
		//add(vlab1);
		//add(vlab2);
		
		vlab1.setBounds(10,4,75,20);
		vlab2.setBounds(85,4,25,20);
		slider.setBounds(125,6,470,15);	
		Panel slide1 = new Panel();
		    slide1.setBackground(Color.black);
		    add(slide1);
		    slide1.setBounds(124,5,472,17);
                    
                // CHANGE  =====================================================
                b2a.setBounds(125,6,14,15);
                b2b.setBounds(581,6,14,15);
                //==============================================================
                
                // CHANGE ======================================================
                b2a.addActionListener(this);
                b2b.addActionListener(this);
                //==============================================================
	}
	
	
	public void paint(Graphics g){
		g.setColor(bgcolor);
		g.fill3DRect(0,0,getSize().width-1,getSize().height-1,true);
	}
	
    	public void adjustmentValueChanged(AdjustmentEvent evt){
		SCROLLMAX = slider.getMaximum();
		SCROLLMIN = slider.getMinimum();
		if((SCROLLMAX-SCROLLMIN-1-slider.getValue())*lineLength/(SCROLLMAX-SCROLLMIN-1)<0){
		    vlab1.setText(""+lineLength);
		    slider.setValue(SCROLLMAX-1);
		}
		else{ 
		    if(Is_Millimeter){   
			vlab1.setText(" "+MaestroA.rounder(lineLength-((SCROLLMAX-SCROLLMIN-1-slider.getValue())*
					lineLength/(SCROLLMAX-SCROLLMIN-1)),5));    
		    }
		    else{
			vlab1.setText(" "+MaestroA.rounder(lineLength -((SCROLLMAX-SCROLLMIN-1-slider.getValue())*
					(lineLength/0.0254)/(SCROLLMAX-SCROLLMIN-1)),5));    
		    }
		}
	}
	
        public void actionPerformed(ActionEvent evt){
            if(evt.getSource()==b2a){
                slider.setValue(slider.getValue()-1);
                repaint();
            }
            else if(evt.getSource()==b2b){
                slider.setValue(slider.getValue()+1);
                repaint();
            }
            
        }
        
	public double getValue(){
		SCROLLMAX = slider.getMaximum();
		SCROLLMIN = slider.getMinimum();
		
		if((SCROLLMAX-SCROLLMIN-1-slider.getValue())*lineLength/(SCROLLMAX-SCROLLMIN-1)<0){
		    return (lineLength);
		}
		else{	
		    if(Is_Millimeter){
			return (MaestroA.rounder((SCROLLMAX-SCROLLMIN-1-slider.getValue())*lineLength/(SCROLLMAX-SCROLLMIN-1),5)); 	
		    }
		    else{
			return (MaestroA.rounder((SCROLLMAX-SCROLLMIN-1-slider.getValue())*(lineLength/0.0254)/(SCROLLMAX-SCROLLMIN-1),5)); 
		    }
		}
	}
	
	public synchronized void setValue(int x){
	    slider.setValues(x-1,1,SCROLLMIN,x);
	    slider.setBlockIncrement(10000);
	   
	}	
	
	public synchronized void setLineLength(double lineLength){
	    this.lineLength=lineLength;
	}
	
	public synchronized void SetMillimeter(boolean x){
	    this.Is_Millimeter=x;
	}
	
	public synchronized void reset(){
	    
	    slider.setValue(slider.getMaximum());
	    if(Is_Millimeter){
	       vlab1.setText(""+lineLength);
	       vlab2.setText("mm");
	    }
	    else{
	       vlab1.setText(""+lineLength/0.0254);
	       vlab2.setText("mils");
	    }
	}
}//End of TransSlidePanel

