/*
 * @(#) JAIImageBrowser.java 1.0 2000/10/4
 * Copyright (c) 2000 Larry Rodrigues
 */
package app;
import java.io.*;
import java.beans.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import javax.media.jai.*;
import com.vistech.util.*;
import com.vistech.events.*;
import com.vistech.jai.imageio.*;
import com.vistech.jai.render.*;

/** An application that loads and displays multiple images in a single frame.
  * @version 1.0 4 Oct 2000
  * @author Lawrence Rodrigues
  **/
public class JAIImageBrowser extends com.vistech.util.ApplicationFrame{
   protected PlanarImageLoader il;
   protected String saveFileName = null;
   protected ViewerPanel viewer;
   public JAIImageBrowser() {
      createUI();
      saveFileName = "ImageLoaderGUI.jpg";
   }

   protected void setAboutStrings() {
      setAboutString(" JAI Image Browser \n Version 1.0 \n"+
                        "Author: Lawrence Rodrigues\n");
      setAboutTitle(" JAI Image Browser");
   }

   private void createUI() {
      setAboutStrings();
      il = new PlanarImageLoader();
      il.setLoadButtonLabel("Launch Image Browser");
      il.addPlanarImageLoadedListener(
          new PlanarImageLoadedListener() {
             public void imageLoaded(PlanarImageLoadedEvent e) {
                PlanarImage[] imset = e.getImages();
                displayImage(imset);
                il.setLoadButtonLabel("Load Images");
             }
          });
      this.getContentPane().add(il);
      this.setSize(il.getPreferredSize().width+30, il.getPreferredSize().height+50);
   }

   public void save() {
      if(saveFileName == null) saveAs();
      Graphics g = this.getContentPane().getGraphics();
      Container contpane = this.getContentPane();
      JpegUtil.saveComponentAsJPEG(il,this.getContentPane(), saveFileName);
      contpane.add(il);
      this.validate();
   }

   public void saveAs() {
      String filename = JpegUtil.launchFileDialog();
      if(filename != null){
         saveFileName = filename;
         save();
      }
   }

   public void displayImage(PlanarImage[] imset) {
      if(viewer == null) {
         viewer = new ViewerPanel();
         viewer.setTitle("Image Browser");
         viewer.addWindowListener(
           new WindowAdapter() {
           public void windowClosing(WindowEvent e){
             System.exit(0);
          }
         });
      }
      viewer.setImageSet(imset);
      viewer.show();
      viewer.repaint();
  }

  public static void  main(String[] args){
     JAIImageBrowser ib = new JAIImageBrowser();
     ib.setTitle("JAI Image Browser");
     ib.show();
  }
}