//SetRangePanel.java
/*
 * panel to set up input ranges
 * authors: Singh T. Junior and Umberto Ravaioli
 * version 1.0 - Copyright: Amanogawa.com - All Rights Reserved
 */

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

public class SetRangePanel extends Panel implements ActionListener{
	protected Color bgcolor = new Color(236,236,221);
	
	protected TextField text1, text2;
	protected Label lab1, lab2;
	private Label titlelabel, unitlabel1, unitlabel2;
	public Button b1;
	protected String titulo="Unknown Title", unidade="Unknown Unit";
	private double minimum, maximum;
	Font labfont=new Font("SanSerif",Font.PLAIN,12);
	Font titlefont=new Font("SanSerif",Font.BOLD,16);
        Font textfont=new Font("SanSerif",Font.BOLD,14);
	private boolean setUnit =false;
	private boolean IsMilsOn = false;
        
	GridBagLayout gb = new GridBagLayout();
	GridBagConstraints gbc = new GridBagConstraints();
        private NewGuide_State state;
        
	public SetRangePanel(String titulo, String unidade, NewGuide_State state){
            super();
            this.state = state;

            labfont=new Font("SanSerif",Font.PLAIN,state.font12);
            titlefont=new Font("SanSerif",Font.BOLD,state.font16);
            textfont=new Font("SanSerif",Font.PLAIN,state.font12);
            
            //setLayout(gb);
            setLayout(null);
            setBackground(bgcolor);
            titlelabel = new Label(titulo,Label.CENTER);
            titlelabel.setFont(titlefont);

            unitlabel1 = new Label(unidade,Label.LEFT);
            unitlabel1.setFont(labfont);

            unitlabel2 = new Label(unidade,Label.LEFT);
            unitlabel2.setFont(labfont);

            lab1 = new Label("Maximum = ",Label.RIGHT);
            lab1.setFont(labfont);

            lab2 = new Label("Minimum = ",Label.RIGHT);
            lab2.setFont(labfont);	

            text1 = new TextField("",8);
            text2 = new TextField("",8);
            text1.setFont(textfont);
            text2.setFont(textfont);
            
            b1 = new Button("Update / Exit");
            b1.setFont(textfont);
            
            maximum = 1.0;
            minimum = 0.0;
            
            titlelabel.setBounds(state.s50,state.s60, state.s200,state.s20);
            add(titlelabel);
            
            lab1.setBounds(state.s10,state.s110, state.s90,state.s20);
            add(lab1);
            
            text1.setBounds(state.s110,state.s110, state.s80,state.s20);
            add(text1);
            
            Panel ptext1 = new Panel();
                ptext1.setBackground(Color.black);
                ptext1.setBounds(state.s110-1,state.s110-1, state.s80+2,state.s20+2);
                add(ptext1);
            
            unitlabel1.setBounds(state.s200,state.s110, state.s50,state.s20);
            add(unitlabel1);
            
            lab2.setBounds(state.s10,state.s150, state.s90,state.s20);
            add(lab2);
            
            text2.setBounds(state.s110,state.s150, state.s80,state.s20);
            add(text2);
            
            Panel ptext2 = new Panel();
                ptext2.setBackground(Color.black);
                ptext2.setBounds(state.s110-1,state.s150-1, state.s80+2,state.s20+2);
                add(ptext2);
            
            unitlabel2.setBounds(state.s200,state.s150, state.s50,state.s20);
            add(unitlabel2);
            
            b1.setBounds(state.s75,state.s200,state.s150,state.s25);
            b1.setBackground(Color.white);
            Panel p40 = new Panel();
                p40.setBackground(Color.cyan);
                p40.setBounds(state.s75-1,state.s200-1,state.s150+2,state.s25+2);
    
            Panel p4 = new Panel();
                p4.setBackground(Color.black);
                p4.setBounds(state.s75-2,state.s200-2,state.s150+4,state.s25+4);
	
            add(b1);
            add(p40);
            add(p4);
            /*
            try{
            MaestroG.addComponent(this,titlelabel,0,0,3,1,
                    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
            }  catch(Exception e){e.printStackTrace();}

            try{
            MaestroG.addComponent(this,lab1,0,1,1,1,
                    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
            }  catch(Exception e){e.printStackTrace();}

            try{
            MaestroG.addComponent(this,lab2,0,2,1,1,
                    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
            }  catch(Exception e){e.printStackTrace();}

            try{
            MaestroG.addComponent(this,text1,1,1,1,1,
                    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
            }  catch(Exception e){e.printStackTrace();}	

            try{
            MaestroG.addComponent(this,text2,1,2,1,1,
                    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
            }  catch(Exception e){e.printStackTrace();}	

            try{
            MaestroG.addComponent(this,unitlabel1,2,1,1,1,
                    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
            }  catch(Exception e){e.printStackTrace();}		

            try{
            MaestroG.addComponent(this,unitlabel2,2,2,1,1,
                    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
            }  catch(Exception e){e.printStackTrace();}			

            try{
            MaestroG.addComponent(this,b1,0,3,3,1,
                        GridBagConstraints.BOTH,GridBagConstraints.CENTER);
            }
            catch(Exception e){e.printStackTrace();}
            */
            


            //Listeners
            b1.addActionListener(this);
	}
	
	public double getMinimum(){
	    return minimum;
	}
	
	public double getMaximum(){
	    return maximum;
	}
	
	public synchronized void setMinimum(double minimum){
	    this.minimum = minimum;
	    text2.setText(""+minimum);
	}
	
	public synchronized void setUnit(boolean setUnit){
	    this.setUnit = setUnit;
	}
	
	public synchronized void setMils(boolean IsMilsOn){
	    this.IsMilsOn = IsMilsOn;
	}
	
	public synchronized void setMaximum(double maximum){
	    this.maximum = maximum;
	    text1.setText(""+maximum);
	}
	
	public void paint(Graphics g){
	    g.setColor(bgcolor);
	    g.fillRect(0,0,getSize().width,getSize().height);
	    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);
	    g.setColor(Color.black);
            g.drawRect(0,0,getSize().width-1,getSize().height-1);
                    
	    if(setUnit){
		//if(IsMilsOn){
		//    g.drawString("[ mils ]", state.s245, state.s137);
		//    g.drawString("[ mils ]", state.s245, state.s157);
		//}
		//else{
		//    g.drawString("[ mm ]", state.s245,state.s137);
		//    g.drawString("[ mm ]", state.s245,state.s157);
		//}
	    }
	}	
	
	public synchronized void setBackg(Color bgcolor){
	    this.bgcolor = bgcolor;
	    try{
		b1.setBackground(Color.white);
		lab1.setBackground(this.bgcolor);
		lab2.setBackground(this.bgcolor);
		text1.setBackground(Color.white);
		text2.setBackground(Color.white);
		titlelabel.setBackground(this.bgcolor);
		unitlabel1.setBackground(this.bgcolor);
		unitlabel2.setBackground(this.bgcolor);
	    }
	    catch(NullPointerException e){
	    
	    }
	    repaint();
	}
    
	public void actionPerformed(ActionEvent evt){
	if(evt.getSource() == b1){
	    try{
		  maximum=Double.valueOf(text1.getText()).doubleValue();
		  minimum=Double.valueOf(text2.getText()).doubleValue();
		  if(maximum < minimum) { maximum = 2 * minimum + 1; }
	    }
	    catch(NumberFormatException e){
		 maximum=1.0;
		 minimum=0.0;
		 text1.setText(""+maximum);
		 text2.setText(""+minimum);
	    }
	}
    }
}

/*
public class SetRangePanel extends Panel implements ActionListener{
	// CHANGE ==============================================================
        //protected Color bgcolor = new Color(180,147,112);
	protected Color bgcolor = new Color(236,236,221);
        //======================================================================
        
	protected TextField text1, text2;
	protected Label lab1, lab2;
	private Label titlelabel, unitlabel1, unitlabel2;
	public Button b1;
	protected String titulo="Unknown Title", unidade="Unknown Unit";
	private double minimum, maximum;
    //protected static final Font labfont=new Font("SanSerif",Font.PLAIN,12);
    //protected static final Font titlefont=new Font("SanSerif",Font.BOLD,16);
    public static  Font labfont;
    public static  Font titlefont;

	private boolean setUnit =false;
	private boolean IsMilsOn = false;
        
	GridBagLayout gb = new GridBagLayout();
	GridBagConstraints gbc = new GridBagConstraints();
	
	public SetRangePanel(String titulo, String unidade ){
		super();
		setLayout(gb);
		setBackground(bgcolor);
		titlelabel = new Label(titulo,Label.CENTER);
		titlelabel.setFont(titlefont);
		
		unitlabel1 = new Label(unidade,Label.CENTER);
		unitlabel1.setFont(labfont);
		
		unitlabel2 = new Label(unidade,Label.CENTER);
		unitlabel2.setFont(labfont);
		
		
		lab1 = new Label("Maximum : ",Label.LEFT);
		lab1.setFont(labfont);
		
		lab2 = new Label("Minimum : ",Label.LEFT);
		lab2.setFont(labfont);	
		
		text1 = new TextField("",8);
		text2 = new TextField("",8);
		
		maximum = 1.0;
		minimum = 0.0;
		
		try{
		MaestroG.addComponent(this,titlelabel,0,0,3,1,
			GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}  catch(Exception e){e.printStackTrace();}
		
		try{
		MaestroG.addComponent(this,lab1,0,1,1,1,
			GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}  catch(Exception e){e.printStackTrace();}
			
		try{
		MaestroG.addComponent(this,lab2,0,2,1,1,
			GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}  catch(Exception e){e.printStackTrace();}
		
		try{
		MaestroG.addComponent(this,text1,1,1,1,1,
			GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}  catch(Exception e){e.printStackTrace();}	
		
		try{
		MaestroG.addComponent(this,text2,1,2,1,1,
			GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}  catch(Exception e){e.printStackTrace();}	
		
		try{
		MaestroG.addComponent(this,unitlabel1,2,1,1,1,
			GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}  catch(Exception e){e.printStackTrace();}		
		
		try{
		MaestroG.addComponent(this,unitlabel2,2,2,1,1,
			GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}  catch(Exception e){e.printStackTrace();}			
					
							
		b1 = new Button("Update");
		try{
		MaestroG.addComponent(this,b1,0,3,3,1,
			    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}
		catch(Exception e){e.printStackTrace();}
		
		//Listeners
		b1.addActionListener(this);
	}
	
	public double getMinimum(){
	    return minimum;
	}
	
	public double getMaximum(){
	    return maximum;
	}
	
	public synchronized void setMinimum(double minimum){
	    this.minimum = minimum;
	    text2.setText(""+minimum);
	}
	
	public synchronized void setUnit(boolean setUnit){
	    this.setUnit = setUnit;
	}
	
	public synchronized void setMils(boolean IsMilsOn){
	    this.IsMilsOn = IsMilsOn;
	}
	
	public synchronized void setMaximum(double maximum){
	    this.maximum = maximum;
	    text1.setText(""+maximum);
	}
	
	public void paint(Graphics g){
	    g.setColor(bgcolor);
	    g.fillRect(0,0,getSize().width,getSize().height);
	    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);
	    g.setColor(Color.black);
	    if(setUnit){
		if(IsMilsOn){
		    g.drawString("[mils]", 248, 137);
		    g.drawString("[mils]", 248, 157);
		}
		else{
		    g.drawString("[mm]", 248,137);
		    g.drawString("[mm]", 248,157);
		}
	    }
	}	
	
	public synchronized void setBackg(Color bgcolor){
	    this.bgcolor = bgcolor;
	    try{
		b1.setBackground(Color.lightGray);
		lab1.setBackground(this.bgcolor);
		lab2.setBackground(this.bgcolor);
		text1.setBackground(Color.white);
		text2.setBackground(Color.white);
		titlelabel.setBackground(this.bgcolor);
		unitlabel1.setBackground(this.bgcolor);
		unitlabel2.setBackground(this.bgcolor);
	    }
	    catch(NullPointerException e){
	    
	    }
	    repaint();
	}
    
	public void actionPerformed(ActionEvent evt){
	if(evt.getSource() == b1){
	    try{
		  maximum=Double.valueOf(text1.getText()).doubleValue();
		  minimum=Double.valueOf(text2.getText()).doubleValue();
		  if(maximum < minimum) { maximum = 2 * minimum + 1; }
	    }
	    catch(NumberFormatException e){
		 maximum=1.0;
		 minimum=0.0;
		 text1.setText(""+maximum);
		 text2.setText(""+minimum);
	    }
	}
    }
}

*/