import java.awt.*;
import java.awt.event.*;
import java.math.*;
public class Einstellungen extends Panel implements ActionListener , ItemListener
{
    private Schallquelle schallquelle;
    private Sensor sensor;
    private Choice GfreqC;
    private Choice GphaseC;
    private TextField GfreqT;
    private TextField GphaseT;
    static private char c;
    static private int number;
    static private double number2;
    static private int laufvar;
    static private int i;
    static private boolean kommaflag;
    static private boolean piflag;
    private String text;


    public Einstellungen(Schallquelle schallquelle, Sensor sensor)
    {
	this.schallquelle=schallquelle;
	this.sensor=sensor;
	
	setSize(359,300);
	setBackground(Color.yellow);
	setLayout(null);

	GfreqC = new Choice();
	GfreqC.addItemListener(this);
	GfreqC.add("50Hz");
	GfreqC.add("100Hz");
	GfreqC.add("500Hz");
	GfreqC.add("1kHz");
	GfreqC.add("5kHz");
	GfreqC.add("10kHz");
	GfreqC.add("50kHz");
	GfreqC.add("100kHz");
	GfreqC.add("500kHz");
	GfreqC.add("1MHz");
	GphaseC = new Choice();
	GphaseC.addItemListener(this);
	GphaseC.add("0.00 PI");
	GphaseC.add("0.25 PI");
	GphaseC.add("0.33 PI");
	GphaseC.add("0.50 PI");
	GphaseC.add("0.67 PI");
	GphaseC.add("0.75 PI");
	GphaseC.add("1.00 PI");
	GphaseC.add("2.00 PI");
	Label label = new Label("Frequenz-Einstellung");
	label.setBounds(50,20,160,20);	

	Label label2 =new Label("Nullphasen-Einstellung");
	label2.setBounds(50,140,200,20);

	GfreqT = new TextField("50Hz",10);
	GfreqT.addActionListener(this);
	GfreqT.setBackground(Color.white);

	GphaseT = new TextField("0",10);
	GphaseT.addActionListener(this);
	GphaseT.setBackground(Color.white);
	
	GfreqC.setBounds(50,60,160,20);
	GfreqT.setBounds(50,100,160,20);
	GphaseC.setBounds(50,180,160,20);
	GphaseT.setBounds(50,220,160,20);
		
	add(label);
	add(label2);
	add(GfreqC);
	add(GfreqT);
	add(GphaseC);
	add(GphaseT);

	sensor.setPhase(0.0);
	schallquelle.setPhase(0.0);
	schallquelle.setFrequenz(1000.0);
	sensor.setFrequenz(1000.0);

    }

    static int toInt(String s)
    {
	number=0;	
	for(i=0; i<s.length(); i++)
                {
		c = s.charAt(i);
		if((c>47)&&(c<58)) number=number*10+(c-48);
		if(c==107) number*=1000;   	 // k= Kilo
		if(c==77) number*=1000000;	// M= Mega
	}
	return(number);
    }

    static double toDouble(String s)
    {
	number2=0.0;
	kommaflag=false;
	piflag=false;
	laufvar=0;
	for(i=0; i<s.length(); i++)
	{
		c = s.charAt(i);
		if((c>47)&&(c<58))
		{
			if(kommaflag==false) number2=number2*10+(c-48);
			else {laufvar++; number2=(double)number2+(c-48)*Math.exp((-laufvar)*Math.log(10));}
		}
		if((c==44)||(c==46)) kommaflag=true;		// Komma
		if((c==80)||(c==112)) piflag=true;		// p oder P für Pi
	}
	//f=(float)number2;
	//f=(float)(f/Math.exp(Math.ceil(Math.log(f)/Math.log(10.0))*Math.log(10.0)));
	//f+=(float)number;
	//if(piflag==true) f*=3.1415926;
	if (piflag==true) number2*=3.141592653589;
	return(number2);
     }
		
   public	void actionPerformed(ActionEvent e)
  {
           Object obj = e.getSource();
           if (obj instanceof TextField)
           {
	text = e.getActionCommand();
	if (obj.equals(GfreqT))
	{
           	      schallquelle.setFrequenz(toInt(text));
	      sensor.bringUpToDate();
           	      repaint();
	}
	if (obj.equals(GphaseT))
	{
	      schallquelle.setPhase(toDouble(text));		
	      sensor.bringUpToDate();
	      repaint();
	}
           }
  }

   public void itemStateChanged(ItemEvent e)
  {	
	Choice obj = (Choice) e.getSource();
	text = obj.getSelectedItem();
	if (obj.equals(GfreqC))
	{
		schallquelle.setFrequenz(toInt(text));
		sensor.bringUpToDate();
		repaint();
	}
	if (obj.equals(GphaseC))
	{
		schallquelle.setPhase(toDouble(text));
		sensor.bringUpToDate();
		repaint();
	}
   }

    public void paint (Graphics g)
    {
	g.drawString("Frequenz "+String.valueOf(schallquelle.getFrequenz())+"Hz", 50, 270);
	g.drawString("NullPhase "+String.valueOf(schallquelle.getPhase())+"rad", 150,270);
	g.drawString("SensorFreq"+String.valueOf(sensor.getFrequenz())+"Hz",50,290);
	g.drawString("SensorPhase "+String.valueOf(sensor.getPhase())+"rad", 150,290);
	//g.drawString(text, 50, 220);
	
	//Label label3 = new Label("Phase am Sensor: " + String.valueOf((float)schallquelle.getPhase()) + " rad");
	//label3.setBounds(50, 200, 280, 20);
		
    }
 //  public void update(Graphics g)
   //{ }
    // hier muessen die Einstellmöglichkeit der Generatorfrequenz und Phase stehen
    // auîderdem, sollten Frequenz und Phasenverschiebung am Sensor angez.werden
    // Event-Handling!
}
