package fplot;

import java.awt.*;
import java.awt.event.*;
/**
 * Der Funktionsplotter als Applikation.
 * @author Christian Semrau
 * <a href="mailto:Christian.Semrau@student.uni-magdeburg.de">
 * Christian.Semrau@student.uni-magdeburg.de</a>
 * <br><a href="http://chsemrau.de">Homepage</a>
 */
public class PlotFrame extends Frame implements ActionListener {
	private DialogFenster dialog = null;

	private GraphFlaeche flaeche = null;
	private Button einstellungButton = null;
	private Button aboutButton = null;
	private TextField koords = null;
	Frame aboutFrame = null;

	private Point dialogLoc = null;
	private Dimension dialogSize = null;
	private boolean dialogExtended = false;
/**
 * Konstruiert das PlotFrame.
 */
public PlotFrame(String title) {
	super(title);
//	setSize(500,560);
	setLayout(new BorderLayout());
	setBackground(Color.lightGray);

	einstellungButton = new Button("Einstellungen");

	koords = new TextField(20);
	koords.setEditable(false);

	aboutButton = new Button("About");

	flaeche = new GraphFlaeche(this);
	add(flaeche, "Center");

	Panel south = new Panel();
	south.add(einstellungButton);
	south.add(koords);
	south.add(aboutButton);
	add(south, "South");

	einstellungButton.addActionListener(this);
	aboutButton.addActionListener(this);
	pack();
}
/**
 * Reagiert auf den einstellungButton
 */
public void actionPerformed(ActionEvent e) {
	if (e.getSource()==einstellungButton){
		openDialog();
	}else
	if (e.getSource()==aboutButton){
		aboutFrame.setVisible(true);
	}
}
/**
 * 
 */
DialogFenster createDialog() {
	DialogFenster d = new DialogFenster(this, dialogExtended);
	if (dialogLoc!=null)
		d.setLocation(dialogLoc);
	if (dialogSize!=null){
		d.setSize(dialogSize.width, d.getSize().height);
	}
	return d;
}
/**
 * Entfernt das Dialogfenster.
 */
void disposeDialog() {
	if (dialog!=null){
		dialogLoc = dialog.getLocation();
		dialogSize = dialog.getSize();
		dialogExtended = dialog.wantExtended();
		dialog.dispose();
	}
	dialog = null;
}
/**
 * Wandelt ein double in einen String mit gegebener Anzahl Nachkommastellen um.
 * Liefert fuer zu kleine Zahlen 0, fuer zu grosse Zahlen die E-Darstellung
 * (in diesem Fall ist die Stellenzahl nicht garantiert).
 *
 * @return String
 * @param d double - der Wert, der umgewandelt werden soll
 * @param ges int - die Gesamtzahl an Zeichen (wird links mit Leerzeichen aufgefuellt)
 * @param nach int - die Zahl der Nachkommastellen
 */
public static String fdouble(double d, int ges, int nach) {
  d=Math.floor(d*Math.pow(10,nach)+0.5d)/Math.pow(10,nach);
  return right(Double.toString(d), ges);
}
/**
 * Liefert die Anzeigeflaeche.
 */
GraphFlaeche getFlaeche() {
	return flaeche;
}
/**
 * Startet das Applet als Applikation.
 */
public static void main(java.lang.String[] args) {
	final PlotApplet app = new PlotApplet();
	app.standalone = true;

	app.init();
	app.start();
}
/**
 * 
 */
void openDialog() {
	if (dialog==null){
		dialog = createDialog();
		dialog.setVisible(true);
	}else
		dialog.setVisible(true);
}
/**
 * 
 */
void reopenDialog() {
	if (dialog!=null){
		dialogLoc = dialog.getLocation();
		dialogSize = dialog.getSize();
		dialogExtended = dialog.wantExtended();
	}
	DialogFenster d = createDialog();
	d.setVisible(true);
	if (dialog!=null){
		dialog.dispose();
	}
	dialog = d;
}
/**
 * Gibt einen rechtsbuendig auf n Zeichen formatieren <code>String</code> zurueck.
 * Ist der <code>String</code> s zu lang, wird er unveraendert zurueckgegeben.
 * Zahlen kann man mit right(long, int) rechtsbuendig ausgeben.
 * @return String - rechtsbuendig formatierter String
 * @param s String - der zu formatierende String
 * @param n int - die Anzahl der Zeichen (wird links mit Leerzeichen aufgefuellt)
 */
public static String right(String s, int n) {
	return (spc(n-s.length(), ' ') + s);
}
/**
 * Liefert einen <code>String</code>, der <code>len</code> Mal das Zeichen <code>ch</code> enthaelt.
 * @return String
 * @param len int - Die gewuenschte Laenge der Zeichenkette
 * @param ch char - Das zu wiederholende Zeichen
 */
public static String spc(int len, char ch) {
  if (len<=0) return "";
  char[] c = new char[len];
  for (int i=0; i<len; c[i++] = ch);
  return new String(c);
}
/**
 * Schreibt die Koordinaten in das Koordinatenfeld.
 * @param x double
 * @param y double
 */
void writeKoords(double x, double y) {
	koords.setText(fdouble(x,8,6)+" / "+fdouble(y,8,6));
}
}