import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import SommerCampWrap;
import GridCanvas;
import AnimCanvas;

class MetaPanel extends Panel implements ActionListener
// Enthält Titel und ein Canvas (Gitter- oder Animations-Canvas)
{
  private Canvas canvas;
  private SommerCampWrap applet;
  private FileDialog dialog = null;

  public MetaPanel(String myTitle, int w, int h, Image myImage,
		   Color myColor, boolean InAnApplet, SommerCampWrap applet)
  {
    // Gitter-Canvas erzeugen
    setLayout(new BorderLayout());
    add("North", new Label(myTitle, Label.CENTER));
    add("Center", canvas = new GridCanvas(w, h, myImage, myColor));
    this.applet = applet;
    if ( !InAnApplet )
      {
	Panel panel;
	Button button;
	add("South", panel = new Panel());
	panel.setLayout(new GridLayout(0, 1, 3, 0));
	panel.add(button = new Button("Gitter Laden"));
	button.addActionListener(this);
	panel.add(button = new Button("Gitter Speichern"));
	button.addActionListener(this);
      }
  }

  public MetaPanel(String myTitle, int w, int h, SommerCampWrap applet)
  {
    // Animations-Canvas erzeugen
    this.applet = applet;
    setLayout(new BorderLayout());
    add("North", new Label(myTitle, Label.CENTER));
    add("Center", canvas = new AnimCanvas(w, h, applet));
  }

  // Weiterleiten der Methoden an Gitter- oder Animations-Canvas
  public void NewCoordinate(int x, int y)
  {
    if (canvas instanceof GridCanvas)
      ((GridCanvas)canvas).NewCoordinate(x, y);
  }

  public Coordinate[][] GetCoordinate()
  {
    if (canvas instanceof GridCanvas)
      return ((GridCanvas)canvas).GetCoordinate();
    return null;
  }

  public void StartThread(int[] sPixels, int[] tPixels, Coordinate[][] sCoord,
			  Coordinate[][] tCoord, int number, boolean doSave)
  {
    if (canvas instanceof AnimCanvas)
      ((AnimCanvas)canvas).StartThread(sPixels, tPixels, sCoord,
				       tCoord, number, doSave);
  }

  public void StopThread()
  {
    if ( (canvas instanceof AnimCanvas) &&
	 (((AnimCanvas)canvas).AnimThread != null) )
      {
	((AnimCanvas)canvas).AnimThread.stop();
	((AnimCanvas)canvas).AnimThread = null;
      }
  }

  // Button-Funktionalität
  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() instanceof Button)
      {
	String label = ((Button)(e.getSource())).getLabel();

	if (dialog==null)
	  {
	    Component comp = this.getParent();
	    while ( !((comp==null) || (comp instanceof Frame)) )
	      {
		comp = comp.getParent();
	      }
	    if (comp!=null)
	      dialog = new FileDialog((Frame)comp,
				      "Gitter Laden/Speichern");
	  }

	if (dialog!=null)
	  {
	    if (((GridCanvas)canvas).GetCoordinate() == null)
	      {
		applet.showStatus("Bitte zuerst ein Gitter " +
				  "(der passenden Größe) erzeugen");
	      }
	    else
	      {
		if (label.compareTo("Gitter Laden") == 0)
		  dialog.setMode(FileDialog.LOAD);
		if (label.compareTo("Gitter Speichern") == 0)
		  dialog.setMode(FileDialog.SAVE);
		dialog.setVisible(true);
		if (dialog.getFile() != null)
		  {
		    if (label.compareTo("Gitter Laden") == 0)
		      ((GridCanvas)canvas).loadGrid(applet,
						    dialog.getDirectory() +
						    dialog.getFile());
		    if (label.compareTo("Gitter Speichern") == 0)
		      ((GridCanvas)canvas).saveGrid(applet,
						    dialog.getDirectory() +
						    dialog.getFile());
		  }
	      }
	  }
      }
  }
}
