//SetRangePanel.java

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

public class SetRangePanel extends Panel implements ActionListener{
	protected Color bgcolor = new Color(255,255,255);
	
	protected TextField text1, text2;
	protected Label lab0, lab00, lab000, lab1, lab2;
	private Label titlelabel, unitlabel1, unitlabel2;
	public Button b1;
	protected String titulo="Unknown Title", unidade="Unknown Unit";
	private double minimum, maximum;
	protected Font labfont=new Font("SanSerif",Font.PLAIN,12);
	protected Font titlefont=new Font("SanSerif",Font.BOLD,16);
	private Font normalfont = new Font("SanSerif",Font.PLAIN,12);
	private Font symbolfont=new Font("Serif",Font.PLAIN,12);
	private boolean setUnit =false;
	private boolean IsMilsOn = false;
        
	GridBagLayout gb = new GridBagLayout();
	GridBagConstraints gbc = new GridBagConstraints();
	
        NewGuide_State state;
        
	public SetRangePanel(String titulo, String unidade, NewGuide_State state){
		super();
		setLayout(gb);
                
                this.state = state;
		setBackground(bgcolor);
                
                labfont=new Font("SanSerif",Font.PLAIN,state.font12);
                titlefont=new Font("SanSerif",Font.BOLD,state.font16);
                normalfont = new Font("SanSerif",Font.PLAIN,state.font12);
                symbolfont=new Font("Serif",Font.PLAIN,state.font12);
	
		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);	
                
                lab0 = new Label("",Label.LEFT);
		lab0.setFont(labfont);
                
                lab00 = new Label("",Label.LEFT);
		lab00.setFont(labfont);
                
                lab000 = new Label("",Label.LEFT);
		lab000.setFont(labfont);
		
		text1 = new TextField("",8);
		text2 = new TextField("",8);
                text1.setFont(labfont);
                text2.setFont(labfont);
		
		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,lab0,0,1,3,1,
			GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}  catch(Exception e){e.printStackTrace();}
		
		try{
		MaestroG.addComponent(this,lab1,0,2,1,1,
			GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}  catch(Exception e){e.printStackTrace();}
			
		try{
		MaestroG.addComponent(this,lab2,0,4,1,1,
			GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}  catch(Exception e){e.printStackTrace();}
		
		try{
		MaestroG.addComponent(this,text1,1,2,1,1,
			GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}  catch(Exception e){e.printStackTrace();}	
		
                try{
		MaestroG.addComponent(this,lab00,0,3,3,1,
			    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}
		catch(Exception e){e.printStackTrace();}
		
		try{
		MaestroG.addComponent(this,text2,1,4,1,1,
			GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}  catch(Exception e){e.printStackTrace();}	
		
		try{
		MaestroG.addComponent(this,unitlabel1,2,2,1,1,
			GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}  catch(Exception e){e.printStackTrace();}		
		
		try{
		MaestroG.addComponent(this,unitlabel2,2,4,1,1,
			GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}  catch(Exception e){e.printStackTrace();}
                
                try{
		MaestroG.addComponent(this,lab000,0,5,3,1,
			    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		}
		catch(Exception e){e.printStackTrace();}
			
							
		b1 = new Button("Update");
		b1.setBackground(bgcolor);
                b1.setFont(labfont);
                try{
		MaestroG.addComponent(this,b1,0,6,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);
	    
	}	
	
	public synchronized void setBackg(Color bgcolor){
	    this.bgcolor = bgcolor;
	    try{
		// CHANGE ======================================================
                //b1.setBackground(Color.lightGray);
                b1.setBackground(this.bgcolor);
                //==============================================================
                lab0.setBackground(this.bgcolor);
                lab00.setBackground(this.bgcolor);
                lab000.setBackground(this.bgcolor);
		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);
	    }
	}
    }
}

