//package maestro.lib.graphics;
//SetRangePanel.java

import java.awt.*;
import java.awt.event.*;
//import java.applet.*;
import java.lang.*;
//import maestro.lib.graphics.*;
//import maestro.lib.math.*;

public class SetRangePanel extends Panel implements ActionListener{
	protected Color bgcolor = new Color(180,147,112);
	
	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);
	private static final Font normalfont = new Font("SanSerif",Font.PLAIN,12);
	private static final Font symbolfont=new Font("Serif",Font.PLAIN,12);
	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");
		b1.setBackground(bgcolor);
                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);
	    
	    String lambda;
	    g.setFont(symbolfont);
	    g.getFontMetrics();
	    lambda="\u03bb";
	    g.setFont(normalfont);
	    g.getFontMetrics();
	    
	    
	    if(setUnit){
		{
		    g.drawString(""+lambda, 235,142);
		    g.drawString(""+lambda, 235,162);
		}
	    }
	}	
	
	public synchronized void setBackg(Color bgcolor){
	    this.bgcolor = bgcolor;
	    try{
		// CHANGE ======================================================
                //b1.setBackground(Color.lightGray);
                b1.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);
	    }
	}
    }
}

