/** Image Loader Class
   * @version 1.0  18 Jan 1999
   * @author Lawrence Rodrigues
   **/
package com.vistech.jai.imageio;
import java.io.*;
import java.util.*;
import java.beans.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.image.RenderedImage;
import javax.media.jai.*;
import javax.imageio.*;
import javax.imageio.event.*;
import com.vistech.events.*;

public class ImageLoaderJAI implements ListSelectListener{
   PlanarImage loadedImage = null;
   public Vector imgLst = new Vector();
   protected PropertyChangeSupport pcNotifier;
   ProgressBar pb;
   public int rangevalue =0;
   public ImageLoaderJAI() {
      pcNotifier= new PropertyChangeSupport(this);
       createPb();
   }
   public void load(ListSelectEvent e){
      File currentDir = e.getPath();
      String[] flist = e.getFileList();
      String path = (currentDir !=null)? currentDir.toString()+File.separator+flist[0]:flist[0];
      if(flist != null) loadImage(path);
   }

   public PlanarImage loadImage(String path) {
      PlanarImage img= readAsPlanarImage(path);
      fireImageLoadedEvent(img);
      return img;
   }
  public PlanarImage readAsPlanarImage(String filename) {
      try {
       //String ext = filename.substring(filename.lastIndexOf(".")+1,filename.length());
       //System.out.println("extension = "+ ext);

       RenderedImage img = loadWithProgressBar(filename);
      return new RenderedImageAdapter(img);
      }catch (Exception e) {
      return null;}
   }

   public class ProgressBar {
      JProgressBar jp; //= new JProgressBar(new RangeModel());
      JFrame fr;
      boolean flag = true;
      boolean firstTime = true;
      int value = 0;
      public ProgressBar(){
         jp= new JProgressBar(new RangeModel());
         createUI();
      }

      public void run() {
        // if(flag){
          try{
            jp.setValue(value);
            System.out.println(value);
            Thread.sleep(1000);
            //SwingUtilities.invokeLater(this);
          } catch (Exception e){}
        //  }
      }

      protected void createUI() {
         fr = new JFrame();
         JLabel lb = new JLabel("Percent Complete");
         JLabel stlb = new JLabel("0%");
         JLabel endlb = new JLabel("100%");
         jp.setValue(10);
         JPanel pan = new JPanel();
         pan.add(stlb);
         pan.add(jp);
         pan.add(endlb);
         fr.getContentPane().setLayout(new GridLayout(2,1));
         fr.getContentPane().add(lb);
         fr.getContentPane().add(pan);
         fr.pack();
         fr.setSize(400,100 );
         fr.show();
      }
      public void updateProgressBar(int val){
         value = val;
         System.out.println(val);
      }
      public void hide(){
         if(fr != null) fr.hide();
         flag = false;
      }

      public void show(){
         if(fr != null) fr.show();
      }

   }

   public void updatePb(int val){
      pb.updateProgressBar(val);
   }

   public void createPb(){
      pb = new ProgressBar();
   }

   public void show() {
      pb.show();
   }

   public void disposePb(){
      pb.hide();
      //pb.destroy();
   }

   public class RangeModel extends javax.swing.DefaultBoundedRangeModel{
      public void setValue(int value){
         rangevalue = value;
      }

      public int getVlaue() {
         return rangevalue;
      }
   }


   public RenderedImage loadWithProgressBar(String filename) {
      RenderedImage img = null;
      try {
         InputStream is1 = new FileInputStream(new File (filename));
         javax.imageio.stream.ImageInputStream is = ImageIO.createImageInputStream(is1);
         Iterator it = ImageIO.getImageReaders(is);
         while(it.hasNext()) {
              ImageReader ir = (ImageReader)it.next();
              ir.addIIOReadProgressListener(
                new IIOReadProgressListener(){
                    public void sequenceStarted(ImageReader source,
                                              int minIndex){ }
                    public void sequenceComplete(ImageReader source){}
                    public void imageStarted(ImageReader source, int imageIndex){
                       rangevalue = 0;
                       show();
                    }
                    public void imageProgress(ImageReader source, float percentageDone){
                       rangevalue = (int) percentageDone;
                    }
                    public void imageComplete(ImageReader source){
                       disposePb();
                    }
                    public void thumbnailStarted(ImageReader source,
                                               int imageIndex,
                                               int thumbnailIndex){}
                    public void thumbnailProgress(ImageReader source,
                                float percentageDone){}
                    public void thumbnailComplete(ImageReader source){}
                    public void readAborted(ImageReader source){}
                }
            );
            ir.setInput(is, true);
            img = ir.read(0);
            break;
         }
      } catch (Exception e) {
       System.out.println(e);
      }
      return img;
   }

   public void setLoadedImage(PlanarImage img){
      pcNotifier.firePropertyChange("loadeImage",
                                     this.loadedImage,
                                     img);
      loadedImage = img;
   }

   public PlanarImage getLoadedImage(){
      return loadedImage;
   }

   public void addPlanarImageLoadedListener(PlanarImageLoadedListener il){
       imgLst.addElement(il);
   }
   public void removePlanarImageLoadedListener(PlanarImageLoadedListener il){
      if(!(imgLst.isEmpty())) imgLst.removeElement(il);
   }
   protected void fireImageLoadedEvent(PlanarImage img) {
      for(Enumeration e= imgLst.elements(); e.hasMoreElements();){
          PlanarImageLoadedEvent ie = new PlanarImageLoadedEvent(this, img);
          PlanarImageLoadedListener il = (PlanarImageLoadedListener)(e.nextElement());
          il.imageLoaded(ie);
      }
      setLoadedImage(img);
   }
}
