package seisinfo;

import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class SlideShowFrame extends JFrame {
  public static SlSh1 t1;
  private Image offScreenImg;
  private Graphics offScreenG;
  Toolkit toolkit = Toolkit.getDefaultToolkit();
  Image image1;
  Image scaled;
  MediaTracker mt;
  boolean done=false;
  boolean go=true;
  boolean pause=false;
  boolean offline=false;
  String Slide;
  File entry;
  String[] fileName;
  String Path;
  String showtitle;
  int nr;
  int number_of_slides;
  int sld=0;
  int onoff=0;
  int sw=0;

  double XX;
  double YY;
  double MaxX;
  double MinX;
  double MaxY;
  double MinY;





  int scrheight;                   // screen height
  int scrwidth;                    // screen width
  int xmarg=10;                    // x-margin
  int ymarg=10;                    // y-margin
  int xx;                          // picture width
  int yy;                          // picture height
  int ysubtitle=70;                // height subtitle
  int ytitle=30;                   // height frame title

  TextField textField1 = new TextField();
  TextField textField2 = new TextField();
  Button button1 = new Button();
  Button button2 = new Button();
  Button button3 = new Button();

  public SlideShowFrame(int nr,String Path, String showtitle) {
    this.nr=nr;
    this.Path=Path;
    this.showtitle=showtitle;
    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    scrheight=screenSize.height;  // get screen height
    scrwidth=screenSize.width;    // get screen width
    System.out.println(scrheight+"  "+scrwidth);

    this.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        done=true;
        offline=true;
        System.out.println("CLOSE");
        Close();
      }
    });
    addComponentListener(new java.awt.event.ComponentAdapter()
    {
      public void componentMoved(java.awt.event.ComponentEvent evt)
      {
        formComponentMoved(evt);
      }
       public void componentResized(java.awt.event.ComponentEvent evt)
       {
         formComponentResized(evt);
       }
    });

    try {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }
  private void formComponentMoved(java.awt.event.ComponentEvent evt) {
    // Add your handling code here:
//        System.out.println("I was moved to "+this.getLocation());

      XX = this.getLocation().getX();
      YY = this.getLocation().getY();
      System.out.println("SLIDE is here: " + XX + "," + YY);

}
private void formComponentResized(java.awt.event.ComponentEvent evt)
{
    // Add your handling code here:
//      System.out.println("TU "+this.getBounds());

      XX = this.getBounds().getMinX();
      YY = this.getBounds().getMinY();
      System.out.println("SLIDE is here: " + XX + "," + YY);
      MaxX = this.getBounds().getMaxX();
      MinX = this.getBounds().getMinX();
      MaxY = this.getBounds().getMaxY();
      MinY = this.getBounds().getMinY();
//        System.out.println((int)MinX+","+(int)MinY+"  "+(int)MaxX+","+(int)MaxY);

}
  public int LocX()
  {
        return ( (int) XX);
  }
  public int LocY()
  {
        return ( (int) YY);
  }

  public void Close()
{
  this.setVisible(false);
  done=true;
  switch(nr)
  {
      case 1:
      SeisInfoFrame.SLIDE1=false;
      break;
      case 2:
      SeisInfoFrame.SLIDE2=false;
  }
}

  public void CrtImg()
  {
          offScreenImg = createImage(getSize().width, getSize().height);
          offScreenG = offScreenImg.getGraphics();
          entry = new File(Path);
          fileName=entry.list();
          number_of_slides=fileName.length;
          System.out.println("Entries: "+fileName.length);
  }
  public void StartShow()
  {
    t1=new SlSh1();
    t1.start();
  }
/*
  public void update(Graphics g)
  {
      paint(offScreenG);
      g.drawImage(offScreenImg,0,0,this);
      textField1.setText(Integer.toString(sld));
  }
*/
  public void paint(Graphics g)
  {
    if(sw==1)
    {
      g.drawImage(scaled, xmarg, ytitle+ymarg, this);
      textField1.setText(Integer.toString(sld));
    }
  }
  public void newslide()
  {
      image1 = toolkit.getImage(Slide);
///  mt=new MediaTracker(this);
///  mt.addImage(image1,0);
//  scaled=image1.getScaledInstance(430,350,Image.SCALE_AREA_AVERAGING);
  scaled=image1.getScaledInstance(scrwidth/2-2*xmarg,scrheight/2-ytitle-ysubtitle,Image.SCALE_AREA_AVERAGING);
/*
      try
      {
        mt.waitForAll();
      }catch(InterruptedException e)
      {
        System.out.println("Waiting for complete");
      }
*/
  System.out.println("Call Paint");
      sw=1;
          repaint();
  }
  class SlSh1 extends Thread
  {

    public void run()
    {
      button1.setEnabled(false);
      button3.setEnabled(false);
      textField1.setBackground(Color.lightGray);
      textField2.setBackground(Color.lightGray);
      sld=1;
//        while(!done)
      while(go)
      {
        while(sld < number_of_slides)
        {

          Slide=Path+"Slide"+String.valueOf(sld)+".jpg";
          System.out.println("Slidenummer= "+Slide);
          newslide();

          if(done==true)
            break;
          try
          {
            sleep(30000);
          }catch(InterruptedException e){}
          while(pause)
          {
            try
            {
              sleep(100);
            }catch(InterruptedException e){}
          }
          sld++;
          if(sld == number_of_slides)
            sld=1;

        }
        if(done==true)
        break;
      }
      System.out.println("Getting out of thread");

    }

  }

  void jbInit() throws Exception {
    textField1.setText("textField1");
//    textField1.setBounds(new Rectangle(325, 265, 63, 21));
    textField1.setBounds(new Rectangle(xmarg+300,scrheight/2-ysubtitle, 20, 21));
    this.getContentPane().setLayout(null);
    textField2.setText("");
    textField2.setBounds(new Rectangle(xmarg+350,scrheight/2-ysubtitle, 20, 21));
    button1.setLabel("Back one slide");
    button1.setBounds(new Rectangle(xmarg,scrheight/2-ysubtitle, 95, 23));
    button2.setLabel("Pause");
    button2.setBounds(new Rectangle(xmarg+100,scrheight/2-ysubtitle, 95, 23));
    button3.setLabel("Forward one slide");
    button3.setBounds(new Rectangle(xmarg+200,scrheight/2-ysubtitle, 95, 23));
    this.getContentPane().add(textField1, null);
    this.getContentPane().add(textField2, null);
    this.getContentPane().add(button1, null);
    this.getContentPane().add(button2, null);
    this.getContentPane().add(button3, null);
  }
}
