/** JAIImageViewer Class
   * @version 1.0  18 apr 2000
   * @author Lawrence Rodrigues
   **/
package app;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.geom.*;
import com.vistech.util.*;
import com.vistech.jai.imageio.*;
import com.vistech.jai.render.*;

import java.awt.image.RenderedImage;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import com.sun.media.jai.codec.*;
import com.vistech.imageviewer.*;

public class JAIImageViewer extends ApplicationFrame{
   JAIImageCanvas viewer;
   PlanarImage image;
   FileBrowser flBrowser;
   ImageLoaderJAI loader;
   ImageSaverPanel saver;
   ScrollGUI scroll;

   public void printPropertyNames(PlanarImage image) {
      String names[] = image.getPropertyNames();
      //System.out.println("Property list");
      //for(int i=0;i<names.length;i++){
      //    System.out.println(names[i]+ " " + image.getProperty(names[i]));
      //}
   }

   protected Dimension getViewerSize(double imageAspectRatio){
      Dimension d= Toolkit.getDefaultToolkit().getScreenSize();
      int maxWidth = (int)(d.width *3/4.0);
      int maxHeight = (int)(d.height*3/4.0);
      double screenAspectRatio = maxWidth/(double)maxHeight;
      int viewerWidth,viewerHeight;
      if(imageAspectRatio  > screenAspectRatio){
          viewerWidth =   maxWidth;
          viewerHeight = (int)( maxHeight* screenAspectRatio/imageAspectRatio);
      }else{
          viewerHeight =   maxHeight;
          viewerWidth = (int)(maxWidth* imageAspectRatio/screenAspectRatio);
      }
      return new Dimension(viewerWidth, viewerHeight);
   }

   public void loadAndDisplay(String filename){
      image = loader.readAsPlanarImage(filename);
      //printPropertyNames(image);
      createUI();
      displayImage(image);
      printPropertyNames(image);
   }

   public void displayImage(PlanarImage img) {
      int width = img.getWidth();
      int height = img.getHeight();
      printPropertyNames(image);
      viewer.setImage(img);
      saver.setImage(img);
   }

   public void createUI() {
      Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
      int width = (int)(dim.width *3/4.0);
      int height = (int)(dim.height*3/4.0);
      setTitle("Image Viewer");
      viewer = new JAIImageCanvas();
      Dimension d = getViewerSize(width/(double)height);
      viewer.setPreferredSize(new Dimension(d.width, d.height));
      createImageLoaderAndSaver();
      loader.addPlanarImageLoadedListener(
          new PlanarImageLoadedListener() {
             public void imageLoaded(PlanarImageLoadedEvent e) {
                PlanarImage image = e.getImage();
                if(image == null) return;
                SwingUtilities.invokeLater(new ImagePaint(image));
             }
         }
      );

      JTabbedPane jtp = new JTabbedPane();
      jtp.add("Load", flBrowser);
      jtp.add("Save",  saver);
      JSplitPane vsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jtp,viewer);
      getContentPane().setLayout(new GridLayout(1,1));
      getContentPane().add(vsp);
      pack();
      setSize(new Dimension(d.width+170, d.height));
      show();
      vsp.setDividerLocation(0.32);
      vsp.setDividerSize(6);
      initializeScroll();
      viewer.repaint();
   }

   protected void initializeScroll(){
      scroll = new ScrollGUI(viewer);
      viewer.addMouseListener(scroll);
      viewer.addMouseMotionListener(scroll);
   }

   protected void createImageLoaderAndSaver(){
      flBrowser = new FileBrowser();
      loader = new ImageLoaderJAI();//flBrowser.getLoader();
      flBrowser.addListSelectListener(loader);
      saver = new ImageSaverPanel();
      saver.setBorder(BorderFactory.createTitledBorder("Image Save"));
   }

   class ImagePaint implements Runnable {
      PlanarImage image;
      boolean firstTime = true;
      public ImagePaint(PlanarImage image){
         this.image = image;
      }
      public void run() {
         if(firstTime) {
            try {
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            viewer.setImage(image);
            printPropertyNames(image);
            saver.setImage(image);
            viewer.repaint();
            firstTime = false;
            SwingUtilities.invokeLater(this);
            com.vistech.util.SystemUtil.runGc();
            }catch(Exception e) {
             SwingUtilities.invokeLater(this);
            }
         }
         else {
            if(!viewer.isImageDrawn()) SwingUtilities.invokeLater(this);
            else  setCursor(Cursor.getDefaultCursor());
         }
      }
   }

   public static void  main(String[] args){
      JAIImageViewer ip = new JAIImageViewer();
      if(args.length <1) ip.createUI();
      else ip.loadAndDisplay(args[0]);
  }
}