package SesameGUI;
import java.awt.Dimension;
import oracle.jdeveloper.layout.XYLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import oracle.jdeveloper.layout.XYConstraints;
import javax.imageio.*;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.print.*;
import java.awt.print.*;
import java.awt.*;
import javax.swing.*;

public class SaveDlg extends javax.swing.JDialog
{
  private XYLayout xYLayout1 = new XYLayout();
  private JButton jpeg = new JButton();
  private JButton png = new JButton();
  private JButton ps = new JButton();

  public SaveDlg(java.awt.Dialog owner)
  {
    super (owner);
    try
    {
      jbInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }

  }

  private void jbInit() throws Exception
  {
    this.setTitle("Save...");
    this.getContentPane().setLayout(xYLayout1);
    xYLayout1.setWidth(100);
    xYLayout1.setHeight(118);
    jpeg.setText("JPEG");
    jpeg.setActionCommand("jpeg");
    jpeg.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          jpeg_actionPerformed(e);
        }
      });
    png.setText("PNG");
    png.setActionCommand("png");
    png.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          png_actionPerformed(e);
        }
      });
    ps.setText("PS");
    ps.setActionCommand("ps");
    ps.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          ps_actionPerformed(e);
        }
      });
    this.getContentPane().add(ps, new XYConstraints(5, 65, 85, 25));
    this.getContentPane().add(png, new XYConstraints(5, 35, 85, 25));
    this.getContentPane().add(jpeg, new XYConstraints(5, 5, 85, 25));

    this.setSize(100 , 128);
  }

  private void jpeg_actionPerformed(ActionEvent e)
  {
  FileDialog    dialog = new FileDialog (new Frame() , null , FileDialog.SAVE);

  if (savePath.length() > 0)
    dialog.setDirectory(savePath);

	dialog.show();

  if(dialog.getFile()!=null) 
    {
    SaveToJPEG(dialog.getDirectory() + dialog.getFile());
    savePath=dialog.getDirectory();
    }
  }

  private void png_actionPerformed(ActionEvent e)
  {
  FileDialog    dialog = new FileDialog (new Frame() , null , FileDialog.SAVE);

  if (savePath.length() > 0)
    dialog.setDirectory(savePath);

	dialog.show();

  if(dialog.getFile()!=null) 
    {
    SaveToPNG(dialog.getDirectory() + dialog.getFile());
    savePath=dialog.getDirectory();
    }
  }

  private void ps_actionPerformed(ActionEvent e)
  {
  FileDialog    dialog = new FileDialog (new Frame() , null , FileDialog.SAVE);

  if (savePath.length() > 0)
    dialog.setDirectory(savePath);

	dialog.show();

  if(dialog.getFile()!=null) 
    {
    SaveToPS(dialog.getDirectory() + dialog.getFile());
    savePath=dialog.getDirectory();
    }
  }

  public void setComponent (Component c)
  {
    component = c;
  }

  public void SaveToJPEG (String fileName)
  {
  try
    {
    Rectangle rect = component.getBounds();

     BufferedImage     bi = new BufferedImage ((int) rect.getWidth() , (int) rect.getHeight() , BufferedImage.TYPE_INT_RGB);

    Graphics2D          g2 = bi.createGraphics ();

    component.paint ((Graphics) g2);

    ImageIO.write (bi , "JPEG" , new File (fileName));
    }
  catch (IOException ie)
    {
    }
  }

  public void SaveToPNG (String fileName)
  {
  try
    {
    Rectangle rect = component.getBounds();

    BufferedImage     bi = new BufferedImage ((int) rect.getWidth() , (int) rect.getHeight() , BufferedImage.TYPE_INT_RGB);

    Graphics2D          g2 = bi.createGraphics ();

    component.paint ((Graphics) g2);

    ImageIO.write (bi , "PNG" , new File (fileName));
    }
  catch (IOException ie)
    {
    }
  }

  PrintStream open_for_write (String fname) 
  {
    try 
      {
      PrintStream fos = new PrintStream (new FileOutputStream (new File (fname)));

      return fos;
      }
    catch (Exception exc) 
      { 
      return null; 
      }
  }

  public void SaveToPS (String fname)
  {
  PrintUtilities.printComponent(component , fname);
  }

  private Component component;
  private static String savePath = "";
}